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.

Simple example flow

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":"1808f66585de11aa","type":"mqtt in","z":"8102f2ad6305cf60","name":"Kitchen temperature","topic":"vscp/25:00:00:00:00:00:00:00:00:00:00:00:06:00:00:01/1040/6/1/4","qos":"2","datatype":"json","broker":"d5c2659b.267728","nl":false,"rap":true,"rh":0,"x":170,"y":1120,"wires":[["eb77935aa44eeb1e"]]},{"id":"83e4c24fab4c374b","type":"ui_gauge","z":"8102f2ad6305cf60","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":920,"y":1120,"wires":[]},{"id":"efc68f7b1ad54ff1","type":"debug","z":"8102f2ad6305cf60","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":1200,"wires":[]},{"id":"0e5e19ded032ba48","type":"function","z":"8102f2ad6305cf60","name":"Set measurement value","func":"msg.value = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":1120,"wires":[["83e4c24fab4c374b"]]},{"id":"21a9fd8531d7d058","type":"ui_chart","z":"8102f2ad6305cf60","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":920,"y":1160,"wires":[[]]},{"id":"e4be5ab290fee203","type":"function","z":"8102f2ad6305cf60","name":"Set measurement value","func":"msg.payload = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":1160,"wires":[["21a9fd8531d7d058"]]},{"id":"eb77935aa44eeb1e","type":"function","z":"8102f2ad6305cf60","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":1120,"wires":[["0e5e19ded032ba48","e4be5ab290fee203","efc68f7b1ad54ff1"]]},{"id":"d5c2659b.267728","type":"mqtt-broker","name":"Demo server","broker":"demo.vscp.org","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":"","credentials":{}},{"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]](https://nodered.org/docs/user-guide/editor/workspace/import-export).

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:00:00:01/1040/6/1/4/#

The

25:00:00:00:00:00:00:00:00:00:00:00:06:00: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:00: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:00: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:00: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

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

Clone this wiki locally