GET Requests not working - CRLF issue
Posted: Thu Feb 12, 2015 6:18 am
Hey guys,
I've been stuck on getting the ESP8266 do a simple GET request on an Arduino Mega. I've tried both with serial using AT Commands and using Arduino Serial with the below code. I have the latest firmware installed (.95) and am getting the headers to work via telnet. Unfortunately I just don't get it to work with the ESP8266 - I've literally spent hours following examples but no luck yet.
It seems to me like the ESP8266 does not recognise \r\n\r\n at the end of the header where it stops it from transmitting. I've tried similar commands using serial with straight AT commands but also there it doesn't seem to allow me to get the Header straight as it continues to give me 400 errors.
If anyone can help me out or can point me in the right direction that would be just awesome!
Arduino Code
Response & Debug
I've been stuck on getting the ESP8266 do a simple GET request on an Arduino Mega. I've tried both with serial using AT Commands and using Arduino Serial with the below code. I have the latest firmware installed (.95) and am getting the headers to work via telnet. Unfortunately I just don't get it to work with the ESP8266 - I've literally spent hours following examples but no luck yet.
It seems to me like the ESP8266 does not recognise \r\n\r\n at the end of the header where it stops it from transmitting. I've tried similar commands using serial with straight AT commands but also there it doesn't seem to allow me to get the Header straight as it continues to give me 400 errors.
If anyone can help me out or can point me in the right direction that would be just awesome!
Arduino Code
Code: Select all
#define SSID "SSID"
#define PASS "PASS"
#define DEST_HOST "www.ntu.edu.sg"
#define TIMEOUT 10000 // mS
#define CONTINUE false
#define HALT true
#define ECHO_COMMANDS // Un-comment to echo AT+ commands to serial monitor
// Print error message and loop stop.
void errorHalt(String msg)
{
Serial.println(msg);
Serial.println("HALT");
while(true){};
}
// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
byte current_char = 0;
byte keyword_length = keyword.length();
// Fail if the target string has not been sent by deadline.
long deadline = millis() + TIMEOUT;
while(millis() < deadline)
{
if (Serial1.available())
{
char ch = Serial1.read();
Serial.write(ch);
if (ch == keyword[current_char])
if (++current_char == keyword_length)
{
Serial.println();
return true;
}
}
}
return false; // Timed out
}
// Read and echo all available module output.
// (Used when we're indifferent to "OK" vs. "no change" responses or to get around firmware bugs.)
void echoFlush()
{while(Serial1.available()) Serial.write(Serial1.read());}
// Echo module output until 3 newlines encountered.
// (Used when we're indifferent to "OK" vs. "no change" responses.)
void echoSkip()
{
echoFind("\n"); // Search for nl at end of command echo
echoFind("\n"); // Search for 2nd nl at end of response.
echoFind("\n"); // Search for 3rd nl at end of blank line.
}
// Send a command to the module and wait for acknowledgement string
// (or flush module output if no ack specified).
// Echoes all data received to the serial monitor.
boolean echoCommand(String cmd, String ack, boolean halt_on_fail)
{
Serial1.println(cmd);
#ifdef ECHO_COMMANDS
Serial.print("--"); Serial.println(cmd);
#endif
// If no ack response specified, skip all available module output.
if (ack == "")
echoSkip();
else
// Otherwise wait for ack.
if (!echoFind(ack)) // timed out waiting for ack string
if (halt_on_fail)
errorHalt(cmd+" failed");// Critical failure halt.
else
return false; // Let the caller handle it.
return true; // ack blank or ack found
}
// Connect to the specified wireless network.
boolean connectWiFi()
{
String cmd = "AT+CWJAP=\""; cmd += SSID; cmd += "\",\""; cmd += PASS; cmd += "\"";
if (echoCommand(cmd, "OK", CONTINUE)) // Join Access Point
{
Serial.println("Connected to WiFi.");
return true;
}
else
{
Serial.println("Connection to WiFi failed.");
return false;
}
}
// ******** SETUP ********
void setup()
{
Serial.begin(115200); // Communication with PC monitor via USB
Serial1.begin(115200); // Communication with ESP8266 via 5V/3.3V level shifter
Serial1.setTimeout(TIMEOUT);
Serial.println("ESP8266 Demo");
delay(2000);
echoCommand("AT+RST", "ready", HALT); // Reset & test if the module is ready
Serial.println("Module is ready.");
echoCommand("AT+GMR", "OK", CONTINUE); // Retrieves the firmware ID (version number) of the module.
echoCommand("AT+CWMODE?","OK", CONTINUE);// Get module access mode.
echoCommand("AT+CWMODE=1", "", HALT); // Station mode
//connect to the wifi
boolean connection_established = false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connection_established = true;
break;
}
}
if (!connection_established) errorHalt("Connection failed");
delay(2000);
//echoCommand("AT+CWSAP=?", "OK", CONTINUE); // Test connection
echoCommand("AT+CIFSR", "", HALT); // Echo IP address. (Firmware bug - should return "OK".)
//echoCommand("AT+CIPMUX=0", "", HALT); // Set single connection mode
delay(2000);
}
// ******** LOOP ********
void loop()
{
// Establish TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\""; cmd += DEST_HOST; cmd += "\",80";
if (!echoCommand(cmd, "OK", CONTINUE)) return;
delay(2000);
// Get connection status
if (!echoCommand("AT+CIPSTATUS", "OK", CONTINUE)) return;
// Build HTTP request.
cmd = "GET / HTTP/1.0\r\n\r\n";
String cipstart = "AT+CIPSEND=";
cipstart += cmd.length();
delay(5000);
if (!echoCommand(cipstart, ">", CONTINUE))
{
echoCommand("AT+CIPCLOSE", "", CONTINUE);
Serial.println("Connection timeout.");
return;
}
delay(500);
// Send the raw HTTP request
echoCommand(cmd, "OK", CONTINUE); // GET
// Loop forever echoing data received from destination server.
while(true)
while (Serial1.available())
Serial.write(Serial1.read());
errorHalt("ONCE ONLY");
}
Response & Debug
Code: Select all
ESP8266 Demo
--AT+RST
AT+RST
OK
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x40100000, len 612, room 16
tail 4
chksum 0x12
load 0x3ffe8000, len 788, room 4
tail 0
chksum 0x50
load 0x3ffe8314, len 264, room 8
tail 0
chksum 0x4a
csum 0x4a
2nd boot version : 1.1
SPI Speed : 40MHz
SPI Mode : QIO
SPI Flash Size : 4Mbit
jump to run user1
rlS
Vendor:www.ai-thinker.com
SDK Version:0.9.5(b1)
Compiled @:Dec 25 2014, 21:50:58
ready
Module is ready.
--AT+GMR
AT+GMR
00200.9.5(b1)
compiled @ Dec 25 2014 21:40:28
AI-THINKER Dec 25 2014
OK
--AT+CWMODE?
AT+CWMODE?
+CWMODE:1
OK
--AT+CWMODE=1
AT+CWMODE=1
--AT+CWJAP="stroopwafel","0403770414"
OK
Connected to WiFi.
--AT+CIFSR
AT+CWJAP="stroopwafel","0403770414"
AT+CIFSR
--AT+CIPSTART="TCP","www.ntu.edu.sg",80
AT+CIPSTART="TCP","www.ntu.edu.sg",80
CONNECT
OK
--AT+CIPSTATUS
AT+CIPSTATUS
STATUS:3
+CIPSTATUS:0,"TCP","155.69.6.173",80,0
OK
--AT+CIPSEND=18
AT+CIPSEND=18
>
--GET / HTTP/1.0
GET / HTTP/1.0CLOSED
ERROR