CO2 Module (Model: MH-Z19B)

CO2 Module (Model: MH-Z19B) reading with Serial node ( usb-uart stick ). Checksum check is enabled. Manual: http://www.winsen-sensor.com/d/files/infrared-gas-sensor/mh-z19b-co2-ver1_0.pdf

[{"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}]
yanovik

Flow Info

created 9 months, 3 weeks ago

Node Types

Core
  • debug (x1)
  • function (x2)
  • inject (x1)
  • serial in (x1)
  • serial out (x1)
  • serial-port (x1)
Other
  • tab (x1)

Tags

  • co2
  • MH-Z19B
  • MH-Z19
  • uart
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option