Publish the CO2 concentration of an MH-Z19 sensor to Domoticz, Arduino code compatible ESP32 / ESP8266

In the previous tutorial, we discovered and tested the MH-Z19 sensor which allows to measure the CO2 concentration without calibration. We saw how to read the CO2 concentration returned on the sensor PWM pin in MicroPython and then publish it on a Domoticz server via the HTTP / JSON interface. In this new tutorial, I propose to do the same thing with Arduino code. The code is compatible with the ESP8266 and the new ESP32. For that, it will be necessary to install the Espressif SDK by following this tutorial for the ESP8266 development bards and this one for the ESP32 boards. If you have not read the previous article, the Winsen MH-Z19 sensor is more accurate than the MQ135 environmental sensor. It is much more expensive (about $24 / 20€) but measuring the CO2 level does not require a “hack” mathematical. The value expressed in ppm is retrieved directly from the PWM pin or from the serial port.

Necessary material

The code works on ESP8266 as well as on ESP32. For this tutorial, we will use the PWM output of the MH-Z19. Only one entry on the ESP8266 / ESP32 card will be required.

Presentation of the CO2 sensor MH-Z19

The MH-Z19 is a self-calibrated sensor, ie the measurement sent by the sensor does not require any mathematical treatment. In other words, the CO2 content expressed in ppm (parts per million) is recovered. The MH-Z19 has a PWM output and a UART digital interface (serial port). For this tutorial, we will already start by exploiting the PWM output.

Measuring range 0 – 5000ppm
Precision ± 50ppm+5%
Power supply 3.6 ~ 5.5 VDC
Current < 18 mA
Numerical outputs UART and PWM
Heat time 3 minutes
Working temperature 0 ~ 50 ℃
Working humidity 0 – 95% (without condensation)
Dimensions 33 mm×20 mm×9 mm (L×W×H)
Weight 21g
Lifetime annonced > 5 years

The PWM signal is proportional to the CO2 concentration.

It will therefore be necessary to measure the time during which the signal remains at the high level. Then, the following formula allows to deduce the CO2 rate.

  • Cppm represents the concentration of CO2 in the atmosphere in ppm
  • Th, the time during which the signal remained high
  • Tl, the time during which the signal has remained low

For a 0-5000ppm sensor, simply replace 2000 by 5000 in the formula. The complete documentation is available here.

Prepare virtual appliances on Domoticz

Go to the Domoticz server to create two virtual devices of the temperature type and get the Idx of each probe. Follow this tutorial to learn how to do it.

domoticz ds18b20 esp8266 esp32 arduino idx device sensor

Cabling the MH-Z19’s Serial Port to an ESP8266

Although an Arduino Uno can be used to perform CO2 concentration measurements, the ESP8266, which incorporates a WiFI module, is much better suited for this project. We can move the probe in the house and why not add a small monochrome OLED screen SSD1306. In order for the CO2 concentration measurement to be correct, the MH-Z19 must be permanently powered. As you can see in the technical data, wait at least 2 minutes before the measurement is correct. Battery operation is not suitable for this type of project.

The ESP8266 has 2 UART serial ports. UART0 port (RXD0 / TXD0) and reserved for communication with the IDE. The port numbered by mistake # 1 (TXD1) is incomplete. There is only the TXD1 pin. It is reserved to flash the memory of the ESP8266. Here we will use the RXT2 / TXD2 serial port. Pin RX is located on pin D7 of the ESP8266. The TX pin is on pin D6. We will therefore wire the serial loan by crossing the pins as usual. The RX pin of the MH-Z19 on the D6. The TX pin of the MH-Z19 on the D7. The MH-Z19 can be powered with a voltage between 3.6 and 5.6V. Connect the MH-Z19’s Win Pin to the 5V Pin of the ESP8266. Close the circuit by connecting the GND pins.

Serial communication with the MH-Z19

I adapted the source code from the previous tutorial that read several Dallas DS18B20 temperature probes. Here is what is particular. To communicate via the serial port with another device, another serial port must be opened. It is the SoftwareSerial.h library that takes care of it. To open a serial port on the RXD2 and TXD2 pins, you must instantiate a SoftwareSerial object that takes the serial port pins as parameter. In the setup (), we start the serial port. Here, it is ported at a speed of 9600 bauds according to Wesen specifications:

  • Baud rate: 9600
  • Data byte: 8 byte
  • Stop byte: 1 byte
  • Parity: no

Here, we have 2 open serial ports. The first at 115200 baud communicates with the serial monitor of the Arduino IDE (for example), the second communicates with the MH-Z19.

For the rest of the code, it’s already seen.

Publish CO2 concentration on Domoticz via HTTP / JSON interface, Arduino code compatible ESP8266 and ESP32

We are not going to review here how does sending data to the Domoticz server via the HTTP / JSON interface. Everything is explained in detail in this tutorial. Create a new sketch and paste the code below. Then change the following parameters in the code

  • wifi_ssid, WiFi network
  • wifi_password, WiFi password
  • host, IP address of the Domoticz server
  • port, default 8080
  • IDX_mhz19, Idx of the Domoticz virtual appliance that will receive the CO2 concentration


Save the sketch and upload it to the ESP8266. If all is correct, you must receive a response with the status OK for each HTTP request.

The virtual device displaying the CO2 content in the room updates after a few seconds on Domoticz.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

Tags:

DIY Projects