Setup and test the Adafruit HUZZAH ESP8266 Wi Fi SOC. Sets SSID and PASSWORD and prints status messages. For use on mbed LPC1768. See https://developer.mbed.org/users/ausdong/notebook/using-the-adafruit-huzzah-esp8266-to-add-wi-fi-to-/
Fork of ESP8266-configuration-mbed-LPC1768 by
Diff: main.cpp
- Revision:
- 6:05ae8673a743
- Parent:
- 5:9f46b8cdd469
diff -r 9f46b8cdd469 -r 05ae8673a743 main.cpp --- a/main.cpp Fri Aug 28 01:21:15 2015 +0000 +++ b/main.cpp Fri Mar 18 19:01:48 2016 +0000 @@ -2,48 +2,55 @@ Serial pc(USBTX, USBRX); Serial esp(p28, p27); // tx, rx -DigitalOut reset(p26); Timer t; int count,ended,timeout; -char buf[1024]; -char snd[255]; +char buf[2024]; +char snd[1024]; -char ssid[32] = "mySSID"; // enter WiFi router ssid inside the quotes -char pwd [32] = "myPASSWORD"; // enter WiFi router password inside the quotes +char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes +char pwd [32] = "austin123"; // enter WiFi router password inside the quotes void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); int main() { - reset=0; //hardware reset for 8266 - pc.baud(115200); // set what you want here depending on your terminal program speed - pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); - wait(0.5); - reset=1; - timeout=2; - getreply(); - - esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. - - //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** + pc.baud(9600); // set what you want here depending on your terminal program speed ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** - - // continuosly get AP list and IP while(1) { - pc.printf("\n---------- Listing Acces Points ----------\r\n"); - strcpy(snd, "AT+CWLAP\r\n"); + pc.printf("\n---------- Listing Access Points ----------\r\n"); + strcpy(snd, "function listap(t)\r\n"); + wait(1); + strcpy(snd, "for k,v in pairs(t) do\r\n"); + SendCMD(); + wait(1); + strcpy(snd, "print(k..\" : \"..v)\r\n"); SendCMD(); + wait(1); + strcpy(snd, "end\r\n"); + SendCMD(); + wait(1); + strcpy(snd, "end\r\n"); + SendCMD(); + wait(1); + strcpy(snd, "wifi.sta.getap(listap)\r\n"); + SendCMD(); + wait(1); timeout=15; getreply(); pc.printf(buf); wait(2); pc.printf("\n---------- Get IP and MAC ----------\r\n"); - strcpy(snd, "AT+CIFSR\r\n"); + strcpy(snd, "print(wifi.sta.getip())\r\n"); + SendCMD(); + timeout=10; + getreply(); + pc.printf(buf); + strcpy(snd, "print(wifi.sta.getmac())\r\n"); SendCMD(); timeout=10; getreply(); @@ -53,32 +60,13 @@ } -// Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed -void ESPsetbaudrate() -{ - strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate - SendCMD(); -} - // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ void ESPconfig() { wait(5); - strcpy(snd,"AT\r\n"); - SendCMD(); - wait(1); - strcpy(snd,"AT\r\n"); - SendCMD(); - wait(1); - strcpy(snd,"AT\r\n"); - SendCMD(); - timeout=1; - getreply(); - wait(1); pc.printf("\f---------- Starting ESP Config ----------\r\n\n"); - pc.printf("---------- Reset & get Firmware ----------\r\n"); - strcpy(snd,"AT+RST\r\n"); + strcpy(snd,"node.restart()\r\n"); SendCMD(); timeout=5; getreply(); @@ -87,7 +75,7 @@ wait(2); pc.printf("\n---------- Get Version ----------\r\n"); - strcpy(snd,"AT+GMR\r\n"); + strcpy(snd,"print(node.info())\r\n"); SendCMD(); timeout=4; getreply(); @@ -97,17 +85,7 @@ // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) pc.printf("\n---------- Setting Mode ----------\r\n"); - strcpy(snd, "AT+CWMODE=1\r\n"); - SendCMD(); - timeout=4; - getreply(); - pc.printf(buf); - - wait(2); - - // set CIPMUX to 0=Single,1=Multi - pc.printf("\n---------- Setting Connection Mode ----------\r\n"); - strcpy(snd, "AT+CIPMUX=1\r\n"); + strcpy(snd, "wifi.setmode(wifi.STATION)\r\n"); SendCMD(); timeout=4; getreply(); @@ -116,8 +94,24 @@ wait(2); pc.printf("\n---------- Listing Access Points ----------\r\n"); - strcpy(snd, "AT+CWLAP\r\n"); + strcpy(snd, "function listap(t) \r\n"); + SendCMD(); + wait(1); + strcpy(snd, "for k,v in pairs(t) do \r\n"); + SendCMD(); + wait(1); + strcpy(snd, "print(k..\" : \"..v)\r\n"); SendCMD(); + wait(1); + strcpy(snd, "end\r\n"); + SendCMD(); + wait(1); + strcpy(snd, "end\r\n"); + SendCMD(); + wait(1); + strcpy(snd, "wifi.sta.getap(listap) \r\n"); + SendCMD(); + wait(1); timeout=15; getreply(); pc.printf(buf); @@ -126,20 +120,20 @@ pc.printf("\n---------- Connecting to AP ----------\r\n"); pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); - strcpy(snd, "AT+CWJAP=\""); + strcpy(snd, "wifi.sta.config(\""); strcat(snd, ssid); strcat(snd, "\",\""); strcat(snd, pwd); - strcat(snd, "\"\r\n"); + strcat(snd, "\")\r\n"); SendCMD(); timeout=10; getreply(); pc.printf(buf); - wait(5); + wait(10); pc.printf("\n---------- Get IP's ----------\r\n"); - strcpy(snd, "AT+CIFSR\r\n"); + strcpy(snd, "print(wifi.sta.getip())\r\n"); SendCMD(); timeout=3; getreply(); @@ -148,7 +142,7 @@ wait(1); pc.printf("\n---------- Get Connection Status ----------\r\n"); - strcpy(snd, "AT+CIPSTATUS\r\n"); + strcpy(snd, "print(wifi.sta.status())\r\n"); SendCMD(); timeout=5; getreply(); @@ -182,4 +176,4 @@ t.reset(); } } -} +} \ No newline at end of file