wifi1

Dependencies:   mbed

Fork of ESP8266-configuaration-baudrate by Paul Staron

Committer:
luke_markham
Date:
Wed Jun 08 13:50:32 2016 +0000
Revision:
4:5b59cd16199e
Parent:
3:14e33fec26c3
wifi1;

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);
luke_markham 4:5b59cd16199e 4 Serial esp(p9,p10); // 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
luke_markham 4:5b59cd16199e 12 char ssid[32] = "CWMWIFI"; // enter router ssid inside the quotes
luke_markham 4:5b59cd16199e 13 char pwd [32] = "CWM2016TT"; // 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 2:c5515c7eba46 20 pc.baud(115200); // set what you want here depending on your terminal program speed
luke_markham 4:5b59cd16199e 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 3:14e33fec26c3 26
star297 3:14e33fec26c3 27
star297 3:14e33fec26c3 28 // contiuosly get AP list and IP
star297 3:14e33fec26c3 29 while(1){
star297 3:14e33fec26c3 30 pc.printf("\n---------- Listing Acces Points ----------\r\n");
star297 3:14e33fec26c3 31 strcpy(snd, "AT+CWLAP\r\n");
star297 3:14e33fec26c3 32 SendCMD();
star297 3:14e33fec26c3 33 timeout=3;
star297 3:14e33fec26c3 34 getreply();
star297 3:14e33fec26c3 35 pc.printf(buf);
star297 3:14e33fec26c3 36
star297 3:14e33fec26c3 37 pc.printf("\n---------- Get IP's ----------\r\n");
star297 3:14e33fec26c3 38 strcpy(snd, "AT+CIFSR\r\n");
star297 3:14e33fec26c3 39 SendCMD();
star297 3:14e33fec26c3 40 timeout=2;
star297 3:14e33fec26c3 41 getreply();
star297 3:14e33fec26c3 42 pc.printf(buf);
star297 3:14e33fec26c3 43 }
star297 0:5ebf44bd3694 44
star297 0:5ebf44bd3694 45 }
star297 0:5ebf44bd3694 46
star297 0:5ebf44bd3694 47 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
star297 0:5ebf44bd3694 48 void ESPsetbaudrate()
star297 0:5ebf44bd3694 49 {
star297 0:5ebf44bd3694 50 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate
star297 0:5ebf44bd3694 51 SendCMD();
star297 0:5ebf44bd3694 52 }
star297 0:5ebf44bd3694 53
star297 0:5ebf44bd3694 54 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
star297 0:5ebf44bd3694 55 void ESPconfig()
star297 0:5ebf44bd3694 56 {
star297 0:5ebf44bd3694 57
star297 0:5ebf44bd3694 58 pc.printf("---------- Starting ESP Config ----------\r\n\n");
star297 0:5ebf44bd3694 59 wait(2);
star297 0:5ebf44bd3694 60 pc.printf("---------- Reset & get Firmware ----------\r\n");
star297 0:5ebf44bd3694 61 strcpy(snd,"AT+RST\r\n");
star297 0:5ebf44bd3694 62 SendCMD();
star297 0:5ebf44bd3694 63 timeout=2;
star297 0:5ebf44bd3694 64 getreply();
star297 0:5ebf44bd3694 65 pc.printf(buf);
star297 0:5ebf44bd3694 66
star297 2:c5515c7eba46 67 wait(1);
star297 0:5ebf44bd3694 68
star297 0:5ebf44bd3694 69 pc.printf("\n---------- Get Version ----------\r\n");
star297 0:5ebf44bd3694 70 strcpy(snd,"AT+GMR\r\n");
star297 0:5ebf44bd3694 71 SendCMD();
star297 0:5ebf44bd3694 72 timeout=1;
star297 0:5ebf44bd3694 73 getreply();
star297 2:c5515c7eba46 74 pc.printf(buf);
star297 0:5ebf44bd3694 75
star297 2:c5515c7eba46 76 wait(1);
star297 2:c5515c7eba46 77
star297 0:5ebf44bd3694 78 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
star297 0:5ebf44bd3694 79 pc.printf("\n---------- Setting Mode ----------\r\n");
star297 0:5ebf44bd3694 80 strcpy(snd, "AT+CWMODE=1\r\n");
star297 0:5ebf44bd3694 81 SendCMD();
star297 0:5ebf44bd3694 82 timeout=1;
star297 0:5ebf44bd3694 83 getreply();
star297 0:5ebf44bd3694 84 pc.printf(buf);
star297 0:5ebf44bd3694 85
star297 0:5ebf44bd3694 86 wait(1);
star297 0:5ebf44bd3694 87
star297 2:c5515c7eba46 88 // set CIPMUX to 0=Single,1=Multi
star297 2:c5515c7eba46 89 pc.printf("\n---------- Setting Connection Mode ----------\r\n");
star297 2:c5515c7eba46 90 strcpy(snd, "AT+CIPMUX=1\r\n");
star297 2:c5515c7eba46 91 SendCMD();
star297 2:c5515c7eba46 92 timeout=1;
star297 2:c5515c7eba46 93 getreply();
star297 2:c5515c7eba46 94 pc.printf(buf);
star297 2:c5515c7eba46 95
star297 2:c5515c7eba46 96 wait(1);
star297 2:c5515c7eba46 97
star297 0:5ebf44bd3694 98 pc.printf("\n---------- Listing Acces Points ----------\r\n");
star297 0:5ebf44bd3694 99 strcpy(snd, "AT+CWLAP\r\n");
star297 0:5ebf44bd3694 100 SendCMD();
star297 0:5ebf44bd3694 101 timeout=3;
star297 0:5ebf44bd3694 102 getreply();
star297 0:5ebf44bd3694 103 pc.printf(buf);
star297 0:5ebf44bd3694 104
star297 0:5ebf44bd3694 105 wait(1);
star297 0:5ebf44bd3694 106
star297 0:5ebf44bd3694 107 pc.printf("\n---------- Connecting to AP ----------\r\n");
star297 0:5ebf44bd3694 108 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
star297 0:5ebf44bd3694 109 strcpy(snd, "AT+CWJAP=\"");
star297 0:5ebf44bd3694 110 strcat(snd, ssid);
star297 0:5ebf44bd3694 111 strcat(snd, "\",\"");
star297 0:5ebf44bd3694 112 strcat(snd, pwd);
star297 0:5ebf44bd3694 113 strcat(snd, "\"\r\n");
star297 0:5ebf44bd3694 114 SendCMD();
star297 0:5ebf44bd3694 115 timeout=8;
star297 0:5ebf44bd3694 116 getreply();
star297 0:5ebf44bd3694 117 pc.printf(buf);
star297 0:5ebf44bd3694 118
star297 0:5ebf44bd3694 119 wait(5);
star297 0:5ebf44bd3694 120
star297 0:5ebf44bd3694 121 pc.printf("\n---------- Get IP's ----------\r\n");
star297 0:5ebf44bd3694 122 strcpy(snd, "AT+CIFSR\r\n");
star297 0:5ebf44bd3694 123 SendCMD();
star297 0:5ebf44bd3694 124 timeout=2;
star297 0:5ebf44bd3694 125 getreply();
star297 0:5ebf44bd3694 126 pc.printf(buf);
star297 0:5ebf44bd3694 127
star297 3:14e33fec26c3 128 wait(1);
star297 3:14e33fec26c3 129
star297 3:14e33fec26c3 130 pc.printf("\n---------- Get Connection Status ----------\r\n");
star297 3:14e33fec26c3 131 strcpy(snd, "AT+CIPSTATUS\r\n");
star297 3:14e33fec26c3 132 SendCMD();
star297 3:14e33fec26c3 133 timeout=2;
star297 3:14e33fec26c3 134 getreply();
star297 3:14e33fec26c3 135 pc.printf(buf);
star297 3:14e33fec26c3 136
star297 0:5ebf44bd3694 137 pc.printf("\n\n\n If you get a valid IP, ESP8266 has been set up.\r\n");
star297 0:5ebf44bd3694 138 pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n");
star297 0:5ebf44bd3694 139 }
star297 0:5ebf44bd3694 140
star297 0:5ebf44bd3694 141 void SendCMD()
star297 0:5ebf44bd3694 142 {
star297 0:5ebf44bd3694 143 esp.printf("%s", snd);
star297 0:5ebf44bd3694 144 }
star297 0:5ebf44bd3694 145
star297 0:5ebf44bd3694 146 void getreply()
star297 0:5ebf44bd3694 147 {
star297 0:5ebf44bd3694 148 memset(buf, '\0', sizeof(buf));
star297 0:5ebf44bd3694 149 t.start();
star297 0:5ebf44bd3694 150 ended=0;count=0;
star297 0:5ebf44bd3694 151 while(!ended) {
star297 0:5ebf44bd3694 152 if(esp.readable()) {
star297 0:5ebf44bd3694 153 buf[count] = esp.getc();count++;
star297 0:5ebf44bd3694 154 }
star297 0:5ebf44bd3694 155 if(t.read() > timeout) {
star297 0:5ebf44bd3694 156 ended = 1;t.stop();t.reset();
star297 0:5ebf44bd3694 157 }
star297 0:5ebf44bd3694 158 }
star297 0:5ebf44bd3694 159 }
star297 0:5ebf44bd3694 160