Skip to content

Instantly share code, notes, and snippets.

@yanovik
Last active March 4, 2021 06:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanovik/1879bff8985da444e03ad069c023fca9 to your computer and use it in GitHub Desktop.
Save yanovik/1879bff8985da444e03ad069c023fca9 to your computer and use it in GitHub Desktop.
CO2 Module (Model: MH-Z19B)
[{"id":"77932454.ba2f5c","type":"tab","label":"MH-Z19","disabled":false,"info":""},{"id":"9b80e442.964708","type":"serial in","z":"77932454.ba2f5c","name":"USB UART RX","serial":"135cce08.b651f2","x":100,"y":40,"wires":[["df03da44.6f2ed8"]]},{"id":"ba03bc4f.90de2","type":"serial out","z":"77932454.ba2f5c","name":"USB UART TX","serial":"135cce08.b651f2","x":520,"y":100,"wires":[]},{"id":"a478c2e2.0867a","type":"inject","z":"77932454.ba2f5c","name":"5 Sec","topic":"","payload":"","payloadType":"date","repeat":"5","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":100,"wires":[["2d5bea48.a8e316"]]},{"id":"2d5bea48.a8e316","type":"function","z":"77932454.ba2f5c","name":"Request CO2 concentration","func":"msg.payload = new Buffer([0xff,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79]); \nreturn msg;","outputs":1,"noerr":0,"x":300,"y":100,"wires":[["ba03bc4f.90de2"]]},{"id":"df03da44.6f2ed8","type":"function","z":"77932454.ba2f5c","name":"Read CO2 concentration","func":"msg.str = msg.payload.toString('hex');\nmsg.int = parseInt(msg.str, 16);\nif (msg.str == 'ff')\n {\n context.set('co2buffer',msg.payload);\n context.set('count',1);\n context.set('checksumm',msg.int);\n }\n else\n {\n var co2buffer = Buffer.concat([context.get('co2buffer'),msg.payload]);\n context.set('co2buffer',co2buffer);\n var count = context.get('count');\n var checksumm = context.get('checksumm');\n count += 1;\n context.set('count',count);\n if (count == 3)\n context.set('co2',msg.int*256);\n if (count == 4)\n context.set('co2',context.get('co2')+msg.int);\n if (count != 9)\n {\n checksumm += msg.int;\n context.set('checksumm',checksumm);\n }\n }\nif (count == 9 && msg.int == (255 - (checksumm % 256)) ){\n msg.payload = context.get('co2');\n return msg;\n}\n\n","outputs":1,"noerr":0,"x":310,"y":40,"wires":[["4e11b25.6a3e34c"]]},{"id":"4e11b25.6a3e34c","type":"debug","z":"77932454.ba2f5c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":490,"y":40,"wires":[]},{"id":"135cce08.b651f2","type":"serial-port","z":"","serialport":"/dev/tty.SLAB_USBtoUART","serialbaud":"9600","databits":"8","parity":"none","stopbits":"1","newline":"","bin":"bin","out":"char","addchar":false}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment