So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Kristian2210
#90215 As the title describes, I am not getting the expected results from the HTTP request. I am using an ESP 01 module attached to my Arduino UNO. As a test project, I used an API that should give me the amount of Covid 19 infected. However, both filtered and raw results give me random characters, and what seems to be corrupt messages afterward. I really appreciate any good advice that I can get. :D :D

I don't think that there is anything wrong with my setup as I was able to send data to a thingspeak channel. This is that same code and setup, adapted to hopefully be able to get data as well.

Please let me know if you need more information.
Serial log:
Image

Code: Select all#include <SoftwareSerial.h>
#define RX 2
#define TX 3
String AP = "ap";     
String PASS = "pw";
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int buttonState = 0;
int valSensor = 1;

SoftwareSerial esp8266(RX,TX);
 
 
void setup() {
  pinMode(7, INPUT);
  Serial.begin(9600);
  esp8266.begin(9600);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=3",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

void loop()
{
  while (esp8266.available() > 0) {
    Serial.println(esp8266.read());
  }
 
  buttonState = digitalRead(7);
  if (buttonState == HIGH){
    Send();
  }
}

void Send() {
 String res;
 String subStr;
 int index;

 String getData = "GET /apps/thinghttp/send_request?api_key=HPEZ840TM16SIVAU HTTP/1.1";
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);
 esp8266.println("Host: api.thingspeak.com");
 esp8266.println("Connection: keep-alive");
 esp8266.println();
 delay(1500);countTrueCommand++;

 res = esp8266.readString();
 index = res.indexOf("formatted");
 subStr = res.substring(index+12, index+28);
 Serial.println("Raw Result:"+res);
 //Serial.println("Filtered Result:"+subStr);
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}

void sendCommand(String command, int maxTime, char readReplay[]) {
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);
    if(esp8266.find(readReplay))
    {
      found = true;
      break;
    }
    countTimeCommand++;
  }
  if(found == true)
  {
    Serial.println(" => SUCCESS");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  if(found == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  found = false;
 }