Skip to content

Instantly share code, notes, and snippets.

@mharizanov
Created September 2, 2014 13:28
Show Gist options
  • Save mharizanov/e8c01130c72909393b50 to your computer and use it in GitHub Desktop.
Save mharizanov/e8c01130c72909393b50 to your computer and use it in GitHub Desktop.
Solar water tank node with DQ checks
[{"id":"ba386057.845d3","type":"mqtt-broker","broker":"localhost","port":"1883"},{"id":"650ec6ce.937bc","type":"mqtt in","name":"Solar water tank raw","topic":"home/solar/raw","broker":"ba386057.845d3","x":94,"y":1469,"z":"31c82d7c.ce37d2","wires":[["1a8bcb53.521cf5"]]},{"id":"f6db14b.48de768","type":"trigger","op1":"","op2":"","op1type":"nul","op2type":"pay","duration":"15","extend":"true","units":"min","name":"Heratbeat monitor","x":476,"y":1400,"z":"31c82d7c.ce37d2","wires":[["fc298d6a.cf894"]]},{"id":"650a1683.332eb8","type":"inject","name":"Initial heartbeat","topic":"","payload":"","payloadType":"none","repeat":"","crontab":"","once":true,"x":103,"y":1400,"z":"31c82d7c.ce37d2","wires":[["f6db14b.48de768"]]},{"id":"a0c7803f.d6f2a","type":"function","name":"Parse solar node payload","func":"// The received message is stored in 'msg'\n// It will have at least a 'payload' property:\n// console.log(msg.payload);\n// The 'context' object is available to store state\n// between invocations of the function\n// context = {};\n\nvar raw= JSON.parse(msg.payload);\nmsg.environment = new Object();\n\nmsg.environment.Temp= (raw[0]/100); // temp is *100 to avoid floating pint\nmsg.environment.Battery = (raw[1])/1000; // battery level is in mV\n\n\nreturn msg;","outputs":1,"x":116.5,"y":1612,"z":"31c82d7c.ce37d2","wires":[["91b9e838.1f8c28"]]},{"id":"91b9e838.1f8c28","type":"function","name":"Validate data","func":"\n// Solar hot water tank's temperature can't be more than 100 degrees C\n// Solar hot water tank's temperature can't be less than -40 degrees C wow..\n// The DS18b20 temperature sensor gives temperature 85 in case there is some sort of communication failure\n// Battery reading Can't be more than 3.5V\n// Battery reading can't be less than 2.5 or the BOD will kick in\n \nif (\n (msg.environment.Temp!=null && !isNaN(msg.environment.Temp) )\n && (msg.environment.Temp > -40) \n && (msg.environment.Temp < 100)\n && (msg.environment.Temp != 85)\n \n && (msg.environment.Battery!=null && !isNaN(msg.environment.Battery) )\n && (msg.environment.Battery >2.5)\n && (msg.environment.Battery <3.5) \n ) \n {\n \tmsg.environment.valid=1\n }\n else {\n \tmsg.environment.valid=0\n }\n\nreturn msg;","outputs":1,"x":156,"y":1657,"z":"31c82d7c.ce37d2","wires":[["3c896595.95a1fa"]]},{"id":"14cb5c5f.c145dc","type":"switch","name":"Battery low?","property":"environment.Battery","rules":[{"t":"lt","v":2.8,"v2":0}],"checkall":"true","outputs":1,"x":463,"y":1483,"z":"31c82d7c.ce37d2","wires":[["f4e263bc.a9ad08"]]},{"id":"3c896595.95a1fa","type":"switch","name":"Is data valid?","property":"environment.valid","rules":[{"t":"eq","v":1,"v2":0},{"t":"else"}],"checkall":"true","outputs":2,"x":156,"y":1702,"z":"31c82d7c.ce37d2","wires":[["ab547d0f.54ab8"],["4bec0e86.280fd8"]]},{"id":"f4e263bc.a9ad08","type":"function","name":"Prepare 'low battery' alert","func":"// The received message is stored in 'msg'\n// It will have at least a 'payload' property:\n// console.log(msg.payload);\n// The 'context' object is available to store state\n// between invocations of the function\n// context = {};\n\nmsg.payload=\"Solar sensor node battery low : \" + msg.environment.Battery + \"V\";\nmsg.topic=\"Alert!\";\n\nreturn msg;","outputs":1,"x":781,"y":1481,"z":"31c82d7c.ce37d2","wires":[["565f5bfc.a9a0a4"]]},{"id":"e4c15533.6309f8","type":"switch","name":"Detect dangerous conditions","property":"environment.Temp","rules":[{"t":"gt","v":89,"v2":0},{"t":"lt","v":5,"v2":0}],"checkall":"true","outputs":2,"x":516,"y":1544,"z":"31c82d7c.ce37d2","wires":[["64fc7a9c.f83ecc"],["64fc7a9c.f83ecc"]]},{"id":"4bec0e86.280fd8","type":"debug","name":"Debug bad data","active":false,"console":"false","complete":"true","x":477,"y":1705,"z":"31c82d7c.ce37d2","wires":[]},{"id":"64fc7a9c.f83ecc","type":"function","name":"Prepare 'dangerous conditions' alert","func":"\nmsg.payload=\"Solar sensor node reading is \" + msg.environment.Temp;\nmsg.topic=\"Alert!\";\n\nreturn msg;","outputs":1,"x":811,"y":1539,"z":"31c82d7c.ce37d2","wires":[["565f5bfc.a9a0a4"]]},{"id":"7bdac806.ebffa8","type":"mqtt out","name":"Send to EmonCMS","topic":"home/emoncms/out/7","broker":"ba386057.845d3","x":764,"y":1629,"z":"31c82d7c.ce37d2","wires":[]},{"id":"b5c409f6.864e3","type":"function","name":"Route messages","func":"// The received message is stored in 'msg'\n// It will have at least a 'payload' property:\n// console.log(msg.payload);\n// The 'context' object is available to store state\n// between invocations of the function\n// context = {};\n//create json text\n\nif(msg.environment == null)\n{\n\t//no data - stop here\n\treturn null;\n}\n\njsonText = JSON.stringify(msg.environment);\n\nmsg1 = new Object();\n\n//var msg1 = {payload:JSON.stringify(msg.payoad)};\nmsg1.payload = msg.payload;\nvar msg2 = {payload:msg.environment.Temp};\nvar msg3 = {payload:msg.environment.Battery};\nvar msg4 = {payload:Date.now()};\n\nreturn [msg1,msg2,msg3,msg4];","outputs":"4","x":475,"y":1614,"z":"31c82d7c.ce37d2","wires":[["7bdac806.ebffa8","23e8a8db.564d5"],["6dd7817f.18005"],["399bbc7e.33387c"],["9b2ac460.64d538"]]},{"id":"23e8a8db.564d5","type":"debug","name":"","active":false,"console":"false","complete":"true","x":729,"y":1589,"z":"31c82d7c.ce37d2","wires":[]},{"id":"6dd7817f.18005","type":"mqtt out","name":"","topic":"home/solar/temperature","broker":"ba386057.845d3","x":780,"y":1672,"z":"31c82d7c.ce37d2","wires":[]},{"id":"399bbc7e.33387c","type":"mqtt out","name":"","topic":"home/solar/battery","broker":"ba386057.845d3","x":765,"y":1718,"z":"31c82d7c.ce37d2","wires":[]},{"id":"fc298d6a.cf894","type":"function","name":"Prepare 'node not available' alert","func":"msg.payload= \"Haven't heard from Solar sensor node for a while..\";\nmsg.topic=\"Solar alert!\";\n","outputs":1,"x":805,"y":1400,"z":"31c82d7c.ce37d2","wires":[["565f5bfc.a9a0a4"]]},{"id":"9b2ac460.64d538","type":"mqtt out","name":"","topic":"home/solar/lastupdate","broker":"ba386057.845d3","x":778,"y":1768,"z":"31c82d7c.ce37d2","wires":[]},{"id":"565f5bfc.a9a0a4","type":"delay","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","rateUnits":"hour","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":1017,"y":1481,"z":"31c82d7c.ce37d2","wires":[["fcac8add.035378"]]},{"id":"8035ef1b.7fca1","type":"comment","name":"Solar water tank temperature sensor","info":"","x":145,"y":1330.0000534057617,"z":"31c82d7c.ce37d2","wires":[]},{"id":"fcac8add.035378","type":"pushbullet","title":"","name":"","x":1092.500015258789,"y":1400.0000200271606,"z":"31c82d7c.ce37d2","wires":[]},{"id":"ab547d0f.54ab8","type":"delay","name":"2 messges/min","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"2","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":155,"y":1745,"z":"31c82d7c.ce37d2","wires":[["b5c409f6.864e3","e4c15533.6309f8","14cb5c5f.c145dc","f6db14b.48de768"]]},{"id":"1a8bcb53.521cf5","type":"function","name":"Check for completeness","func":"// Check for data completeness\nvar raw= JSON.parse(msg.payload);\n\nif(raw.length==3) {\n\treturn msg;\n}","outputs":1,"x":122,"y":1566,"z":"31c82d7c.ce37d2","wires":[["a0c7803f.d6f2a"]]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment