Introduction: ESP8266 Wifi Temperature Logger

Picture of ESP8266 Wifi Temperature Logger

They day I read at hackaday (http://hackaday.com/tag/esp8266/) that a new $5 wifi module was available, I order a few of them to test. Now, a few weeks later I want to share my experience.

This is a very simple demo using the ESP8266 and Arduino to update a remote server (https://thingspeak.com/) using a digital temperature sensor.

These are really exciting times for the Internet of Things (r)evolution. Prices are coming down and the Maker community is eager to develop the next generation of all things connected.

The following setup could be done under $20. This is using off the shelf "pricey" components (like Arduino), but you could program your own MCU with UART support and make it cheaper.

Step 1: Materials

Picture of Materials

Step 2: ESP8266 Setup

Picture of ESP8266 Setup

This is where things got tricky. I spent a lot of time testing different configurations. Note that are two version of the ESP8266 going around. The first one has the notification LEDs right next to the board pins. The second one (newer) has the LEDs by the antenna. I have the second one.

The best results came when I loaded V0.922 which allowed me to change the baud rate to 9600. Follow these steps to load this firmware.

http://www.electrodragon.com/w/Wi07c#Firmware_uploading_tool

The best way to test a good connection is by using a USB-to-TTL cable and using a terminal like CoolTerm. Here is the command to change the baud rate:

  • AT+CIOBAUD=9600

These are the pin connections I used on ESP8266 to USB-to-TTL. I used the regulated 3.3v vcc of the Arduino to power the ESP8266. I know that Arduino vcc 3.3v max is output is 150 mA and ESP8266 will peak at 240 mA. But at the time I had no other regulated 3.3v available. Regular usage for the ESP8266 is at 70mA.

Remember to connect GPIO0 to GND when you are uploading new firmware. After that remove for normal operation.

---------------------------------------------------------------------------------------------------------

UTXD --> RX (USB-to-TTL)

CH_PD <--> VCC

RST

VCC --> VCC (power source)

---------------------------------------------------------------------------------------------------------

GND --> GND (power source)

GPIO2

GPIO0

URXD --> TX (USB-to-TTL)

---------------------------------------------------------------------------------------------------------

*Note also had to do USB-to-TTL GND to Arduino GND

Step 3: Arduino Setup and Sketch

Picture of Arduino Setup and Sketch

ESP8266 to Arduino

---------------------------------------------------------------------------------------------------------

UTXD --> RX Arduino

CH_PD <--> VCC

RST

VCC --> VCC Arduino

---------------------------------------------------------------------------------------------------------
GND --> GND Arduino

GPIO2

GPIO0

URXD --> TX Arduino

---------------------------------------------------------------------------------------------------------

Digital Temperature Sensor to Arduino

---------------------------------------------------------------------------------------------------------

Arduino GND -- > DS18B20 GND(1) -- > DS18B20 VDD(3)

DS18B20 DQ(2) -- > 4.7K R --> VCC Arduino 3.3v

---------------------------------------------------------------------------------------------------------

#include<stdlib.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);</p><p>
#define SSID "[YOUR_SSID]"
#define PASS "[YOUR_PASSWORD]"
#define IP "184.106.153.149" // thingspeak.com
String GET = "GET /update?key=[THINGSPEAK_KEY]&field1=";
SoftwareSerial monitor(10, 11); // RX, TX

void setup()
{
  monitor.begin(9600);
  Serial.begin(9600);
  sensors.begin();
  sendDebug("AT");
  delay(5000);
  if(Serial.find("OK")){
    monitor.println("RECEIVED: OK");
    connectWiFi();
  }
}

void loop(){
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);
  tempC = DallasTemperature::toFahrenheit(tempC);
  char buffer[10];
  String tempF = dtostrf(tempC, 4, 1, buffer);
  updateTemp(tempF);
  delay(60000);
}

void updateTemp(String tenmpF){
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  sendDebug(cmd);
  delay(2000);
  if(Serial.find("Error")){
    monitor.print("RECEIVED: Error");
    return;
  }
  cmd = GET;
  cmd += tenmpF;
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    monitor.print(">");
    monitor.print(cmd);
    Serial.print(cmd);
  }else{
    sendDebug("AT+CIPCLOSE");
  }
  if(Serial.find("OK")){
    monitor.println("RECEIVED: OK");
  }else{
    monitor.println("RECEIVED: Error");
  }
}
void sendDebug(String cmd){
  monitor.print("SEND: ");
  monitor.println(cmd);
  Serial.println(cmd);
} 
 
