Source cgit github
See Also Issue Tracker
ESP8266 Lua/NodeMCU module for MH-Z19 CO₂ sensor
esp8266-nodemcu-mh-z19 provides an ESP8266 NodeMCU Lua module (
as well as MQTT / HomeAssistant / InfluxDB gateway application example
init.lua) for the MH-Z19 carbon dioxide (CO₂) sensor.
mh-z19.lua has been tested with Lua 5.1 on NodeMCU firmware 3.0.1 (Release 202112300746, integer build). It requires the following modules.
Most practical applications (such as the example in init.lua) also need the following modules.
Connect the MH-Z19 sensor to your ESP8266/NodeMCU board as follows.
- MH-Z19 GND (black wire) → ESP8266/NodeMCU GND
- MH-Z19 Vin (red wire) → ESP8266/NodeMCU 5V
- MH-Z19 Rx (blue wire) → NodeMCU D1 (ESP8266 GPIO5)
- MH-Z19 Tx (green wire) → NodeMCU D2 (ESP8266 GPIO4)
If you use different pins for TXD and RXD, you need to adjust the softuart.setup call in the examples provided in this repository to reflect those changes. Keep in mind that some ESP8266 pins must have well-defined logic levels at boot time and may therefore be unsuitable for MH-Z19 connection.
Copy mh-z19.lua to your NodeMCU board and set it up as follows.
mh_z19 = require("mh-z19") port = softuart.setup(9600, 1, 2) port:on("data", 9, uart_callback) function uart_callback(data) if mh_z19.parse_frame(data) then -- mh_z19.co2 : CO₂ concentration [ppm] -- mh_z19.temp : device temperature [°c] -- mh_z19.abc_ticks : ticks since last Automatic Baseline Correction -- mh_z19.abc_count : number of Automatic Baseline Corrections performed since power-on end end port:write(mhz19.c_query)
The sensor performs a CO₂ measurement every one to five seconds, depending on hardware revision.
init.lua is an example application with HomeAssistant integration. To use it, you need to create a config.lua file with WiFI and MQTT settings:
station_cfg.ssid = "..." station_cfg.pwd = "..." mqtt_host = "..."
Optionally, it can also publish readings to an InfluxDB. To do so, configure URL and attribute:
influx_url = "..." influx_attr = "..."
Readings will be stored as