I'm trying to get data from a website by sending a HTTP GET request via AT commands for the ESP8266. Here is my current code:
#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial ESP8266 (rxPin, txPin);
void setup() {
Serial.begin(9600);
ESP8266.begin(9600);
delay(2000);
}
void printResponse() {
while (ESP8266.available()) {
Serial.println(ESP8266.readStringUntil('\n'));
}
}
void loop() {
ESP8266.println("AT+CIPMUX=1");
delay(1000);
printResponse();
ESP8266.println("AT+CIPSTART=4,\"TCP\",\"192.168.1.19\",80");
delay(1000);
printResponse();
String cmd = "GET /test.html HTTP/1.1";
ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
delay(1000);
ESP8266.println(cmd);
delay(1000);
ESP8266.println();
delay(1000);
printResponse();
delay(5000);
}
Running this code I get the following response:
AT+CIPMUX=1
OK
AT+CIPSTART=4,"TCP","192.168.1.19",80
4,CONNECT
OK
AT+CIPSEND=4,27
OK
>
Recv 27 bytes
SEND OK
+IPD,4,
It looks like it gets a response, but nothing is printed.
I tried doing the same thing by sending the AT commands manually using this code:
#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial ESP8266 (rxPin, txPin);
void setup() {
Serial.begin(9600);
ESP8266.begin(9600);
}
void loop() {
if (ESP8266.available()) {
Serial.write(ESP8266.read());
}
if (Serial.available()) {
ESP8266.write(Serial.read());
}
}
With this approach I actually get a response:
AT+CIPMUX=1
OK
AT+CIPSTART=4,"TCP","192.168.1.19",80
4,CONNECT
OK
AT+CIPSEND=4,27
OK
>
Recv 27 bytes
SEND OK
+IPD,4,275:HTTP/1.1 200 OK
Date: Thu, 22 Dec 2016 20:20:47 GMT
Server: Apache/2.4.23 (Win64) PHP/5.6.25
Last-Modified: Thu, 22 Dec 2016 19:37:22 GMT
ETag: "13-5444465f69339"
Accept-Ranges: bytes
Content-Length: 19
Connection: close
Content-Type: text/html
THIS IS MY WEBSITE!4,CLOSED
Does anyone know what is happening and how I can get this to work with my Arduino code?
Other ways to get data from a website are also welcome.
printResponse()
function working as expected? It relies on all the data being available at the time time (more or less) and on it ending with a\n
. If it comes a bit at a time, it'll terminate early. If the data doesn't end with a\n
it will never end.