boolean connectWiFi(){
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  sendDebug(cmd);
  delay(5000);
  if(Serial.find("OK")){
    monitor.println("RECEIVED: OK");
    return true;
  }else{
    monitor.println("RECEIVED: Error");
    return false;
  }
}

*UPDATE. Once your done testing/monitoring. Load the same sketch without SoftwareSerial Monitor. This gave me better results as stand alone.

#include 
#include 
#include
#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define SSID "[YOUR_SSID]"
#define PASS "[YOUR_PASSWORD]"
#define IP "184.106.153.149" // thingspeak.com
String GET = "GET /update?key=[THINGSPEAK_KEY]&field1=";


void setup()
{
  Serial.begin(9600);
  sensors.begin();
  Serial.println("AT");
  delay(5000);
  if(Serial.find("OK")){
    connectWiFi();
  }
}

void loop(){
  sensors.requestTemperatures();
  float tempC = sensors.getTempCByIndex(0);
  tempC = DallasTemperature::toFahrenheit(tempC);
  char buffer[10];
  String tempF = dtostrf(tempC, 4, 1, buffer);
  updateTemp(tempF);
  delay(60000);
}

void updateTemp(String tenmpF){
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += IP;
  cmd += "\",80";
  Serial.println(cmd);
  delay(2000);
  if(Serial.find("Error")){
    return;
  }
  cmd = GET;
  cmd += tenmpF;
  cmd += "\r\n";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if(Serial.find(">")){
    Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
}

 
boolean connectWiFi(){
  Serial.println("AT+CWMODE=1");
  delay(2000);
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";
  Serial.println(cmd);
  delay(5000);
  if(Serial.find("OK")){
    return true;
  }else{
    return false;
  }
}

Step 4: ThingSpeak Setup

Picture of ThingSpeak Setup

ThinkSpeak is just awesome! Follow these simple steps to start you own feed:

  1. Sign up for a FREE account at https://thingspeak.com/
  2. Go to Channels --> Create New Channel (you can leave all defaults)
  3. Go to API Keys and get your KEY
  4. Test by putting this on your browser
    http://api.thingspeak.com/update?key=[THINGSPEAK_KEY]&field1=0
  5. Check your results

    http://api.thingspeak.com/channels/[CHANNEL_ID]/feed.json?key=[THINGSPEAK_KEY]

Now you are ready to start sending data.

Comments

ErikN14 (author)2016-02-09

I've been trying two days now to get this to work and I'm tearing my hair here!

I've followed the guide but instead of powering the ESP from the arduino 3.3v I got a separate powerbar that gives me both 5V and 3.3 so I'm getting all the juice from there. Like some others here in the comments I just keep getting:

AT
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=49
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=49
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=49
AT+CIPCLOSE

etc. etc. when I try and run it and monitor it in the IDE, I followed RidhwanA's suggestion to connect the reset of the ESP to the 3.3V to and I read that somewhere else to that it's needed for the thing to work.

chrisrust drawings made it a bit clearer to since I'm a noob and thought that the part of the code that says "SoftwareSerial monitor(10, 11); // RX, TX" ment that I should connect the RX from the ESP to 10 and the TX to 11 but still no go!

Is there anyway to check if the ESP really is getting a connection since my thingspeak refuses to upload!

If anyone want to see my version of the code it's here: https://github.com/Naesstrom/Arduino_temp_pcv/blob...

camilowarlock (author)ErikN142017-10-07

Just solved it!! Guys, swap the TX, RX wires. Arduino TX ---> RX esp; Arduino RX -----> TX esp. Be careful with the voltage. Must convert arduino's 5v to 3.3 on esp's RX pin.

Also, I deleted the 16s delay function, and the error ==> goto start loop and inserted a 15s delay without the "delay" function. This means my program which can't have delay function, will try to update data to thingspeak every 15 seconds, but if it fails it will continue with other tasks instead of being stuck in that infinite loop.

camilowarlock (author)ErikN142017-10-07

I got the same problem. I'm using an ESP 12-F.

bobsmith652 (author)ErikN142016-11-25

Not sure if you've given up on this, It looks like you have the monitor connected to the port the AT commands are coming out of. The code is written to send the ESP commands to the hardware serial port and the monitor should be on the software serial port.

You need your USB adaptor on the hardware serial port to download the sketch, but you then need the ESP on the hardware com port to get anything faster than 9600 working, so you will have to switch if you are developing.

In my application, I put the ESP on the software serial port and switch it to 9600 straight away. The arduino can send at 115200 on the software serial port to change the baud rate, but can't receive at that speed.

MichałK56 (author)ErikN142016-09-15

I got the same problem too...Did you solve this?

bobsmith652 (author)MichałK562016-11-23

Have you got the ESP connected to the hardware serial port? It looks like it is sending the commands to your monitor instead of the ESP. The code sends the commands to the hardware serial port, and if you have your USB adaptor on this it will not work. You will need your USB adaptor on the hardware serial to download the sketch, then swap the ESP to the hardware monitor, and your USB adaptor to pins 10 and 11 to monitor it.

PetrU5 (author)MichałK562016-11-16

Hello, I spend hours and hours to solve this problem. Solution is simple. ESP82666 must be powered by breadboard power supply (3,3V), and breadboard power supply module needs 12V and min 1A adapter. Adapters with lower voltages causes that ESP8266 dont work correctly.

FlorentV (author)2015-04-24

Hello,

I tried to connect a Mega Arduino , a sensor DS18B20 and esp8266 to send data from my sensor on the Internet.

After configuration, I sed https://codebender.cc/sketch:98754#thingspeak%20ds18b20%20esp8266.ino this code

but the data does not reach the server, why?

AT

AT + CIPSTART = " TCP ", " 184106153149 " 80

AT + CIPSEND = 47

AT + CIPCLOSE

AT + CIPSTART = " TCP ", " 184106153149 " 80

AT + CIPSEND = 47

AT + CIPCLOSE

AT + CIPSTART = " TCP ", " 184106153149 " 80

AT + CIPSEND = 47

AT + CIPCLOSE

Can anyone help me please ?

Thank you in advance

camilowarlock (author)FlorentV2017-10-07

Just solved it!! Guys, swap the TX, RX wires. Arduino TX ---> RX esp; Arduino RX -----> TX esp. Be careful with the voltage. Must convert arduino's 5v to 3.3 on esp's RX pin.

camilowarlock (author)FlorentV2017-10-07

I got the same problem. I'm using an ESP 12-F.

MichałK56 (author)FlorentV2016-09-15

I got the same problem too...Did you solve this?

NomanAhmed3 (author)FlorentV2015-11-02

did your problem solved? i am having same error...i am using esp8266_03 module

RidhwanA (author)FlorentV2015-09-12

Please read my comment above. Hope this helps.

gphat (author)2015-04-07

when I run this sketch, i show as below

AT
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=54
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=54
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=54
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=54
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=54
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=54
I cant upload to thingspeak. the 2 led on esp still on, I connect nothing to Uno. here is my code

#include <SoftwareSerial.h>

#define SSID " " // I delete it when post here
#define PASS " "
// I delete it when post here

#define IP "184.106.153.149" // thingspeak.com
String GET = "GET /update?key=EDPAJHIWQLX2A8VB&field1=";
SoftwareSerial monitor(10, 11); // RX, TX

void setup()
{
monitor.begin(9600);
Serial.begin(9600);

sendDebug("AT");
delay(1000);
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
connectWiFi();
}
}

