Arduino based CO2 Sensor with Wifi and MQTT
Introduction
The drive for a CO2 sensor came from the installation of a water bubbler. CO2 bottles are actually harmless. Even if they start to leak. But since we took a 10kg bottle and also have cats in the house, we are in the critical area and I wanted to be on the safe side.
After a long search for a CO2 sensor which sends its data to my smart home server, I came to the conclusion that there is no such thing as a commercial solution or not in the price range of a maximum of 150 € per device. So I started looking in various craft forums. As with my weatherstation I came to the conclusion that an Arduino-based solution is the method of choice. Fortunately, there was already someone who implemented a corresponding solution and therefore offered the perfect basis for my efforts.
Since the project was a bit older, some of the libraries used were out of date or could no longer be compiled. So I started to revise the project and adapt it to my needs.
Function
As the hardware platform I chose a D1 Mini NodeMcu with ESP8266-12F, because it can be easily programmed via the Arduino IDE and at the same time has WiFi support on board. It is also comparatively cheap.
The data from the sensor is read out at an adjustable interval and sent to a configurable MQTT broker via WiFi.
The sensor itself is connected to the Arduino IDE via its serial interface.
Circuit diagram
D1 Mini | MHZ19B |
---|---|
D1 | RX (PIN 2) |
D2 | TX (PIN 3) |
+5V | +5V (PIN 6) |
GND | GND (PIN 7) |
Firmware upload
First you download the Arduino IDE. There the support for the D1 Mini has to be configured. Then select the “Lolin (Wemos) D1 R2 & mini” as the board.
In addition, the library pupsubclient has to be downloaded from github and installed.
To upload the firmware via the IDE, the bridge between D0 and RST on the D1 Mini has to be temporarily removed. Otherwise it prevents the IDE from triggering the reset signal. Now nothing should stand in the way of a successful upload.
After a successful upload you can set the bridge again. It is necessary for smooth waking up from deep sleep mode.
Initial Setup
To initially set up the sensor, simply set a bridge between D5 and GND and restart the D1 Mini. This activates the setup mode which is acknowledged by 2 short flashes. The D1 Mini now boots in AccessPoint mode and you can connect to the WLAN with the SSID “CO2 Sensor Setup” and the password “connect1234”. Via the address 192.168.4.1 you can reach a web interface via which all relevant settings can be made. After saving and, the D5 / GND jumper must be removed again.
Inbetriebnahme
After a restart, the D1 Mini connects to the configured WLAN (LED blink code: 500ms on, 500ms off), waits for the sensor (LED blink code: 1000ms on, 1000ms off) and starts sending data.
Kalibrierung
To calibrate the sensor, a bridge must be set between D6 and GND. After a restart, the D1 Mini activates the calibration mode (LED blink code: 2000ms on, 2000ms off) which takes approx. 20 minutes and should take place outdoors. Once the calibration process has been completed, the D1 Mini switches to normal mode in which it connects to the WLAN and sends data. The bridge D6 / GND must now be removed again.
This should be repeated every 6-8 weeks for perfect results.
LED Blink Codes
LED Code | Explanation |
---|---|
2x 500ms An, 500ms Aus | Setup Modus is active |
Nx 2000ms An, 2000ms Aus | Calibration mode is in progress (ca. 20 minutes) |
Nx 500ms An, 500ms Aus | WiFi is connected |
Nx 1000ms An, 1000ms Aus | Waiting for sensor warmup |
Nx 250ms An, 250ms Aus | Sensor error |
Visualisation
Software
As always, the source code for this project can be found on github.
https://github.com/HolgerHees/arduino_cos2_sensor