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
main.cpp@4:f40e7eb1ba1e, 2015-08-28 (annotated)
- Committer:
- 4180_1
- Date:
- Fri Aug 28 01:12:23 2015 +0000
- Revision:
- 4:f40e7eb1ba1e
- Parent:
- 3:14e33fec26c3
- Child:
- 5:9f46b8cdd469
ver 1.0
Who changed what in which revision?
User | Revision | Line number | New 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 |
4180_1 | 4:f40e7eb1ba1e | 5 | DigitalOut reset(p26); |
star297 | 0:5ebf44bd3694 | 6 | Timer t; |
star297 | 0:5ebf44bd3694 | 7 | |
star297 | 0:5ebf44bd3694 | 8 | int count,ended,timeout; |
star297 | 0:5ebf44bd3694 | 9 | char buf[1024]; |
star297 | 0:5ebf44bd3694 | 10 | char snd[255]; |
star297 | 0:5ebf44bd3694 | 11 | |
4180_1 | 4:f40e7eb1ba1e | 12 | char ssid[32] = "ATT864"; // enter WiFi router ssid inside the quotes |
4180_1 | 4:f40e7eb1ba1e | 13 | char pwd [32] = "2419932397"; // enter WiFi router password inside the quotes |
star297 | 0:5ebf44bd3694 | 14 | |
star297 | 0:5ebf44bd3694 | 15 | void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); |
star297 | 0:5ebf44bd3694 | 16 | |
star297 | 0:5ebf44bd3694 | 17 | |
4180_1 | 4:f40e7eb1ba1e | 18 | int main() |
4180_1 | 4:f40e7eb1ba1e | 19 | { |
4180_1 | 4:f40e7eb1ba1e | 20 | reset=0; //hardware reset for 8266 |
star297 | 2:c5515c7eba46 | 21 | pc.baud(115200); // set what you want here depending on your terminal program speed |
4180_1 | 4:f40e7eb1ba1e | 22 | pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); |
4180_1 | 4:f40e7eb1ba1e | 23 | wait(0.5); |
4180_1 | 4:f40e7eb1ba1e | 24 | reset=1; |
4180_1 | 4:f40e7eb1ba1e | 25 | timeout=2; |
4180_1 | 4:f40e7eb1ba1e | 26 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 27 | |
4180_1 | 4:f40e7eb1ba1e | 28 | esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. |
4180_1 | 4:f40e7eb1ba1e | 29 | |
star297 | 0:5ebf44bd3694 | 30 | //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** |
star297 | 0:5ebf44bd3694 | 31 | |
star297 | 0:5ebf44bd3694 | 32 | ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** |
4180_1 | 4:f40e7eb1ba1e | 33 | |
4180_1 | 4:f40e7eb1ba1e | 34 | |
4180_1 | 4:f40e7eb1ba1e | 35 | |
4180_1 | 4:f40e7eb1ba1e | 36 | // continuosly get AP list and IP |
4180_1 | 4:f40e7eb1ba1e | 37 | while(1) { |
star297 | 3:14e33fec26c3 | 38 | pc.printf("\n---------- Listing Acces Points ----------\r\n"); |
star297 | 3:14e33fec26c3 | 39 | strcpy(snd, "AT+CWLAP\r\n"); |
star297 | 3:14e33fec26c3 | 40 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 41 | timeout=15; |
4180_1 | 4:f40e7eb1ba1e | 42 | getreply(); |
star297 | 3:14e33fec26c3 | 43 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 44 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 45 | pc.printf("\n---------- Get IP and MAC ----------\r\n"); |
star297 | 3:14e33fec26c3 | 46 | strcpy(snd, "AT+CIFSR\r\n"); |
star297 | 3:14e33fec26c3 | 47 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 48 | timeout=10; |
4180_1 | 4:f40e7eb1ba1e | 49 | getreply(); |
star297 | 3:14e33fec26c3 | 50 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 51 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 52 | } |
4180_1 | 4:f40e7eb1ba1e | 53 | |
star297 | 0:5ebf44bd3694 | 54 | } |
star297 | 0:5ebf44bd3694 | 55 | |
star297 | 0:5ebf44bd3694 | 56 | // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed |
star297 | 0:5ebf44bd3694 | 57 | void ESPsetbaudrate() |
star297 | 0:5ebf44bd3694 | 58 | { |
star297 | 0:5ebf44bd3694 | 59 | strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate |
4180_1 | 4:f40e7eb1ba1e | 60 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 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 | strcpy(snd,"AT\r\n"); |
4180_1 | 4:f40e7eb1ba1e | 68 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 69 | wait(1); |
4180_1 | 4:f40e7eb1ba1e | 70 | strcpy(snd,"AT\r\n"); |
4180_1 | 4:f40e7eb1ba1e | 71 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 72 | wait(1); |
4180_1 | 4:f40e7eb1ba1e | 73 | strcpy(snd,"AT\r\n"); |
4180_1 | 4:f40e7eb1ba1e | 74 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 75 | timeout=1; |
4180_1 | 4:f40e7eb1ba1e | 76 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 77 | wait(1); |
4180_1 | 4:f40e7eb1ba1e | 78 | pc.printf("\f---------- Starting ESP Config ----------\r\n\n"); |
star297 | 0:5ebf44bd3694 | 79 | |
star297 | 0:5ebf44bd3694 | 80 | pc.printf("---------- Reset & get Firmware ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 81 | strcpy(snd,"AT+RST\r\n"); |
star297 | 0:5ebf44bd3694 | 82 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 83 | timeout=5; |
star297 | 0:5ebf44bd3694 | 84 | getreply(); |
star297 | 0:5ebf44bd3694 | 85 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 86 | |
4180_1 | 4:f40e7eb1ba1e | 87 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 88 | |
star297 | 0:5ebf44bd3694 | 89 | pc.printf("\n---------- Get Version ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 90 | strcpy(snd,"AT+GMR\r\n"); |
star297 | 0:5ebf44bd3694 | 91 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 92 | timeout=4; |
star297 | 0:5ebf44bd3694 | 93 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 94 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 95 | |
4180_1 | 4:f40e7eb1ba1e | 96 | wait(3); |
4180_1 | 4:f40e7eb1ba1e | 97 | |
star297 | 0:5ebf44bd3694 | 98 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
star297 | 0:5ebf44bd3694 | 99 | pc.printf("\n---------- Setting Mode ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 100 | strcpy(snd, "AT+CWMODE=1\r\n"); |
star297 | 0:5ebf44bd3694 | 101 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 102 | timeout=4; |
star297 | 0:5ebf44bd3694 | 103 | getreply(); |
star297 | 0:5ebf44bd3694 | 104 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 105 | |
4180_1 | 4:f40e7eb1ba1e | 106 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 107 | |
4180_1 | 4:f40e7eb1ba1e | 108 | // set CIPMUX to 0=Single,1=Multi |
star297 | 2:c5515c7eba46 | 109 | pc.printf("\n---------- Setting Connection Mode ----------\r\n"); |
4180_1 | 4:f40e7eb1ba1e | 110 | strcpy(snd, "AT+CIPMUX=1\r\n"); |
star297 | 2:c5515c7eba46 | 111 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 112 | timeout=4; |
star297 | 2:c5515c7eba46 | 113 | getreply(); |
star297 | 2:c5515c7eba46 | 114 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 115 | |
4180_1 | 4:f40e7eb1ba1e | 116 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 117 | |
4180_1 | 4:f40e7eb1ba1e | 118 | pc.printf("\n---------- Listing Access Points ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 119 | strcpy(snd, "AT+CWLAP\r\n"); |
star297 | 0:5ebf44bd3694 | 120 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 121 | timeout=15; |
4180_1 | 4:f40e7eb1ba1e | 122 | getreply(); |
star297 | 0:5ebf44bd3694 | 123 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 124 | |
4180_1 | 4:f40e7eb1ba1e | 125 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 126 | |
star297 | 0:5ebf44bd3694 | 127 | pc.printf("\n---------- Connecting to AP ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 128 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
star297 | 0:5ebf44bd3694 | 129 | strcpy(snd, "AT+CWJAP=\""); |
star297 | 0:5ebf44bd3694 | 130 | strcat(snd, ssid); |
star297 | 0:5ebf44bd3694 | 131 | strcat(snd, "\",\""); |
star297 | 0:5ebf44bd3694 | 132 | strcat(snd, pwd); |
4180_1 | 4:f40e7eb1ba1e | 133 | strcat(snd, "\"\r\n"); |
star297 | 0:5ebf44bd3694 | 134 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 135 | timeout=10; |
4180_1 | 4:f40e7eb1ba1e | 136 | getreply(); |
star297 | 0:5ebf44bd3694 | 137 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 138 | |
star297 | 0:5ebf44bd3694 | 139 | wait(5); |
4180_1 | 4:f40e7eb1ba1e | 140 | |
4180_1 | 4:f40e7eb1ba1e | 141 | pc.printf("\n---------- Get IP's ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 142 | strcpy(snd, "AT+CIFSR\r\n"); |
star297 | 0:5ebf44bd3694 | 143 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 144 | timeout=3; |
4180_1 | 4:f40e7eb1ba1e | 145 | getreply(); |
star297 | 0:5ebf44bd3694 | 146 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 147 | |
star297 | 3:14e33fec26c3 | 148 | wait(1); |
4180_1 | 4:f40e7eb1ba1e | 149 | |
4180_1 | 4:f40e7eb1ba1e | 150 | pc.printf("\n---------- Get Connection Status ----------\r\n"); |
star297 | 3:14e33fec26c3 | 151 | strcpy(snd, "AT+CIPSTATUS\r\n"); |
star297 | 3:14e33fec26c3 | 152 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 153 | timeout=5; |
4180_1 | 4:f40e7eb1ba1e | 154 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 155 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 156 | |
4180_1 | 4:f40e7eb1ba1e | 157 | pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n"); |
star297 | 0:5ebf44bd3694 | 158 | pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n"); |
4180_1 | 4:f40e7eb1ba1e | 159 | pc.printf(" It saves the SSID and password settings internally\r\n");_ |
4180_1 | 4:f40e7eb1ba1e | 160 | wait(10); |
4180_1 | 4:f40e7eb1ba1e | 161 | } |
star297 | 0:5ebf44bd3694 | 162 | |
star297 | 0:5ebf44bd3694 | 163 | void SendCMD() |
4180_1 | 4:f40e7eb1ba1e | 164 | { |
4180_1 | 4:f40e7eb1ba1e | 165 | esp.printf("%s", snd); |
4180_1 | 4:f40e7eb1ba1e | 166 | } |
star297 | 0:5ebf44bd3694 | 167 | |
star297 | 0:5ebf44bd3694 | 168 | void getreply() |
4180_1 | 4:f40e7eb1ba1e | 169 | { |
star297 | 0:5ebf44bd3694 | 170 | memset(buf, '\0', sizeof(buf)); |
star297 | 0:5ebf44bd3694 | 171 | t.start(); |
4180_1 | 4:f40e7eb1ba1e | 172 | ended=0; |
4180_1 | 4:f40e7eb1ba1e | 173 | count=0; |
star297 | 0:5ebf44bd3694 | 174 | while(!ended) { |
star297 | 0:5ebf44bd3694 | 175 | if(esp.readable()) { |
4180_1 | 4:f40e7eb1ba1e | 176 | buf[count] = esp.getc(); |
4180_1 | 4:f40e7eb1ba1e | 177 | count++; |
4180_1 | 4:f40e7eb1ba1e | 178 | } |
star297 | 0:5ebf44bd3694 | 179 | if(t.read() > timeout) { |
4180_1 | 4:f40e7eb1ba1e | 180 | ended = 1; |
4180_1 | 4:f40e7eb1ba1e | 181 | t.stop(); |
4180_1 | 4:f40e7eb1ba1e | 182 | t.reset(); |
4180_1 | 4:f40e7eb1ba1e | 183 | } |
4180_1 | 4:f40e7eb1ba1e | 184 | } |
4180_1 | 4:f40e7eb1ba1e | 185 | } |