void loop(){

String tempF = String(5.4,DEC);
updateTemp(tempF);
delay(1000);
}

void updateTemp(String tenmpF){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(1000);
if(Serial.find("Error")){
monitor.print("RECEIVED: Error");
return;
}
cmd = GET;
cmd += tenmpF;
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
monitor.print(">");
monitor.print(cmd);
Serial.print(cmd);
}else{
sendDebug("AT+CIPCLOSE");
}
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
}else{
monitor.println("RECEIVED: Error");
}
}
void sendDebug(String cmd){
monitor.print("SEND: ");
monitor.println(cmd);
Serial.println(cmd);
}

boolean connectWiFi(){
Serial.println("AT+CWMODE=1");
delay(1000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(1000);
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
return true;
}else{
monitor.println("RECEIVED: Error");
return false;
}
}

MichałK56 (author)gphat2016-09-15

I got the same problem too...Did you solve this?

camilowarlock (author)MichałK562017-10-07

Just solved it!! Guys, swap the TX, RX wires. Arduino TX ---> RX esp; Arduino RX -----> TX esp. Be careful with the voltage. Must convert arduino's 5v to 3.3 on esp's RX pin.

camilowarlock (author)gphat2017-10-07

Just solved it!! Guys, swap the TX, RX wires. Arduino TX ---> RX esp; Arduino RX -----> TX esp. Be careful with the voltage. Must convert arduino's 5v to 3.3 on esp's RX pin.

