Reading last field value from Thingspeak using ESP8266_01 and Arduino Mega

In this code, esp8266 writes data in thingspeak server. But unable to read the field value from the server. Is there anyone who can help me ?

#include <SoftwareSerial.h>
#include <String.h>
SoftwareSerial esp8266(10,11);

String AP = "Sadman"; // WiFi NAME
String PASS = "sadman1234"; // WiFi PASSWORD
String API = "3AN19RF1XKLLX9NF"; // Write API key (dal ThingSpeak channel)
String APIR = "FVNOVYMX02X819BK";
String HOST = "api.thingspeak.com";
String PORT = "80";
String CHANNEL = " 1487644";
int countTimeCommand;
String message;
String messager;
char buffer[200];

int X1=1; // variabili di prova
int X2=2;
int X3=3;
int x01;
int x02;
int x03;
//char x001;
//char x002;
//char x003;

void setup() {
Serial.begin(9600); // comunica via seriale (0, 1) : NOTA: durante il caricamento script stacca i pin 0 e 1 (ricollegali dopo)
esp8266.begin(115200); //38400

Serial.println("Connecting to Wifi...");
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=""+AP+"",""+PASS+""",10,"OK"); //connessione al router wifi
Serial.println("Connection succeded!");
}

void loop() {

message="GET /update?api_key="+API+"&field1="+X1+"&field2="+X2+"&field3="+X3;
messager = "GET /channels/"+CHANNEL+"/feeds.json?api_key="+APIR+"&results=2";

Serial.println("Starting to write...");

sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,"TCP",""+HOST+"","+PORT,15,"OK");
sendCommand("AT+CIPSTO=10",5,"OK");
sendCommand("AT+CIPSEND=0,"+String(message.length()+4),4,">");
esp8266.println(message);
sendCommand("AT+CIPCLOSE=0",5,"OK");

Serial.println("Writing done!");

//LETTURA
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,"TCP",""+HOST+"","+PORT,15,"OK");
sendCommand("AT+CIPSTO=10",5,"OK");
sendCommand("AT+CIPSEND=0,"+String(messager.length()+4),4,">");
esp8266.println(messager);
Serial.println("Starting to read...");

sendCommand("AT+CIPCLOSE=0",5,"OK");

unsigned long currentMillis = millis();
bool si = false; // bool si serve a prendere solo i caratteri dentro [ ], in modo tale da non appesantire troppo il buffer
int count = 0;
while((millis() - currentMillis) < 10000)
{
if(esp8266.available())
{
char ch;
ch=esp8266.read();
if(ch == '['){
si = true;
}
if(ch == ']'){
si = false;
}
if(si == true){
buffer[count] = ch;
count++;
// Serial.print(ch);
}
}
}

   buffer[count] = "\0";
   if(count > 199){
    Serial.println("Unsufficient space on the buffer (200 char)");
   }

Serial.println("Reading done!");

Serial.println("Buffer:");
for (int i = 0; i < count; i++){
  Serial.print(buffer[i]);
}
Serial.print("\n");


char* pos = strstr(buffer, "field1");

x01 = atoi(&pos[9]);
pos = strstr(buffer, "field2");
x02 = atoi(&pos[9]);
pos = strstr(buffer, "field3");
x03 = atoi(&pos[9]);
Serial.println("x1 is:");
Serial.println(x01);
Serial.println("x2 is:");
Serial.println(x02);
Serial.println("x3 is:");
Serial.println(x03);

delay(5000); // ricorda che su ThingSpeak non devono passare meno di 15 s tra un invio e l'altro

}

void sendCommand(String command,int maxTime,char readReplay[]) // funzione di invio comando tra arduino e l'esp
{
while(countTimeCommand<maxTime)
{
esp8266.println(command);
if(esp8266.find(readReplay))
{
break;
}
countTimeCommand++;
}
countTimeCommand=0;
}

Please format you code properly using ctrl-t (auto-format) and post within </> code-tags.

about your code, using the esp-01 with AT-commands is cumbersome compared to programming the ESP directly to perform the tasks you want. The processor on the ESP is much faster, has more flash and RAM than the Mega you are using to control it.
On that note, if you have a Mega, you don't need swSerial, a Mega has 4 UARTs that you can use (ok 1 is used by the USB port)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.