data loaded from "www.example.com" |
www.example.com displayed at normal browser |
Connection between Arduino Due and ESP8266, make sure both Due and ESP8266 are worked on 3.3V.
The ESP8266 is flashed with firmware "v0.9.5.2 AT Firmware.bin".
Due8266Client.ino
/*
Arduino Due + ESP 8266 WiFi Module
- As STA to Join AP
- Connect to website as client
Serial (Tx/Rx) communicate to PC via USB
Serial3 (Tx3/Rx3) connect to ESP8266
Tx3 - ESP8266 Rx
Rx3 - ESP8266 Tx
ESP8266 CH_PD Connect to ESP8266 VCC
for firmware:
"v0.9.5.2 AT Firmware"
(http://goo.gl/oRdG3s)
AT version:0.21.0.0
SDK version:0.9.5
*/
#define ESP8266 Serial3
String SSID = "TestAP";
String PASSWORD = "12345678";
int LED = 13;
boolean FAIL_8266 = false;
#define BUFFER_SIZE 1024
char buffer[BUFFER_SIZE];
void setup() {
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(300);
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
do{
//Serial.begin(9600);
//ESP8266.begin(9600);
Serial.begin(115200);
ESP8266.begin(115200);
//Wait Serial Monitor to start
while(!Serial);
Serial.println("--- Start ---");
ESP8266.println("AT+RST");
delay(1000);
if(ESP8266.find("ready"))
{
Serial.println("Module is ready");
ESP8266.println("AT+GMR");
delay(1000);
clearESP8266SerialBuffer();
ESP8266.println("AT+CWMODE=1");
//ESP8266.println("AT+CWMODE=3");
delay(2000);
//Quit existing AP, for demo
Serial.println("Quit AP");
ESP8266.println("AT+CWQAP");
delay(1000);
clearESP8266SerialBuffer();
if(cwJoinAP())
{
Serial.println("CWJAP Success");
FAIL_8266 = false;
delay(3000);
clearESP8266SerialBuffer();
//Get and display my IP
sendESP8266Cmdln("AT+CIFSR", 1000);
//Set multi connections
sendESP8266Cmdln("AT+CIPMUX=1", 1000);
//sendESP8266Cmdln("AT+CIPMUX=0", 1000);
Serial.println("Setup finish");
}else{
Serial.println("CWJAP Fail");
delay(500);
FAIL_8266 = true;
}
}else{
Serial.println("Module have no response.");
delay(500);
FAIL_8266 = true;
}
}while(FAIL_8266);
digitalWrite(LED, HIGH);
//set timeout duration ESP8266.readBytesUntil
ESP8266.setTimeout(1000);
}
void loop(){
/*
AT+CIPSTART=id,"type","addr",port
id = 0
type = "TCP"
addr = "www.example.com"
port = 80
*/
String TARGET_ID="0";
String TARGET_TYPE="TCP";
String TARGET_ADDR="www.example.com";
String TARGET_PORT="80";
//String TARGET_ADDR="www.google.com";
//String TARGET_PORT="80";
String cmd="AT+CIPSTART=" + TARGET_ID;
cmd += ",\"" + TARGET_TYPE + "\",\"" + TARGET_ADDR + "\"";
cmd += ","+ TARGET_PORT;
Serial.println(cmd);
ESP8266.println(cmd);
delay(1000);
//Assume OK
//display and clear buffer
clearESP8266SerialBuffer();
/*
GET / HTTP/1.1\r\n
Host: www.example.com:80\r\n\r\n
*/
String HTTP_RQS = "GET / HTTP/1.1\r\n";
HTTP_RQS += "Host: " + TARGET_ADDR;
HTTP_RQS += ":" + TARGET_PORT + "\r\n\r\n";
String cmdSEND_length = "AT+CIPSEND=";
cmdSEND_length += TARGET_ID + "," + HTTP_RQS.length() +"\r\n";
ESP8266.print(cmdSEND_length);
Serial.println(cmdSEND_length);
Serial.println("waiting >");
if(!ESP8266.available());
if(ESP8266.find(">")){
Serial.println("> received");
ESP8266.println(HTTP_RQS);
Serial.println(HTTP_RQS);
boolean OK_FOUND = false;
//program blocked untill "SEND OK" return
while(!OK_FOUND){
if(ESP8266.readBytesUntil('\n', buffer, BUFFER_SIZE)>0){
Serial.println("...");
Serial.println(buffer);
if(strncmp(buffer, "SEND OK", 7)==0){
OK_FOUND = true;
Serial.println("SEND OK found");
}else{
Serial.println("Not SEND OK...");
}
}
}
if(OK_FOUND){
//Dummy display received data
//until connection CLOSED, "</HTML>" or "</html>" received
//only compare beginning of lines
int i;
while((i=ESP8266.readBytesUntil('\n', buffer, BUFFER_SIZE-1))>=0){
buffer[i] = '\0'; //insert terminator
Serial.println(buffer);
//have to match with TARGET_ID
if(strncmp(buffer, "0,CLOSED", 7)==0){
Serial.println("CLOSED");
break;
}
if(strncmp(buffer, "</HTML>", 7)==0){
Serial.println("</HTML> found");
break;
}
if(strncmp(buffer, "</html>", 7)==0){
Serial.println("</html> found");
break;
}
}
}
}else{
Serial.println("> NOT received, something wrong!");
}
//Close connection
String cmdCIPCLOSE = "AT+CIPCLOSE=" + TARGET_ID;
ESP8266.println(cmdCIPCLOSE);
Serial.println(cmdCIPCLOSE);
delay(10000);
}
boolean waitOKfromESP8266(int timeout)
{
do{
Serial.println("wait OK...");
delay(1000);
if(ESP8266.find("OK"))
{
return true;
}
}while((timeout--)>0);
return false;
}
boolean cwJoinAP()
{
String cmd="AT+CWJAP=\"" + SSID + "\",\"" + PASSWORD + "\"";
ESP8266.println(cmd);
return waitOKfromESP8266(10);
}
//Send command to ESP8266, assume OK, no error check
//wait some time and display respond
void sendESP8266Cmdln(String cmd, int waitTime)
{
ESP8266.println(cmd);
delay(waitTime);
clearESP8266SerialBuffer();
}
//Basically same as sendESP8266Cmdln()
//But call ESP8266.print() instead of call ESP8266.println()
void sendESP8266Data(String data, int waitTime)
{
//ESP8266.print(data);
ESP8266.print(data);
delay(waitTime);
clearESP8266SerialBuffer();
}
//Clear and display Serial Buffer for ESP8266
void clearESP8266SerialBuffer()
{
Serial.println("= clearESP8266SerialBuffer() =");
while (ESP8266.available() > 0) {
char a = ESP8266.read();
Serial.write(a);
}
Serial.println("==============================");
}
- We can also host our simple web server using python SimpleHTTPServer. To be reached by Arduino Due + ESP8266 web client. Read next "Arduino Due + ESP8266 as client, connect to Python SimpleHTTPServer".
Related:
- Arduino Due + ESP8266 + DHT11, to update dweet.io
Thanks for this great tutorial. I'm struggling in finding an Arduino library for my ESP8266 module that works with the Arduino DUE. Do you find along the street one library that works?
ReplyDeleteHello, are you using the same library as the Uno board?
ReplyDeleteThis example not work on Uno, because Uno support one Serial port only.
Delete