ThingSpeak

TTL to ESP01 thingspeak DTH11   Arduinesp esp8266 arduino thingspeak.com
Shopping list
– 1 ESP8266 (for example version ESP01)
– DHT11
– 1 USB to TTL (I use FT232RL)
– 8 wires
– 1 breadboard
– a 3.3V power source (I use an battery in this example, power from your USB TTL will most likely might NOT work)

Hardware
The hardware is based on a  USB to TTL with DTR and RST
If you do not have this have look at the Blink Manual Flash example, and add 2 buttons.
TTL to ESP01 thingspeak DTH11

USB TTL ESP8266 ESP-01
—————————
GND — GND
TX     — RX
RX    — TX
RST — RST
DTR — GPIO0

———————–
+3.3V                     — CH_PD
battery –               — GND
battery +              — VCC
DHT11 VDD (1)   — VCC
DHT11 data  (2) — GPIO2
DHT11 (3)             —  not used
DHT11 (4)             — GND

thingspeak
Create an account on www.thingspeak.com . It is very easy.
After login, create a new channel.

setup ThingSpeak

Add field1 and field2 in the tab “Chanel settings” like the picture above

Coding

Copy this code. Change ssid into your wifi accesspoint, and change the password into yours. And do not forget to change the API key into yours
and compile.

 

[cpp]
// www.arduinesp.com
//
// Plot DTH11 data on thingspeak.com using an ESP8266
// April 11 2015
// Author: Jeroen Beemster
// Website: www.arduinesp.com

#include <DHT.h>
#include <ESP8266WiFi.h>

// replace with your channel’s thingspeak API key,
String apiKey = "4XZ9NA14HQZ7BYD7";
const char* ssid = "ssid";
const char* password = "password";

const char* server = "api.thingspeak.com";
#define DHTPIN 2 // what pin we’re connected to

DHT dht(DHTPIN, DHT11,15);
WiFiClient client;

void setup() {
Serial.begin(115200);
delay(10);
dht.begin();

WiFi.begin(ssid, password);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

}

void loop() {

float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr += "\r\n\r\n";

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to Thingspeak");
}
client.stop();

Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
delay(20000);
}
[/cpp]

Download sorce code here: ThingspeakESP8266
DHT library (from adafruit) can be downloaded here

Uploading
Press upload in the Arduino IDE, arduino will compile and upload.

Testing
Open the serial monitor from the menu tools. You will see if the data will be send to www.thingspeak.com

The strange characters on the first line after a reset is normal.

Go to www.thingspeak.com and open your channel.
www.arduinesp.com - ThingSpeak -

When you have uploaded the sketch, you may remove all the wires required for uploading.
This is what is left.
ESP01 thingspeak DTH11

Good luck.

 

 

25 thoughts on “ThingSpeak

  1. In the picture there are 2 AA batteries. I found out that this is 0.3 volt too less. So I used the power of my TTL to USB for the DTH11. The battery is still in use to power the ESP8266

  2. Thank you very much for this sketch, worked perfectly first time, I did change the sensor to a DHT22 without any problem (the DHT library includes both), after having spend the last 2 days struggling to get a similar sketch running reliably under Lua this has been so much easier, I had a problem under Lua causing the program to randomly lock up.
    The plan now is to see if I can add a motion sensor to the other pin and use this to help monitor a couple of vacation homes I looked after for friends.
    I’m using a 3.3v power supply for the DHT22 and ESP

  3. Hello!

    Could you please show a link to where I can download:
    ESP8266WiFi.h

    What I could find that does not work with your program.
    It works fine with the example provided with the library (c:

  4. Thank you for writing this project up. It was useful to me.
    A note to any other beginners, the DHT library used here is the Adafruit DHT-sensor-library, not the libraries found on the main Arduino site.

    1. That was a useful information. I request admin to mention this with link in the main topic so that beginners like me will be benefited without any confusion.

      Thanks NigelTow!

  5. Thank You, Thank You, Thank You… Have been looking for a “simple” esp8266 talking to the dhtXX sensors. Your post gives a great start / foundation to expand…

  6. is it possible using a arduino for this ? i connected the esp8266 to the arduino uno then setting it up to dht11 but the problem is i dont have #include because im using arduino uno as the board

  7. Thanks for this code and the introduction to ThingSpeak – a great service. Your code worked the first time on an ESP8266-12, however, a few refinements may be helpful:

    – The WiFi.begin() at line 27 is redundant with line 34. Comment out line 27.
    – The & in front of field1= in line 56 is unnecessary.
    – The “\r\n\r\n” in line 60 is unnecessary.
    – The delay() in line 82 is problematic. It could block ESP8266 background processes. Better to use a timer based on millis(). See article at https://learn.adafruit.com/multi-tasking-the-arduino-part-1/using-millis-for-timing

    See my project in development at http://thingspeak.com/channels/64954

    1. Well, in fact, just removing the & in front of field1= in line 56 does not work.
      This is because the previous line includes (wrongly) the API key, probably because the code prevously used the GET method. It is now useless as the key is part of the headers.
      So lines 55-56 should simply be replaced by :
      String postStr = “field1=”;

  8. Hello,
    Thank you for these posts.

    I followed your instructions and I get these warnings/errors continuously during Uploading:
    warning: espcomm_sync failed
    error: espcomm_open failed
    error: espcomm_upload_mem failed

    What could be the problem?

    What I tried:
    – change ESP module
    – change TX/RX wires
    – connect CH_P to ground/+3.3V
    – connect RST to ground/+3.3V

    The Arduino IDE is the latest (1.6.7).

    Thank you. :-)

  9. Hi, This sketch worked also fine on my NodeMcu board (dev. kit v0.9). On the NodeMcu board v1.0 the values of the temperature and the humidity are both equal to zero. Has anyone any idea what to change?

    regards, Vincent

Leave a Reply