-
-
Notifications
You must be signed in to change notification settings - Fork 20
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.
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":"345d5894.d62588","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":"cfd5e8c8.289d38","nl":false,"rap":true,"rh":0,"x":210,"y":320,"wires":[["eb77935aa44eeb1e"]]},{"id":"83e4c24fab4c374b","type":"ui_gauge","z":"345d5894.d62588","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":860,"y":320,"wires":[]},{"id":"efc68f7b1ad54ff1","type":"debug","z":"345d5894.d62588","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":670,"y":400,"wires":[]},{"id":"0e5e19ded032ba48","type":"function","z":"345d5894.d62588","name":"","func":"msg.value = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":320,"wires":[["83e4c24fab4c374b"]]},{"id":"21a9fd8531d7d058","type":"ui_chart","z":"345d5894.d62588","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":860,"y":360,"wires":[[]]},{"id":"e4be5ab290fee203","type":"function","z":"345d5894.d62588","name":"","func":"msg.payload = msg.payload.measurement.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":360,"wires":[["21a9fd8531d7d058"]]},{"id":"eb77935aa44eeb1e","type":"function","z":"345d5894.d62588","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":420,"y":320,"wires":[["0e5e19ded032ba48","e4be5ab290fee203","efc68f7b1ad54ff1"]]},{"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 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
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.
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
The VSCP Project (https://www.vscp.org) - Copyright © 2000-2024 Åke Hedman, the VSCP Project