CO2/Humidity/Temperature Sensor

This project is a mashup of different experimentations. I wanted to try the SHT21 sensors and I was looking for a CO2 sensor at the same time accurate and not too expensive.

Working screen

CO2 Sensor

Until recently, gaz sensors were either analogue (and totally uncalibrated) or relatively expensive (when not both at the same time). Then came the MH-Z14 which is a affordable "Non-Dispersive InfraRed" sensor, in theory quite selective.

The MH-Z19 from the same company can be found for ~ $25. The main issue is the partial documentation. Some commands are still a bit obscure.


A great source of information is the wiki and there a quite a few examples on github to get started.

At first experimentations were a bit frustrating but after a while things started to stabilise a bit and sensor's readings from the past few month seem to make sense. The solution was to reject sub-standard readings and to average valid ones over a period of 5 minutes.

One quite annoying thing a bit this module when using breadboard or prototype PCB is that it has weird dimensions (not a multiple of 2.54mm) and won't fit nicely.


Calibration can also be an issue, specially because of the ABC calibration every 24h. The system would be great if it spanned over let's say a week which would give you several chances to open the window but as it is (i.e. using the minimum value of previous day as base), reading were becoming strange day after day. It turns out that it works a lot better without ABC and a manual calibration from time to time.

Then manual calibration can be triggered by software or using the hardware pin. To avoid trying to fit a switch on the enclosure, the solution retained was to use a reed switch inside and to press a neodymium magnet on the cover once the air of the room has been well recycled.


Wemos D1 mini

Although I initially planned to use nodemcu modules, I decided to try Wemos D1 mini which are smaller, cheaper and able to cope with higher speed communication via USB so firmware upload is twice as fast.

Besides this, there really behave like any other ESP8266 solution. Wemos offers a good selection of shields (power, relay, LED, sensor, ...) which I haven't tried as well as modules based on the more powerful ESP32.

It is probably best to use their official online shop as there seems to be quite a lot of fake modules around, and not necessarily cheaper.

I2C Oled

Having used a 0.96" Oled screen for the time bomb, I really thought that using a 0.91" 128x32 I2C SSD1306 screen would be trivial. As it turns out, library for the former is not compatible with the latter because they lack of a proper framebuffer. After digging around, I found that the Adafruit library was working (one parameter is of a different value) even if the hardware looks quite different (more pins).

OLED I2C 0.91"

To be perfectly honest, if I was to make another sensor, I would give up the screen and use coloured Led shinning through (the plastic is slightly translucent) as the physical fitting was probably the most challenging part of this project (even if simple by DIY standard).

A sprinkle of MQTT

Communication with base is, of course, via MQTT, nothing new or complicated here. There is a sampling every minute for the CO2. The average value as well as the current values of temperature and relative humidity are sent every 5 minutes.

Wifi off (really?)

Since this new sensor is installed in our bedroom, I wanted to switch the Wifi on only when needed MQTT communication to send the values. I basically used the dedicated function for this. Is is really switching off "radiations"? Who knows... as I discovered with the electricity meter project, electric consumption is not lowered by it.

Powering the sensor

I knew from the start that using battery would be a challenge and I didn't even bother. Between the screen, MH-Z19 and the Wifi, it is probably around 200mA anyway. The USB port of the Wemos D1 mini is plugged directly on a small power adapter. The usb cable can also be used to upgrade the firmware as no OTA was implemented on this project.

All in a "sensor box"

Like almost everything else, directly from China, I found an enclosure designed for sensors (or thermostat?) which was the right size to fit everything. The USB power adapter is external, the ESP8266 at the top and the thermometer at the bottom, by the vents in order to stay as accurate as possible. Despite this, the temperature appears 1.5°C higher than without cover. The offset can be corrected in software.

Work in progress

Starting screen


