- First upload a blank sketch to arduino while pressing the GPIO0 button and then test the AT commands.
- Once AT commands are working, you need to connect your esp8266 with a wifi.
- Now change the circuit to the one shown in the video. (Image given after the code)
- NOTE: Use only 1k resistors for making the voltage divider.
- Then create an account on thingspeak and create a new channel as shown in the video.
- Now copy the write API key and paste it in your code of LM35. (Code given below)
- 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.
- Now you can play around with different sensors and use more than 1 fields.
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);
}
This comment has been removed by the author.
ReplyDeleteHello..
ReplyDeleteI'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...