ESP8266 low power DHT11 temperature and moisture remote sensor

I have a problem with moisture in my loft.  After planning some intervention, I wanted a way to remotely monitor the humidity and temperature up there, and to visualise the data on a server.

The ubiquitous ESP8266 modules seem ideal for this purpose, combined with a DHT11 temperature and humidity sensor.  Of course, I am not the first person to do such a thing (i.e. here and with DHT22 and low-power version here). As is usual for remote sensors, I wanted to run the thing off two AA batteries and to use as little power as possible.  For logging and plotting the data I have chosen ThingSpeak.  I have used ThinkSpeak before, for my tweeting cat litter tray "BeemoPoops!", where I used the Twitter API, but there is a wealth of stuff ThingSpeak can do with your data, not least plot graphs and run MatLab scripts.

Exciting live data! The possibilities are almost endless for what you can do by gathering data from sensors remotely and running Matlab scripts on it.

DeepSleep and wakeup

If our ESP8266 board and DHT11 module are left on constantly, we will run out of power in a few days.  Luckily the ESP8266 chip has a deep sleep feature for ultra-low power applications, which means we'll be able to run for much longer.  We can wake up, connect to WiFi, read the DHT11 sensor, send the data to the ThingSpeak server and then go back to sleep. It's super easy to enter deep sleep mode, just by issuing the command:

ESP.deepSleep(0); // Puts ESP8266 IC in low power mode until RESET is pulled pulsed low

If we want to wake the device after a set number of microseconds, this can be achieved with:

ESP.deepSleep(10*1000000L); // Puts ESP8266 IC in low power mode for 10 seconds 

After 10 seconds, GPIO16 goes low for at least 10 microseconds and when connected to the RESET pin, triggers a reset.   HOWEVER, the ESP-01S module doesn't have GPIO16 conveniently broken out, so we have to do some careful soldering and join them ourselves!  Luckily the pin in question is quite "easily" (I use that word advisedly) accessible.  These references were really helpful:

https://www.instructables.com/id/Enable-DeepSleep-on-an-ESP8266-01/

A bit of  hot glue on the connections helps to strengthen the contact.  With this pin soldered like this, the chip can correctly wakeup after deep sleep.

For the record, I found this really awesome mark up of an ESP8266-01 module here:

As you can see, the pin we are connecting to RESET is labelled XPD_DCDC.

Reading DHT11 sensor on ESP8266

I'm using the Adafruit DHT library, for which you'll also need the Adafruit Unified Sensor library.  I typically prefer to understand how things are working "under the hood" and end up writing my own versions of libraries - I learn more that way - but on this occasion I just wanted to get something up and running in as short a time as possible!  To install the libraries in the Arduino IDE, click the "Clone or download" green button, and select "Download as ZIP".  Install the library from the downloaded ZIP files via the Arduino IDE in the usual way.

Reading the temperature and humidity from the DHT11 sensor is as easy as connecting the GPIO2 of the ESP01S to the DHT11 signal, and hooking up Vcc and GND.  In terms of software, we need to include the library include files and define our sensor setup:

// Define what kind of sensor we have and what GPIO it is hooked up to
#define DHT_PIN 2 //GPIO 2
#define DHT_TYPE DHT11

DHT dht(DHT_PIN, DHT_TYPE)

Then in setup:

dht.begin();

Now in our main loop, we can read temperature and humidity like so:

float temp = dht.readTemperature();
float humidity = dht.readHumidity();

Note that invalid readings are sometimes returned, so it is up to us to check if the returned values are Not A Number (NaN).  The complete code is on Github, and is fairly self-explanatory so I won't go through it in detail here.

Sending data to ThingSpeak

The most interesting aspect for me is how we can send data to ThingSpeak.  You'll need an account, and then to set up a channel and grab the API write key for your channel - this is really straightforward.  We can then assemble a URL in a function I've called "SendData", and pass on the temperature and the humidity to it.  Note the code isn't foolproof, but does take care of percent-encoding the decimal point in floating point numbers for you.

Putting it together

I soldered together a small board with a socket for the ESP01S module, a power switch and a three-pin header for the DHT11 module.  There is a pull-up resistor on the RESET pin, the CH_PD pin and GPIO2.  The latter may not be strictly required, but some application circuits included it.  Incidentally I salvaged the power switch from a set of broken computer speakers, and it's pleasing to re-purpose it here!


The whole thing fits neatly on a battery pack for 2xAA batteries, giving me around 3 volts.  Whilst this is in specifications for the DHT11, it is certainly towards the lower end!  If I were to remake this, I think I would have a 3xAA = 4.5 V battery pack, and either risk running the ESP8266 for brief periods at 4.5V, or use a lower quiescent current 3.3V LDO to power the ESP8266 (i.e. the MCP1700).  I simply didn't have a 3 battery pack holder on my bench at the time of construction and I wanted to get this up in the loft that same evening.

Finally I put the device into a plastic bag, with just the sensor poking out, to avoid any problems of condensation on the ESP8266.  Setting the device in place on a rafter and switching it on, I left it to report remotely on the temperature and humidity.  The live data from the sensors should be updated below:

As can be seen, the temperature readings are sometimes clearly anomalous - a bit more code, or averaging over several readings might help sort this out, but for now, this is good enough!

References

DHT11 datasheet:
http://www.circuitbasics.com/wp-content/uploads/2015/11/DHT11-Datasheet.pdf

ESP8266 and DHT11/22 Arduino IDE web server:
https://randomnerdtutorials.com/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide/

ESP-01S deep sleep:
https://www.instructables.com/id/Enable-DeepSleep-on-an-ESP8266-01/
https://tzapu.com/minimalist-battery-powered-esp8266-wifi-temperature-logger/

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. Looking forward to recreate this!

    ReplyDelete

Post a Comment

Popular posts from this blog

Getting started with the Pro Micro Arduino Board

Arduino and Raspberry Pi serial communciation