camilowarlock (author)gphat2017-10-07

I got the same problem. I'm using an ESP 12-F.

NomanAhmed3 (author)gphat2015-11-02

did your problem solved? i am having same error...i am using esp8266_03 module

RidhwanA (author)gphat2015-09-12

Please read my comment above. Hope this helps.

JiříB (author)2015-06-27

Hi why mi in serial ports arduino duemilanove writing?:

AT

AT + CIPSTART = " TCP ", " 184106153149 " 80

AT + CIPSEND = 47

AT + CIPCLOSE

and temp no send to thingspeak.com ....

camilowarlock (author)JiříB2017-10-07

I got the same problem. I'm using an ESP 12-F.

MichałK56 (author)JiříB2016-09-15

I got the same problem too...Did you solve this?

RidhwanA (author)JiříB2015-09-12

I got the same problem. To solve I connected RESET to VCC 3.3V. Hope this helps.

LashaP (author)JiříB2015-07-21

I have same problem, did you solve? if yes, please send me instructions, thank you !

MordimerM (author)2016-05-11

wait a minute, you can just use ESP8266 module for that? It has GPIOs to which you can connect the temp probe and has even more memory than arduino to put your soft there, am I right?

Darry2000 (author)MordimerM2016-05-31

yes. That's how I've got it running... no arduino of any sort. Just used the arduino IDE to upload the sketch on an ESP 8266 running nodemcu v2

BeauU (author)Darry20002017-01-18

I'd love to know more about how you did this, any chance you could enlighten me?

MordimerM (author)Darry20002016-06-01

wow thats awesome

aasteven999 (author)2016-07-27

Thank you

bhoegger (author)2016-07-02

awsome sketch ... I have to learn to use the WiFi module ESP8266 ... thanks al lot ! Bruno

spetyo (author)2016-06-12

https://www.youtube.com/watch?v=_A8iyLPsGAw

spetyo (author)2016-06-12

https://www.youtube.com/watch?v=_A8iyLPsGAw

BallscrewBob (author)2015-11-27

