ESP8266 connecting to internet
You should connect to the module as outlined in my first article about this module (which also shows use of some other commands).
To try out the commands in this post you should already have the module set up in STA or AP+STA mode and connected to router/internet.
If bray terminal works for you you can try using it for this example as it really has a neat function of being able to store multiple predefined send sentences (“Macros”) and access them at the click of a button. On my machine however for some reason the bray terminal doesn’t read the port correctly when there is a lot of data coming in (like receiving the content of a webpage) – I guess this has something to do with Windows 8. The display in bray terminal is also neater than the RealTerm used here.
AT Commands:
AT+CWMODE=3 – set the module operating mode to STA+AP.
AT+RST – reset the module to finish changing the operating mode.
AT+CIPMUX=1 – enable multiple connections.
AT+CWLAP – check the available access points to connect to.
AT+CWJAP=”ssid”,”password” to connect to the access point.
AT+CIFSR – check the module ip and connection status.
AT+CIPSTART=4,”TCP”,”www.google.com”,80 – start the connection to google (you may occasionally get DNS fail, just retry the command)
AT+CIPSEND=4,42 -send 42 characters
When you receive > from the module send the following data (replacing \r\n with CR and LF characters):
GET / HTTP/1.1\r\n
Host: www.google.com\r\n\r\n\r\n
(The first two of the request in green are sent from the PC, the rest is reply from the modem.)
You should get SEND OK and after that the content of the webpage requested in +IPD format. I get a 302 page moved result and a link to local google page google.si. If you malformed the GET request you will get a 400 error. The +IPD frames will be maximum of 1024 bytes long. If the webpage content is larger it will be split into several frames.
You should use AT+CIPCLOSE=4 to close the connection if the server doesn’t close it automatically. Otherwise you may get unexpected results.
By replacing the first / in the GET request with the url of your choice you can request any url on the www.google.com host. You can also send GET data to the server with it. When changing the request you also have to adjust the number of characters of request in CIPSEND command.
Obivously you can also get data from the server by looking at the content that is returned. Note that also HTTP header is output, not only the HTML code /content
Macros for bray terminal:
AT+CIPSTART=4,”TCP”,”www.google.com”,80#013#010
AT+CIPSEND=4,42#013#010
GET / HTTP/1.1#013#010Host: www.google.com#013#010#013#010#013#010
AT+CIPCLOSE=4#013#010
I tried to get data from a website (I set DST_IP to “www.google.de”) and the only thing I get back is a few characters. Do you know where the mistake is which makes my ESP8266 only receive/output a few characters instead what you got?
This is the important part of code I used:
void loop()
{
String cmd = “AT+CIPSTART=\”TCP\”,\””;
cmd += DST_IP;
cmd += “\”,80″;
Serial1.println(cmd);
Serial.println(cmd);
if (Serial1.find(“Error”)) return;
cmd = “GET / HTTP/1.0\r\n\r\n”;
Serial1.print(“AT+CIPSEND=”);
Serial1.println(cmd.length());
if (Serial1.find(“>”))
{
Serial.print(“>”);
}
else
{
Serial1.println(“AT+CIPCLOSE”);
Serial.println(“connect timeout”);
delay(1000);
return;
}
Serial1.print(cmd);
delay(2000);
while (Serial1.available())
{
char c = Serial1.read();
Serial.write(c);
if (c == ‘\r’) Serial.print(‘\n’);
}
Serial.println(“====”);
delay(1000);
}
…and the output I get:
AT+CIPSTART=”TCP”,”www.google.de”,80
> GET / HTTP/1.0
SEND OK
+IPD,520:HTTP/1.0 302 Found
Cach====
I would be very pleased if you could find the mistake, because i didn’t find anything on the internet which could help 🙁 thanks!
I think the problem with your code is actually a problem with Arduino itself… Arduino doesn’t have much memory available, so only 64 characters are reserved for serial buffering.
You can increase the memory used for serial buffer, however that will only alleviate the problem (you will be able to read more content), not fix it completely:
http://www.hobbytronics.co.uk/arduino-serial-buffer-size
The characters are being kept in serial buffer only until you read them, so if you remove the delay(2000); line from your code you will probably also be able to read some more data. Note that if the buffer gets filled while you read it (if the reading is too slow) you will get data with some characters / content missing in between.
If you set the baudrate on ESP8266 to a lower setting you may also be able to get the arduino to read some more data.
How is one supposed to replace the \r\n with CR and LF? What characters are you referring to? I ask because I’ve been trying to issue an http request via my ESP8266 and keep receiving a reply of Bad Request (400) back. If it helps this is my request:
GET channels/1417/field/2/last.txt HTTP/1.0\r\nHost: api.thinkspeak.com\r\n\r\n
Hi Seth, “\r” character is sent as CR and “\n” character is sent as LF. I think your request is missing the leading slash in the url. Try “GET /channels/1417/field/2/last.txt HTTP/1.0\r\nHost: api.thinkspeak.com\r\n\r\n” instead, this gives me 200 OK reply with some content…
I send http GET request using serial terminal like putty,flash magic,minicom and respond comes 400 bad request but same http GET request send using Realterm serial terminal then respond comes 200 OK so Realterm serial terminal works fine but problem in putty,minicom etc. please gives solution anybody that how can works fine in minicom,putty.
I send AT command in esp8266 wifi modules follwing way
AT+CIPSTART=4,”TCP”,”api.openweathermap.org”,80\r\n
AT+CIPSEND=4,85\r\n
GET /data/2.5/weather?q=San%20francisco,us HTTP/1.0\r\nHost:api.openweathermap.org\r\n\r\n
Hi Pratik, It’s hard to tell from your post what the problem might be. How are you sending the request? Are you copy/pasting it or are you typing it manually? My guess would be that \r and \n special characters need to be entered differently in different programs. I think you need to use first Enter key (\r) and then Ctrl+J (\n) to get a \r\n newline in putty. Many serial port communication programs have settings which enable you to choose how does the Enter key behave (what it sends, eg. ^M, \r, \n, \r\n), but I couldn’t find one in putty.