Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
This has been bugging me for days - it seems to simple! Since you don't have any replies that, here's my suggestion: is your 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. – Mark Smith Dec 25 '16 at 16:43
up vote 0 down vote accepted

As pointed out in the first comment, my printResponse() function was not working correctly when waiting for a response from a website. I changed my code to the following, and now it seems to work every time.

#include <SoftwareSerial.h>

const byte rxPin = 2;
const byte txPin = 3;

SoftwareSerial ESP8266 (rxPin, txPin);

unsigned long lastTimeMillis = 0;

void setup() {
  Serial.begin(9600);   
  ESP8266.begin(9600);
  delay(2000);
}

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\",\"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(""); 
  }

  if (ESP8266.available()) {
    Serial.write(ESP8266.read());
  }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.