ESP8266 based Temperature Data logger using Arduino

ESP8266 is a wifi module controlled by simple AT commands. This post describes the usage of a web api developed developed by thingspeak.com to plot data on the web.

First of all you need to register on the thingspeak website and obtain an API key. The Key can be found once you login in into the website

Capture1

Connect the ESP8266 module to your Arduino board as following. Please note that Software serial is used to send and receive data to the Wifi module from the Arduino so that any input/output pin can be used for interfacing with the module.

Following code is used to upload the temperate sensor data to the thingspeak website. User need to edit the API key value to match the one obtained when you login. It is also assumed that you are connected to a router and the network is accessible.

Note: A good idea is to read the IP address of your module using AT+CIFSR command. Also can check the connectivity by pinging the IP obtained from another computer. Use ping and check the response.


// esp8266_test.ino
//
// Plot LM35 data on thingspeak.com using an Arduino and an ESP8266 WiFi
// module.
#include <SoftwareSerial.h>
#include <stdlib.h>

// LED
int ledPin = 13;
// LM35 analog input
int lm35Pin = A0;

// replace with your channel's thingspeak API key
String apiKey = "ZP1PZO62773HXWVU";

// connect 10 to TX of Serial USB
// connect 11 to RX of serial USB
SoftwareSerial ser(10, 11); // RX, TX

// this runs once
void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);

// enable debug serial
Serial.begin(9600);
// enable software serial
ser.begin(9600);

// reset ESP8266
ser.println("AT+RST");
}
// the loop
void loop() {

// blink LED on board
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);

// read the value from LM35.
// read 10 values for averaging.
int val = 0;
for(int i = 0; i < 10; i++) {
val += analogRead(lm35Pin);
delay(500);
}

// convert to temp:
// temp value is in 0-1023 range
// LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
// So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt
float temp = val*50.0f/1023.0f;

// convert to string
char buf[16];
String strTemp = dtostrf(temp, 4, 1, buf);

Serial.println(strTemp);

// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);

if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}

// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(strTemp);
getStr += "\r\n\r\n";

// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);

if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}

// thingspeak needs 15 sec delay between updates
delay(16000);
}

Connect LM35 temperature sensor using the pinout shown below. A0 pin should be connected to the out pin of LM35.

.

connection

LM35_500x500_1_

Once the connection is made, connect the programming cable and upload the code to your Arduino board.

Capture

Open a webbrowser and login into the thingspeak website using the appropriate credentials, Then browse to the public channel to see the graphical representation of the data uploaded. Thingspeak also has several customisation options. You will become familiar with these if you are exploring the settings section in their website.

Advertisements

