In the words of Tim Gun... make it work

Dependencies:   mbed

Fork of ESP8266-configuaration-baudrate by Paul Staron

Committer:
hydroguy45
Date:
Thu Nov 01 20:51:56 2018 +0000
Revision:
4:699631e7f60d
Parent:
3:14e33fec26c3
Please work

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