Problem Reading Data From Cloud (Thingspeak)

Hi, I am using this code to retrieve data from a Thingspeak channel in a JSON format. I am using an ESP8266 along with an Arduino Nano 33 BLE. The Nono 33 BLE Arduino board is not compatible with Thingspeak library and it doesn't support SoftwareSerial. Alternatively, I am using serial1 to communicate with the ESP8266 and REST API method to get the data from Thingspeak as it is shown in the code. However, I cannot get it to work. It is worth noting that Serial1.available() always gives zero even after adding delays.

I have used the same code to send data to the same Thingspeak channel and it works perfectly. I have no clue why it is not reading from the channel. I would really appreciate any help with this problem

Thanks in advance.

String AP ="xxxxxxxxxxx"; 
String PASS ="xxxxxxxxxxxxxx"; 
String PORT = "80";
String field = "Id";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 

 
String Data ="";
void setup() {
  Serial.begin(9600);
  Serial1.begin(115200);
  sendCommand("AT",5,"OK",false);
  Serial1.println("AT+UART_DEF=9600,8,1,0,0");
  delay(1000);
  Serial1.end();
  Serial1.begin(9600);
  ConnectToWifi();
}
void loop() {
  

String getData="GET https://api.thingspeak.com/channels/1335558/fields/1?api_key=7XFZKXEGOV5HY4TQ";

 sendCommand("AT+CIPMUX=1",5,"OK",false);
 sendCommand("AT+CIPSTART=4,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK",false);
 sendCommand("AT+CIPSEND=4," +String(getData.length()+4),4,">",false);


 sendCommand(getData,20,"OK",true);
 delay(1500);
 countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK",false);
}

bool ConnectToWifi(){
  for (int a=0; a<15; a++)
  {
    sendCommand("AT",5,"OK",false);
    sendCommand("AT+CWMODE=1",5,"OK",false);
    boolean isConnected = sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK",false);
    if(isConnected)
    {
      return true;
    }
  }
}

bool sendCommand(String command, int maxTime, char readReplay[],boolean isGetData) {
  boolean result=false;

  //Test Purpose
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    Serial1.println(command);
    if(Serial1.find(readReplay))//ok
    {   
      if(isGetData)
      {      
        if(Serial1.find(readReplay))
        {
          Serial.println("Success : Request is taken from the server");
        }
         while(Serial1.available())
        {
            char character = Serial1.read()
            Data.concat(character); /
            if (character == '\n')
             {
             Serial.print("Received: ");
             Serial.println(Data);
             delay(50);
             Data = "";
        }
        }
                 
      }
      result = true;
      break;
    }
    countTimeCommand++;
  }
  
  if(result == true)
  {
    Serial.println("Success");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(result == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
  return result;

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