ESP8266 Wifi Temperature Logger

772,799

510

141

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

Step 2: 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

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

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.

6 People Made This Project!

Recommendations

  • Make it Glow Contest 2018

    Make it Glow Contest 2018
  • PCB Contest

    PCB Contest
  • First Time Author

    First Time Author

141 Discussions

0
None
pshiveshwar

Question 3 months ago

can I use the esp8266 connected to my hotspot of my phone..... and using my wifi connected to my laptop can I access the thing speak..... and get data of the temperature.....

0
None
ErikN14

2 years ago

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...

7 replies
0
None
camilowarlockErikN14

Reply 1 year ago

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.

0
None
camilowarlockcamilowarlock

Reply 1 year ago

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.

0
None
bobsmith652ErikN14

Reply 2 years ago

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.

0
None
MichałK56ErikN14

Reply 2 years ago

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

0
None
bobsmith652MichałK56

Reply 2 years ago

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.

0
None
PetrU5MichałK56

Reply 2 years ago

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.

0
None
FlorentV

3 years ago on Introduction

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

5 replies
0
None
camilowarlockFlorentV

Reply 1 year ago

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.

0
None
MichałK56FlorentV

Reply 2 years ago

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

0
None
NomanAhmed3FlorentV

Reply 3 years ago

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

0
None
gphat

3 years ago on Introduction

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;
}
}

4 replies
0
None
MichałK56gphat

Reply 2 years ago

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

0
None
camilowarlockMichałK56

Reply 1 year ago

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.

0
None
camilowarlockgphat

Reply 1 year ago

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.

0
None
camilowarlockgphat

Reply 1 year ago

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