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-/

Dependencies:   mbed

Fork of ESP8266-configuration-mbed-LPC1768 by jim hamblen

Committer:
ausdong
Date:
Fri Mar 18 19:01:48 2016 +0000
Revision:
6:05ae8673a743
Parent:
5:9f46b8cdd469
Setup and test the Adafruit ESP8266 Wi Fi board. 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-/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:5ebf44bd3694 1 #include "mbed.h"
star297 0:5ebf44bd3694 2
star297 0:5ebf44bd3694 3 Serial pc(USBTX, USBRX);
4180_1 4:f40e7eb1ba1e 4 Serial esp(p28, p27); // tx, rx
star297 0:5ebf44bd3694 5 Timer t;
star297 0:5ebf44bd3694 6
star297 0:5ebf44bd3694 7 int count,ended,timeout;
ausdong 6:05ae8673a743 8 char buf[2024];
ausdong 6:05ae8673a743 9 char snd[1024];
star297 0:5ebf44bd3694 10
ausdong 6:05ae8673a743 11 char ssid[32] = "hsd"; // enter WiFi router ssid inside the quotes
ausdong 6:05ae8673a743 12 char pwd [32] = "austin123"; // enter WiFi router password inside the quotes
star297 0:5ebf44bd3694 13
star297 0:5ebf44bd3694 14 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate();
star297 0:5ebf44bd3694 15
star297 0:5ebf44bd3694 16
4180_1 4:f40e7eb1ba1e 17 int main()
4180_1 4:f40e7eb1ba1e 18 {
ausdong 6:05ae8673a743 19 pc.baud(9600); // set what you want here depending on your terminal program speed
star297 0:5ebf44bd3694 20
star297 0:5ebf44bd3694 21 ESPconfig(); //****************** include Config to set the ESP8266 configuration ***********************
4180_1 4:f40e7eb1ba1e 22
4180_1 4:f40e7eb1ba1e 23 // continuosly get AP list and IP
4180_1 4:f40e7eb1ba1e 24 while(1) {
ausdong 6:05ae8673a743 25 pc.printf("\n---------- Listing Access Points ----------\r\n");
ausdong 6:05ae8673a743 26 strcpy(snd, "function listap(t)\r\n");
ausdong 6:05ae8673a743 27 wait(1);
ausdong 6:05ae8673a743 28 strcpy(snd, "for k,v in pairs(t) do\r\n");
ausdong 6:05ae8673a743 29 SendCMD();
ausdong 6:05ae8673a743 30 wait(1);
ausdong 6:05ae8673a743 31 strcpy(snd, "print(k..\" : \"..v)\r\n");
star297 3:14e33fec26c3 32 SendCMD();
ausdong 6:05ae8673a743 33 wait(1);
ausdong 6:05ae8673a743 34 strcpy(snd, "end\r\n");
ausdong 6:05ae8673a743 35 SendCMD();
ausdong 6:05ae8673a743 36 wait(1);
ausdong 6:05ae8673a743 37 strcpy(snd, "end\r\n");
ausdong 6:05ae8673a743 38 SendCMD();
ausdong 6:05ae8673a743 39 wait(1);
ausdong 6:05ae8673a743 40 strcpy(snd, "wifi.sta.getap(listap)\r\n");
ausdong 6:05ae8673a743 41 SendCMD();
ausdong 6:05ae8673a743 42 wait(1);
4180_1 4:f40e7eb1ba1e 43 timeout=15;
4180_1 4:f40e7eb1ba1e 44 getreply();
star297 3:14e33fec26c3 45 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 46 wait(2);
4180_1 4:f40e7eb1ba1e 47 pc.printf("\n---------- Get IP and MAC ----------\r\n");
ausdong 6:05ae8673a743 48 strcpy(snd, "print(wifi.sta.getip())\r\n");
ausdong 6:05ae8673a743 49 SendCMD();
ausdong 6:05ae8673a743 50 timeout=10;
ausdong 6:05ae8673a743 51 getreply();
ausdong 6:05ae8673a743 52 pc.printf(buf);
ausdong 6:05ae8673a743 53 strcpy(snd, "print(wifi.sta.getmac())\r\n");
star297 3:14e33fec26c3 54 SendCMD();
4180_1 4:f40e7eb1ba1e 55 timeout=10;
4180_1 4:f40e7eb1ba1e 56 getreply();
star297 3:14e33fec26c3 57 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 58 wait(2);
4180_1 4:f40e7eb1ba1e 59 }
4180_1 4:f40e7eb1ba1e 60
star297 0:5ebf44bd3694 61 }
star297 0:5ebf44bd3694 62
star297 0:5ebf44bd3694 63 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
star297 0:5ebf44bd3694 64 void ESPconfig()
star297 0:5ebf44bd3694 65 {
4180_1 4:f40e7eb1ba1e 66 wait(5);
4180_1 4:f40e7eb1ba1e 67 pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
star297 0:5ebf44bd3694 68 pc.printf("---------- Reset & get Firmware ----------\r\n");
ausdong 6:05ae8673a743 69 strcpy(snd,"node.restart()\r\n");
star297 0:5ebf44bd3694 70 SendCMD();
4180_1 4:f40e7eb1ba1e 71 timeout=5;
star297 0:5ebf44bd3694 72 getreply();
star297 0:5ebf44bd3694 73 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 74
4180_1 4:f40e7eb1ba1e 75 wait(2);
4180_1 4:f40e7eb1ba1e 76
star297 0:5ebf44bd3694 77 pc.printf("\n---------- Get Version ----------\r\n");
ausdong 6:05ae8673a743 78 strcpy(snd,"print(node.info())\r\n");
star297 0:5ebf44bd3694 79 SendCMD();
4180_1 4:f40e7eb1ba1e 80 timeout=4;
star297 0:5ebf44bd3694 81 getreply();
4180_1 4:f40e7eb1ba1e 82 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 83
4180_1 4:f40e7eb1ba1e 84 wait(3);
4180_1 4:f40e7eb1ba1e 85
star297 0:5ebf44bd3694 86 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
star297 0:5ebf44bd3694 87 pc.printf("\n---------- Setting Mode ----------\r\n");
ausdong 6:05ae8673a743 88 strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
star297 2:c5515c7eba46 89 SendCMD();
4180_1 4:f40e7eb1ba1e 90 timeout=4;
star297 2:c5515c7eba46 91 getreply();
star297 2:c5515c7eba46 92 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 93
4180_1 4:f40e7eb1ba1e 94 wait(2);
4180_1 4:f40e7eb1ba1e 95
4180_1 4:f40e7eb1ba1e 96 pc.printf("\n---------- Listing Access Points ----------\r\n");
ausdong 6:05ae8673a743 97 strcpy(snd, "function listap(t) \r\n");
ausdong 6:05ae8673a743 98 SendCMD();
ausdong 6:05ae8673a743 99 wait(1);
ausdong 6:05ae8673a743 100 strcpy(snd, "for k,v in pairs(t) do \r\n");
ausdong 6:05ae8673a743 101 SendCMD();
ausdong 6:05ae8673a743 102 wait(1);
ausdong 6:05ae8673a743 103 strcpy(snd, "print(k..\" : \"..v)\r\n");
star297 0:5ebf44bd3694 104 SendCMD();
ausdong 6:05ae8673a743 105 wait(1);
ausdong 6:05ae8673a743 106 strcpy(snd, "end\r\n");
ausdong 6:05ae8673a743 107 SendCMD();
ausdong 6:05ae8673a743 108 wait(1);
ausdong 6:05ae8673a743 109 strcpy(snd, "end\r\n");
ausdong 6:05ae8673a743 110 SendCMD();
ausdong 6:05ae8673a743 111 wait(1);
ausdong 6:05ae8673a743 112 strcpy(snd, "wifi.sta.getap(listap) \r\n");
ausdong 6:05ae8673a743 113 SendCMD();
ausdong 6:05ae8673a743 114 wait(1);
4180_1 4:f40e7eb1ba1e 115 timeout=15;
4180_1 4:f40e7eb1ba1e 116 getreply();
star297 0:5ebf44bd3694 117 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 118
4180_1 4:f40e7eb1ba1e 119 wait(2);
4180_1 4:f40e7eb1ba1e 120
star297 0:5ebf44bd3694 121 pc.printf("\n---------- Connecting to AP ----------\r\n");
star297 0:5ebf44bd3694 122 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
ausdong 6:05ae8673a743 123 strcpy(snd, "wifi.sta.config(\"");
star297 0:5ebf44bd3694 124 strcat(snd, ssid);
star297 0:5ebf44bd3694 125 strcat(snd, "\",\"");
star297 0:5ebf44bd3694 126 strcat(snd, pwd);
ausdong 6:05ae8673a743 127 strcat(snd, "\")\r\n");
star297 0:5ebf44bd3694 128 SendCMD();
4180_1 4:f40e7eb1ba1e 129 timeout=10;
4180_1 4:f40e7eb1ba1e 130 getreply();
star297 0:5ebf44bd3694 131 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 132
ausdong 6:05ae8673a743 133 wait(10);
4180_1 4:f40e7eb1ba1e 134
4180_1 4:f40e7eb1ba1e 135 pc.printf("\n---------- Get IP's ----------\r\n");
ausdong 6:05ae8673a743 136 strcpy(snd, "print(wifi.sta.getip())\r\n");
star297 0:5ebf44bd3694 137 SendCMD();
4180_1 4:f40e7eb1ba1e 138 timeout=3;
4180_1 4:f40e7eb1ba1e 139 getreply();
star297 0:5ebf44bd3694 140 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 141
star297 3:14e33fec26c3 142 wait(1);
4180_1 4:f40e7eb1ba1e 143
4180_1 4:f40e7eb1ba1e 144 pc.printf("\n---------- Get Connection Status ----------\r\n");
ausdong 6:05ae8673a743 145 strcpy(snd, "print(wifi.sta.status())\r\n");
star297 3:14e33fec26c3 146 SendCMD();
4180_1 4:f40e7eb1ba1e 147 timeout=5;
4180_1 4:f40e7eb1ba1e 148 getreply();
4180_1 4:f40e7eb1ba1e 149 pc.printf(buf);
4180_1 4:f40e7eb1ba1e 150
4180_1 4:f40e7eb1ba1e 151 pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
star297 0:5ebf44bd3694 152 pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n");
4180_1 5:9f46b8cdd469 153 pc.printf(" It saves the SSID and password settings internally\r\n");
4180_1 4:f40e7eb1ba1e 154 wait(10);
4180_1 4:f40e7eb1ba1e 155 }
star297 0:5ebf44bd3694 156
star297 0:5ebf44bd3694 157 void SendCMD()
4180_1 4:f40e7eb1ba1e 158 {
4180_1 4:f40e7eb1ba1e 159 esp.printf("%s", snd);
4180_1 4:f40e7eb1ba1e 160 }
star297 0:5ebf44bd3694 161
star297 0:5ebf44bd3694 162 void getreply()
4180_1 4:f40e7eb1ba1e 163 {
star297 0:5ebf44bd3694 164 memset(buf, '\0', sizeof(buf));
star297 0:5ebf44bd3694 165 t.start();
4180_1 4:f40e7eb1ba1e 166 ended=0;
4180_1 4:f40e7eb1ba1e 167 count=0;
star297 0:5ebf44bd3694 168 while(!ended) {
star297 0:5ebf44bd3694 169 if(esp.readable()) {
4180_1 4:f40e7eb1ba1e 170 buf[count] = esp.getc();
4180_1 4:f40e7eb1ba1e 171 count++;
4180_1 4:f40e7eb1ba1e 172 }
star297 0:5ebf44bd3694 173 if(t.read() > timeout) {
4180_1 4:f40e7eb1ba1e 174 ended = 1;
4180_1 4:f40e7eb1ba1e 175 t.stop();
4180_1 4:f40e7eb1ba1e 176 t.reset();
4180_1 4:f40e7eb1ba1e 177 }
4180_1 4:f40e7eb1ba1e 178 }
ausdong 6:05ae8673a743 179 }