Skip to content

node red

Åke Hedman edited this page Aug 17, 2021 · 21 revisions

node-red

To test VSCP with node-red you can use the VSCP demo server located at demo.vscp.org. This server has a MQTT broker active for public use at port 1883 which can be accessed with user="vscp" and password="secret". Also this server have websocket support on port 9001. The demo server is fully described here.

Example flows

Display kitchen temperature

This is a simple flow that display the temperature in our kitchen. The temperature is displayed in a gauge as well as in a diagram and looks like this

The code for the flow is


[{"id":"5805cd97.e51434","type":"mqtt in","z":"85a63c9d.216f5","name":"Kitchen temperature","topic":"vscp/25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:01/1040/6/1/4","qos":"2","datatype":"json","broker":"cfd5e8c8.289d38","nl":false,"rap":true,"rh":0,"x":170,"y":100,"wires":[["ca29d748.513268"]]},{"id":"68657e2d.35aec","type":"ui_gauge","z":"85a63c9d.216f5","name":"Kitchen temperature","group":"ff769916b5262def","order":1,"width":"6","height":"4","gtype":"gage","title":"Kitchen temperature","label":"Degrees Celsius","format":"{{value}}","min":"0","max":"40","colors":["#00b500","#e6e600","#ca3838"],"seg1":"25","seg2":"30","x":820,"y":100,"wires":[]},{"id":"b46ea708.67aa88","type":"debug","z":"85a63c9d.216f5","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":180,"wires":[]},{"id":"fc19a640.2808e8","type":"function","z":"85a63c9d.216f5","name":"","func":"msg.value = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":100,"wires":[["68657e2d.35aec"]]},{"id":"f5775f7a.0b7d","type":"ui_chart","z":"85a63c9d.216f5","name":"Kitchen temperature","group":"ff769916b5262def","order":5,"width":"11","height":"5","label":"Kitchen Temperature","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"No data","dot":false,"ymin":"0","ymax":"40","removeOlder":"12","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1d6faa","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":820,"y":140,"wires":[[]]},{"id":"9b531124.e0861","type":"function","z":"85a63c9d.216f5","name":"","func":"msg.payload = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":140,"wires":[["f5775f7a.0b7d"]]},{"id":"ca29d748.513268","type":"function","z":"85a63c9d.216f5","name":"Add localtime","func":"var utcDate = msg.payload.vscpDateTime;\nmsg.payload.localtime = new Date(utcDate);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":100,"wires":[["fc19a640.2808e8","9b531124.e0861","b46ea708.67aa88"]]},{"id":"cfd5e8c8.289d38","type":"mqtt-broker","name":"demo.vscp.org","broker":"demo.vscp.org","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"ff769916b5262def","type":"ui_group","name":"Brattberg","tab":"a4efd49613773787","order":1,"disp":true,"width":"11","collapse":false},{"id":"a4efd49613773787","type":"ui_tab","name":"Kitchen","icon":"dashboard","disabled":false,"hidden":false}]

Copy to clipboard and import in node-red.

Note that you need to add the user and the password (vscp/secret) manually to the security tab of the node-red MQTT in node.

The topic

The subscribe topic may need an explanation. It is set to

vscp/25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:01/1040/6/1/4/#

The

25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:01

is the GUID for the node that publish the events we are interested in. In this case

25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:00

is assigned to a CAN driver CAN4VSCP and the 01 at the end of the GUID is one of a nodes on the bus. In this case a Kelvin NTC10K module.

The

1040/6

says that we are interested only in temperature events from the node.

The

1

is for node with nickname 1. and the

4

is for sensor 4 on this board. Changing this to

vscp/25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:01/1040/6/#

would give us all temperature event from all sensors of the node.

Changing it to

vscp/25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:01/#

would give all events from the node (heartbeats for instance). Not just temperature events.

This topic organisation is just one way to do it so don't expect all VSCP MQTT topics to be on this form. How this is setup on the demo server is described here

Important

In the above flow we don't care about the unit of the temperature measurement. If we look in the specification we see that it is actually there as for all measurement events. In this case set to 1 which is degrees Celsius. The other unit options Kelvin (=0) and Fahrenheit (=2) could also be expected here. General code should handle this and don't expect the measurement unit to be in a specific unit as in this simple example.

Display kitchen fridge temperature

The flow is the same as the one above. The only difference is the sensor (sensorindex=1) in the topic whish in this case is set to

vscp/25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:01/1040/6/1/1/#

The full flow is

[{"id":"fcfc9205225da3e1","type":"mqtt in","z":"8102f2ad6305cf60","name":"Fridge temperature","topic":"vscp/25:00:00:00:00:00:00:00:00:00:00:00:06:01:00:01/1040/6/1/1/#","qos":"2","datatype":"json","broker":"5438645a.6577cc","nl":false,"rap":true,"rh":0,"x":170,"y":540,"wires":[["b88d9004557f918b","b764a68b8c2b279a","677ae44fd21716e0"]]},{"id":"df692dc6de983a9d","type":"ui_gauge","z":"8102f2ad6305cf60","name":"Fridge temperature","group":"ff769916b5262def","order":1,"width":"6","height":"4","gtype":"gage","title":"Fridge temperature","label":"Degrees Celsius","format":"{{value}}","min":"-40","max":"20","colors":["#00b500","#e6e600","#ca3838"],"seg1":"-15","seg2":"-10","x":670,"y":540,"wires":[]},{"id":"b88d9004557f918b","type":"debug","z":"8102f2ad6305cf60","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":490,"y":640,"wires":[]},{"id":"b764a68b8c2b279a","type":"function","z":"8102f2ad6305cf60","name":"","func":"msg.value = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":540,"wires":[["df692dc6de983a9d"]]},{"id":"677ae44fd21716e0","type":"function","z":"8102f2ad6305cf60","name":"","func":"msg.payload = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":580,"wires":[["0970931d00cf465d"]]},{"id":"0970931d00cf465d","type":"ui_chart","z":"8102f2ad6305cf60","name":"Fridge temperature","group":"ff769916b5262def","order":5,"width":"11","height":"5","label":"Fridge Temperature","chartType":"line","legend":"false","xformat":"HH:mm","interpolate":"linear","nodata":"No data","dot":false,"ymin":"-30","ymax":"20","removeOlder":"12","removeOlderPoints":"","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1d6faa","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":670,"y":580,"wires":[[]]},{"id":"5438645a.6577cc","type":"mqtt-broker","name":"Local","broker":"localhost","port":"1883","clientid":"lynx","usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"ff769916b5262def","type":"ui_group","name":"Brattberg","tab":"a4efd49613773787","order":1,"disp":true,"width":"11","collapse":false},{"id":"a4efd49613773787","type":"ui_tab","name":"Kitchen","icon":"dashboard","disabled":false,"hidden":false}]

Tips

Add localtime to event in flow

VSCP event have date and time set in UTC to be universally usable. To get local time you have to convert this UTC time to a local time. But don't despair the conversion is easy.

Add a function node in the flow from the MQTT broker with the following content

var utcDate = msg.payload.vscpDateTime;
msg.payload.localtime = new Date(utcDate);
return msg;

you will now have the localtime in msg.payload.localtime and can