Skip to content

Instantly share code, notes, and snippets.

@dexterlabora
Last active September 12, 2016 22:06
Show Gist options
  • Save dexterlabora/247f4cc8e51da4728adf86da5904542d to your computer and use it in GitHub Desktop.
Save dexterlabora/247f4cc8e51da4728adf86da5904542d to your computer and use it in GitHub Desktop.
This Node-RED flow uses the node-red-contrib-gpio nodes to control a LEGO train crossing using an Arduino nano via Raspberry Pi.
[{"id":"9444f83b.ef0ad8","type":"nodebot","z":"a95b80de.67f24","name":"","username":"","password":"","boardType":"firmata","serialportName":"/dev/ttyUSB0","connectionType":"local","mqttServer":"","socketServer":"","pubTopic":"","subTopic":"","tcpHost":"","tcpPort":"","sparkId":"","sparkToken":"","beanId":"","impId":"","meshbluServer":"https://meshblu.octoblu.com","uuid":"","token":"","sendUuid":""},{"id":"98648f72.7b1ad","type":"inject","z":"a95b80de.67f24","name":"Activate","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":103.5,"y":202,"wires":[["3b80da05.e922c6"]]},{"id":"73514cd1.a256c4","type":"function","z":"a95b80de.67f24","name":"Servo 90 degrees","func":"msg.payload = 90;\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":180,"wires":[["71805320.3193ac"]]},{"id":"52f22a6f.2717d4","type":"function","z":"a95b80de.67f24","name":"Servo 180 degrees","func":"msg.payload = 180;\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":240,"wires":[["7ab41623.1c8488"]]},{"id":"9e587773.317e08","type":"gpio out","z":"a95b80de.67f24","name":"[6] LED1","state":"OUTPUT","pin":"6","i2cDelay":"0","i2cAddress":"","i2cRegister":"","outputs":0,"board":"9444f83b.ef0ad8","x":740,"y":320,"wires":[]},{"id":"e98d0f47.4e2e1","type":"gpio out","z":"a95b80de.67f24","name":"[2] LED2","state":"OUTPUT","pin":"2","i2cDelay":"0","i2cAddress":"","i2cRegister":"","outputs":0,"board":"9444f83b.ef0ad8","x":740,"y":440,"wires":[]},{"id":"71805320.3193ac","type":"gpio out","z":"a95b80de.67f24","name":"[4] Servo","state":"SERVO","pin":"4","i2cDelay":"0","i2cAddress":"","i2cRegister":"","outputs":0,"board":"9444f83b.ef0ad8","x":740,"y":180,"wires":[]},{"id":"7ab41623.1c8488","type":"gpio out","z":"a95b80de.67f24","name":"[4] Servo","state":"SERVO","pin":"4","i2cDelay":"0","i2cAddress":"","i2cRegister":"","outputs":0,"board":"9444f83b.ef0ad8","x":740,"y":240,"wires":[]},{"id":"ab3ce00.3a3962","type":"debug","z":"a95b80de.67f24","name":"LED1 Toggle","active":true,"console":"false","complete":"payload","x":730,"y":380,"wires":[]},{"id":"3b80da05.e922c6","type":"trigger","z":"a95b80de.67f24","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"2","extend":true,"units":"s","reset":"","name":"","x":260,"y":260,"wires":[["8580cbe8.20f328","3fedc3d.dfca43c"]]},{"id":"8580cbe8.20f328","type":"switch","z":"a95b80de.67f24","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","outputs":2,"x":370,"y":220,"wires":[["73514cd1.a256c4"],["52f22a6f.2717d4"]]},{"id":"8320372b.203128","type":"gpio in","z":"a95b80de.67f24","name":"[5] Train Sensor","state":"INPUT","samplingInterval":"300","pin":"5","board":"9444f83b.ef0ad8","x":103.5,"y":122,"wires":[["3b80da05.e922c6","6ac8d722.5c6548"]]},{"id":"6ac8d722.5c6548","type":"debug","z":"a95b80de.67f24","name":"Train Sensor","active":true,"console":"false","complete":"payload","x":730,"y":120,"wires":[]},{"id":"1ef4d96b.0269d7","type":"comment","z":"a95b80de.67f24","name":"Train Crossing","info":"This flow uses the J5 node modules.\nA sensor will detect the train and trigger \nthe servo to move the crossing arm.\nA J5 function will blink two LEDs\n\nMore info:\nhttps://github.com/monteslu/node-red-contrib-gpio\n\n\nWritten by\n\nCory Guynn\n2016\nwww.InternetOfLEGO.com\n","x":83.5,"y":42,"wires":[]},{"id":"9d256d4f.ad452","type":"inject","z":"a95b80de.67f24","name":"LED1 - Off","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":100,"y":360,"wires":[["9e587773.317e08"]]},{"id":"3488924a.b38a0e","type":"inject","z":"a95b80de.67f24","name":"LED1 - On","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":100,"y":320,"wires":[["9e587773.317e08"]]},{"id":"82efae24.701c8","type":"inject","z":"a95b80de.67f24","name":"LED2 - Off","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":100,"y":480,"wires":[["e98d0f47.4e2e1"]]},{"id":"8900ee67.75fa6","type":"inject","z":"a95b80de.67f24","name":"LED2 - On","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":100,"y":440,"wires":[["e98d0f47.4e2e1"]]},{"id":"e681bd62.24ea7","type":"inject","z":"a95b80de.67f24","name":"On","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":155.5,"y":807,"wires":[["53204e3a.c0455"]]},{"id":"1de5b25f.da9f6e","type":"inject","z":"a95b80de.67f24","name":"Up","topic":"","payload":"180","payloadType":"num","repeat":"","crontab":"","once":false,"x":155.5,"y":707,"wires":[["4be32c13.f81384"]]},{"id":"82d70429.3eeab8","type":"inject","z":"a95b80de.67f24","name":"Off","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":155.5,"y":847,"wires":[["53204e3a.c0455"]]},{"id":"613c7ea2.00ee2","type":"inject","z":"a95b80de.67f24","name":"Off","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"x":154.5,"y":941,"wires":[["34bddcb5.aeaa04"]]},{"id":"84039f39.d1118","type":"inject","z":"a95b80de.67f24","name":"Down","topic":"","payload":"90","payloadType":"num","repeat":"","crontab":"","once":false,"x":155.5,"y":747,"wires":[["a7003960.931d78"]]},{"id":"d4617c37.236ee","type":"inject","z":"a95b80de.67f24","name":"On","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"x":154.5,"y":901,"wires":[["34bddcb5.aeaa04"]]},{"id":"4be32c13.f81384","type":"function","z":"a95b80de.67f24","name":"Servo 180 degrees","func":"msg.payload = 180;\nreturn msg;","outputs":1,"noerr":0,"x":535.5,"y":707,"wires":[["99e28d94.8b235"]]},{"id":"a7003960.931d78","type":"function","z":"a95b80de.67f24","name":"Servo 90 degrees","func":"msg.payload = 90;\nreturn msg;","outputs":1,"noerr":0,"x":535.5,"y":747,"wires":[["99e28d94.8b235"]]},{"id":"c114e912.dde9f8","type":"comment","z":"a95b80de.67f24","name":"Utilities","info":"","x":55,"y":648,"wires":[]},{"id":"53204e3a.c0455","type":"gpio out","z":"a95b80de.67f24","name":"[2] LED2","state":"OUTPUT","pin":"2","i2cDelay":"0","i2cAddress":"","i2cRegister":"","outputs":0,"board":"9444f83b.ef0ad8","x":740,"y":800,"wires":[]},{"id":"34bddcb5.aeaa04","type":"gpio out","z":"a95b80de.67f24","name":"[6] LED1","state":"OUTPUT","pin":"6","i2cDelay":"0","i2cAddress":"","i2cRegister":"","outputs":0,"board":"9444f83b.ef0ad8","x":740,"y":900,"wires":[]},{"id":"3fedc3d.dfca43c","type":"function","z":"a95b80de.67f24","name":"Toggle 500ms - 2 Outputs","func":"// Toggles a 1 or 0\n// if payload == 1, toggle interval starts\n// if payload != 1, toggle interval stops\n\ncontext.state = context.state || 1;\nvar msg2 = {};\n\n// Turn interval cycle on or off\nif (msg.payload == 1){\n // send initial msg since setInterval waits a cycle before sending the first message\n node.send(msg);\n context.toggle = setInterval(function(){\n // set initial state to first output\n msg2.payload = context.state;\n // flip the bit\n (context.state === 0) ? context.state = 1 : context.state = 0;\n // set opposite bit to second output\n msg.payload = context.state;\n // send message object to outputs\n node.send([msg,msg2]);\n }, 500); // milliseconds between inteverals\n}else{\n // Turn off \n clearTimeout(context.toggle);\n msg.payload = 0;\n msg2.payload = 0;\n node.send([msg,msg2]);\n}\nreturn null;","outputs":"2","noerr":0,"x":450,"y":400,"wires":[["ab3ce00.3a3962","9e587773.317e08"],["e98d0f47.4e2e1","b398a647.6047b8"]]},{"id":"b398a647.6047b8","type":"debug","z":"a95b80de.67f24","name":"LED2 Toggle","active":true,"console":"false","complete":"payload","x":730,"y":500,"wires":[]},{"id":"99e28d94.8b235","type":"gpio out","z":"a95b80de.67f24","name":"[4] Servo","state":"SERVO","pin":"4","i2cDelay":"0","i2cAddress":"","i2cRegister":"","outputs":0,"board":"9444f83b.ef0ad8","x":740,"y":720,"wires":[]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment