ESP8266 Environment Sensor

Posted by Fred C (W6BSD) on Oct 01 2020

Humidity control is essential to make sure your home is healthy and comfortable. Too much humidity is bad but so is too little. In the summer, air conditioning dries up the air. I have been running a humidifier to help mitigate that problem. To ensure it provides enough moisture in the air, I have decided to build a temperature and relative humidity sensor.

For this project, I have decided to use an ESP8266 and a Bosch BME280 sensor for my device's core. I chose an OLED mini-display for a local display of the temperature and humidity levels. I also wanted to use MQTT1 for historical data and graphics. Additionally, it will give me access to that data from any device, anywhere in the world.


ESP8266 Wemos D1

I choose the ESP8266 for its price, performance, and features. The ESP micro-controller family comes with an RTC2, WiFi, GPIO3, and a lot of memory compared to an Arduino. The ESP family of microcontrollers can be programmed in C or C++ just like any IoT controller on the market. You can re-flash them with new languages such as Lua, NodeJS, MicroPython, and more. This makes them very fun to program.

The price makes the ESP microcontroller's family very attractive. For $14, you can get a pack of 5 on Amazon, which prices each microcontroller at around $3.

Using Python is really attractive to me. I know the language well. Most importantly, there is no need for an IDE or a compiler. You can develop any program using only your favorite editor.

The display and the sensor use an I2C bus. Only four wires are necessary: the usual Ground, VCC, to carry the 3.3 Volt power, then SCL and SDA for the clock and the signal.

Prototype on a breadbord


The Python program uses coroutines to update the display, send data to the MQTT service, and other tasks. Coroutines are lightweight threads. Each coroutine can be considered an independent task, all running in "parallel."

The program's core looks as follows: an event loop is created and the tasks are attached to the main loop. The jobs maintain accurate time, display the sensor information on the little OLED display, and communicate with the MQTT service.

loop = asyncio.get_event_loop()

The complete program is posted on my page. You will find a README file with how to install and configure it.

Before running the sensor, you need to create an account on or any other MQTT service. Once your account is set up, add the API key and username into the file Don't forget to also add your WiFi credentials in that file.


Once your device has been correctly configured and is running, the collected data are sent to the servers.

You can go on the adafruit web server and see a nice graph of your data. Adafruit also lets you create your own dashboard.

Adafruit Dashboard

You can also use an MQTT application on your phone to display or remote control your device.

iPhone dashboard

With MQTT services, you can set up triggers or alerts. For example, Adafruit can send you an email if the temperature or the humidity reaches a certain level. You can also build other devices that subscribe to the feed and take action when values reach a threshold.

You could have a second IoT device somewhere else in the house starting or stopping a fan depending on the temperature. The only limit is your imagination.

  1. Message Queuing Telemetry Transport is an open OASIS and ISO standard, lightweight network protocol that transports messages between devices 

  2. Internal real-time clock 

  3. General-Purpose Input/Output 

 Python      DIY