Getting feeds from ThingSpeak using ESP8266 and Arduino

Getting feeds from ThingSpeak using ESP8266 and Arduino

ThingSpeak is a popular cloud for small IOT Based projects, which can store feeds or data at runtime from various sensors. Once data is written user also needs to read the feeds from ThingSpeak to display or to make some actions based on readings.

Here we have interfaced Arduino Mega 2560 with ESP8266 to read feeds from ThingSpeak.

Connections:

Interfacing Arduino with ESP8266 to get feeds from thingspeak

 

See table for connection description:

Arduino Mega 2560 ESP8266 Arduino UNO *
3.3V Vcc 3.3V
3.3V CH_PD 3.3V
TX1 RX TX
RX1 TX RX
GND GND GND

* if you are using Arduino UNO, before uploading program to Arduino, kindly remove any connections to RX and TX pins of Arduino

 

Program:

/*
Getting feeds from ThingSpeak using ESP8266 and Arduino
http://www.galaxysofts.com/new/getting-feeds-thingspeak-using-arduino/Version: 1.0
Updated: 16-11-2016
By: www.GalaxySofts.com
*/
String data="";
long time2=0;

void setup()
{
Serial.begin(9600);
Serial1.begin(115200);

Serial1.println("AT");
while(!Serial1.find("OK"));
Serial1.println("AT+RST");
while(!Serial1.find("OK"));

Serial1.println("AT+CWMODE=1");
while(!Serial1.find("OK"));

Serial1.println("AT+CIFSR");
while(!Serial1.find("OK"));

Serial1.println("AT+CIPMUX=0");
while(!Serial1.find("OK"));

Serial1.println("AT+CWJAP=\"SSID\",\"password\"");
time2=millis();
while(!Serial1.find("WIFI CONNECTED"))
{
if(((millis() - time2)/1000) > 15)
Serial1.println("AT+CWJAP=\"SSID\",\"password\"");

if(Serial.find("ERROR"))
{
Serial.print("ERROR... Restarting");
asm volatile (" jmp 0"); 
}
}
Serial.print(".");
time2=millis();
while(!Serial1.find("WIFI GOT IP"))
{
if(((millis() - time2)/1000) > 15)
Serial1.println("AT+CWJAP=\"SSID\",\"password\"");

if(Serial.find("ERROR"))
{
Serial.print("ERROR... Restarting");
asm volatile (" jmp 0"); 
}
}
Serial.print(".");
// --------------------------------------------------
time2=millis(); 
while(!Serial1.find("OK"))
{
if(((millis() - time2)/1000) > 15)
Serial1.println("AT+CWJAP=\"SSID\",\"password\"");
if(Serial.find("ERROR"))
{
Serial.print("ERROR... Restarting");
asm volatile (" jmp 0"); 
}
}
// -----------------------------------------------------
Serial.print(".");
Serial1.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80");

time2=millis();
while(!Serial1.find("CONNECT"))
{
if(((millis() - time2)/1000) > 15)
Serial1.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",80");

if(Serial.find("ERROR"))
{
Serial.print("ERROR... Restarting");
asm volatile (" jmp 0"); 
}
}
Serial.print(".");

Serial1.println("AT+CIPSEND=68");
time2=millis();
while(!Serial1.find(">"))
{
if(((millis() - time2)/1000) > 15)
Serial1.println("AT+CIPSEND=68");
if(Serial.find("ERROR"))
{
Serial.print("ERROR... Restarting");
asm volatile (" jmp 0"); 
}
}

Serial1.println("GET /channels/152333/feeds.json?results=2&api_key=X1XMADMMELRDZUC7");
time2=millis();
while(!Serial1.find("description\":\""))
{
if(((millis() - time2)/1000) > 15)
Serial1.println("GET /channels/152333/feeds.json?results=2&api_key=X1XMADMMELRDZUC7");
}
Serial.println(".");
data="";
time2=millis();
while(1)
{
int a=Serial1.read();
// Serial.print(char(a));
if(a == '\"')
break;
else if((a>=32) && (a<=127))
data +=char(a);

}
if(data == "")
{
Serial.print("RESET");
delay(100);
asm volatile (" jmp 0"); 
}
Serial.print("Title: ");
Serial.println(data);
time2=millis();
while((!Serial1.find("feeds\":")));
while((!Serial1.find("field1\":\"")));
data="";
while(1)
{
if(Serial1.available())
{
int a=Serial1.read();
if(a == '\"')
break;
else
data +=char(a);
}
}
Serial.print("field1=");
Serial.println(data);

