Serial ESP8266 and Arduino

Serial ESP8266 and Arduino

This page is to Setup and Config Serial ESP8266 and Arduino

Hardware and Software

Hardware

Serial ESP8266 and Arduino

Software

Arduino IDE

Example :

Important Notes

  • Case 1 : Control By Arduino (Programming only on Arduino) (Example 2)

    • Upload standard frimware to ESP-01 (ESP8266的固件将更改为V0.9.2.3版本)

      • [System Ready, Vendor:www.ai-thinker.com]

      • AT+GMR

      • 0018000902

      • Modify ESP-01 SoftAP (SSID and Password for Client connect)

    • Case 2 : Send and Receive Data between Arduino and ESP-01

      • Upload Program to Arduino and ESP-01 (Example)

    • Case 3 : Only ESP-01 for Software Server

Sanki Notes

    • SoftwareSerial on Arduino

Example : Simple Serial Between ESP-01 and Arduino

  • This example is very simple Serial Communication ESP8266 and Arduino

  • ESP8266 ESP-01 have to update frimware to AT Command Mode

Example 2 : Web Client ---> ESP-01 Server --> Arduino (By AT Command Call)

  • Very Good for Web Client ---> ESP-01 Server --> Arduino (By AT Command Call)

Examples - Different SDK Version

  • Beware Different SDK Version ( This Example Version is : SDK 1.5.3 as at 2016) Check Here

    • This Sample Have Client and SoftAP but both IP can go into same Service by Arduino Programming

    • Preset on ESP-01

      • Client and SoftAP Mode : AT+CWMODE=3

      • SoftAP : AT+CWSAP="<SSID>","<Password>",11,3

        • (Two ESP-01 have two SSID)

      • SoftAP IP : AT+CIPAP="192.168.167.1"

      • Active DHCP : AT+CWDHCP=2,0

    • Set MultiConnect : AT+CIPMUX=1

    • Create Server Port : AT+CIPSERVER=1,80

    • Set Client :

      • AT+CWJAP="<SSID>","<Password>"

      • WIFI DISCONNECT

      • WIFI CONNECTED

      • WIFI GOT IP

      • Set Client IP : AT+CIPSTA="192.168.0.150"

  • CHECK

    • Check SoftAP

    • AT+CIPAP="192.168.167.1"

    • OK

    • AT+CIPAP?

      • +CIPAP:ip:"192.168.167.1"

      • +CIPAP:gateway:"192.168.167.1"

      • +CIPAP:netmask:"255.255.255.0"

    • OK

    • AT+CWSAP?

      • +CWSAP:"<SSID>","<Password>",11,3,4,0

    • OK

    • Check Client

    • AT+CWJAP?

      • +CWJAP:"<SSID>","64:71:02:8e:be:42",9,-73

      • OK

    • AT+CIPSTA?

      • +CIPSTA:ip:"192.168.0.150"

      • +CIPSTA:gateway:"192.168.0.1"

      • +CIPSTA:netmask:"255.255.255.0"

