Temperature logging to ThingSpeak using ESP8266 and DHT22

The GPIO pins of the ESP8266 makes it possible to create a very simple temperature logger, which logs the data directly to the free cloud of ThingSpeak. By using only a DHT22 temperature/humidity sensor, and an ESP8266, it is possible to create a realtime feed of temperature and humidity by using your own wifi network.

This post is mainly based on the original tutorial by Arduinoesp.com, but was upgraded to be able to use a DHT22 instead of the older DHT11, and uses the Arduino IDE to program the ESP8266.

The setup contains just four parts. The ESP8266 as microcontroller and for the connectivity, a DHT22 temperature/humidity sensor, a 5v-3.3v step down converter, and a micro-USB pcb which is used to provide easy powering for the project by any compatible micro-usb adapter. This could be an micro-USB cable or the charger that most likely came with your smartphone.




The schematic view of this project is not very difficult. The micro usb pcb gives 5V to the step down converter. The output of 3.3v can be used for powering the ESP8266 (VCC and CH_PD) and the DHT22 (VCC). The data port of the DHT22 (OUT) should be connected to the GPIO0 port of the ESP8266. Then all there is left are the gounds of all the parts, connect them all together and your ready.

Best practice is to try this setup on a breadboard, as seen below. This way you can fix any errors, and it makes it easy to program the ESP8266 using the Arduino IDE.

img_2541.jpeg

I’ve changed the original software to the example below. It requires the DHT_Adafruit and the ESP8266Wifi libraries. The instructions how to program an ESP8266 using the Arduino IDE can be found in the ESP8266 Wifi LED Dimmer post.

You need to configure the wifi credentials to your own settings. Also the API key should be entered. This can be created on the ThingSpeak website, and can be found under ‘Account’ (upper right corner) and select ‘My Account’.

As an example of how this all works together, I’ve created this for myself and it is currently running from inside my house. The results can be viewed on my public ThingSpeak profile, and I’ve also embedded the graphs below.

For me this was just an example of what the ESP8266 is capable of, combining it with a few simple sensors. I used ThingSpeak because it is a fast and easy way to publish sensor data to the public, without the need to create a webservice by myself. I’m not planning on using ThingSpeak for anything similar in the future. I’dd rather keep the data to myself and use self-hosted (or self created) webservices.

12 Comments

  • Just wanted to say your blog is awesome and a great inspiration. My favorite posts are the one that show how to take cheap components from AliExpress and turn them into cool sensors (the ones you’ll find on Adafruit for triple the price and shipping). Already rebuilt the LEDstrip one you had (used MQTT to hook it up to HomeAssistant) and probably am going to do the same thing here.
    Keep going, you’ve got at least one passionate reader!

  • Can’t get this code running 🙁 I’ve searched the interenet for this two libraries and can;t find them.
    #include -> i can find only DHT.h
    #include -> i can find lots of libraries except ESP8266WiFi.h

    Can anyone help me with these two libraries?

    ThankYou 🙂

  • Thank you for your help. I managed to get the code running (no errors). I updated the code with my ssid, pass and api key, as sugested, flashed the code to my ESP and made all the connection as shown above with the DHT22 sensor. Unfortunately my channel on Things Speak doesn’t receive any data from my module. The two charts for temp and humid are blank.

    Is it possilble that i should have inserted in the code the Write API Key from My Channel (on Things Speak)? I saw there are 3 Api Keys (Write Api, Read Api, and a General Api Key ->this one you suggested to be put in the code)???

    Or what else can go wrong? My ESP module has a red light on and a blue light blinking as if it sends/receives some data. On my router the ESP module is connected to wifi.

    Thank You for your support!!!

  • Back. 🙂 I finally got it right (somehow). I conected ESP GPIO0 to ground and the data pin from DHT22 to GPIO2(on ESP) and uploaded the code via FTDI USB converter and it works. On Serial Print i can see the temp and humid and the data is sent to Things Speak populating the chart. 🙂 yeiii (i also inserted in code my write Api key)

    The problem is that when i disconnect the FTDI USB converter and leave only the ESP and the sensor, the data is not sent anymore to the web
    – I tried connecting the data pin from DHT22 to GPIO0 -> no data on web
    – I tried connecting the data pin from DHT22 to GPIO2 -> no data on web
    – I reset the ESP several time -> nothing happens on ThingsSpeak without the FTDI connection

    Can you help me solve this issue? I’m so close to succes now 🙂 . I need this to work without PC connection because i don’t have pc in my greenhouse (and is important to verify via internet what is going on there).

    Thank You for your time and help!

  • Just a quick feedback. I finished the project. It’s working now without the USB converter. The only thing i missed was that GPIO0 must be connected at 3.3V (only when you write your code on ESP8266 the GPIO0 pin must be grounded) so that the ESP8266 to boot in normal mode and then runs the program by itself.

    Thanks for the help and info you’ve shared!

  • Eryk … This project works very well. I used DHT.h instead DHT_Ada.h and seemed to work. I see both Temperature and Humidity on Thingspeak. Now I would like to remotely control a small relay, adding to the code. It doesn’t look like Thingspeak will let you add a switch. How could I do this?

  • Thanks a lot ! Your example helped a lot to figure out how to do this things.
    Based on your example – everything is up and running , but TEMP sensor failing
    and reporting 0 values for temperature and humidity.
    I was thinking that there are low power and connected DHT to 5V and placed resistor 220om between Vcc and Data (which connected to GPIO02), but ESP8266 still connected to 3.3V.
    What can go wrong – to have “0” values and fail reading data from sensor ?

  • Hi Erik

    I did a project similar to this.

    I used an Arduino UNO, temperature and humidity sensor (DHT22), lux sensor (BH1750) and the wi-fi module esp8266 ( the same you used). I use thingspeak to show the results in real-time

    Now I would like to do the next step:

    I have 5 pairs(DHT22 and BH1750) of sensors, 5 esp8266 and only 1 micro-controller (Arduino or Raspberry). Then I would like to sent data to thingspeak from Arduino/Raspberry

    The question is: Can I connect to our wi-fi module 2 different sensors (DHT22 and BH1750) and send it to Arduino or Raspberry ?

    Really tank you 🙂

  • Hi Eryk,

    Everything seems fine, except that Thingspeak is not updated. I used my USB-to TTL adapter to program the ESP. Wifi is working fine, I can see the ESP with nmap. The blue LED is blinking steadily, but no data is transmitted. I have added a 100uF capacitor to the cirquit to see if that made the difference, but alas, no. Any ideas?

    Thank you for the excellent work so far,

    Peter

Leave a Reply

April 5, 2016

Posted In: ESP8266

Tags: , , , ,

12 Comments