Well I gave it a go WITHOUT schematic and it does not post to thingspeak.

I have two other devices that post just fine for multiple sensors so I know its not in my personal settings.

As far as the connection and from the way it is described above

(18B20-DQ)>>>>(4.7K)>>>>(3.3V) simply does not make sense. This means I am feeding the output from the DSB with a voltage reduced by the resistor. whereas the 18B20 will also be putting out a small voltage by way of a signal which means that I have two converging voltages (18B20)>>>>(4.7K)<<<<(3.3V) which is only going to result in heat In the resistor until something breaks down. I know I am missing something here that a schematic would show clearly.

AntInvent (author)BallscrewBob2016-05-24

It's a pull-up resistor to ensure the data line doesn't 'float' between 1 and 0. There's more of an explanation here:

https://en.wikipedia.org/wiki/Pull-up_resistor

:)

LissanroR (author)2016-05-23

Great article, but I would not call Arduino Mini Pro pricey - it just ~$1.4 on eBay (technically it is a clone but I purchased many of them and never had a problem). The same is true for other components. ESP8266 module currently costs about $2.07. And temperature sensor $0.99. Assuming you already have power supply and 4.7K resistor, everything else can be purchased under $4.5 including international shipping. I thought I mention this because some people may not know that these components are available at much better prices than mentioned in the article.

MordimerM (author)2016-05-11

wait a minute, you can just use ESP8266 module for that? It has GPIOs to which you can connect the temp probe and has even more memory than arduino to put your soft there, am I right?

ehsanito (author)2015-10-18

There is an error in this piece of code:

if(Serial.find(">"))

{

Serial.print(cmd);


}

else

{


Serial.println("AT+CIPCLOSE");


}

It should be like this:

Serial.println(cmd);

ĐạtC (author)ehsanito2016-04-25

you can try:

if(Serial.find((char *) ">") ){

Serial.print(cmd);

}

sinet666 (author)2016-04-10

Hi,

My string length is 52 to send values between 0 to 9 on thingspeak.

Serial.print("AT+CIPSEND=");

Serial.println(cmd.length()); // here length is 46

i want length to be 52, how can i change it in program.

I am able to upload using USB TTL and ESP8266 on teraterm.

Need help with arduino ... Thank you , please reply asap !

AbubakarF (author)2016-02-02

Hi
Thank you for this amazing Instructable..
just a simple question..Why you used ConnectWiFi function as Boolean datatype and can we use it without Boolean??

n3tw0rk3r (author)2016-01-22

Thank you for your code! I made some changes to log temperature and humidity from a DHT22 sensor. It works like a charm!

zecanilis (author)2015-11-23

Ok.

I found a bug, i think it's the conversion fault....

Getting no value when temperature is less than 10.0... Anyone?

zecanilis (author)zecanilis2015-11-27

Found out what was the problem:

String tempF = dtostrf(tempC, 4, 1, buffer); ->This doesn't work for values less than 10.

String tempF = dtostrf(tempC, 3, 1, buffer); ->Use it for values less than 10

BallscrewBob (author)2015-11-24

I would have loved to build this one but noticed there is no real schematic apart from a scratching in one of the replies

I was a little confuzed by the description of the connections above too.

Pretty good starting point tho if there is ever a schematic.

zecanilis (author)BallscrewBob2015-11-27

No need schematic. Just follow the connections.

Cheers

JohnW166 (author)2015-10-22

i made this! part of my huge solar inverter controller project. Works like a charm. I even didnt bother to update the firmware, i just instructed the arduino to tell the esp to change port speed to 9600 at start up, and then change the arduino seriel port to 9600 also, and carry on. (too lazy to learn how to update firmware unless i really needed it lol)

anyway, i have a question. I need to have more than one feed (actually about 30)

How do i go about this?

NomanAhmed3 (author)JohnW1662015-11-02

can you please share your code?

Ploopy (author)2015-08-03

Cool!