Node-RED – Voorbeeld – CAN Bus uitlezen
Op deze pagina vind je een voorbeeld hoe je CAN Bus uitleest in Node-Red dmv een aantal vcoorbeelden.
Wat heb je nodig?
- Node-Red can bus plugin: node-red-contrib-socketcan
Voorbeeld 1
Voeg een CAN Bus luister-node toe en configureer:
- CAN Bus device
Daarachter een output naar console, je ziet dan een (JSON) array verschijnen:
Om de data uit de array te filteren kan je een functie node gebruiken met meerdere uitgangen:
1 2 3 4 5 |
return [ {payload:msg.payload.data[0]}, {payload:msg.payload.data[1]}, {payload:msg.payload.data[2]}, ]; |
1 |
[{"id":"c7ece8e2.79b328","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"3bb60df2.f54a32","type":"socketcan-out","z":"c7ece8e2.79b328","name":"CAN Bus in","config":"c9a5916d.73411","x":390,"y":300,"wires":[["d64f8654.53eb38"]]},{"id":"d64f8654.53eb38","type":"function","z":"c7ece8e2.79b328","name":"Array parser","func":"return [\n{payload:msg.payload.data[0]},\n{payload:msg.payload.data[1]},\n{payload:msg.payload.data[2]},\n];\n","outputs":3,"noerr":0,"x":620,"y":300,"wires":[["717b8b39.937114"],["9227be6a.95994"],["33aa93a2.ba045c"]]},{"id":"717b8b39.937114","type":"debug","z":"c7ece8e2.79b328","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":890,"y":220,"wires":[]},{"id":"9227be6a.95994","type":"debug","z":"c7ece8e2.79b328","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":890,"y":280,"wires":[]},{"id":"33aa93a2.ba045c","type":"debug","z":"c7ece8e2.79b328","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":890,"y":340,"wires":[]},{"id":"c9a5916d.73411","type":"socketcan-config","z":"","interface":"can0"}] |
Uitsplitsen van device ID
Dmv een split node kan je de device ID uitsplitsen:
1 |
[{"id":"cbe59932.dff8a8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"14f7c5d1.d0dbea","type":"socketcan-out","z":"cbe59932.dff8a8","name":"CAN Bus in","config":"c9a5916d.73411","x":470,"y":320,"wires":[["d5ede846.4f5368"]]},{"id":"4c9ee744.086d68","type":"function","z":"cbe59932.dff8a8","name":"Array parser","func":"\n\nreturn [\n{payload:msg.payload.data[0]},\n{payload:msg.payload.data[1]},\n{payload:msg.payload.data[2]},\n];","outputs":3,"noerr":0,"x":950,"y":220,"wires":[["262821b3.43cfee"],["86dbe80c.0c8088"],["3fac196b.7c9526"]]},{"id":"262821b3.43cfee","type":"debug","z":"cbe59932.dff8a8","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1250,"y":160,"wires":[]},{"id":"86dbe80c.0c8088","type":"debug","z":"cbe59932.dff8a8","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1250,"y":220,"wires":[]},{"id":"3fac196b.7c9526","type":"debug","z":"cbe59932.dff8a8","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1250,"y":280,"wires":[]},{"id":"d5ede846.4f5368","type":"switch","z":"cbe59932.dff8a8","name":"Filter CAN ID","property":"payload.canid","propertyType":"msg","rules":[{"t":"eq","v":"16","vt":"num"},{"t":"eq","v":"32","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":690,"y":320,"wires":[["4c9ee744.086d68"],["b72a1e20.f0eb5"]]},{"id":"b72a1e20.f0eb5","type":"function","z":"cbe59932.dff8a8","name":"Array parser","func":"\n\nreturn [\n{payload:msg.payload.data[0]},\n{payload:msg.payload.data[1]},\n{payload:msg.payload.data[2]},\n];","outputs":3,"noerr":0,"x":950,"y":420,"wires":[["96ee5314.45c0d"],["e0767783.83de68"],["99dc55de.eeb818"]]},{"id":"96ee5314.45c0d","type":"debug","z":"cbe59932.dff8a8","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1250,"y":360,"wires":[]},{"id":"e0767783.83de68","type":"debug","z":"cbe59932.dff8a8","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1250,"y":420,"wires":[]},{"id":"99dc55de.eeb818","type":"debug","z":"cbe59932.dff8a8","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":1250,"y":480,"wires":[]},{"id":"c03ebeef.f7b78","type":"comment","z":"cbe59932.dff8a8","name":"CAN ID 16 (0x10)","info":"","x":950,"y":160,"wires":[]},{"id":"6a145ea0.bb94c","type":"comment","z":"cbe59932.dff8a8","name":"CAN ID 32 (0x20)","info":"","x":950,"y":360,"wires":[]},{"id":"c9a5916d.73411","type":"socketcan-config","z":"","interface":"can0"}] |
Nog eenvoudiger zonder functie
Het kan nog eenvoudiger door meteen de array[] te gebruiken voor output:
1 |
[{"id":"cf75cd3a.bb7c6","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"99f6d9ae.e85cd8","type":"socketcan-out","z":"cf75cd3a.bb7c6","name":"CAN Bus in","config":"c9a5916d.73411","x":390,"y":420,"wires":[["b228611e.87135"]]},{"id":"7409d32a.fbfc3c","type":"debug","z":"cf75cd3a.bb7c6","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.data[0]","targetType":"msg","x":900,"y":260,"wires":[]},{"id":"60474ed9.b37db","type":"debug","z":"cf75cd3a.bb7c6","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.data[1]","targetType":"msg","x":900,"y":320,"wires":[]},{"id":"5d73ac93.1d0304","type":"debug","z":"cf75cd3a.bb7c6","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.data[2]","targetType":"msg","x":900,"y":380,"wires":[]},{"id":"b228611e.87135","type":"switch","z":"cf75cd3a.bb7c6","name":"Filter CAN ID","property":"payload.canid","propertyType":"msg","rules":[{"t":"eq","v":"16","vt":"num"},{"t":"eq","v":"32","vt":"num"}],"checkall":"true","repair":false,"outputs":2,"x":610,"y":420,"wires":[["7409d32a.fbfc3c","60474ed9.b37db","5d73ac93.1d0304"],["f66f557e.bb14f8","3914ee84.b19c82","948748b8.64a2a8"]]},{"id":"51483890.245058","type":"comment","z":"cf75cd3a.bb7c6","name":"CAN ID 16 (0x10)","info":"","x":630,"y":320,"wires":[]},{"id":"624957df.375a08","type":"comment","z":"cf75cd3a.bb7c6","name":"CAN ID 32 (0x20)","info":"","x":630,"y":520,"wires":[]},{"id":"f66f557e.bb14f8","type":"debug","z":"cf75cd3a.bb7c6","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.data[0]","targetType":"msg","x":900,"y":460,"wires":[]},{"id":"3914ee84.b19c82","type":"debug","z":"cf75cd3a.bb7c6","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.data[1]","targetType":"msg","x":900,"y":520,"wires":[]},{"id":"948748b8.64a2a8","type":"debug","z":"cf75cd3a.bb7c6","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload.data[2]","targetType":"msg","x":900,"y":580,"wires":[]},{"id":"c9a5916d.73411","type":"socketcan-config","z":"","interface":"can0"}] |
Voorbeeld om CAN Bus berichten te ontvangen:
Arduino code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include // Library for using SPI Communication #include // Library for using CAN Communication struct can_frame canMsg; MCP2515 mcp2515(10); // SPI CS Pin 10 void setup() { SPI.begin(); // Begins SPI communication Serial.begin(9600); // Begins Serial Communication at 9600 baud rate mcp2515.reset(); mcp2515.setBitrate(CAN_500KBPS,MCP_8MHZ); // Sets CAN at speed 500KBPS and Clock 8MHz mcp2515.setNormalMode(); // Sets CAN at normal mode } void loop(){ if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) { if (canMsg.can_id == 0x20) { if (canMsg.data[0] == 1) { Serial.println("Relais is AAN!"); } if (canMsg.data[0] == 0) { Serial.println("Relais is UIT!"); } } } }; |
Node-Red nodes:
1 |
[{"id":"6dbffa1a.8eaa44","type":"socketcan-in","z":"cbe59932.dff8a8","name":"CAN Bus out","config":"9a9b63c4.2dde5","x":910,"y":660,"wires":[]},{"id":"5dbe37b4.295df8","type":"inject","z":"cbe59932.dff8a8","name":"Relais AAN","topic":"","payload":"{\"ext\":false,\"rtr\":false,\"canid\":32,\"dlc\":8,\"data\":[1,0,0,0,0,0,0,0]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":670,"y":640,"wires":[["6dbffa1a.8eaa44"]]},{"id":"3861df6d.30471","type":"inject","z":"cbe59932.dff8a8","name":"Relais UIT","topic":"","payload":"{\"ext\":false,\"rtr\":false,\"canid\":32,\"dlc\":8,\"data\":[0,0,0,0,0,0,0,0]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":680,"y":700,"wires":[["6dbffa1a.8eaa44"]]},{"id":"c217698f.47fb28","type":"comment","z":"cbe59932.dff8a8","name":"CAN ID 32 (0x20)","info":"","x":910,"y":600,"wires":[]},{"id":"9a9b63c4.2dde5","type":"socketcan-config","z":"cbe59932.dff8a8","interface":"can0"}] |
Payload code in relais AAN: {“ext”:false,”rtr”:false,”canid”:32,”dlc”:8,”data”:[1,0,0,0,0,0,0,0]}
Payload code in relais UIT: {“ext”:false,”rtr”:false,”canid”:32,”dlc”:8,”data”:[0,0,0,0,0,0,0,0]}