Dependencies: mbed
Fork of ESP8266-configuration-mbed-LPC1768 by
main.cpp@6:2e8125399b54, 2018-03-16 (annotated)
- Committer:
- TeamLegrand
- Date:
- Fri Mar 16 07:57:17 2018 +0000
- Revision:
- 6:2e8125399b54
- Parent:
- 5:9f46b8cdd469
Programme module WIFI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:5ebf44bd3694 | 1 | #include "mbed.h" |
TeamLegrand | 6:2e8125399b54 | 2 | #include <iostream> |
TeamLegrand | 6:2e8125399b54 | 3 | #include <string> |
TeamLegrand | 6:2e8125399b54 | 4 | #include <fstream> |
star297 | 0:5ebf44bd3694 | 5 | |
star297 | 0:5ebf44bd3694 | 6 | Serial pc(USBTX, USBRX); |
4180_1 | 4:f40e7eb1ba1e | 7 | Serial esp(p28, p27); // tx, rx |
4180_1 | 4:f40e7eb1ba1e | 8 | DigitalOut reset(p26); |
star297 | 0:5ebf44bd3694 | 9 | Timer t; |
star297 | 0:5ebf44bd3694 | 10 | |
star297 | 0:5ebf44bd3694 | 11 | int count,ended,timeout; |
star297 | 0:5ebf44bd3694 | 12 | char buf[1024]; |
star297 | 0:5ebf44bd3694 | 13 | char snd[255]; |
TeamLegrand | 6:2e8125399b54 | 14 | char ip[30] = "192.168.10.100"; |
star297 | 0:5ebf44bd3694 | 15 | |
TeamLegrand | 6:2e8125399b54 | 16 | char ssid[32]; |
TeamLegrand | 6:2e8125399b54 | 17 | char pwd [32]; |
star297 | 0:5ebf44bd3694 | 18 | |
TeamLegrand | 6:2e8125399b54 | 19 | LocalFileSystem local("local"); |
star297 | 0:5ebf44bd3694 | 20 | |
TeamLegrand | 6:2e8125399b54 | 21 | void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(),WIFI(), Donnee(); |
TeamLegrand | 6:2e8125399b54 | 22 | |
TeamLegrand | 6:2e8125399b54 | 23 | using namespace std; |
star297 | 0:5ebf44bd3694 | 24 | |
4180_1 | 4:f40e7eb1ba1e | 25 | int main() |
4180_1 | 4:f40e7eb1ba1e | 26 | { |
4180_1 | 4:f40e7eb1ba1e | 27 | reset=0; //hardware reset for 8266 |
TeamLegrand | 6:2e8125399b54 | 28 | pc.baud(9600); // set what you want here depending on your terminal program speed |
4180_1 | 4:f40e7eb1ba1e | 29 | pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); |
4180_1 | 4:f40e7eb1ba1e | 30 | wait(0.5); |
4180_1 | 4:f40e7eb1ba1e | 31 | reset=1; |
4180_1 | 4:f40e7eb1ba1e | 32 | timeout=2; |
4180_1 | 4:f40e7eb1ba1e | 33 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 34 | |
4180_1 | 4:f40e7eb1ba1e | 35 | esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. |
TeamLegrand | 6:2e8125399b54 | 36 | |
star297 | 0:5ebf44bd3694 | 37 | ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** |
4180_1 | 4:f40e7eb1ba1e | 38 | |
4180_1 | 4:f40e7eb1ba1e | 39 | |
4180_1 | 4:f40e7eb1ba1e | 40 | |
4180_1 | 4:f40e7eb1ba1e | 41 | // continuosly get AP list and IP |
4180_1 | 4:f40e7eb1ba1e | 42 | while(1) { |
TeamLegrand | 6:2e8125399b54 | 43 | pc.printf("\n---------- Informations sur la connexion ----------\r\n"); |
TeamLegrand | 6:2e8125399b54 | 44 | strcpy(snd, "AT+CIPSTATUS\r\n"); |
TeamLegrand | 6:2e8125399b54 | 45 | SendCMD(); |
TeamLegrand | 6:2e8125399b54 | 46 | timeout=10; |
TeamLegrand | 6:2e8125399b54 | 47 | getreply(); |
TeamLegrand | 6:2e8125399b54 | 48 | pc.printf(buf); |
TeamLegrand | 6:2e8125399b54 | 49 | wait(15); |
TeamLegrand | 6:2e8125399b54 | 50 | Donnee(); |
4180_1 | 4:f40e7eb1ba1e | 51 | } |
TeamLegrand | 6:2e8125399b54 | 52 | |
star297 | 0:5ebf44bd3694 | 53 | } |
star297 | 0:5ebf44bd3694 | 54 | |
TeamLegrand | 6:2e8125399b54 | 55 | void WIFI() |
star297 | 0:5ebf44bd3694 | 56 | { |
TeamLegrand | 6:2e8125399b54 | 57 | using namespace std; |
TeamLegrand | 6:2e8125399b54 | 58 | pc.printf("\f\r -------------lecture du fichier------------- \f\r"); |
TeamLegrand | 6:2e8125399b54 | 59 | pc.baud(9600); |
TeamLegrand | 6:2e8125399b54 | 60 | |
TeamLegrand | 6:2e8125399b54 | 61 | FILE *set = fopen("/local/ssid.txt", "r"); |
TeamLegrand | 6:2e8125399b54 | 62 | fscanf(set,"%s ",ssid); |
TeamLegrand | 6:2e8125399b54 | 63 | |
TeamLegrand | 6:2e8125399b54 | 64 | FILE *mdp = fopen("/local/pwd.txt", "r"); |
TeamLegrand | 6:2e8125399b54 | 65 | fscanf(mdp,"%s ",pwd); |
TeamLegrand | 6:2e8125399b54 | 66 | |
TeamLegrand | 6:2e8125399b54 | 67 | wait(2); |
TeamLegrand | 6:2e8125399b54 | 68 | pc.printf("\f\r\n Lecture du SSID de connection : \f\r\n"); |
TeamLegrand | 6:2e8125399b54 | 69 | wait(2); |
TeamLegrand | 6:2e8125399b54 | 70 | pc.printf(" ssid : %s \r\n",ssid); |
TeamLegrand | 6:2e8125399b54 | 71 | pc.printf(" password : %s \r\n",pwd); |
TeamLegrand | 6:2e8125399b54 | 72 | fclose(set); |
TeamLegrand | 6:2e8125399b54 | 73 | fclose(mdp); |
4180_1 | 4:f40e7eb1ba1e | 74 | } |
star297 | 0:5ebf44bd3694 | 75 | |
star297 | 0:5ebf44bd3694 | 76 | // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ |
star297 | 0:5ebf44bd3694 | 77 | void ESPconfig() |
star297 | 0:5ebf44bd3694 | 78 | { |
4180_1 | 4:f40e7eb1ba1e | 79 | wait(5); |
4180_1 | 4:f40e7eb1ba1e | 80 | strcpy(snd,"AT\r\n"); |
4180_1 | 4:f40e7eb1ba1e | 81 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 82 | timeout=1; |
4180_1 | 4:f40e7eb1ba1e | 83 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 84 | wait(1); |
TeamLegrand | 6:2e8125399b54 | 85 | pc.printf("---------- Redemarrage du module ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 86 | strcpy(snd,"AT+RST\r\n"); |
star297 | 0:5ebf44bd3694 | 87 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 88 | timeout=5; |
star297 | 0:5ebf44bd3694 | 89 | getreply(); |
star297 | 0:5ebf44bd3694 | 90 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 91 | |
4180_1 | 4:f40e7eb1ba1e | 92 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 93 | |
TeamLegrand | 6:2e8125399b54 | 94 | pc.printf("\n---------- Information sur la version ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 95 | strcpy(snd,"AT+GMR\r\n"); |
star297 | 0:5ebf44bd3694 | 96 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 97 | timeout=4; |
star297 | 0:5ebf44bd3694 | 98 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 99 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 100 | |
4180_1 | 4:f40e7eb1ba1e | 101 | wait(3); |
4180_1 | 4:f40e7eb1ba1e | 102 | |
star297 | 0:5ebf44bd3694 | 103 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
TeamLegrand | 6:2e8125399b54 | 104 | pc.printf("\n---------- Mode Wifi ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 105 | strcpy(snd, "AT+CWMODE=1\r\n"); |
star297 | 0:5ebf44bd3694 | 106 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 107 | timeout=4; |
star297 | 0:5ebf44bd3694 | 108 | getreply(); |
star297 | 0:5ebf44bd3694 | 109 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 110 | |
4180_1 | 4:f40e7eb1ba1e | 111 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 112 | |
4180_1 | 4:f40e7eb1ba1e | 113 | // set CIPMUX to 0=Single,1=Multi |
TeamLegrand | 6:2e8125399b54 | 114 | pc.printf("\n---------- Choix de connexion multiple ou unique ----------\r\n"); |
4180_1 | 4:f40e7eb1ba1e | 115 | strcpy(snd, "AT+CIPMUX=1\r\n"); |
star297 | 2:c5515c7eba46 | 116 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 117 | timeout=4; |
star297 | 2:c5515c7eba46 | 118 | getreply(); |
star297 | 2:c5515c7eba46 | 119 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 120 | |
4180_1 | 4:f40e7eb1ba1e | 121 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 122 | |
TeamLegrand | 6:2e8125399b54 | 123 | pc.printf("\n---------- Liste des points de connexion ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 124 | strcpy(snd, "AT+CWLAP\r\n"); |
star297 | 0:5ebf44bd3694 | 125 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 126 | timeout=15; |
4180_1 | 4:f40e7eb1ba1e | 127 | getreply(); |
star297 | 0:5ebf44bd3694 | 128 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 129 | |
4180_1 | 4:f40e7eb1ba1e | 130 | wait(2); |
TeamLegrand | 6:2e8125399b54 | 131 | WIFI(); |
TeamLegrand | 6:2e8125399b54 | 132 | wait(2); |
4180_1 | 4:f40e7eb1ba1e | 133 | |
TeamLegrand | 6:2e8125399b54 | 134 | pc.printf("\n---------- Connexion au point d acces ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 135 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
star297 | 0:5ebf44bd3694 | 136 | strcpy(snd, "AT+CWJAP=\""); |
star297 | 0:5ebf44bd3694 | 137 | strcat(snd, ssid); |
star297 | 0:5ebf44bd3694 | 138 | strcat(snd, "\",\""); |
star297 | 0:5ebf44bd3694 | 139 | strcat(snd, pwd); |
4180_1 | 4:f40e7eb1ba1e | 140 | strcat(snd, "\"\r\n"); |
star297 | 0:5ebf44bd3694 | 141 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 142 | timeout=10; |
4180_1 | 4:f40e7eb1ba1e | 143 | getreply(); |
star297 | 0:5ebf44bd3694 | 144 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 145 | |
star297 | 0:5ebf44bd3694 | 146 | wait(5); |
4180_1 | 4:f40e7eb1ba1e | 147 | |
TeamLegrand | 6:2e8125399b54 | 148 | pc.printf("\n---------- Adresse IP local ----------\r\n"); |
star297 | 0:5ebf44bd3694 | 149 | strcpy(snd, "AT+CIFSR\r\n"); |
star297 | 0:5ebf44bd3694 | 150 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 151 | timeout=3; |
4180_1 | 4:f40e7eb1ba1e | 152 | getreply(); |
star297 | 0:5ebf44bd3694 | 153 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 154 | |
star297 | 3:14e33fec26c3 | 155 | wait(1); |
4180_1 | 4:f40e7eb1ba1e | 156 | |
TeamLegrand | 6:2e8125399b54 | 157 | pc.printf("\n---------- Informations sur la connexion ----------\r\n"); |
star297 | 3:14e33fec26c3 | 158 | strcpy(snd, "AT+CIPSTATUS\r\n"); |
star297 | 3:14e33fec26c3 | 159 | SendCMD(); |
4180_1 | 4:f40e7eb1ba1e | 160 | timeout=5; |
4180_1 | 4:f40e7eb1ba1e | 161 | getreply(); |
4180_1 | 4:f40e7eb1ba1e | 162 | pc.printf(buf); |
4180_1 | 4:f40e7eb1ba1e | 163 | wait(10); |
4180_1 | 4:f40e7eb1ba1e | 164 | } |
star297 | 0:5ebf44bd3694 | 165 | |
star297 | 0:5ebf44bd3694 | 166 | void SendCMD() |
4180_1 | 4:f40e7eb1ba1e | 167 | { |
4180_1 | 4:f40e7eb1ba1e | 168 | esp.printf("%s", snd); |
4180_1 | 4:f40e7eb1ba1e | 169 | } |
star297 | 0:5ebf44bd3694 | 170 | |
TeamLegrand | 6:2e8125399b54 | 171 | void Donnee() |
TeamLegrand | 6:2e8125399b54 | 172 | { |
TeamLegrand | 6:2e8125399b54 | 173 | pc.printf("\n---------- Envoie de donnees ----------\r\n"); |
TeamLegrand | 6:2e8125399b54 | 174 | strcpy(snd, "AT+CIPSEND=\""); |
TeamLegrand | 6:2e8125399b54 | 175 | strcat(snd, "192.168.10.23"); |
TeamLegrand | 6:2e8125399b54 | 176 | strcat(snd, "\",\""); |
TeamLegrand | 6:2e8125399b54 | 177 | strcat(snd, "1234"); |
TeamLegrand | 6:2e8125399b54 | 178 | strcat(snd, "\"\r\n"); |
TeamLegrand | 6:2e8125399b54 | 179 | SendCMD(); |
TeamLegrand | 6:2e8125399b54 | 180 | timeout=5; |
TeamLegrand | 6:2e8125399b54 | 181 | getreply(); |
TeamLegrand | 6:2e8125399b54 | 182 | pc.printf(buf); |
TeamLegrand | 6:2e8125399b54 | 183 | wait(15); |
TeamLegrand | 6:2e8125399b54 | 184 | } |
TeamLegrand | 6:2e8125399b54 | 185 | |
star297 | 0:5ebf44bd3694 | 186 | void getreply() |
4180_1 | 4:f40e7eb1ba1e | 187 | { |
star297 | 0:5ebf44bd3694 | 188 | memset(buf, '\0', sizeof(buf)); |
star297 | 0:5ebf44bd3694 | 189 | t.start(); |
4180_1 | 4:f40e7eb1ba1e | 190 | ended=0; |
4180_1 | 4:f40e7eb1ba1e | 191 | count=0; |
star297 | 0:5ebf44bd3694 | 192 | while(!ended) { |
star297 | 0:5ebf44bd3694 | 193 | if(esp.readable()) { |
4180_1 | 4:f40e7eb1ba1e | 194 | buf[count] = esp.getc(); |
4180_1 | 4:f40e7eb1ba1e | 195 | count++; |
4180_1 | 4:f40e7eb1ba1e | 196 | } |
star297 | 0:5ebf44bd3694 | 197 | if(t.read() > timeout) { |
4180_1 | 4:f40e7eb1ba1e | 198 | ended = 1; |
4180_1 | 4:f40e7eb1ba1e | 199 | t.stop(); |
4180_1 | 4:f40e7eb1ba1e | 200 | t.reset(); |
4180_1 | 4:f40e7eb1ba1e | 201 | } |
4180_1 | 4:f40e7eb1ba1e | 202 | } |
4180_1 | 4:f40e7eb1ba1e | 203 | } |