27 thoughts on “ESP8266 based Temperature Data logger using Arduino

  1. Sir,
    We are looking for ESP8266 remote serial Port WIFI wireless module.
    We are entering in to home automation and Industrial automation solutions.
    Quantity will be bulk once we start the regular production of our products.
    Kindly send me the quote for 50 / 100 / 500 and 5000.

    Regards,

    • Hi sir,
      Actually, I’m doing the same project where I want to display temperature data in Thingspeak. The circuit connection same as yours but I didn’t get any output both in serial monitor and channel in Thingspeak. The code was successfully uploaded into the MC and both my PC and ESP8266 connected to the same wifi.

      ESP8266 updated version (after flashing new firmware) :
      AT version:0.51.0.0
      SDK version : 1.5.0

      Setting in Arduino IDE :
      Board>Arduino Genuine/Uno
      Port>com4

      Serial Monitor : no line ending and 19200 baud

      Can you check what is wrong with mine?

  2. can you clarify how you are connecting 3.3V from arduino to esp…I see a loop in esp and another connection from 3.3v to loop…can you elaborate more on how this is done?

  3. Pingback: รวมลิ้งข้อมูลของ ESP8266 - Elec-Za.com

    • There are lots of microcontroller families available in the market and is not possible to make tutorial for each one. But the basic data format remains the same. Try understanding the given code and you can easily port it to PIC uCs.

  4. Hey Elementz online May i know the ESP which is from you will readily available with firmware or we want to port the it by manually ,If in this case what is the firmware.I need to port ESP8266 V0.9.5.2 firmware and i am having FTDI 232, but i cant able to upload it. can you able to give some ideas to do it.

  5. HI
    How are you connecting esp8266 guy to internet in the above code, i mean without using ssid and pass of a network. are you using any other method ?
    thank you

    • Hi Srikanth,

      The SSID and Password are mandatory to connect with the network. You can use serial terminal to send the appropriate AT commands to get connected to the network before running the mentioned code in this post.

      • Thanks for the tutorial, I am able to log my data now!

        Quick question on your network comment (as I am still new to esp8266). Will this work as a standalone and not tethered to the PC? I see what you mean by connecting to the network first via serial monitor, but can ssid/pass be included in the sketch in an event that the wifi router cycles power, and have it auto connect?

  6. Pingback: Remote Power Monitoring system AC or DC current measurements | hilo90mhz

  7. HI elementz, I am trying to use the above code with Arduino Due using pre-available RX-TX pins i.e. 0 and 1. The serial print commands are working good but the server cannot be reached and updated. Please suggest changes. Code below. (API purposely wrong)
    2. Why ain’t you using AT+CIPMUX=1 for multiple connections?
    3. If AT+CIPMUX needs to be used how can I go about it?
    Code –
    #include
    #include

    // LED
    int ledPin = 13;
    // LM35 analog input
    int lm35Pin = A0;

    // replace with your channel’s thingspeak API key
    String apiKey = “ZP1PZO62773HXWVU”;

    // connect 10 to TX of Serial USB
    // connect 11 to RX of serial USB
    //SoftwareSerial ser(10, 11); // RX, TX

    // this runs once
    void setup() {
    // initialize the digital pin as an output.
    pinMode(ledPin, OUTPUT);

    // enable debug serial
    Serial.begin(115200);
    // enable software serial
    //ser.begin(9600);

    // reset ESP8266
    Serial.println(“AT”);
    Serial.println(“CIPMUX=1”);
    }
    // the loop
    void loop()
    {
    // blink LED on board
    digitalWrite(ledPin, HIGH);
    delay(200);
    digitalWrite(ledPin, LOW);

    // read the value from LM35.
    // read 10 values for averaging.
    int val = 0;
    for(int i = 0; i 100 degrees C
    // So Temp = (avg_val/1023)*5 Volts * 100 degrees/Volt
    float temp = val*50.0f/1023.0f;
    // convert to string
    char buf[16];
    String strTemp = dtostrf(temp, 4, 1, buf);
    Serial.println(strTemp);
    // TCP connection
    String cmd = “AT+CIPSTART=\”TCP\”,\””;
    cmd += “184.106.153.149”; // api.thingspeak.com
    cmd += “\”,80″;
    ser.println(cmd);
    if(Serial.find(“Error”)){
    Serial.println(“AT+CIPSTART error”);
    return;
    }
    // prepare GET string
    String getStr = “GET /update?api_key=”;
    getStr += apiKey;
    getStr +=”&field1=”;
    getStr += String(strTemp);
    getStr += “\r\n\r\n”;

    // send data length
    cmd = “AT+CIPSEND=”;
    cmd += String(getStr.length());
    Serial.println(cmd);

    if(Serial.find(“>”)){
    Serial.print(getStr);
    }
    else{
    Serial.println(“AT+CIPCLOSE”);
    // alert user
    Serial.println(“AT+CIPCLOSE”);
    }

    // thingspeak needs 15 sec delay between updates
    delay(16000);
    }

  8. I did the same connection with an external 3.3V power supply for esp-01.I’m getting the values printed on serial monitor,But thingspeak channel is not getting updated.

    What do you mean by this line ?
    ” // connect 10 to TX of Serial USB
    // connect 11 to RX of serial USB “

  9. Thank you for this tutorial
    Unfortunately I cannot get further than AT+CIPCLOSE
    when I use raw AT commands, I am able to update my thingspeak channel though…so I guess it is a “software” issue ?
    If someone has a clue, I would be very grateful !
    (I have an external 3.3V power supply for esp-01)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s