while((!Serial1.find("field2\":\"")));
data="";
while(1)
{
if(Serial1.available())
{
int a=Serial1.read();
if(a == '\"')
break;
else
data +=char(a);
}
}
Serial.print("field2=");
Serial.println(data);

while((!Serial1.find("field3\":\"")));
data="";
while(1)
{
if(Serial1.available())
{
int a=Serial1.read();
if(a == '\"')
break;
else
data +=char(a);
}
}
Serial.print("field3=");
Serial.println(data);

// --------------------------
while((!Serial1.find("field4\":\"")));
data="";
while(1)
{
if(Serial1.available())
{
int a=Serial1.read();
if(a == '\"')
break;
else
data +=char(a);
}
}
Serial.print("field4=");
Serial.println(data);
//---------------------

Serial1.flush();

}
void loop()
{
while (Serial1.available()) {
// Serial.write(Serial1.read());
}
while (Serial.available()) {
Serial1.write(Serial.read());
}
}

Bold letters in Code needs attention and modifications, SSID and password should be replaced with your Wifi SSDI and Wifi Password respectively and API Key with the API Read Key from your channel.

Results:

Interfacing Arduino with ESP8266 to get feeds from thingSpeak

 

Code Version: 1.0

Updated on: 16-11-2016

 

Related Links:

