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 2560ESP8266Arduino UNO *
3.3VVcc3.3V
3.3VCH_PD3.3V
TX1RXTX
RX1TXRX
GNDGNDGND

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

 

Program:

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:

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

Leave a Reply