Node-RED Dashboard & HC-SR501 PIR Motion Sensor

Node-RED Dashboard HC-SR501

I am using a Raspberry Pi as a headless computer through VNC, along with Node-RED flow programming, and in this instance Python 3.

This is Revision 2 of my previous blogs, and adds control of a HC-SR501 Motion Sensor to the node-red-dashboard by using a Node-RED Exec Node to spawn a system process to a Python script that interacts with the motion sensor.

This inexpensive (< $2) motion sensor module is easy to connect with two jumpers to the +5v rails and one to Pin26 on the Raspberry Pi. Reference My Pi Workbench for details on the test bench.

The information at Interfacing HC-SR501 PIR Motion Sensor with Raspberry Pi and ModMyPi – Raspberry Pi GPIO Sensing: Motion Detection are very detailed and I thank the authors for their thoroughness!

The ModMyPi link describes two Python scripts to control the HC-SR501 – a Wait & Loop and the Interrupt Driven Script. I used the later with the Raspbian Thonny IDE to enter the script and test the motion sensor. The only changes to the script are ()’s and quotes around the print commands – thereby upgrading the script to Python 3.

Captured standard output (print) messages are returned to the dashboard log and also passed to a Play Audio Node for text to speech alerts. Because the Dashboard is an executing web page on the desktop, and when using a text enabled browser such as Chrome, the output messages become spoken words! Motion Detected could become anything – maybe Danger, Danger – Will Robinson!

The Python system process is terminated on command from the HS-SR501 switch on the dashboard.

I have described building a demonstration Pi Control dashboard and this project Example 3 in Node-RED Dashboard – Pi Control Rev2.

Separately, the wiring diagram is simplified in this revision by using  Link Nodes, and Switch and Change Nodes have been used rather that writing a Javascript Funtion Node.

Copy the following Node-RED Dashboard – HC-SR501 & Log Tabs JSON flow to your clipboard and then import it into Node-RED using the Import From > Clipboard (Ctrl-I) menu option