As usual the code and schematics are available (AS IS) on github. It includes a SHT library and MH-Z19 library.

    Wifi with ESP8266 - Part 6

    This time, it is about playing with a ESP-201 as well as with the Nodemcu devkit but without nodemcu (the firmware). Here are a few notes about what I discovered while playing with these boards.


    As I mentioned in the past, starting with Arduino 1.6.4, there is now full support for ESP8266.


    A majority of the Arduino's functions are directly available to be used of the ESP8266 and some additional libraries have been directly developed specifically. The libraries and documentation are changing extremely fast: between my first attempts in the summer and now, a lot of material was added.


    The biggest hurdle with these chips seems that timing. While a "normal" Arduino will happily wait for any kind of event to happen, the ESP8266 tends to reset very easily. Too easily maybe and I wasn't able to do some tasks such as the La Crosse decoder and its strict timings.

    There is more info about watchdog in the documentation and in this interesting blog entry about porting code from the Spark Core to the ESP8266.


    This is the whole point of these modules! If connecting is easy (at least in theory!), keeping the connection alive is a bit more of a challenge. Keeping a eye on the status is essential.


    The nodemcu devkit already mentioned here has 2 switchs: "RST" and "FLASH".

    But in practice, they are not used thanks to a clever system (See "USB TO UART" on the schematics)... Flashing mode and reset are done automatically!

    Debug with GPIO2


    The second Serial output is something nice and not widely documented with ESP8266 modules: GPIO2 (aka pin D4) can indeed be used for debug purpose.

    For example on one of my modules, if I press reset, I obtain the following message:

    ets Jan  8 2013,rst cause:4, boot mode:(3,7)
    wdt reset
    load 0x40100000, len 29132, room 16 
    tail 12
    chksum 0x4c
    ho 0 tail 12 room 4
    load 0x3ffe8000, len 2888, room 12 
    tail 12
    chksum 0xf7
    ho 0 tail 12 room 4
    load 0x3ffe8b48, len 8, room 12 
    tail 8
    chksum 0xbf
    csum 0xbf

    Not extremely useful (except to check it is properly booting...) but there is more.

    Wifi debugging

    For example, on another module, here is the message when connecting to Wifi:

    f 0, scandone
    add 0
    aid 1
    pm open phy_2,type:2 0 0
    connected with MYSSID, channel 3
    dhcp client start...

    Serial port

    Moreover, it is also possible to write from the programme to this serial port and it is very easy. As the Arduino documentation put it:

    To use Serial1, call Serial1.begin(baudrate).

    But you have to remember that you can only write (only TX exists).


    Simply connect GPIO2/D4 and GND to a serial adapter. Speed settings should be 115200 8N1 but as usual with these modules, your mileage may vary.

    Different models of Nodemcu

    There are quite a few versions of breakout board, all more or less compatible. Below a selection of the most famous ones (I am not even including the clones):

    More information on this blog. Check also this one.

    And all this without even mentioning the new generation!

    Wifi with ESP8266 - Part 5

    In previous posts, I mentioned a problem with ESP8266 modules when using a Orange Livebox (the router let by my ISP). It was confirmed by someone else (thanks you Sébastien H.) that splitting into 2 SSIDs 2.4 Ghz & 5 Ghz and forcing all clients on the 2.4 Ghz helps.

    I also received a few queries about how I worked around this problem...

    Well, basically, I bought a Wifi dongle I installed on my Raspberry and created a new totally private network aside. It probably helps a tiny bit with security but it is a bit of a pain with kernels/modules as my dongle is not recognised with the standard kernel.

    An alternative could have been to use a cheap Wifi repeater/booster since most of them just create a new SSID bridged to the base network. The risk being this new bridge behaving like the Livebox.

    Help with a Wifi dongle on a Raspberry Pi

    Realtek RTL8188EU

    In theory, the process is straightforward:

    apt-get install -y hostapd firmware-realtek isc-dhcp-server iw

    See, for example,

    As indicated above, I have an issue with the default kernel module 8188eu... The Wifi dongle is only half recognised and doesn't initialise properly. Fortunately, a functional version can be found pre-compiled here:

    • First, find the filename name in the list according to the dongle and the kernel version
    uname -a
    • Then the module can be downloaded for Raspberry P1 or P2 (look for or on the page).

    • Once installed, all Wifi tools (and in particular iw ) should work properly.

    Page 1 / 4 »