Created
March 6, 2016 22:39
-
-
Save dexterlabora/07be3176b84b75ebdd25 to your computer and use it in GitHub Desktop.
Node-RED Flow to automate a LEGO Train Track Switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"id":"e33ec540.1cc138","type":"ui_tab","z":"","name":"IoL","icon":"dashboard","order":"1"},{"id":"904e0cc3.6fb1f","type":"pubnub-keys","pub_key":"","sub_key":""},{"id":"3e17c7ef.c1e838","type":"pubnub-keys","pub_key":"","sub_key":""},{"id":"a0f49a5c.5f0b68","type":"pubnub-keys","pub_key":"","sub_key":""},{"id":"38e603df.c719fc","type":"pubnub in","z":"11e87d2f.ee1783","keys":"a0f49a5c.5f0b68","channel":"iol","x":110,"y":2040,"wires":[["78eddfd0.87122"]]},{"id":"78eddfd0.87122","type":"switch","z":"11e87d2f.ee1783","name":"filter","property":"payload.trainCrossing","propertyType":"msg","rules":[{"t":"eq","v":"activated","vt":"str"},{"t":"eq","v":"deactivated","vt":"str"}],"checkall":"true","outputs":2,"x":243.0632553100586,"y":2039.9999885559082,"wires":[["1c1a4db2.e3e5b2"],["fb1a6548.04e598"]]},{"id":"70e3647c.8f1c9c","type":"function","z":"11e87d2f.ee1783","name":"msg.topic = \"safety\";","func":"msg.topic = \"safety\";\nreturn msg;","outputs":1,"noerr":0,"x":598.7888717651367,"y":2188.6197032928467,"wires":[["68acf79c.975308"]]},{"id":"982a74fb.67d588","type":"inject","z":"11e87d2f.ee1783","name":"Make Safe","topic":"","payload":"safe","payloadType":"str","repeat":"","crontab":"","once":false,"x":120,"y":2100,"wires":[["74413724.8bbec8"]]},{"id":"fd1a63a2.02e5a","type":"inject","z":"11e87d2f.ee1783","name":"Make Unsafe","topic":"","payload":"unsafe","payloadType":"str","repeat":"","crontab":"","once":true,"x":128,"y":2184,"wires":[["70e3647c.8f1c9c"]]},{"id":"7083f6f.f8f7c08","type":"function","z":"11e87d2f.ee1783","name":"{\"command\":\"track_straight\"}","func":"msg.payload = {\"command\":\"track_straight\"};\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":1680,"wires":[["eda80d32.1257f"]]},{"id":"7b12acbb.84ed54","type":"function","z":"11e87d2f.ee1783","name":"{\"command\":\"track_turn\"}","func":"msg.payload = {\"command\":\"track_turn\"};\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":1740,"wires":[["eda80d32.1257f"]]},{"id":"cfab2c7b.3054d","type":"inject","z":"11e87d2f.ee1783","name":"Track Switch - Straight","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":160,"y":1680,"wires":[["7083f6f.f8f7c08"]]},{"id":"1f55cfe3.e0aa3","type":"inject","z":"11e87d2f.ee1783","name":"Track Switch - Turn","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"x":150,"y":1740,"wires":[["7b12acbb.84ed54"]]},{"id":"eda80d32.1257f","type":"function","z":"11e87d2f.ee1783","name":"msg.topic = \"command\";","func":"msg.topic = \"command\";\nreturn msg;","outputs":1,"noerr":0,"x":850,"y":1700,"wires":[["68acf79c.975308"]]},{"id":"61e67710.9e1988","type":"pubnub out","z":"11e87d2f.ee1783","keys":"3e17c7ef.c1e838","channel":"iol","x":830,"y":2160,"wires":[]},{"id":"fb1a6548.04e598","type":"function","z":"11e87d2f.ee1783","name":"No Train","func":"msg.payload = \"safe\";\nreturn msg;","outputs":1,"noerr":0,"x":397.3731231689453,"y":2075.6902360916138,"wires":[["74413724.8bbec8"]]},{"id":"1c1a4db2.e3e5b2","type":"function","z":"11e87d2f.ee1783","name":"Train","func":"msg.payload = \"unsafe\";\nreturn msg;","outputs":1,"noerr":0,"x":389.52806091308594,"y":2033.5351314544678,"wires":[["70e3647c.8f1c9c"]]},{"id":"74413724.8bbec8","type":"delay","z":"11e87d2f.ee1783","name":"","pauseType":"delay","timeout":"4","timeoutUnits":"seconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":399.52806854248047,"y":2117.845006942749,"wires":[["70e3647c.8f1c9c"]]},{"id":"c1a90998.3e56f8","type":"pubnub in","z":"11e87d2f.ee1783","keys":"904e0cc3.6fb1f","channel":"trainschedule","x":130,"y":1880,"wires":[["1b49e0a8.e4b61f"]]},{"id":"8c998e73.73667","type":"switch","z":"11e87d2f.ee1783","name":"Enfield (S) or Cheshunt (T)","property":"payload.destination","propertyType":"msg","rules":[{"t":"cont","v":"Enfield","vt":"str"},{"t":"cont","v":"Cheshunt","vt":"str"}],"checkall":"true","outputs":2,"x":590,"y":1840,"wires":[["7083f6f.f8f7c08"],["7b12acbb.84ed54"]]},{"id":"75052294.8afadc","type":"function","z":"11e87d2f.ee1783","name":"Next Train","func":"var trains = [];\ntrains = msg.payload.schedule;\nmsg.payload = trains[0];\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":1940,"wires":[["8c998e73.73667","ccd4bf0f.332b4"]]},{"id":"ccd4bf0f.332b4","type":"debug","z":"11e87d2f.ee1783","name":"Next Train","active":true,"console":"false","complete":"payload","x":850,"y":1940,"wires":[]},{"id":"1b49e0a8.e4b61f","type":"switch","z":"11e87d2f.ee1783","name":"London Fields Station","property":"payload.station","propertyType":"msg","rules":[{"t":"cont","v":"London Fields","vt":"str"}],"checkall":"true","outputs":1,"x":360,"y":1880,"wires":[["75052294.8afadc"]]},{"id":"e7b810ae.1847f","type":"comment","z":"11e87d2f.ee1783","name":"Track Switch Automation","info":"Connect PubNub to Switch node","x":130,"y":1840,"wires":[]},{"id":"e8913619.176ec8","type":"comment","z":"11e87d2f.ee1783","name":"Check if Train is Approaching","info":"","x":140,"y":2000,"wires":[]},{"id":"d40d49aa.2bf2b8","type":"comment","z":"11e87d2f.ee1783","name":"Train Track Switch","info":"","x":110,"y":1540,"wires":[]},{"id":"8c793c93.7386c","type":"ui_switch","z":"11e87d2f.ee1783","tab":"e33ec540.1cc138","name":"Track Switch","topic":"command","group":"Train Track Switch","order":1,"onvalue":"{\"command\":\"track_turn\"}","offvalue":"{\"command\":\"track_straight\"}","x":590,"y":1560,"wires":[["89876.fff7678a","819e7c09.7e618"]]},{"id":"6735669b.98ca98","type":"pubnub in","z":"11e87d2f.ee1783","keys":"a0f49a5c.5f0b68","channel":"iol","x":110,"y":1600,"wires":[["528fa3c5.ad705c"]]},{"id":"89876.fff7678a","type":"json","z":"11e87d2f.ee1783","name":"","x":970,"y":1600,"wires":[["68acf79c.975308"]]},{"id":"528fa3c5.ad705c","type":"switch","z":"11e87d2f.ee1783","name":"filter","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"{\"command\":\"track_straight\"}","vt":"str"},{"t":"eq","v":"{\"command\":\"track_turn\"}","vt":"str"}],"checkall":"true","outputs":2,"x":370,"y":1600,"wires":[["8c793c93.7386c"],["8c793c93.7386c"]]},{"id":"46db9d95.b92464","type":"status","z":"11e87d2f.ee1783","name":"Safety Gate Status","scope":["68acf79c.975308"],"x":840,"y":2020,"wires":[["5e2edecb.a1d12"]]},{"id":"5e2edecb.a1d12","type":"function","z":"11e87d2f.ee1783","name":"status text","func":"msg.payload = msg.status.text;\nreturn msg;","outputs":1,"noerr":0,"x":850,"y":2060,"wires":[["1f42ea27.e0bd16"]]},{"id":"1f42ea27.e0bd16","type":"ui_template","z":"11e87d2f.ee1783","tab":"e33ec540.1cc138","name":"Safety","group":"Train Track Switch","order":1,"format":"<div layout=\"row\" layout-align=\"space-between\">\n <p>The track switch is:</p>\n <p ng-style=\"{color: msg.payload == 'safe' ? 'green' : 'red'}\">\n {{msg.payload == 'safe' ? 'safe' : 'unsafe'}}\n </p>\n</div>","storeOutMessages":true,"fwdInMessages":true,"x":830,"y":2100,"wires":[[]]},{"id":"944577ac.6bba88","type":"ui_button","z":"11e87d2f.ee1783","tab":"e33ec540.1cc138","name":"Make Safe","payload":"safe","topic":"","group":"Train Track Switch","order":1,"x":120.5,"y":2139,"wires":[["74413724.8bbec8"]]},{"id":"9c3f59a2.63c0a8","type":"ui_button","z":"11e87d2f.ee1783","tab":"e33ec540.1cc138","name":"Make Unsafe","payload":"unsafe","topic":"","group":"Train Track Switch","order":1,"x":130.5,"y":2219,"wires":[["70e3647c.8f1c9c"]]},{"id":"68acf79c.975308","type":"function","z":"11e87d2f.ee1783","name":"Safety Gate","func":"// this function stores the state of two inputs, safety && command. If the \n// safety state is not safe, the command is stored until the safety state changes to \n// safe. The node will notify state status by color and text\n\nflow.set('state', flow.get('data') || {});\ncontext.set('data', context.get('data') || {});\n\n// check initial safety state\nif (flow.get('state.safety') === undefined){\n node.status({fill:\"yellow\",shape:\"ring\",text:flow.get('state.safety')}); \n}\n\n// store all messages and update safety state color\nswitch(msg.topic){\n case \"safety\":\n // set safety state\n //context.data.safety = msg.payload;\n flow.set('data.safety', msg.payload);\n if (flow.get('state.safety') == \"safe\"){\n node.status({fill:\"green\",shape:\"ring\",text:\"safe\"});\n }\n if (flow.get('state.safety') == \"unsafe\"){\n node.status({fill:\"red\",shape:\"ring\",text:\"unsafe\"}); \n }\n break;\n case \"command\":\n context.set('data.command', msg.payload);\n break;\n default:\n break;\n}\n\n\nif(flow.get('state.safety') == \"safe\"){\n if(context.get('data.command') !== undefined){\n // send command\n msg.payload = context.get('data.command');\n context.set('data.command', undefined); // reset command data for next trigger\n }else{\n //msg.payload = {\"news\":\"safe, but no command available\"};\n }\n}else{\n msg.payload = {\"news\":\"unsafe to switch track\"};\n}\nreturn msg;","outputs":1,"noerr":0,"x":630.6055450439453,"y":2039.2608127593994,"wires":[["61e67710.9e1988"]]},{"id":"2cbe18ad.d341e8","type":"delay","z":"11e87d2f.ee1783","name":"","pauseType":"delay","timeout":"500","timeoutUnits":"milliseconds","rate":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":670,"y":1600,"wires":[["8c793c93.7386c"]]},{"id":"819e7c09.7e618","type":"function","z":"11e87d2f.ee1783","name":"","func":"if (flow.get('state.safety')){\n console.log(\"msg.payload.command = \"+msg.payload.command)\n if (flow.get('state.safety') == \"unsafe\"){\n node.status({fill:\"red\",shape:\"ring\",text:\"unsafe\"}); \n if(msg.payload.command == \"track_straight\"){\n msg.payload.command = \"track_turn\";\n }else{\n msg.payload.command = \"track_straight\";\n }\n return msg;\n }\n}else{\n node.status({fill:\"yellow\",shape:\"ring\",text:\"safety state unkown\"});\n}\nreturn null\n","outputs":1,"noerr":0,"x":530,"y":1600,"wires":[["2cbe18ad.d341e8"]]}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment