Hi,
I am calling a web API using ESP8266-01 and Arduino UNO using AT Commands. The response is taken successfully. I need to omit the header from the response. And the JSON value is scrambled.
Here is my code
#include <SoftwareSerial.h>
#define RX 6
#define TX 7
String AP ="AndroidAP";
String PASS ="mvvu9640";
String HOST = "smart-dress-hanger.brain.lk";
String PORT = "80";
String field = "Id";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
SoftwareSerial esp8266(RX,TX);
String Data ="";
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
sendCommand("AT",5,"OK",false);
esp8266.println("AT+UART_DEF=9600,8,1,0,0");
delay(1000);
esp8266.end();
esp8266.begin(9600);
ConnectToWifi();
}
void loop() {
String uri= "/api/ip-addresses/18787/weather"
String getData=
"GET " + uri + " HTTP/1.0\n" +
"Host: " + HOST + "\n" +
"Accept: application/json\n" +
"Content-Type: application/json\n" +
"Connection: Keep-Alive\n" +
"\n";
sendCommand("AT+CIPMUX=1",5,"OK",false);
sendCommand("AT+CIPSTART=4,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK",false);
sendCommand("AT+CIPSEND=4," +String(getData.length()+50),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))
{
esp8266.println(command);
if(esp8266.find(readReplay))
{
if(isGetData)
{
if(esp8266.find(readReplay))
{
Serial.println("Success : Request is taken from the server");
}
while(esp8266.available())
{
char character = esp8266.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;
}
The result I am getting