Thursday, 26 May 2016

HOW TO UPLOAD DATA OF LM35 TO THINGSPEAK USING ESP8266


  1. First upload a blank sketch to arduino while pressing the GPIO0 button and then test the AT commands.
  2. Once AT commands are working, you need to connect your esp8266 with a wifi.
  3. Now change the circuit to the one shown in the video. (Image given after the code)
  4. NOTE: Use only 1k resistors for making the voltage divider. 
  5. Then create an account on thingspeak and create a new channel as shown in the video.
  6. Now copy the write API key and paste it in your code of LM35. (Code given below)
  7. After this is done and if all the connections are done correctly you should start receiving data on your channel and a graph starts plotting on the field that you have made.
  8. Now you can play around with different sensors and use more than 1 fields.
EXPLORE AND LEARN..

CODE-


#include <SoftwareSerial.h>
#include <stdlib.h>

// replace with your channel's thingspeak API key
String apiKey = " ";                             //copy your write API key here from thingspeak

SoftwareSerial ser(2, 3);   //RX, TX

void setup() 
{                
  pinMode(A0,INPUT);      //LM35 output
  Serial.begin(9600);           //Baud rate
  ser.begin(9600);
  
  ser.println("AT+RST");    // reset ESP8266
  
}


void loop() {
  

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

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


2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello..
    I'm having trouble displaying data on my thingspeak channel. The arduino serial is showing data every 15 seconds with the command AT+CIPCLOSE. The ESP shows connected to my hotspot. Evrything is going fine except for the fact that i can't view my data on my channel.
    Any help on how to proceed further will be appreciated...

    ReplyDelete

in m mf mv fm mvf dmv mfv fv dfv fdvd