Dependencies:   mbed

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

Committer:
star297
Date:
Tue Feb 10 16:59:16 2015 +0000
Revision:
2:c5515c7eba46
Parent:
1:4a9cc6d8c33d
Child:
3:14e33fec26c3
code tidy

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 2:c5515c7eba46 12 char ssid[32] = "ssid"; // enter router ssid inside the quotes
star297 2:c5515c7eba46 13 char pwd [32] = "password"; // 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
star297 2:c5515c7eba46 21 esp.baud(9600); // 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 2:c5515c7eba46 49 wait(1);
star297 0:5ebf44bd3694 50
star297 0:5ebf44bd3694 51 pc.printf("\n---------- Get Version ----------\r\n");
star297 0:5ebf44bd3694 52 strcpy(snd,"AT+GMR\r\n");
star297 0:5ebf44bd3694 53 SendCMD();
star297 0:5ebf44bd3694 54 timeout=1;
star297 0:5ebf44bd3694 55 getreply();
star297 2:c5515c7eba46 56 pc.printf(buf);
star297 0:5ebf44bd3694 57
star297 2:c5515c7eba46 58 wait(1);
star297 2:c5515c7eba46 59
star297 0:5ebf44bd3694 60 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
star297 0:5ebf44bd3694 61 pc.printf("\n---------- Setting Mode ----------\r\n");
star297 0:5ebf44bd3694 62 strcpy(snd, "AT+CWMODE=1\r\n");
star297 0:5ebf44bd3694 63 SendCMD();
star297 0:5ebf44bd3694 64 timeout=1;
star297 0:5ebf44bd3694 65 getreply();
star297 0:5ebf44bd3694 66 pc.printf(buf);
star297 0:5ebf44bd3694 67
star297 0:5ebf44bd3694 68 wait(1);
star297 0:5ebf44bd3694 69
star297 2:c5515c7eba46 70 // set CIPMUX to 0=Single,1=Multi
star297 2:c5515c7eba46 71 pc.printf("\n---------- Setting Connection Mode ----------\r\n");
star297 2:c5515c7eba46 72 strcpy(snd, "AT+CIPMUX=1\r\n");
star297 2:c5515c7eba46 73 SendCMD();
star297 2:c5515c7eba46 74 timeout=1;
star297 2:c5515c7eba46 75 getreply();
star297 2:c5515c7eba46 76 pc.printf(buf);
star297 2:c5515c7eba46 77
star297 2:c5515c7eba46 78 wait(1);
star297 2:c5515c7eba46 79
star297 0:5ebf44bd3694 80 pc.printf("\n---------- Listing Acces Points ----------\r\n");
star297 0:5ebf44bd3694 81 strcpy(snd, "AT+CWLAP\r\n");
star297 0:5ebf44bd3694 82 SendCMD();
star297 0:5ebf44bd3694 83 timeout=3;
star297 0:5ebf44bd3694 84 getreply();
star297 0:5ebf44bd3694 85 pc.printf(buf);
star297 0:5ebf44bd3694 86
star297 0:5ebf44bd3694 87 wait(1);
star297 0:5ebf44bd3694 88
star297 0:5ebf44bd3694 89 pc.printf("\n---------- Connecting to AP ----------\r\n");
star297 0:5ebf44bd3694 90 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
star297 0:5ebf44bd3694 91 strcpy(snd, "AT+CWJAP=\"");
star297 0:5ebf44bd3694 92 strcat(snd, ssid);
star297 0:5ebf44bd3694 93 strcat(snd, "\",\"");
star297 0:5ebf44bd3694 94 strcat(snd, pwd);
star297 0:5ebf44bd3694 95 strcat(snd, "\"\r\n");
star297 0:5ebf44bd3694 96 SendCMD();
star297 0:5ebf44bd3694 97 timeout=8;
star297 0:5ebf44bd3694 98 getreply();
star297 0:5ebf44bd3694 99 pc.printf(buf);
star297 0:5ebf44bd3694 100
star297 0:5ebf44bd3694 101 wait(5);
star297 0:5ebf44bd3694 102
star297 0:5ebf44bd3694 103 pc.printf("\n---------- Get IP's ----------\r\n");
star297 0:5ebf44bd3694 104 strcpy(snd, "AT+CIFSR\r\n");
star297 0:5ebf44bd3694 105 SendCMD();
star297 0:5ebf44bd3694 106 timeout=2;
star297 0:5ebf44bd3694 107 getreply();
star297 0:5ebf44bd3694 108 pc.printf(buf);
star297 0:5ebf44bd3694 109
star297 0:5ebf44bd3694 110 pc.printf("\n\n\n If you get a valid IP, ESP8266 has been set up.\r\n");
star297 0:5ebf44bd3694 111 pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n");
star297 0:5ebf44bd3694 112 }
star297 0:5ebf44bd3694 113
star297 0:5ebf44bd3694 114 void SendCMD()
star297 0:5ebf44bd3694 115 {
star297 0:5ebf44bd3694 116 esp.printf("%s", snd);
star297 0:5ebf44bd3694 117 }
star297 0:5ebf44bd3694 118
star297 0:5ebf44bd3694 119 void getreply()
star297 0:5ebf44bd3694 120 {
star297 0:5ebf44bd3694 121 memset(buf, '\0', sizeof(buf));
star297 0:5ebf44bd3694 122 t.start();
star297 0:5ebf44bd3694 123 ended=0;count=0;
star297 0:5ebf44bd3694 124 while(!ended) {
star297 0:5ebf44bd3694 125 if(esp.readable()) {
star297 0:5ebf44bd3694 126 buf[count] = esp.getc();count++;
star297 0:5ebf44bd3694 127 }
star297 0:5ebf44bd3694 128 if(t.read() > timeout) {
star297 0:5ebf44bd3694 129 ended = 1;t.stop();t.reset();
star297 0:5ebf44bd3694 130 }
star297 0:5ebf44bd3694 131 }
star297 0:5ebf44bd3694 132 }
star297 0:5ebf44bd3694 133