12 Comments on “Getting feeds from ThingSpeak using ESP8266 and Arduino”

    1. Have you changed, and set your wifi’s SSID, Password in the program? and you also need API_KEY for write, also replace that in this program.

      1. All OK, now I can read. The problem was an empty field on thingspeak.
        Thanks for the reply. I ask you if you can integrate writing on the same program.

      2. I apologize for the trouble. sometimes the reading is perfect. Sometimes he does not read any field. What could be the problem? I ask for advice. Thanks for your cooperation.

  1. Hello, I got struck with the above code. Can you please help me with the errors? It gives me error with the time2 variable.

    ERROR:-

    Arduino: 1.8.5 (Windows 10), Board: “Generic ESP8266 Module, 80 MHz, ck, 26 MHz, 40MHz, QIO, 512K (no SPIFFS), 2, v2 Lower Memory, Disabled, None, Only Sketch, 115200”

    HelloServer:27: error: stray ‘\226’ in program

    if(((millis() – time2)/1000) > 15)

    ^

    HelloServer:40: error: stray ‘\226’ in program

    if(((millis() – time2)/1000) > 15)

    ^

    HelloServer:69: error: stray ‘\226’ in program

    if(((millis() – time2)/1000) > 15)

    ^

    HelloServer:84: error: stray ‘\226’ in program

    if(((millis() – time2)/1000) > 15)

    ^

    C:\Users\AppData\Local\Temp\arduino_modified_sketch_483477\HelloServer.ino: In function ‘void setup()’:

    HelloServer:27: error: expected ‘)’ before ‘time2’

    if(((millis() – time2)/1000) > 15)

    ^

    HelloServer:28: error: expected ‘)’ before ‘;’ token

    Serial1.println(“AT+CWJAP=\”ABC\”,\”1234\””);

    ^

    HelloServer:28: error: expected ‘)’ before ‘;’ token

    HelloServer:40: error: expected ‘)’ before ‘time2’

    if(((millis() – time2)/1000) > 15)

    ^

    HelloServer:41: error: expected ‘)’ before ‘;’ token

    Serial1.println(“AT+CWJAP=\”ABC\”,\”1234\””);

    ^

    HelloServer:41: error: expected ‘)’ before ‘;’ token

    HelloServer:69: error: expected ‘)’ before ‘time2’

    if(((millis() – time2)/1000) > 15)

    ^

    HelloServer:70: error: expected ‘)’ before ‘;’ token

    Serial1.println(“AT+CIPSTART=\”TCP\”,\”api.thingspeak.com\”,80″);

    ^

    HelloServer:70: error: expected ‘)’ before ‘;’ token

    HelloServer:84: error: expected ‘)’ before ‘time2’

    if(((millis() – time2)/1000) > 15)

    ^

    HelloServer:85: error: expected ‘)’ before ‘;’ token

    Serial1.println(“AT+CIPSEND=68″);

    ^

    HelloServer:85: error: expected ‘)’ before ‘;’ token

    exit status 1
    stray ‘\226’ in program

    ——————————————————————————————————————————–

    CODE EXECUTED:-

    String data=””;
    long time2=0;

    void setup()
    {
    Serial.begin(9600);
    Serial1.begin(115200);

    Serial1.println(“AT”);
    while(!Serial1.find(“OK”));
    Serial1.println(“AT+RST”);
    while(!Serial1.find(“OK”));

    Serial1.println(“AT+CWMODE=1”);
    while(!Serial1.find(“OK”));

    Serial1.println(“AT+CIFSR”);
    while(!Serial1.find(“OK”));

    Serial1.println(“AT+CIPMUX=0”);
    while(!Serial1.find(“OK”));

    Serial1.println(“AT+CWJAP=\”ABC\”,\”1234\””);
    time2=millis();
    while(!Serial1.find(“WIFI CONNECTED”))
    {
    if(((millis() – time2)/1000) > 15)
    Serial1.println(“AT+CWJAP=\”ABC\”,\”1234\””);

    if(Serial.find(“ERROR”))
    {
    Serial.print(“ERROR… Restarting”);
    asm volatile (” jmp 0″);
    }
    }
    Serial.print(“.”);
    time2=millis();
    while(!Serial1.find(“WIFI GOT IP”))
    {
    if(((millis() – time2)/1000) > 15)
    Serial1.println(“AT+CWJAP=\”ABC\”,\”1234\””);

    if(Serial.find(“ERROR”))
    {
    Serial.print(“ERROR… Restarting”);
    asm volatile (” jmp 0″);
    }
    }
    Serial.print(“.”);
    // ————————————————–
    time2=millis();
    while(!Serial1.find(“OK”))
    {
    if(((millis())/1000) > 15)
    Serial1.println(“AT+CWJAP=\”ABC\”,\”1234\””);
    if(Serial.find(“ERROR”))
    {
    Serial.print(“ERROR… Restarting”);
    asm volatile (” jmp 0″);
    }
    }
    // —————————————————–
    Serial.print(“.”);
    Serial1.println(“AT+CIPSTART=\”TCP\”,\”api.thingspeak.com\”,80″);

    time2=millis();
    while(!Serial1.find(“CONNECT”))
    {
    if(((millis() – time2)/1000) > 15)
    Serial1.println(“AT+CIPSTART=\”TCP\”,\”api.thingspeak.com\”,80″);

    if(Serial.find(“ERROR”))
    {
    Serial.print(“ERROR… Restarting”);
    asm volatile (” jmp 0″);
    }
    }
    Serial.print(“.”);

    Serial1.println(“AT+CIPSEND=68”);
    time2=millis();
    while(!Serial1.find(“>”))
    {
    if(((millis() – time2)/1000) > 15)
    Serial1.println(“AT+CIPSEND=68”);
    if(Serial.find(“ERROR”))
    {
    Serial.print(“ERROR… Restarting”);
    asm volatile (” jmp 0″);
    }
    }

    Serial1.println(“GET /channels/xxxx/feeds.json?results=2&api_key=xxxxxxxxxxxx”);
    time2=millis();
    while(!Serial1.find(“description\”:\””))
    {
    if(((millis() – time2)/1000) > 15)
    Serial1.println(“GET /channels/xxxxx/feeds.json?results=2&api_key=xxxxxxxxxxxx”);
    }
    Serial.println(“.”);
    data=””;
    time2=millis();
    while(1)
    {
    int a=Serial1.read();
    // Serial.print(char(a));
    if(a == ‘\”‘)
    break;
    else if((a>=32) && (a<=127))
    data +=char(a);

    }
    if(data == "")
    {
    Serial.print("RESET");
    delay(100);
    asm volatile (" jmp 0");
    }
    Serial.print("Title: ");
    Serial.println(data);
    time2=millis();
    while((!Serial1.find("feeds\":")));
    while((!Serial1.find("field1\":\"")));
    data="";
    while(1)
    {
    if(Serial1.available())
    {
    int a=Serial1.read();
    if(a == '\"')
    break;
    else
    data +=char(a);
    }
    }
    Serial.print("field1=");
    Serial.println(data);

    Serial1.flush();

    }
    void loop()
    {
    while (Serial1.available()) {
    // Serial.write(Serial1.read());
    }
    while (Serial.available()) {
    Serial1.write(Serial.read());
    }
    }

    1. Errors may be due to Unicode characters, if you re-write the Unicode characters like (“, ‘) again by yourself, then errors will decrease. The code is working fine.

  2. Hey i’m new to arduino programming and i have an error like this

    Serial
    exit status 1
    ‘Serial1’ was not declared in this scope

    I use arduino uno and esp8266 01
    pls help

    1. any error you faced with uno
      i using uno and ESP8266-1S and I don’t have any error but it’s not working good

Leave a Reply

Your email address will not be published.