my repo

Dependencies:   mbed

Committer:
Victordang
Date:
Tue Feb 26 08:33:58 2019 +0000
Revision:
1:cd76b77e35a4
Parent:
0:c265e9b31b1d
Child:
2:3704512ed2d4
new

Who changed what in which revision?

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