Last update : 20160505

        1. #include <SoftwareSerial.h>

        2. SoftwareSerial ESPserial(2, 3); // RX | TX

        3. void setup()

        4. {

        5. Serial.begin(9600); // communication with the host computer

        6. //while (!Serial) { ; }

        7. // Start the software serial for communication with the ESP8266

        8. ESPserial.begin(9600);

        9. Serial.println("");

        10. Serial.println("Remember to to set Both NL & CR in the serial monitor.");

        11. Serial.println("Ready");

        12. Serial.println("");

        13. }

        14. void loop()

        15. {

        16. // listen for communication from the ESP8266 and then write it to the serial monitor

        17. if ( ESPserial.available() ) { Serial.write( ESPserial.read() ); }

        18. // listen for user input and send it to the ESP8266

        19. if ( Serial.available() ) { ESPserial.write( Serial.read() ); }

        20. }

        21. /*

        22. Arduino Due - ESP 8266 WiFi Module

        23. Act as AP mode, CWMODE=2

        24. Serial (Tx/Rx) communicate to PC via USB

        25. Serial3 (Tx3/Rx3) connect to ESP8266

        26. Arduino Pin 2 --- Tx3 - ESP8266 Rx

        27. Arduino Pin 3 --- Rx3 - ESP8266 Tx

        28. ESP8266 CH_PD Connect to ESP8266 VCC

        29. #include <SoftwareSerial.h>

        30. SoftwareSerial ESP8266(2, 3); // RX | TX

        31. for firmware:

        32. "v0.9.5.2 AT Firmware"

        33. (http://goo.gl/oRdG3s)

        34. AT version:0.21.0.0

        35. SDK version:0.9.5

        36. How to Use

        37. post : http://<Server>/len=0 or len=1

        38. Respore : message after post

        39. Link

        40. +IPD,0,443:GET /len=1 HTTP/1.1

        41. Host: 192.168.4.1

        42. Conn

        43. */

        44. #include <SoftwareSerial.h>

        45. SoftwareSerial ESP8266(2, 3); // RX | TX

        46. //#define ESP8266 Serial3

        47. int LED = 13;

        48. boolean FAIL_8266 = false;

        49. #define BUFFER_SIZE 128

        50. char buffer[BUFFER_SIZE];

        51. void setup() {

        52. pinMode(LED, OUTPUT);

        53. digitalWrite(LED, LOW);

        54. delay(300);

        55. digitalWrite(LED, HIGH);

        56. delay(200);

        57. digitalWrite(LED, LOW);

        58. delay(300);

        59. digitalWrite(LED, HIGH);

        60. delay(200);

        61. digitalWrite(LED, LOW);

        62. delay(300);

        63. digitalWrite(LED, HIGH);

        64. delay(200);

        65. digitalWrite(LED, LOW);

        66. do{

        67. Serial.begin(9600);

        68. ESP8266.begin(9600);

        69. //Wait Serial Monitor to start

        70. while(!Serial);

        71. Serial.println("--- Start ---");

        72. ESP8266.println("AT+RST");

        73. delay(1000);

        74. if(ESP8266.find("OK"))

        75. {

        76. delay(5000);

        77. Serial.println("Module is ready");

        78. ESP8266.println("AT+CWMODE=2");

        79. delay(1000);

        80. clearESP8266SerialBuffer();

        81. //Get and display my IP

        82. sendESP8266Cmdln("AT+CIFSR", 1000);

        83. //Get and display my IP

        84. /* Arduino Due - ESP 8266 WiFi Module Act as AP mode, CWMODE=2 Serial (Tx/Rx) communicate to PC via USB Serial3 (Tx3/Rx3) connect to ESP8266 Arduino Pin 2 --- Tx3 - ESP8266 Rx Arduino Pin 3 --- Rx3 - ESP8266 Tx ESP8266 CH_PD Connect to ESP8266 VCC #include <SoftwareSerial.h> SoftwareSerial ESP8266(2, 3); // RX | TX for firmware: AT version:1.0.0.0 SDK version:1.5.2 How to Use post : http://<Server>/len=0 or len=1 Respore : message after post Link +IPD,0,443:GET /len=1 HTTP/1.1 Host: 192.168.4.1 Conn In This Programming ----- Receive Data from Serial ESP8266 to Arduino Find Postion : Serial.println("+IPD, found"); Read Next char : int connectionId = ESP8266.read()-48; ------------------------------------------------------- Default Setup on ESP8266 ESP-01 SSID : RemoteServer Password : <password> SoftAP IP : 192.168.167.1 SoftAP Gateway : "192.168.167.1" SoftAP netmask : 255.255.255.0 Setup ------ AT+CWSAP="RemoteServer","<password>",11,3 Testing --------- AT+CWSAP? +CWSAP:"RemoteServer","<password>",11,3,4,0 */ #include <SoftwareSerial.h> SoftwareSerial ESP8266(2, 3); // RX | TX //#define ESP8266 Serial3 int LED = 13; boolean FAIL_8266 = false; #define BUFFER_SIZE 128 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); delay(300); digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW); do{ Serial.begin(115200); ESP8266.begin(115200); //Wait Serial Monitor to start while(!Serial); Serial.println("--- Start ---"); ESP8266.println("AT+RST"); delay(1000); /* Serial.println("--------------- DEBUG ----------------ORGINIAL"); while (ESP8266.available()) Serial.print(char(ESP8266.read())); Serial.println("\n--------------- END DEBUG ----------------"); */ if(ESP8266.find("OK")) { delay(5000); Serial.println("Module is ready"); /* ESP8266.println("AT+CWMODE=2"); delay(1000); clearESP8266SerialBuffer(); //Get and display my IP sendESP8266Cmdln("AT+CIFSR", 1000); //Get and display my IP //sendESP8266Cmdln("AT+CIPSTA=\"192.168.10.1\"", 1000); //ESP8266.println("AT+RST"); */ //Set multi connections sendESP8266Cmdln("AT+CIPMUX=1", 1000); //Setup web server on port 80 sendESP8266Cmdln("AT+CIPSERVER=1,80",1000); Serial.println("Server setup finish"); FAIL_8266 = false; }else{ Serial.println("Module have no response."); delay(500); FAIL_8266 = true; } }while(FAIL_8266); digitalWrite(LED, HIGH); ESP8266.setTimeout(1000); } void loop(){ if(ESP8266.available()) // check if the esp is sending a message { Serial.println("Something received"); delay(1000); //while (ESP8266.available()) // Serial.print(char(ESP8266.read())); //Serial.println(""); /* * --------------- DEBUG ----------------ORGINIAL 1,CLOSED 1,CONNECT +IPD,1,444:GET /len=1 HTTP/1.1 Host: 19 --------------- END DEBUG ---------------- */ /* ----------------------------------------------------------------------- String str = ""; Serial.println("--------------- DEBUG ----------------ORGINIAL"); // // String Control // while (ESP8266.available()) { str += char(ESP8266.read()); } Serial.println(str); // // Serial Control // //while (ESP8266.available()) { // Serial.print(char(ESP8266.read())); //} Serial.println("\n--------------- END DEBUG ----------------"); //------------------------------------------------------------------------------- */ if(ESP8266.find("+IPD,")) { String action; Serial.println("+IPD, found"); int connectionId = ESP8266.read()-48; Serial.println("connectionId: " + String(connectionId)); Serial.println("--------------- DEBUG ----------------A"); char temp = char(ESP8266.read()); while ((temp != '/') && (ESP8266.available())) { Serial.print(temp); temp = char(ESP8266.read()); } Serial.println("\n--------------- END DEBUG ----------------"); boolean isFound = ESP8266.find("len="); Serial.print("is Found : "); Serial.println(isFound); /* Serial.println("--------------- DEBUG ----------------B"); while (ESP8266.available()) Serial.print(char(ESP8266.read())); Serial.println("\n--------------- END DEBUG ----------------"); */ char s = ESP8266.read(); if(s=='0'){ action = "led=0"; digitalWrite(LED, LOW); }else if(s=='1'){ action = "led=1"; digitalWrite(LED, HIGH); }else{ action = "led=?"; } Serial.println(action); sendHTTPResponse(connectionId, action); //Close TCP/UDP //String cmdCIPCLOSE = "AT+CIPCLOSE="; //cmdCIPCLOSE += connectionId; //sendESP8266Cmdln(cmdCIPCLOSE, 1000); } } } void sendHTTPResponse(int id, String content) { String response; /* response = "HTTP/1.1 200 OK\r\n"; response += "Content-Type: text/html; charset=UTF-8\r\n"; response += "Content-Length: "; response += content.length(); response += "\r\n"; response +="Connection: close\r\n\r\n"; */ response = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n"; response += "<body align=center>"; //response += "<img src=\"http://www.imediabank.com/_/rsrc/1462251563655/home/title.png\" />"; //response += "<img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAALVBMVEX///8EBwfBwsLw8PAzNjaCg4NTVVUjJiZDRUUUFxdiZGSho6OSk5Pg4eFydHTCjaf3AAAAZElEQVQ4je2NSw7AIAhEBamKn97/uMXEGBvozkWb9C2Zx4xzWykBhFAeYp9gkLyZE0zIMno9n4g19hmdY39scwqVkOXaxph0ZCXQcqxSpgQpONa59wkRDOL93eAXvimwlbPbwwVAegLS1HGfZAAAAABJRU5ErkJggg==\" />"; response += "<p></p>"; response += "<a href=\"\len=1\"><font size=\"6\">ON</font></a><p>"; response += "<a href=\"\len=0\"><font size=\"6\">OFF</font></a><p>"; response += "<font size=\"6\">"; response += content; response += "</font>"; response += "</body></html>\n"; String cmd = "AT+CIPSEND="; cmd += id; cmd += ","; cmd += response.length(); Serial.println("--- AT+CIPSEND ---"); sendESP8266Cmdln(cmd, 1000); Serial.println("--- data ---"); sendESP8266Data(response, 1000); } boolean waitOKfromESP8266(int timeout) { do{ Serial.println("wait OK..."); delay(1000); if(ESP8266.find("OK")) { return true; } }while((timeout--)>0); return false; } //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); 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.write(char(ESP8266.read())); } Serial.println(""); Serial.println("==============================\n"); }

        85. //sendESP8266Cmdln("AT+CIPSTA=\"192.168.10.1\"", 1000);

        86. //ESP8266.println("AT+RST");

        87. //Get and display my IP

        88. sendESP8266Cmdln("AT+CIFSR", 1000);

        89. //Set multi connections

        90. sendESP8266Cmdln("AT+CIPMUX=1", 1000);

        91. //Setup web server on port 80

        92. sendESP8266Cmdln("AT+CIPSERVER=1,80",1000);

        93. Serial.println("Server setup finish");

        94. FAIL_8266 = false;

        95. }else{

        96. Serial.println("Module have no response.");

        97. delay(500);

        98. FAIL_8266 = true;

        99. }

        100. }while(FAIL_8266);

        101. digitalWrite(LED, HIGH);

        102. ESP8266.setTimeout(1000);

        103. }

        104. void loop(){

        105. if(ESP8266.available()) // check if the esp is sending a message

        106. {

        107. Serial.println("Something received");

        108. delay(1000);

        109. //while (ESP8266.available())

        110. // Serial.print(char(ESP8266.read()));

        111. //Serial.println("");

        112. /*

        113. Serial.println("--------------- DEBUG ----------------ORGINIAL");

        114. while (ESP8266.available())

        115. Serial.print(char(ESP8266.read()));

        116. Serial.println("\n--------------- END DEBUG ----------------");

        117. */

        118. if(ESP8266.find("+IPD,"))

        119. {

        120. String action;

        121. Serial.println("+IPD, found");

        122. int connectionId = ESP8266.read()-48;

        123. Serial.println("connectionId: " + String(connectionId));

        124. Serial.println("--------------- DEBUG ----------------A");

        125. char temp = char(ESP8266.read());

        126. while ((temp != '/') && (ESP8266.available())) {

        127. Serial.print(temp);

        128. temp = char(ESP8266.read());

        129. }

        130. Serial.println("\n--------------- END DEBUG ----------------");

        131. boolean isFound = ESP8266.find("len=");

        132. Serial.print("is Found : ");

        133. Serial.println(isFound);

        134. /*

        135. Serial.println("--------------- DEBUG ----------------B");

        136. while (ESP8266.available())

        137. Serial.print(char(ESP8266.read()));

        138. Serial.println("\n--------------- END DEBUG ----------------");

        139. */

        140. char s = ESP8266.read();

        141. if(s=='0'){

        142. action = "led=0";

        143. digitalWrite(LED, LOW);

        144. }else if(s=='1'){

        145. action = "led=1";

        146. digitalWrite(LED, HIGH);

        147. }else{

        148. action = "led=?";

        149. }

        150. Serial.println(action);

        151. sendHTTPResponse(connectionId, action);

        152. //Close TCP/UDP

        153. //String cmdCIPCLOSE = "AT+CIPCLOSE=";

        154. //cmdCIPCLOSE += connectionId;

        155. //sendESP8266Cmdln(cmdCIPCLOSE, 1000);

        156. }

        157. }

        158. }

        159. void sendHTTPResponse(int id, String content)

        160. {

        161. String response;

        162. /*

        163. response = "HTTP/1.1 200 OK\r\n";

        164. response += "Content-Type: text/html; charset=UTF-8\r\n";

        165. response += "Content-Length: ";

        166. response += content.length();

        167. response += "\r\n";

        168. response +="Connection: close\r\n\r\n";

        169. */

        170. response = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";

        171. response += "<body align=center>";

        172. response += "<p>";

        173. response += "<a href=\"http://192.168.4.1/len=1\">ON</a><p>";

        174. response += "<a href=\"http://192.168.4.1/len=0\">OFF</a><p>";

        175. response += content;

        176. response += "</body></html>\n";

        177. String cmd = "AT+CIPSEND=";

        178. cmd += id;

        179. cmd += ",";

        180. cmd += response.length();

        181. Serial.println("--- AT+CIPSEND ---");

        182. sendESP8266Cmdln(cmd, 1000);

        183. Serial.println("--- data ---");

        184. sendESP8266Data(response, 1000);

        185. }

        186. boolean waitOKfromESP8266(int timeout)

        187. {

        188. do{

        189. Serial.println("wait OK...");

        190. delay(1000);

        191. if(ESP8266.find("OK"))

        192. {

        193. return true;

        194. }

        195. }while((timeout--)>0);

        196. return false;

        197. }

        198. //Send command to ESP8266, assume OK, no error check

        199. //wait some time and display respond

        200. void sendESP8266Cmdln(String cmd, int waitTime)

        201. {

        202. ESP8266.println(cmd);

        203. delay(waitTime);

        204. clearESP8266SerialBuffer();

        205. }

        206. //Basically same as sendESP8266Cmdln()

        207. //But call ESP8266.print() instead of call ESP8266.println()

        208. void sendESP8266Data(String data, int waitTime)

        209. {

        210. ESP8266.print(data);

        211. delay(waitTime);

        212. clearESP8266SerialBuffer();

        213. }

        214. //Clear and display Serial Buffer for ESP8266

        215. void clearESP8266SerialBuffer()

        216. {

        217. Serial.println("= clearESP8266SerialBuffer() =");

        218. while (ESP8266.available() > 0) {

        219. char a = ESP8266.read();

        220. Serial.write(a);

        221. }

        222. Serial.println("==============================");

        223. }