I am trying to get JSON data from my website using an HTTP GET request, but I'm getting a 400 bad request error. Can someone let me know why am I getting this error and how to fix that? I am tired of looking at online resources.
I am trying to fetch data from www.himalayavats.com/to_fetch/system_info.json
I am using this code:
#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial ESP8266 (rxPin, txPin);
unsigned long lastTimeMillis = 0;
void setup() {
sendData("AT+RST\r\n",1000, true);
Serial.begin(9600);
ESP8266.begin(9600);
delay(2000);
connectToWifi();
}
void connectToWifi() {
String ssid = "moto g";
String pass = "hvats555";
sendData("AT+CWMODE=1\r\n", 3000, true); // Configure as client
sendData("AT+CWJAP=\"moto g\",\"hvats555\"\r\n", 5000, true); // Connects to wifi
}
String sendData(String command, const int timeout, boolean debug) {
String response = "";
ESP8266.print(command); // send the read character to ESP8266
long int time = millis();
while( (time+timeout) > millis() ) {
while(ESP8266.available()) {
// The ESP has data so display its output to serial window
char c = ESP8266.read(); // read the next character
response+=c;
}
}
if(debug) {
Serial.print(response);
}
return response;
}
void printResponse() {
while (ESP8266.available()) {
Serial.println(ESP8266.readStringUntil('\n'));
}
}
void loop() {
if (millis() - lastTimeMillis > 30000) {
lastTimeMillis = millis();
ESP8266.println("AT+CIPMUX=1");
delay(1000);
printResponse();
ESP8266.println("AT+CIPSTART=4,\"TCP\",\"himalayavats.com\", 80");
delay(1000);
printResponse();
String cmd = "GET /to_fetch/system_info.json HTTP/1.1";
ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
delay(1000);
ESP8266.println(cmd);
delay(1000);
ESP8266.println("");
}
if (ESP8266.available()) {
Serial.write(ESP8266.read());
}
}
I get this response every time:
AT+CWMODE=1
OK
AT+CWJAP="moto g","hvats555"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP
OK
AT+CIPMUX=1
OK
AT+CIPSTART=4,"TCP","himalayavats.com",80
4,CONNECT
OK
AT+CIPSEND=4,43
OK
>
Recv 43 bytes
SEND OK
+IPD,4,1370:HTTP/1.1 400 Bad Request
Date: Tue, 19 Dec 2017 08:30:21 GMT
Server: Apache
Accept-Ranges: bytes
Vary: Accept-Encoding,User-Agent
Connection: close
Content-Type: text/html
My JSON file on the website looks like this:
{"power" : "high"}
delay(1000);
betweenESP8266.println(cmd);
andESP8266.println("");
? That's bound to break things...