wifi1

Dependencies:   mbed

Fork of ESP8266-configuaration-baudrate by Paul Staron

Committer:
star297
Date:
Tue Feb 10 16:28:26 2015 +0000
Revision:
0:5ebf44bd3694
Child:
1:4a9cc6d8c33d
ESP8266 config program v1.0

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);
star297 0:5ebf44bd3694 4 Serial esp(PTE0, PTE1); // tx, rx
star297 0:5ebf44bd3694 5
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
star297 0:5ebf44bd3694 12 char ssid[32] = "NETGEAR21"; // enter router ssid inside the quotes
star297 0:5ebf44bd3694 13 char pwd [32] = "PANASONIC"; // enter 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
star297 0:5ebf44bd3694 18 int main() {
star297 0:5ebf44bd3694 19
star297 0:5ebf44bd3694 20 pc.baud(115200); // set what you want here depending on your terminal program speed
star297 0:5ebf44bd3694 21 esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time.
star297 0:5ebf44bd3694 22
star297 0:5ebf44bd3694 23 //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ******************
star297 0:5ebf44bd3694 24
star297 0:5ebf44bd3694 25 ESPconfig(); //****************** include Config to set the ESP8266 configuration ***********************
star297 0:5ebf44bd3694 26
star297 0:5ebf44bd3694 27 }
star297 0:5ebf44bd3694 28
star297 0:5ebf44bd3694 29 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
star297 0:5ebf44bd3694 30 void ESPsetbaudrate()
star297 0:5ebf44bd3694 31 {
star297 0:5ebf44bd3694 32 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate
star297 0:5ebf44bd3694 33 SendCMD();
star297 0:5ebf44bd3694 34 }
star297 0:5ebf44bd3694 35
star297 0:5ebf44bd3694 36 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
star297 0:5ebf44bd3694 37 void ESPconfig()
star297 0:5ebf44bd3694 38 {
star297 0:5ebf44bd3694 39
star297 0:5ebf44bd3694 40 pc.printf("---------- Starting ESP Config ----------\r\n\n");
star297 0:5ebf44bd3694 41 wait(2);
star297 0:5ebf44bd3694 42 pc.printf("---------- Reset & get Firmware ----------\r\n");
star297 0:5ebf44bd3694 43 strcpy(snd,"AT+RST\r\n");
star297 0:5ebf44bd3694 44 SendCMD();
star297 0:5ebf44bd3694 45 timeout=2;
star297 0:5ebf44bd3694 46 getreply();
star297 0:5ebf44bd3694 47 pc.printf(buf);
star297 0:5ebf44bd3694 48
star297 0:5ebf44bd3694 49 wait(1);
star297 0:5ebf44bd3694 50
star297 0:5ebf44bd3694 51
star297 0:5ebf44bd3694 52 pc.printf("\n---------- Get Version ----------\r\n");
star297 0:5ebf44bd3694 53 strcpy(snd,"AT+GMR\r\n");
star297 0:5ebf44bd3694 54 SendCMD();
star297 0:5ebf44bd3694 55 timeout=1;
star297 0:5ebf44bd3694 56 getreply();
star297 0:5ebf44bd3694 57 pc.printf(buf);
star297 0:5ebf44bd3694 58
star297 0:5ebf44bd3694 59
star297 0:5ebf44bd3694 60 wait(1);
star297 0:5ebf44bd3694 61 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
star297 0:5ebf44bd3694 62 pc.printf("\n---------- Setting Mode ----------\r\n");
star297 0:5ebf44bd3694 63 strcpy(snd, "AT+CWMODE=1\r\n");
star297 0:5ebf44bd3694 64 SendCMD();
star297 0:5ebf44bd3694 65 timeout=1;
star297 0:5ebf44bd3694 66 getreply();
star297 0:5ebf44bd3694 67 pc.printf(buf);
star297 0:5ebf44bd3694 68
star297 0:5ebf44bd3694 69 wait(1);
star297 0:5ebf44bd3694 70
star297 0:5ebf44bd3694 71 pc.printf("\n---------- Listing Acces Points ----------\r\n");
star297 0:5ebf44bd3694 72 strcpy(snd, "AT+CWLAP\r\n");
star297 0:5ebf44bd3694 73 SendCMD();
star297 0:5ebf44bd3694 74 timeout=3;
star297 0:5ebf44bd3694 75 getreply();
star297 0:5ebf44bd3694 76 pc.printf(buf);
star297 0:5ebf44bd3694 77
star297 0:5ebf44bd3694 78 wait(1);
star297 0:5ebf44bd3694 79
star297 0:5ebf44bd3694 80 pc.printf("\n---------- Connecting to AP ----------\r\n");
star297 0:5ebf44bd3694 81 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
star297 0:5ebf44bd3694 82 strcpy(snd, "AT+CWJAP=\"");
star297 0:5ebf44bd3694 83 strcat(snd, ssid);
star297 0:5ebf44bd3694 84 strcat(snd, "\",\"");
star297 0:5ebf44bd3694 85 strcat(snd, pwd);
star297 0:5ebf44bd3694 86 strcat(snd, "\"\r\n");
star297 0:5ebf44bd3694 87 SendCMD();
star297 0:5ebf44bd3694 88 timeout=8;
star297 0:5ebf44bd3694 89 getreply();
star297 0:5ebf44bd3694 90 pc.printf(buf);
star297 0:5ebf44bd3694 91
star297 0:5ebf44bd3694 92 wait(5);
star297 0:5ebf44bd3694 93
star297 0:5ebf44bd3694 94 pc.printf("\n---------- Get IP's ----------\r\n");
star297 0:5ebf44bd3694 95 strcpy(snd, "AT+CIFSR\r\n");
star297 0:5ebf44bd3694 96 SendCMD();
star297 0:5ebf44bd3694 97 timeout=2;
star297 0:5ebf44bd3694 98 getreply();
star297 0:5ebf44bd3694 99 pc.printf(buf);
star297 0:5ebf44bd3694 100
star297 0:5ebf44bd3694 101 pc.printf("\n\n\n If you get a valid IP, ESP8266 has been set up.\r\n");
star297 0:5ebf44bd3694 102 pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n");
star297 0:5ebf44bd3694 103 }
star297 0:5ebf44bd3694 104
star297 0:5ebf44bd3694 105 void SendCMD()
star297 0:5ebf44bd3694 106 {
star297 0:5ebf44bd3694 107 esp.printf("%s", snd);
star297 0:5ebf44bd3694 108 }
star297 0:5ebf44bd3694 109
star297 0:5ebf44bd3694 110 void getreply()
star297 0:5ebf44bd3694 111 {
star297 0:5ebf44bd3694 112 memset(buf, '\0', sizeof(buf));
star297 0:5ebf44bd3694 113 t.start();
star297 0:5ebf44bd3694 114 ended=0;count=0;
star297 0:5ebf44bd3694 115 while(!ended) {
star297 0:5ebf44bd3694 116 if(esp.readable()) {
star297 0:5ebf44bd3694 117 buf[count] = esp.getc();count++;
star297 0:5ebf44bd3694 118 }
star297 0:5ebf44bd3694 119 if(t.read() > timeout) {
star297 0:5ebf44bd3694 120 ended = 1;t.stop();t.reset();
star297 0:5ebf44bd3694 121 }
star297 0:5ebf44bd3694 122 }
star297 0:5ebf44bd3694 123 }
star297 0:5ebf44bd3694 124