[{"id":"52700294.bedf9c","type":"tab","label":"Dashboard - HC-SR501","disabled":false,"info":""},{"id":"96c74cb5.3a1e4","type":"tab","label":"Log","disabled":false,"info":""},{"id":"6b8667fc.4b0c98","type":"ui_base","theme":{"name":"theme-light","lightTheme":{"default":"#0094CE","baseColor":"#0094CE","baseFont":"Helvetica Neue","edited":true,"reset":false},"darkTheme":{"default":"#097479","baseColor":"#097479","baseFont":"Helvetica Neue","edited":true,"reset":false},"customTheme":{"name":"Pi Control Theme","default":"#4B7930","baseColor":"#0094ce","baseFont":"Helvetica Neue","reset":false},"themeState":{"base-color":{"default":"#0094CE","value":"#0094CE","edited":false},"page-titlebar-backgroundColor":{"value":"#0094CE","edited":true},"page-backgroundColor":{"value":"#fafafa","edited":false},"page-sidebar-backgroundColor":{"value":"#ffffff","edited":false},"group-textColor":{"value":"#000000","edited":false},"group-borderColor":{"value":"#ffffff","edited":true},"group-backgroundColor":{"value":"#ffffff","edited":false},"widget-textColor":{"value":"#111111","edited":false},"widget-backgroundColor":{"value":"#0094ce","edited":false},"widget-borderColor":{"value":"#ffffff","edited":false}}},"site":{"name":"Pi Control","hideToolbar":"false","allowSwipe":"true","dateFormat":"DD/MM/YYYY","sizes":{"sx":48,"sy":48,"gx":6,"gy":6,"cx":6,"cy":6,"px":0,"py":0}}},{"id":"7b28ce76.8d16c","type":"ui_tab","z":"","name":"Home","icon":"home","order":1},{"id":"dc750d48.9b06","type":"ui_group","z":"","name":"Control","tab":"7b28ce76.8d16c","order":1,"disp":true,"width":"5"},{"id":"17fa93d3.0d985c","type":"ui_group","z":"96c74cb5.3a1e4","name":"Log","tab":"7b28ce76.8d16c","order":3,"disp":true,"width":"6"},{"id":"2ee3b487.6fff4c","type":"ui_switch","z":"52700294.bedf9c","name":"Log Switch","label":"Log Off/On","group":"dc750d48.9b06","order":0,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"Log Enabled?","style":"","onvalue":"1","onvalueType":"num","onicon":"fa-toggle-on","oncolor":" #008000","offvalue":"0","offvalueType":"num","officon":"fa-toggle-off","offcolor":"#800080","x":330,"y":280,"wires":[["49f12ea7.ce79e","3116731d.3a4a0c"]]},{"id":"2ee2d708.73c5b8","type":"ui_switch","z":"52700294.bedf9c","name":"LED Switch","label":"LED @ Pin 37","group":"dc750d48.9b06","order":0,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"LED @ pin 37","style":"","onvalue":"1","onvalueType":"num","onicon":"radio_button_checked","oncolor":"#008000","offvalue":"0","offvalueType":"num","officon":"radio_button_unchecked","offcolor":"#FF0000","x":330,"y":140,"wires":[["297849f9.58a866","2e3f48cf.c83658"]]},{"id":"2b0daa35.ef1ff6","type":"ui_switch","z":"52700294.bedf9c","name":"BlinkM Switch","label":"BlinkM Off/On","group":"dc750d48.9b06","order":0,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"BlinkM","style":"","onvalue":" -y 0x01 0x09 0x6e 0xff 0xff 0xff i","onvalueType":"str","onicon":"radio_button_checked","oncolor":"#008000","offvalue":" -y 0x01 0x09 0x6e 0x00 0x00 0x00 i","offvalueType":"str","officon":"radio_button_unchecked","offcolor":"#FF0000","x":340,"y":440,"wires":[["6c1a6e20.9e44d","2a7b42cc.1b7a1e"]]},{"id":"c447ac58.2ae6","type":"ui_colour_picker","z":"52700294.bedf9c","name":"BlinkM Color","label":"BlinkM Color","group":"dc750d48.9b06","format":"hex","outformat":"string","showSwatch":true,"showPicker":true,"showValue":true,"showAlpha":false,"showLightness":false,"order":0,"width":"4","height":"4","passthru":true,"topic":"BlinM Color","x":330,"y":600,"wires":[["b8861ed5.3541","3343098a.669f16"]]},{"id":"49f12ea7.ce79e","type":"function","z":"52700294.bedf9c","name":"Toggle Global.LogSw","func":"\nif (msg.payload === 1){\n global.set(\"LogSw\",1); \n} \n\nif (msg.payload === 0){\n global.set(\"LogSw\",0); \n} \nreturn msg;","outputs":1,"noerr":0,"x":600,"y":280,"wires":[[]]},{"id":"297849f9.58a866","type":"rpi-gpio out","z":"52700294.bedf9c","name":"LED Off/On","pin":"37","set":true,"level":"0","freq":"","out":"out","x":570,"y":140,"wires":[]},{"id":"6c1a6e20.9e44d","type":"exec","z":"52700294.bedf9c","command":"i2cset","addpay":true,"append":"","useSpawn":"","timer":"","name":"Exec BlinkM @ I2C","x":810,"y":440,"wires":[[],[],[]]},{"id":"b8861ed5.3541","type":"function","z":"52700294.bedf9c","name":"Hex to i2c cmd","func":"// Accepts a chosen color for the BlinkM in Hex and strings the\n// values into a parameter set for an i2cset command\nvar hx = msg.payload;\nvar i2c_cmd = (\" -y 0x01 0x09 0x6e 0x\" + \nhx.substring(1,3) + \" 0x\" + \nhx.substring(3,5) +\" 0x\" + \nhx.substring(5,7) + \n\" i\"); \nmsg.payload = i2c_cmd;\nreturn msg;","outputs":1,"noerr":0,"x":580,"y":600,"wires":[["6c1a6e20.9e44d"]]},{"id":"37266d63.491cb2","type":"config","z":"52700294.bedf9c","name":"","properties":[{"p":"LogSw","pt":"global","to":"0","tot":"num"}],"active":true,"x":110,"y":60,"wires":[]},{"id":"d1673d3e.2ea5e","type":"exec","z":"52700294.bedf9c","command":"","addpay":true,"append":"","useSpawn":"true","timer":"","oldrc":false,"name":"HC-SR501 Motion Sensor","x":990,"y":780,"wires":[["4c53fb6a.664094","908390f9.6aa74"],[],[]]},{"id":"f2b2c017.59b0d","type":"change","z":"52700294.bedf9c","name":"Start Sensing","rules":[{"t":"set","p":"payload","pt":"msg","to":"python -u /home/pi/MyWeb/Python/HC-SR501_Motion_Int.py","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":740,"wires":[["d1673d3e.2ea5e"]]},{"id":"8e9d36a5.14f398","type":"change","z":"52700294.bedf9c","name":"Kill SIGTERM","rules":[{"t":"set","p":"kill","pt":"msg","to":"SIGTERM","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":820,"wires":[["d1673d3e.2ea5e"]]},{"id":"2e3f48cf.c83658","type":"link out","z":"52700294.bedf9c","name":"Log","links":["d3d09753.88bdd8"],"x":515,"y":200,"wires":[]},{"id":"f02fe3ea.13c6d","type":"function","z":"96c74cb5.3a1e4","name":"Record Events","func":"// initialise the counter to 0 if it doesn't exist already\nvar dashboardLog = context.get('dashboardLog')|| [];\n\n// stop flow when LogSw Off\nif (global.get(\"LogSw\") === 0){\n return null;\n} \n\ndashboardLog.push(msg);\nif (dashboardLog.length > 12){\n // Delete oldest message if > 12\n dashboardLog.shift();\n dashboardLog.length = 12;\n} \n// store the value back\ncontext.set('dashboardLog',dashboardLog);\n// make it part of the outgoing msg object\nmsg = {};\nmsg.payload = dashboardLog;\nreturn msg;","outputs":"1","noerr":0,"x":380,"y":160,"wires":[["673b7a21.456814"]]},{"id":"673b7a21.456814","type":"ui_template","z":"96c74cb5.3a1e4","group":"17fa93d3.0d985c","name":"Event Log","order":1,"width":"6","height":"9","format":"<ul>\n <li ng-repeat=\"x in msg.payload\">\n <font size=\"2\" color=\"green\">{{x.topic}}</font>\n <ul>\n <li>\n <font size=\"2\" color=\"black\">{{x.payload}}</font>\n </li>\n </ul>\n </li>\n</ul>","storeOutMessages":true,"fwdInMessages":true,"x":600,"y":160,"wires":[[]]},{"id":"d3d09753.88bdd8","type":"link in","z":"96c74cb5.3a1e4","name":"Log","links":["2e3f48cf.c83658","3116731d.3a4a0c","3343098a.669f16","2a7b42cc.1b7a1e","8ef17243.6bf8e","4c53fb6a.664094"],"x":215,"y":160,"wires":[["f02fe3ea.13c6d"]]},{"id":"3116731d.3a4a0c","type":"link out","z":"52700294.bedf9c","name":"","links":["d3d09753.88bdd8"],"x":515,"y":340,"wires":[]},{"id":"3343098a.669f16","type":"link out","z":"52700294.bedf9c","name":"","links":["d3d09753.88bdd8"],"x":515,"y":660,"wires":[]},{"id":"2a7b42cc.1b7a1e","type":"link out","z":"52700294.bedf9c","name":"","links":["d3d09753.88bdd8"],"x":515,"y":500,"wires":[]},{"id":"e0beaaa8.1d1d68","type":"inject","z":"52700294.bedf9c","name":"Null","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":110,"y":140,"wires":[["2ee2d708.73c5b8","2ee3b487.6fff4c","2b0daa35.ef1ff6","c447ac58.2ae6","51ed9321.3e801c"]]},{"id":"51ed9321.3e801c","type":"ui_switch","z":"52700294.bedf9c","name":"Motion Switch","label":"HC-SR501 Motion Sensor","group":"dc750d48.9b06","order":0,"width":"3","height":"1","passthru":true,"decouple":"false","topic":"HC-SR501 Motion Sensor","style":"","onvalue":"1","onvalueType":"num","onicon":"fa-toggle-on","oncolor":"#008000","offvalue":"0","offvalueType":"num","officon":"fa-toggle-off","offcolor":"#800080","x":340,"y":780,"wires":[["139d4230.06db0e","8ef17243.6bf8e"]]},{"id":"139d4230.06db0e","type":"switch","z":"52700294.bedf9c","name":"On or Off?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"0","vt":"num"}],"checkall":"true","outputs":2,"x":570,"y":780,"wires":[["f2b2c017.59b0d"],["8e9d36a5.14f398"]]},{"id":"8ef17243.6bf8e","type":"link out","z":"52700294.bedf9c","name":"","links":["d3d09753.88bdd8"],"x":515,"y":840,"wires":[]},{"id":"4c53fb6a.664094","type":"link out","z":"52700294.bedf9c","name":"","links":["d3d09753.88bdd8"],"x":1155,"y":800,"wires":[]},{"id":"908390f9.6aa74","type":"ui_audio","z":"52700294.bedf9c","name":"","group":"dc750d48.9b06","voice":"1","always":"","x":1200,"y":740,"wires":[]}]

This is a very simple demonstration of Node-RED Dashboard that interacts with a Python process, detects motion, and gives a spoken word alert. Very clever stuff and so easily done!  Have fun!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s