For test

Dependencies:   mbed

Committer:
shennongmin
Date:
Wed Feb 04 09:39:10 2015 +0000
Revision:
20:43451f36281e
Parent:
18:37254b357abd
Child:
23:4cec7b9e3715
ArduinoSerial okay

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shennongmin 10:9d4ec0359a5c 1 #include "ESP8266.h"
shennongmin 10:9d4ec0359a5c 2
shennongmin 10:9d4ec0359a5c 3
shennongmin 10:9d4ec0359a5c 4 ESP8266::ESP8266(PinName tx, PinName rx):esp_uart(tx, rx) {
shennongmin 10:9d4ec0359a5c 5 }
shennongmin 10:9d4ec0359a5c 6
shennongmin 10:9d4ec0359a5c 7 int ESP8266::get_chlID(void) {
shennongmin 10:9d4ec0359a5c 8 return chlID;
shennongmin 10:9d4ec0359a5c 9 }
shennongmin 10:9d4ec0359a5c 10
shennongmin 18:37254b357abd 11 bool ESP8266::begin(void)
shennongmin 10:9d4ec0359a5c 12 {
shennongmin 10:9d4ec0359a5c 13 esp_uart.begin(9600); //The default baud rate of ESP8266 is 115200
shennongmin 10:9d4ec0359a5c 14 esp_uart.flush();
shennongmin 10:9d4ec0359a5c 15 esp_uart.setTimeout(5000);
shennongmin 10:9d4ec0359a5c 16 esp_uart.println("AT+RST");
shennongmin 18:37254b357abd 17 return esp_uart.find("eady");
shennongmin 10:9d4ec0359a5c 18 }
shennongmin 10:9d4ec0359a5c 19
shennongmin 10:9d4ec0359a5c 20
shennongmin 10:9d4ec0359a5c 21 /*************************************************************************
shennongmin 10:9d4ec0359a5c 22 //Initialize port
shennongmin 10:9d4ec0359a5c 23
shennongmin 10:9d4ec0359a5c 24 mode: setting operation mode
shennongmin 10:9d4ec0359a5c 25
shennongmin 10:9d4ec0359a5c 26 chl: channel number
shennongmin 10:9d4ec0359a5c 27 ecn: encryption
shennongmin 10:9d4ec0359a5c 28
shennongmin 10:9d4ec0359a5c 29 return:
shennongmin 10:9d4ec0359a5c 30 true - successfully
shennongmin 10:9d4ec0359a5c 31 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 32
shennongmin 10:9d4ec0359a5c 33 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 34 bool ESP8266::Initialize(uint8_t mode, String ssid, String pwd, uint8_t chl, uint8_t ecn)
shennongmin 10:9d4ec0359a5c 35 {
shennongmin 10:9d4ec0359a5c 36 if (mode == ESP8266_MODE_STA)
shennongmin 10:9d4ec0359a5c 37 {
shennongmin 10:9d4ec0359a5c 38 bool b = confMode(mode);
shennongmin 10:9d4ec0359a5c 39 if (!b)
shennongmin 10:9d4ec0359a5c 40 {
shennongmin 10:9d4ec0359a5c 41 return false;
shennongmin 10:9d4ec0359a5c 42 }
shennongmin 10:9d4ec0359a5c 43 Reset();
shennongmin 10:9d4ec0359a5c 44 confJAP(ssid, pwd);
shennongmin 10:9d4ec0359a5c 45 }
shennongmin 10:9d4ec0359a5c 46 else if (mode == ESP8266_MODE_SAP)
shennongmin 10:9d4ec0359a5c 47 {
shennongmin 10:9d4ec0359a5c 48 bool b = confMode(mode);
shennongmin 10:9d4ec0359a5c 49 if (!b)
shennongmin 10:9d4ec0359a5c 50 {
shennongmin 10:9d4ec0359a5c 51 return false;
shennongmin 10:9d4ec0359a5c 52 }
shennongmin 10:9d4ec0359a5c 53 Reset();
shennongmin 10:9d4ec0359a5c 54 confSAP(ssid, pwd, chl, ecn);
shennongmin 10:9d4ec0359a5c 55 }
shennongmin 10:9d4ec0359a5c 56 else if (mode == ESP8266_MODE_STA_SAP)
shennongmin 10:9d4ec0359a5c 57 {
shennongmin 10:9d4ec0359a5c 58 bool b = confMode(mode);
shennongmin 10:9d4ec0359a5c 59 if (!b)
shennongmin 10:9d4ec0359a5c 60 {
shennongmin 10:9d4ec0359a5c 61 return false;
shennongmin 10:9d4ec0359a5c 62 }
shennongmin 10:9d4ec0359a5c 63 Reset();
shennongmin 10:9d4ec0359a5c 64 confJAP(ssid, pwd);
shennongmin 10:9d4ec0359a5c 65 confSAP(ssid, pwd, chl, ecn);
shennongmin 10:9d4ec0359a5c 66 }
shennongmin 10:9d4ec0359a5c 67
shennongmin 10:9d4ec0359a5c 68 return true;
shennongmin 10:9d4ec0359a5c 69 }
shennongmin 10:9d4ec0359a5c 70
shennongmin 10:9d4ec0359a5c 71 /*************************************************************************
shennongmin 10:9d4ec0359a5c 72 //Set up tcp or udp connection
shennongmin 10:9d4ec0359a5c 73
shennongmin 10:9d4ec0359a5c 74 type: tcp or udp
shennongmin 10:9d4ec0359a5c 75
shennongmin 10:9d4ec0359a5c 76 addr: ip address
shennongmin 10:9d4ec0359a5c 77
shennongmin 10:9d4ec0359a5c 78 port: port number
shennongmin 10:9d4ec0359a5c 79
shennongmin 10:9d4ec0359a5c 80 a: set multiple connection
shennongmin 10:9d4ec0359a5c 81 0 for sigle connection
shennongmin 10:9d4ec0359a5c 82 1 for multiple connection
shennongmin 10:9d4ec0359a5c 83
shennongmin 10:9d4ec0359a5c 84 id: id number(0-4)
shennongmin 10:9d4ec0359a5c 85
shennongmin 10:9d4ec0359a5c 86 return:
shennongmin 10:9d4ec0359a5c 87 true - successfully
shennongmin 10:9d4ec0359a5c 88 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 89
shennongmin 10:9d4ec0359a5c 90 ***************************************************************************/
shennongmin 17:c449356787f1 91 bool ESP8266::ipConfig(uint8_t type, String addr, int port, uint8_t mux, uint8_t id)
shennongmin 10:9d4ec0359a5c 92 {
shennongmin 10:9d4ec0359a5c 93 bool result = false;
shennongmin 17:c449356787f1 94 if (mux == 0 )
shennongmin 10:9d4ec0359a5c 95 {
shennongmin 17:c449356787f1 96 confMux(mux);
shennongmin 10:9d4ec0359a5c 97
shennongmin 10:9d4ec0359a5c 98 long timeStart = millis();
shennongmin 10:9d4ec0359a5c 99 while (1)
shennongmin 10:9d4ec0359a5c 100 {
shennongmin 10:9d4ec0359a5c 101 long time0 = millis();
shennongmin 10:9d4ec0359a5c 102 if (time0 - timeStart > 5000)
shennongmin 10:9d4ec0359a5c 103 {
shennongmin 10:9d4ec0359a5c 104 break;
shennongmin 10:9d4ec0359a5c 105 }
shennongmin 10:9d4ec0359a5c 106 }
shennongmin 10:9d4ec0359a5c 107 result = newMux(type, addr, port);
shennongmin 10:9d4ec0359a5c 108 }
shennongmin 17:c449356787f1 109 else if (mux == 1)
shennongmin 10:9d4ec0359a5c 110 {
shennongmin 17:c449356787f1 111 confMux(mux);
shennongmin 10:9d4ec0359a5c 112 long timeStart = millis();
shennongmin 10:9d4ec0359a5c 113 while (1)
shennongmin 10:9d4ec0359a5c 114 {
shennongmin 10:9d4ec0359a5c 115 long time0 = millis();
shennongmin 10:9d4ec0359a5c 116 if (time0 - timeStart > 5000)
shennongmin 10:9d4ec0359a5c 117 {
shennongmin 10:9d4ec0359a5c 118 break;
shennongmin 10:9d4ec0359a5c 119 }
shennongmin 10:9d4ec0359a5c 120 }
shennongmin 10:9d4ec0359a5c 121 result = newMux(id, type, addr, port);
shennongmin 10:9d4ec0359a5c 122 }
shennongmin 10:9d4ec0359a5c 123 return result;
shennongmin 10:9d4ec0359a5c 124 }
shennongmin 10:9d4ec0359a5c 125
shennongmin 10:9d4ec0359a5c 126 /*************************************************************************
shennongmin 10:9d4ec0359a5c 127 //receive message from wifi
shennongmin 10:9d4ec0359a5c 128
shennongmin 10:9d4ec0359a5c 129 buf: buffer for receiving data
shennongmin 10:9d4ec0359a5c 130
shennongmin 10:9d4ec0359a5c 131 chlID: <id>(0-4)
shennongmin 10:9d4ec0359a5c 132
shennongmin 10:9d4ec0359a5c 133 return: size of the buffer
shennongmin 10:9d4ec0359a5c 134
shennongmin 10:9d4ec0359a5c 135
shennongmin 10:9d4ec0359a5c 136 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 137 int ESP8266::ReceiveMessage(char *buf)
shennongmin 10:9d4ec0359a5c 138 {
shennongmin 10:9d4ec0359a5c 139 //+IPD,<len>:<data>
shennongmin 10:9d4ec0359a5c 140 //+IPD,<id>,<len>:<data>
shennongmin 10:9d4ec0359a5c 141 String data = "";
shennongmin 10:9d4ec0359a5c 142 if (esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 143 {
shennongmin 10:9d4ec0359a5c 144
shennongmin 10:9d4ec0359a5c 145 unsigned long start;
shennongmin 10:9d4ec0359a5c 146 start = millis();
shennongmin 20:43451f36281e 147 char c0 = esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 148 if (c0 == '+')
shennongmin 10:9d4ec0359a5c 149 {
shennongmin 10:9d4ec0359a5c 150
shennongmin 10:9d4ec0359a5c 151 while (millis()-start<5000)
shennongmin 10:9d4ec0359a5c 152 {
shennongmin 10:9d4ec0359a5c 153 if (esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 154 {
shennongmin 20:43451f36281e 155 char c = esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 156 data += c;
shennongmin 10:9d4ec0359a5c 157 }
shennongmin 10:9d4ec0359a5c 158 if (data.indexOf("\nOK")!=-1)
shennongmin 10:9d4ec0359a5c 159 {
shennongmin 10:9d4ec0359a5c 160 break;
shennongmin 10:9d4ec0359a5c 161 }
shennongmin 10:9d4ec0359a5c 162 }
shennongmin 10:9d4ec0359a5c 163 //Serial.println(data);
shennongmin 10:9d4ec0359a5c 164 int sLen = strlen(data.c_str());
shennongmin 10:9d4ec0359a5c 165 int i,j;
shennongmin 10:9d4ec0359a5c 166 for (i = 0; i <= sLen; i++)
shennongmin 10:9d4ec0359a5c 167 {
shennongmin 10:9d4ec0359a5c 168 if (data[i] == ':')
shennongmin 10:9d4ec0359a5c 169 {
shennongmin 10:9d4ec0359a5c 170 break;
shennongmin 10:9d4ec0359a5c 171 }
shennongmin 10:9d4ec0359a5c 172
shennongmin 10:9d4ec0359a5c 173 }
shennongmin 10:9d4ec0359a5c 174 bool found = false;
shennongmin 10:9d4ec0359a5c 175 for (j = 4; j <= i; j++)
shennongmin 10:9d4ec0359a5c 176 {
shennongmin 10:9d4ec0359a5c 177 if (data[j] == ',')
shennongmin 10:9d4ec0359a5c 178 {
shennongmin 10:9d4ec0359a5c 179 found = true;
shennongmin 10:9d4ec0359a5c 180 break;
shennongmin 10:9d4ec0359a5c 181 }
shennongmin 10:9d4ec0359a5c 182
shennongmin 10:9d4ec0359a5c 183 }
shennongmin 10:9d4ec0359a5c 184 int iSize;
shennongmin 10:9d4ec0359a5c 185 if(found ==true)
shennongmin 10:9d4ec0359a5c 186 {
shennongmin 10:9d4ec0359a5c 187 String _id = data.substring(4, j);
shennongmin 10:9d4ec0359a5c 188 chlID = _id.toInt();
shennongmin 10:9d4ec0359a5c 189 String _size = data.substring(j+1, i);
shennongmin 10:9d4ec0359a5c 190 iSize = _size.toInt();
shennongmin 14:40b1decf03f3 191
shennongmin 10:9d4ec0359a5c 192 String str = data.substring(i+1, i+1+iSize);
shennongmin 10:9d4ec0359a5c 193 strcpy(buf, str.c_str());
shennongmin 14:40b1decf03f3 194
shennongmin 10:9d4ec0359a5c 195
shennongmin 10:9d4ec0359a5c 196 }
shennongmin 10:9d4ec0359a5c 197 else
shennongmin 10:9d4ec0359a5c 198 {
shennongmin 10:9d4ec0359a5c 199 String _size = data.substring(4, i);
shennongmin 10:9d4ec0359a5c 200 iSize = _size.toInt();
shennongmin 14:40b1decf03f3 201
shennongmin 14:40b1decf03f3 202
shennongmin 10:9d4ec0359a5c 203 String str = data.substring(i+1, i+1+iSize);
shennongmin 10:9d4ec0359a5c 204 strcpy(buf, str.c_str());
shennongmin 14:40b1decf03f3 205
shennongmin 10:9d4ec0359a5c 206 }
shennongmin 10:9d4ec0359a5c 207 return iSize;
shennongmin 10:9d4ec0359a5c 208 }
shennongmin 10:9d4ec0359a5c 209 }
shennongmin 10:9d4ec0359a5c 210
shennongmin 10:9d4ec0359a5c 211 return 0;
shennongmin 10:9d4ec0359a5c 212 }
shennongmin 10:9d4ec0359a5c 213
shennongmin 10:9d4ec0359a5c 214 //////////////////////////////////////////////////////////////////////////
shennongmin 10:9d4ec0359a5c 215
shennongmin 10:9d4ec0359a5c 216
shennongmin 10:9d4ec0359a5c 217 /*************************************************************************
shennongmin 10:9d4ec0359a5c 218 //reboot the wifi module
shennongmin 10:9d4ec0359a5c 219
shennongmin 10:9d4ec0359a5c 220
shennongmin 10:9d4ec0359a5c 221
shennongmin 10:9d4ec0359a5c 222 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 223 void ESP8266::Reset(void)
shennongmin 10:9d4ec0359a5c 224 {
shennongmin 10:9d4ec0359a5c 225 esp_uart.println("AT+RST");
shennongmin 10:9d4ec0359a5c 226 unsigned long start;
shennongmin 10:9d4ec0359a5c 227 start = millis();
shennongmin 10:9d4ec0359a5c 228 while (millis()-start<5000) {
shennongmin 18:37254b357abd 229 if(esp_uart.find("eady")==true)
shennongmin 10:9d4ec0359a5c 230 {
shennongmin 10:9d4ec0359a5c 231 break;
shennongmin 10:9d4ec0359a5c 232 }
shennongmin 10:9d4ec0359a5c 233 }
shennongmin 10:9d4ec0359a5c 234 }
shennongmin 10:9d4ec0359a5c 235
shennongmin 10:9d4ec0359a5c 236 /*********************************************
shennongmin 10:9d4ec0359a5c 237 *********************************************
shennongmin 10:9d4ec0359a5c 238 *********************************************
shennongmin 10:9d4ec0359a5c 239 ESP8266 Function Commands
shennongmin 10:9d4ec0359a5c 240 *********************************************
shennongmin 10:9d4ec0359a5c 241 *********************************************
shennongmin 10:9d4ec0359a5c 242 *********************************************
shennongmin 10:9d4ec0359a5c 243 */
shennongmin 10:9d4ec0359a5c 244
shennongmin 10:9d4ec0359a5c 245 /*************************************************************************
shennongmin 10:9d4ec0359a5c 246 //inquire the current mode of wifi module
shennongmin 10:9d4ec0359a5c 247
shennongmin 10:9d4ec0359a5c 248 return: string of current mode
shennongmin 10:9d4ec0359a5c 249 Station
shennongmin 10:9d4ec0359a5c 250 AP
shennongmin 10:9d4ec0359a5c 251 AP+Station
shennongmin 10:9d4ec0359a5c 252
shennongmin 10:9d4ec0359a5c 253 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 254 String ESP8266::showMode()
shennongmin 10:9d4ec0359a5c 255 {
shennongmin 10:9d4ec0359a5c 256 String data;
shennongmin 10:9d4ec0359a5c 257 esp_uart.println("AT+CWMODE?");
shennongmin 10:9d4ec0359a5c 258 unsigned long start;
shennongmin 10:9d4ec0359a5c 259 start = millis();
shennongmin 10:9d4ec0359a5c 260 while (millis()-start<2000) {
shennongmin 10:9d4ec0359a5c 261 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 262 {
shennongmin 20:43451f36281e 263 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 264 data=data+a;
shennongmin 10:9d4ec0359a5c 265 }
shennongmin 10:9d4ec0359a5c 266 if (data.indexOf("OK")!=-1)
shennongmin 10:9d4ec0359a5c 267 {
shennongmin 10:9d4ec0359a5c 268 break;
shennongmin 10:9d4ec0359a5c 269 }
shennongmin 10:9d4ec0359a5c 270 }
shennongmin 10:9d4ec0359a5c 271 if(data.indexOf("1")!=-1)
shennongmin 10:9d4ec0359a5c 272 {
shennongmin 10:9d4ec0359a5c 273 return "Station";
shennongmin 10:9d4ec0359a5c 274 }else if(data.indexOf("2")!=-1)
shennongmin 10:9d4ec0359a5c 275 {
shennongmin 10:9d4ec0359a5c 276 return "AP";
shennongmin 10:9d4ec0359a5c 277 }else if(data.indexOf("3")!=-1)
shennongmin 10:9d4ec0359a5c 278 {
shennongmin 10:9d4ec0359a5c 279 return "AP+Station";
shennongmin 10:9d4ec0359a5c 280 }else{
shennongmin 10:9d4ec0359a5c 281 return "Invalid Mode!";
shennongmin 10:9d4ec0359a5c 282 }
shennongmin 10:9d4ec0359a5c 283 }
shennongmin 10:9d4ec0359a5c 284
shennongmin 10:9d4ec0359a5c 285
shennongmin 10:9d4ec0359a5c 286
shennongmin 10:9d4ec0359a5c 287 /*************************************************************************
shennongmin 10:9d4ec0359a5c 288 //configure the operation mode
shennongmin 10:9d4ec0359a5c 289
shennongmin 10:9d4ec0359a5c 290 a:
shennongmin 10:9d4ec0359a5c 291 1 - Station
shennongmin 10:9d4ec0359a5c 292 2 - AP
shennongmin 10:9d4ec0359a5c 293 3 - AP+Station
shennongmin 10:9d4ec0359a5c 294
shennongmin 10:9d4ec0359a5c 295 return:
shennongmin 10:9d4ec0359a5c 296 true - successfully
shennongmin 10:9d4ec0359a5c 297 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 298
shennongmin 10:9d4ec0359a5c 299 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 300
shennongmin 17:c449356787f1 301 bool ESP8266::confMode(uint8_t mode)
shennongmin 10:9d4ec0359a5c 302 {
shennongmin 10:9d4ec0359a5c 303 String data;
shennongmin 10:9d4ec0359a5c 304 esp_uart.print("AT+CWMODE=");
shennongmin 17:c449356787f1 305 esp_uart.println(String(mode));
shennongmin 10:9d4ec0359a5c 306 unsigned long start;
shennongmin 10:9d4ec0359a5c 307 start = millis();
shennongmin 10:9d4ec0359a5c 308 while (millis()-start<2000) {
shennongmin 10:9d4ec0359a5c 309 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 310 {
shennongmin 20:43451f36281e 311 char mode =esp_uart.readChr();
shennongmin 17:c449356787f1 312 data=data+mode;
shennongmin 10:9d4ec0359a5c 313 }
shennongmin 10:9d4ec0359a5c 314 if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1)
shennongmin 10:9d4ec0359a5c 315 {
shennongmin 10:9d4ec0359a5c 316 return true;
shennongmin 10:9d4ec0359a5c 317 }
shennongmin 10:9d4ec0359a5c 318 if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1)
shennongmin 10:9d4ec0359a5c 319 {
shennongmin 10:9d4ec0359a5c 320 return false;
shennongmin 10:9d4ec0359a5c 321 }
shennongmin 10:9d4ec0359a5c 322
shennongmin 10:9d4ec0359a5c 323 }
shennongmin 10:9d4ec0359a5c 324 return false;
shennongmin 10:9d4ec0359a5c 325 }
shennongmin 10:9d4ec0359a5c 326
shennongmin 10:9d4ec0359a5c 327
shennongmin 10:9d4ec0359a5c 328 /*************************************************************************
shennongmin 10:9d4ec0359a5c 329 //show the list of wifi hotspot
shennongmin 10:9d4ec0359a5c 330
shennongmin 10:9d4ec0359a5c 331 return: string of wifi information
shennongmin 10:9d4ec0359a5c 332 encryption,SSID,RSSI
shennongmin 10:9d4ec0359a5c 333
shennongmin 10:9d4ec0359a5c 334
shennongmin 10:9d4ec0359a5c 335 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 336
shennongmin 10:9d4ec0359a5c 337 String ESP8266::showAP(void)
shennongmin 10:9d4ec0359a5c 338 {
shennongmin 10:9d4ec0359a5c 339 String data;
shennongmin 10:9d4ec0359a5c 340 esp_uart.flush();
shennongmin 10:9d4ec0359a5c 341 esp_uart.print("AT+CWLAP\r\n");
shennongmin 10:9d4ec0359a5c 342 delay(5000);
shennongmin 10:9d4ec0359a5c 343
shennongmin 10:9d4ec0359a5c 344 unsigned long start;
shennongmin 10:9d4ec0359a5c 345 start = millis();
shennongmin 10:9d4ec0359a5c 346 while (millis()-start<8000) {
shennongmin 10:9d4ec0359a5c 347 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 348 {
shennongmin 20:43451f36281e 349 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 350 data=data+a;
shennongmin 10:9d4ec0359a5c 351 }
shennongmin 10:9d4ec0359a5c 352 if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
shennongmin 10:9d4ec0359a5c 353 {
shennongmin 10:9d4ec0359a5c 354 break;
shennongmin 10:9d4ec0359a5c 355 }
shennongmin 10:9d4ec0359a5c 356 }
shennongmin 10:9d4ec0359a5c 357 if(data.indexOf("ERROR")!=-1)
shennongmin 10:9d4ec0359a5c 358 {
shennongmin 10:9d4ec0359a5c 359 return "ERROR";
shennongmin 10:9d4ec0359a5c 360 }
shennongmin 10:9d4ec0359a5c 361 else{
shennongmin 10:9d4ec0359a5c 362 char head[4] = {0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 363 char tail[7] = {0x0D,0x0A,0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 364 data.replace("AT+CWLAP","");
shennongmin 10:9d4ec0359a5c 365 data.replace("OK","");
shennongmin 10:9d4ec0359a5c 366 data.replace("+CWLAP","ESP8266");
shennongmin 10:9d4ec0359a5c 367 data.replace(tail,"");
shennongmin 10:9d4ec0359a5c 368 data.replace(head,"");
shennongmin 10:9d4ec0359a5c 369
shennongmin 10:9d4ec0359a5c 370 return data;
shennongmin 10:9d4ec0359a5c 371 }
shennongmin 10:9d4ec0359a5c 372 }
shennongmin 10:9d4ec0359a5c 373
shennongmin 10:9d4ec0359a5c 374
shennongmin 10:9d4ec0359a5c 375 /*************************************************************************
shennongmin 10:9d4ec0359a5c 376 //show the name of current wifi access port
shennongmin 10:9d4ec0359a5c 377
shennongmin 10:9d4ec0359a5c 378 return: string of access port name
shennongmin 10:9d4ec0359a5c 379 AP:<SSID>
shennongmin 10:9d4ec0359a5c 380
shennongmin 10:9d4ec0359a5c 381
shennongmin 10:9d4ec0359a5c 382 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 383 String ESP8266::showJAP(void)
shennongmin 10:9d4ec0359a5c 384 {
shennongmin 10:9d4ec0359a5c 385 esp_uart.flush();
shennongmin 10:9d4ec0359a5c 386 esp_uart.println("AT+CWJAP?");
shennongmin 10:9d4ec0359a5c 387 String data;
shennongmin 10:9d4ec0359a5c 388 unsigned long start;
shennongmin 10:9d4ec0359a5c 389 start = millis();
shennongmin 10:9d4ec0359a5c 390 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 391 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 392 {
shennongmin 20:43451f36281e 393 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 394 data=data+a;
shennongmin 10:9d4ec0359a5c 395 }
shennongmin 10:9d4ec0359a5c 396 if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
shennongmin 10:9d4ec0359a5c 397 {
shennongmin 10:9d4ec0359a5c 398 break;
shennongmin 10:9d4ec0359a5c 399 }
shennongmin 10:9d4ec0359a5c 400 }
shennongmin 14:40b1decf03f3 401
shennongmin 10:9d4ec0359a5c 402 char head[4] = {0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 403 char tail[7] = {0x0D,0x0A,0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 404 data.replace("AT+CWJAP?","");
shennongmin 10:9d4ec0359a5c 405 data.replace("+CWJAP","AP");
shennongmin 10:9d4ec0359a5c 406 data.replace("OK","");
shennongmin 10:9d4ec0359a5c 407 data.replace(tail,"");
shennongmin 10:9d4ec0359a5c 408 data.replace(head,"");
shennongmin 10:9d4ec0359a5c 409
shennongmin 10:9d4ec0359a5c 410 return data;
shennongmin 10:9d4ec0359a5c 411 }
shennongmin 10:9d4ec0359a5c 412
shennongmin 10:9d4ec0359a5c 413
shennongmin 10:9d4ec0359a5c 414 /*************************************************************************
shennongmin 10:9d4ec0359a5c 415 //configure the SSID and password of the access port
shennongmin 10:9d4ec0359a5c 416
shennongmin 10:9d4ec0359a5c 417 return:
shennongmin 10:9d4ec0359a5c 418 true - successfully
shennongmin 10:9d4ec0359a5c 419 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 420
shennongmin 10:9d4ec0359a5c 421
shennongmin 10:9d4ec0359a5c 422 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 423 bool ESP8266::confJAP(String ssid , String pwd)
shennongmin 10:9d4ec0359a5c 424 {
shennongmin 10:9d4ec0359a5c 425
shennongmin 10:9d4ec0359a5c 426 esp_uart.print("AT+CWJAP=");
shennongmin 10:9d4ec0359a5c 427 esp_uart.print("\""); //"ssid"
shennongmin 10:9d4ec0359a5c 428 esp_uart.print(ssid);
shennongmin 10:9d4ec0359a5c 429 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 430
shennongmin 10:9d4ec0359a5c 431 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 432
shennongmin 10:9d4ec0359a5c 433 esp_uart.print("\""); //"pwd"
shennongmin 10:9d4ec0359a5c 434 esp_uart.print(pwd);
shennongmin 10:9d4ec0359a5c 435 esp_uart.println("\"");
shennongmin 10:9d4ec0359a5c 436
shennongmin 10:9d4ec0359a5c 437
shennongmin 10:9d4ec0359a5c 438 unsigned long start;
shennongmin 10:9d4ec0359a5c 439 start = millis();
shennongmin 10:9d4ec0359a5c 440 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 441 if(esp_uart.find("OK")==true)
shennongmin 10:9d4ec0359a5c 442 {
shennongmin 10:9d4ec0359a5c 443 return true;
shennongmin 10:9d4ec0359a5c 444
shennongmin 10:9d4ec0359a5c 445 }
shennongmin 10:9d4ec0359a5c 446 }
shennongmin 10:9d4ec0359a5c 447 return false;
shennongmin 10:9d4ec0359a5c 448 }
shennongmin 10:9d4ec0359a5c 449 /*************************************************************************
shennongmin 10:9d4ec0359a5c 450 //quite the access port
shennongmin 10:9d4ec0359a5c 451
shennongmin 10:9d4ec0359a5c 452 return:
shennongmin 10:9d4ec0359a5c 453 true - successfully
shennongmin 10:9d4ec0359a5c 454 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 455
shennongmin 10:9d4ec0359a5c 456
shennongmin 10:9d4ec0359a5c 457 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 458
shennongmin 10:9d4ec0359a5c 459 bool ESP8266::quitAP(void)
shennongmin 10:9d4ec0359a5c 460 {
shennongmin 10:9d4ec0359a5c 461 esp_uart.println("AT+CWQAP");
shennongmin 10:9d4ec0359a5c 462 unsigned long start;
shennongmin 10:9d4ec0359a5c 463 start = millis();
shennongmin 10:9d4ec0359a5c 464 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 465 if(esp_uart.find("OK")==true)
shennongmin 10:9d4ec0359a5c 466 {
shennongmin 10:9d4ec0359a5c 467 return true;
shennongmin 10:9d4ec0359a5c 468
shennongmin 10:9d4ec0359a5c 469 }
shennongmin 10:9d4ec0359a5c 470 }
shennongmin 10:9d4ec0359a5c 471 return false;
shennongmin 10:9d4ec0359a5c 472
shennongmin 10:9d4ec0359a5c 473 }
shennongmin 10:9d4ec0359a5c 474
shennongmin 10:9d4ec0359a5c 475 /*************************************************************************
shennongmin 10:9d4ec0359a5c 476 //show the parameter of ssid, password, channel, encryption in AP mode
shennongmin 10:9d4ec0359a5c 477
shennongmin 10:9d4ec0359a5c 478 return:
shennongmin 10:9d4ec0359a5c 479 mySAP:<SSID>,<password>,<channel>,<encryption>
shennongmin 10:9d4ec0359a5c 480
shennongmin 10:9d4ec0359a5c 481 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 482 String ESP8266::showSAP()
shennongmin 10:9d4ec0359a5c 483 {
shennongmin 10:9d4ec0359a5c 484 esp_uart.println("AT+CWSAP?");
shennongmin 10:9d4ec0359a5c 485 String data;
shennongmin 10:9d4ec0359a5c 486 unsigned long start;
shennongmin 10:9d4ec0359a5c 487 start = millis();
shennongmin 10:9d4ec0359a5c 488 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 489 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 490 {
shennongmin 20:43451f36281e 491 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 492 data=data+a;
shennongmin 10:9d4ec0359a5c 493 }
shennongmin 10:9d4ec0359a5c 494 if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
shennongmin 10:9d4ec0359a5c 495 {
shennongmin 10:9d4ec0359a5c 496 break;
shennongmin 10:9d4ec0359a5c 497 }
shennongmin 10:9d4ec0359a5c 498 }
shennongmin 10:9d4ec0359a5c 499 char head[4] = {0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 500 char tail[7] = {0x0D,0x0A,0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 501 data.replace("AT+CWSAP?","");
shennongmin 10:9d4ec0359a5c 502 data.replace("+CWSAP","mySAP");
shennongmin 10:9d4ec0359a5c 503 data.replace("OK","");
shennongmin 10:9d4ec0359a5c 504 data.replace(tail,"");
shennongmin 10:9d4ec0359a5c 505 data.replace(head,"");
shennongmin 10:9d4ec0359a5c 506
shennongmin 10:9d4ec0359a5c 507 return data;
shennongmin 10:9d4ec0359a5c 508 }
shennongmin 10:9d4ec0359a5c 509
shennongmin 10:9d4ec0359a5c 510 /*************************************************************************
shennongmin 10:9d4ec0359a5c 511 //configure the parameter of ssid, password, channel, encryption in AP mode
shennongmin 10:9d4ec0359a5c 512
shennongmin 10:9d4ec0359a5c 513 return:
shennongmin 10:9d4ec0359a5c 514 true - successfully
shennongmin 10:9d4ec0359a5c 515 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 516
shennongmin 10:9d4ec0359a5c 517 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 518
shennongmin 10:9d4ec0359a5c 519 bool ESP8266::confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn)
shennongmin 10:9d4ec0359a5c 520 {
shennongmin 10:9d4ec0359a5c 521 esp_uart.print("AT+CWSAP=");
shennongmin 10:9d4ec0359a5c 522 esp_uart.print("\""); //"ssid"
shennongmin 10:9d4ec0359a5c 523 esp_uart.print(ssid);
shennongmin 10:9d4ec0359a5c 524 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 525
shennongmin 10:9d4ec0359a5c 526 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 527
shennongmin 10:9d4ec0359a5c 528 esp_uart.print("\""); //"pwd"
shennongmin 10:9d4ec0359a5c 529 esp_uart.print(pwd);
shennongmin 10:9d4ec0359a5c 530 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 531
shennongmin 10:9d4ec0359a5c 532 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 533 esp_uart.print(String(chl));
shennongmin 10:9d4ec0359a5c 534
shennongmin 10:9d4ec0359a5c 535 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 536 esp_uart.println(String(ecn));
shennongmin 10:9d4ec0359a5c 537 unsigned long start;
shennongmin 10:9d4ec0359a5c 538 start = millis();
shennongmin 10:9d4ec0359a5c 539 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 540 if(esp_uart.find("OK")==true )
shennongmin 10:9d4ec0359a5c 541 {
shennongmin 10:9d4ec0359a5c 542 return true;
shennongmin 10:9d4ec0359a5c 543 }
shennongmin 10:9d4ec0359a5c 544 }
shennongmin 10:9d4ec0359a5c 545
shennongmin 10:9d4ec0359a5c 546 return false;
shennongmin 10:9d4ec0359a5c 547
shennongmin 10:9d4ec0359a5c 548 }
shennongmin 10:9d4ec0359a5c 549
shennongmin 10:9d4ec0359a5c 550
shennongmin 10:9d4ec0359a5c 551 /*********************************************
shennongmin 10:9d4ec0359a5c 552 *********************************************
shennongmin 10:9d4ec0359a5c 553 *********************************************
shennongmin 10:9d4ec0359a5c 554 TPC/IP Function Command
shennongmin 10:9d4ec0359a5c 555 *********************************************
shennongmin 10:9d4ec0359a5c 556 *********************************************
shennongmin 10:9d4ec0359a5c 557 *********************************************
shennongmin 10:9d4ec0359a5c 558 */
shennongmin 10:9d4ec0359a5c 559
shennongmin 10:9d4ec0359a5c 560 /*************************************************************************
shennongmin 10:9d4ec0359a5c 561 //inquire the connection status
shennongmin 10:9d4ec0359a5c 562
shennongmin 10:9d4ec0359a5c 563 return: string of connection status
shennongmin 10:9d4ec0359a5c 564 <ID> 0-4
shennongmin 10:9d4ec0359a5c 565 <type> tcp or udp
shennongmin 10:9d4ec0359a5c 566 <addr> ip
shennongmin 10:9d4ec0359a5c 567 <port> port number
shennongmin 10:9d4ec0359a5c 568
shennongmin 10:9d4ec0359a5c 569 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 570
shennongmin 10:9d4ec0359a5c 571 String ESP8266::showStatus(void)
shennongmin 10:9d4ec0359a5c 572 {
shennongmin 10:9d4ec0359a5c 573 esp_uart.println("AT+CIPSTATUS");
shennongmin 10:9d4ec0359a5c 574 String data;
shennongmin 10:9d4ec0359a5c 575 unsigned long start;
shennongmin 10:9d4ec0359a5c 576 start = millis();
shennongmin 10:9d4ec0359a5c 577 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 578 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 579 {
shennongmin 20:43451f36281e 580 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 581 data=data+a;
shennongmin 10:9d4ec0359a5c 582 }
shennongmin 10:9d4ec0359a5c 583 if (data.indexOf("OK")!=-1)
shennongmin 10:9d4ec0359a5c 584 {
shennongmin 10:9d4ec0359a5c 585 break;
shennongmin 10:9d4ec0359a5c 586 }
shennongmin 10:9d4ec0359a5c 587 }
shennongmin 10:9d4ec0359a5c 588
shennongmin 10:9d4ec0359a5c 589 char head[4] = {0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 590 char tail[7] = {0x0D,0x0A,0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 591 data.replace("AT+CIPSTATUS","");
shennongmin 10:9d4ec0359a5c 592 data.replace("OK","");
shennongmin 10:9d4ec0359a5c 593 data.replace(tail,"");
shennongmin 10:9d4ec0359a5c 594 data.replace(head,"");
shennongmin 10:9d4ec0359a5c 595
shennongmin 10:9d4ec0359a5c 596 return data;
shennongmin 10:9d4ec0359a5c 597 }
shennongmin 10:9d4ec0359a5c 598
shennongmin 10:9d4ec0359a5c 599 /*************************************************************************
shennongmin 10:9d4ec0359a5c 600 //show the current connection mode(sigle or multiple)
shennongmin 10:9d4ec0359a5c 601
shennongmin 10:9d4ec0359a5c 602 return: string of connection mode
shennongmin 10:9d4ec0359a5c 603 0 - sigle
shennongmin 10:9d4ec0359a5c 604 1 - multiple
shennongmin 10:9d4ec0359a5c 605
shennongmin 10:9d4ec0359a5c 606 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 607 String ESP8266::showMux(void)
shennongmin 10:9d4ec0359a5c 608 {
shennongmin 10:9d4ec0359a5c 609 String data;
shennongmin 10:9d4ec0359a5c 610 esp_uart.println("AT+CIPMUX?");
shennongmin 10:9d4ec0359a5c 611
shennongmin 10:9d4ec0359a5c 612 unsigned long start;
shennongmin 10:9d4ec0359a5c 613 start = millis();
shennongmin 10:9d4ec0359a5c 614 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 615 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 616 {
shennongmin 20:43451f36281e 617 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 618 data=data+a;
shennongmin 10:9d4ec0359a5c 619 }
shennongmin 10:9d4ec0359a5c 620 if (data.indexOf("OK")!=-1)
shennongmin 10:9d4ec0359a5c 621 {
shennongmin 10:9d4ec0359a5c 622 break;
shennongmin 10:9d4ec0359a5c 623 }
shennongmin 10:9d4ec0359a5c 624 }
shennongmin 10:9d4ec0359a5c 625 char head[4] = {0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 626 char tail[7] = {0x0D,0x0A,0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 627 data.replace("AT+CIPMUX?","");
shennongmin 10:9d4ec0359a5c 628 data.replace("+CIPMUX","showMux");
shennongmin 10:9d4ec0359a5c 629 data.replace("OK","");
shennongmin 10:9d4ec0359a5c 630 data.replace(tail,"");
shennongmin 10:9d4ec0359a5c 631 data.replace(head,"");
shennongmin 10:9d4ec0359a5c 632
shennongmin 10:9d4ec0359a5c 633 return data;
shennongmin 10:9d4ec0359a5c 634 }
shennongmin 10:9d4ec0359a5c 635
shennongmin 10:9d4ec0359a5c 636 /*************************************************************************
shennongmin 10:9d4ec0359a5c 637 //configure the current connection mode(sigle or multiple)
shennongmin 10:9d4ec0359a5c 638
shennongmin 10:9d4ec0359a5c 639 a: connection mode
shennongmin 10:9d4ec0359a5c 640 0 - sigle
shennongmin 10:9d4ec0359a5c 641 1 - multiple
shennongmin 10:9d4ec0359a5c 642
shennongmin 10:9d4ec0359a5c 643 return:
shennongmin 10:9d4ec0359a5c 644 true - successfully
shennongmin 10:9d4ec0359a5c 645 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 646 ***************************************************************************/
shennongmin 17:c449356787f1 647 bool ESP8266::confMux(int mux)
shennongmin 10:9d4ec0359a5c 648 {
shennongmin 10:9d4ec0359a5c 649 esp_uart.print("AT+CIPMUX=");
shennongmin 17:c449356787f1 650 esp_uart.println(mux);
shennongmin 10:9d4ec0359a5c 651 unsigned long start;
shennongmin 10:9d4ec0359a5c 652 start = millis();
shennongmin 10:9d4ec0359a5c 653 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 654 if(esp_uart.find("OK")==true )
shennongmin 10:9d4ec0359a5c 655 {
shennongmin 10:9d4ec0359a5c 656 return true;
shennongmin 10:9d4ec0359a5c 657 }
shennongmin 10:9d4ec0359a5c 658 }
shennongmin 10:9d4ec0359a5c 659
shennongmin 10:9d4ec0359a5c 660 return false;
shennongmin 10:9d4ec0359a5c 661 }
shennongmin 10:9d4ec0359a5c 662
shennongmin 10:9d4ec0359a5c 663
shennongmin 10:9d4ec0359a5c 664 /*************************************************************************
shennongmin 10:9d4ec0359a5c 665 //Set up tcp or udp connection (signle connection mode)
shennongmin 10:9d4ec0359a5c 666
shennongmin 10:9d4ec0359a5c 667 type: tcp or udp
shennongmin 10:9d4ec0359a5c 668
shennongmin 10:9d4ec0359a5c 669 addr: ip address
shennongmin 10:9d4ec0359a5c 670
shennongmin 10:9d4ec0359a5c 671 port: port number
shennongmin 10:9d4ec0359a5c 672
shennongmin 10:9d4ec0359a5c 673
shennongmin 10:9d4ec0359a5c 674 return:
shennongmin 10:9d4ec0359a5c 675 true - successfully
shennongmin 10:9d4ec0359a5c 676 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 677
shennongmin 10:9d4ec0359a5c 678 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 679 bool ESP8266::newMux(uint8_t type, String addr, int port)
shennongmin 10:9d4ec0359a5c 680
shennongmin 10:9d4ec0359a5c 681 {
shennongmin 10:9d4ec0359a5c 682 String data;
shennongmin 10:9d4ec0359a5c 683 esp_uart.flush();
shennongmin 10:9d4ec0359a5c 684 esp_uart.print("AT+CIPSTART=");
shennongmin 10:9d4ec0359a5c 685 if(type>0)
shennongmin 10:9d4ec0359a5c 686 {
shennongmin 10:9d4ec0359a5c 687 esp_uart.print("\"TCP\"");
shennongmin 10:9d4ec0359a5c 688 }else
shennongmin 10:9d4ec0359a5c 689 {
shennongmin 10:9d4ec0359a5c 690 esp_uart.print("\"UDP\"");
shennongmin 10:9d4ec0359a5c 691 }
shennongmin 10:9d4ec0359a5c 692 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 693 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 694 esp_uart.print(addr);
shennongmin 10:9d4ec0359a5c 695 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 696 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 697 // esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 698 esp_uart.println(String(port));
shennongmin 10:9d4ec0359a5c 699 // esp_uart.println("\"");
shennongmin 10:9d4ec0359a5c 700 unsigned long start;
shennongmin 10:9d4ec0359a5c 701 start = millis();
shennongmin 10:9d4ec0359a5c 702 while (millis()-start<10000) {
shennongmin 10:9d4ec0359a5c 703 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 704 {
shennongmin 20:43451f36281e 705 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 706 data=data+a;
shennongmin 10:9d4ec0359a5c 707 }
shennongmin 10:9d4ec0359a5c 708 if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1)
shennongmin 10:9d4ec0359a5c 709 {
shennongmin 10:9d4ec0359a5c 710 return true;
shennongmin 10:9d4ec0359a5c 711 }
shennongmin 10:9d4ec0359a5c 712 }
shennongmin 10:9d4ec0359a5c 713 return false;
shennongmin 10:9d4ec0359a5c 714 }
shennongmin 10:9d4ec0359a5c 715 /*************************************************************************
shennongmin 10:9d4ec0359a5c 716 //Set up tcp or udp connection (multiple connection mode)
shennongmin 10:9d4ec0359a5c 717
shennongmin 10:9d4ec0359a5c 718 type: tcp or udp
shennongmin 10:9d4ec0359a5c 719
shennongmin 10:9d4ec0359a5c 720 addr: ip address
shennongmin 10:9d4ec0359a5c 721
shennongmin 10:9d4ec0359a5c 722 port: port number
shennongmin 10:9d4ec0359a5c 723
shennongmin 10:9d4ec0359a5c 724 id: id number(0-4)
shennongmin 10:9d4ec0359a5c 725
shennongmin 10:9d4ec0359a5c 726 return:
shennongmin 10:9d4ec0359a5c 727 true - successfully
shennongmin 10:9d4ec0359a5c 728 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 729
shennongmin 10:9d4ec0359a5c 730 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 731 bool ESP8266::newMux( uint8_t id, uint8_t type, String addr, int port)
shennongmin 10:9d4ec0359a5c 732
shennongmin 10:9d4ec0359a5c 733 {
shennongmin 10:9d4ec0359a5c 734
shennongmin 10:9d4ec0359a5c 735 esp_uart.print("AT+CIPSTART=");
shennongmin 10:9d4ec0359a5c 736 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 737 esp_uart.print(String(id));
shennongmin 10:9d4ec0359a5c 738 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 739 if(type>0)
shennongmin 10:9d4ec0359a5c 740 {
shennongmin 10:9d4ec0359a5c 741 esp_uart.print("\"TCP\"");
shennongmin 10:9d4ec0359a5c 742 }
shennongmin 10:9d4ec0359a5c 743 else
shennongmin 10:9d4ec0359a5c 744 {
shennongmin 10:9d4ec0359a5c 745 esp_uart.print("\"UDP\"");
shennongmin 10:9d4ec0359a5c 746 }
shennongmin 10:9d4ec0359a5c 747 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 748 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 749 esp_uart.print(addr);
shennongmin 10:9d4ec0359a5c 750 esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 751 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 752 // esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 753 esp_uart.println(String(port));
shennongmin 10:9d4ec0359a5c 754 // esp_uart.println("\"");
shennongmin 10:9d4ec0359a5c 755 String data;
shennongmin 10:9d4ec0359a5c 756 unsigned long start;
shennongmin 10:9d4ec0359a5c 757 start = millis();
shennongmin 10:9d4ec0359a5c 758 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 759 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 760 {
shennongmin 20:43451f36281e 761 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 762 data=data+a;
shennongmin 10:9d4ec0359a5c 763 }
shennongmin 10:9d4ec0359a5c 764 if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 )
shennongmin 10:9d4ec0359a5c 765 {
shennongmin 10:9d4ec0359a5c 766 return true;
shennongmin 10:9d4ec0359a5c 767 }
shennongmin 10:9d4ec0359a5c 768 }
shennongmin 10:9d4ec0359a5c 769 return false;
shennongmin 10:9d4ec0359a5c 770
shennongmin 10:9d4ec0359a5c 771
shennongmin 10:9d4ec0359a5c 772 }
shennongmin 10:9d4ec0359a5c 773 /*************************************************************************
shennongmin 10:9d4ec0359a5c 774 //send data in sigle connection mode
shennongmin 10:9d4ec0359a5c 775
shennongmin 10:9d4ec0359a5c 776 str: string of message
shennongmin 10:9d4ec0359a5c 777
shennongmin 10:9d4ec0359a5c 778 return:
shennongmin 10:9d4ec0359a5c 779 true - successfully
shennongmin 10:9d4ec0359a5c 780 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 781
shennongmin 10:9d4ec0359a5c 782 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 783 bool ESP8266::Send(String str)
shennongmin 10:9d4ec0359a5c 784 {
shennongmin 10:9d4ec0359a5c 785 esp_uart.flush();
shennongmin 10:9d4ec0359a5c 786 esp_uart.print("AT+CIPSEND=");
shennongmin 10:9d4ec0359a5c 787 // esp_uart.print("\"");
shennongmin 10:9d4ec0359a5c 788 esp_uart.println(str.length());
shennongmin 10:9d4ec0359a5c 789 // esp_uart.println("\"");
shennongmin 10:9d4ec0359a5c 790 unsigned long start;
shennongmin 10:9d4ec0359a5c 791 start = millis();
shennongmin 10:9d4ec0359a5c 792 bool found = false;
shennongmin 10:9d4ec0359a5c 793 while (millis()-start<5000) {
shennongmin 10:9d4ec0359a5c 794 if(esp_uart.find(">")==true )
shennongmin 10:9d4ec0359a5c 795 {
shennongmin 10:9d4ec0359a5c 796 found = true;
shennongmin 10:9d4ec0359a5c 797 break;
shennongmin 10:9d4ec0359a5c 798 }
shennongmin 10:9d4ec0359a5c 799 }
shennongmin 10:9d4ec0359a5c 800 if(found)
shennongmin 10:9d4ec0359a5c 801 esp_uart.print(str);
shennongmin 10:9d4ec0359a5c 802 else
shennongmin 10:9d4ec0359a5c 803 {
shennongmin 10:9d4ec0359a5c 804 closeMux();
shennongmin 10:9d4ec0359a5c 805 return false;
shennongmin 10:9d4ec0359a5c 806 }
shennongmin 10:9d4ec0359a5c 807
shennongmin 10:9d4ec0359a5c 808
shennongmin 10:9d4ec0359a5c 809 String data;
shennongmin 10:9d4ec0359a5c 810 start = millis();
shennongmin 10:9d4ec0359a5c 811 while (millis()-start<5000) {
shennongmin 10:9d4ec0359a5c 812 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 813 {
shennongmin 20:43451f36281e 814 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 815 data=data+a;
shennongmin 10:9d4ec0359a5c 816 }
shennongmin 10:9d4ec0359a5c 817 if (data.indexOf("SEND OK")!=-1)
shennongmin 10:9d4ec0359a5c 818 {
shennongmin 10:9d4ec0359a5c 819 return true;
shennongmin 10:9d4ec0359a5c 820 }
shennongmin 10:9d4ec0359a5c 821 }
shennongmin 10:9d4ec0359a5c 822 return false;
shennongmin 10:9d4ec0359a5c 823 }
shennongmin 10:9d4ec0359a5c 824
shennongmin 10:9d4ec0359a5c 825 /*************************************************************************
shennongmin 10:9d4ec0359a5c 826 //send data in multiple connection mode
shennongmin 10:9d4ec0359a5c 827
shennongmin 10:9d4ec0359a5c 828 id: <id>(0-4)
shennongmin 10:9d4ec0359a5c 829
shennongmin 10:9d4ec0359a5c 830 str: string of message
shennongmin 10:9d4ec0359a5c 831
shennongmin 10:9d4ec0359a5c 832 return:
shennongmin 10:9d4ec0359a5c 833 true - successfully
shennongmin 10:9d4ec0359a5c 834 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 835
shennongmin 10:9d4ec0359a5c 836 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 837 bool ESP8266::Send(uint8_t id, String str)
shennongmin 10:9d4ec0359a5c 838 {
shennongmin 10:9d4ec0359a5c 839 esp_uart.print("AT+CIPSEND=");
shennongmin 10:9d4ec0359a5c 840
shennongmin 10:9d4ec0359a5c 841 esp_uart.print(String(id));
shennongmin 10:9d4ec0359a5c 842 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 843 esp_uart.println(str.length());
shennongmin 10:9d4ec0359a5c 844 unsigned long start;
shennongmin 10:9d4ec0359a5c 845 start = millis();
shennongmin 10:9d4ec0359a5c 846 bool found = false;
shennongmin 10:9d4ec0359a5c 847 while (millis()-start<5000) {
shennongmin 10:9d4ec0359a5c 848 if(esp_uart.find(">")==true )
shennongmin 10:9d4ec0359a5c 849 {
shennongmin 10:9d4ec0359a5c 850 found = true;
shennongmin 10:9d4ec0359a5c 851 break;
shennongmin 10:9d4ec0359a5c 852 }
shennongmin 10:9d4ec0359a5c 853 }
shennongmin 10:9d4ec0359a5c 854 if(found)
shennongmin 10:9d4ec0359a5c 855 esp_uart.print(str);
shennongmin 10:9d4ec0359a5c 856 else
shennongmin 10:9d4ec0359a5c 857 {
shennongmin 10:9d4ec0359a5c 858 closeMux(id);
shennongmin 10:9d4ec0359a5c 859 return false;
shennongmin 10:9d4ec0359a5c 860 }
shennongmin 10:9d4ec0359a5c 861
shennongmin 10:9d4ec0359a5c 862
shennongmin 10:9d4ec0359a5c 863 String data;
shennongmin 10:9d4ec0359a5c 864 start = millis();
shennongmin 10:9d4ec0359a5c 865 while (millis()-start<5000) {
shennongmin 10:9d4ec0359a5c 866 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 867 {
shennongmin 20:43451f36281e 868 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 869 data=data+a;
shennongmin 10:9d4ec0359a5c 870 }
shennongmin 10:9d4ec0359a5c 871 if (data.indexOf("SEND OK")!=-1)
shennongmin 10:9d4ec0359a5c 872 {
shennongmin 10:9d4ec0359a5c 873 return true;
shennongmin 10:9d4ec0359a5c 874 }
shennongmin 10:9d4ec0359a5c 875 }
shennongmin 10:9d4ec0359a5c 876 return false;
shennongmin 10:9d4ec0359a5c 877 }
shennongmin 10:9d4ec0359a5c 878
shennongmin 10:9d4ec0359a5c 879 /*************************************************************************
shennongmin 10:9d4ec0359a5c 880 //Close up tcp or udp connection (sigle connection mode)
shennongmin 10:9d4ec0359a5c 881
shennongmin 10:9d4ec0359a5c 882
shennongmin 10:9d4ec0359a5c 883 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 884 void ESP8266::closeMux(void)
shennongmin 10:9d4ec0359a5c 885 {
shennongmin 10:9d4ec0359a5c 886 esp_uart.println("AT+CIPCLOSE");
shennongmin 10:9d4ec0359a5c 887
shennongmin 10:9d4ec0359a5c 888 String data;
shennongmin 10:9d4ec0359a5c 889 unsigned long start;
shennongmin 10:9d4ec0359a5c 890 start = millis();
shennongmin 10:9d4ec0359a5c 891 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 892 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 893 {
shennongmin 20:43451f36281e 894 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 895 data=data+a;
shennongmin 10:9d4ec0359a5c 896 }
shennongmin 10:9d4ec0359a5c 897 if (data.indexOf("Linked")!=-1 || data.indexOf("ERROR")!=-1 || data.indexOf("we must restart")!=-1)
shennongmin 10:9d4ec0359a5c 898 {
shennongmin 10:9d4ec0359a5c 899 break;
shennongmin 10:9d4ec0359a5c 900 }
shennongmin 10:9d4ec0359a5c 901 }
shennongmin 10:9d4ec0359a5c 902 }
shennongmin 10:9d4ec0359a5c 903
shennongmin 10:9d4ec0359a5c 904
shennongmin 10:9d4ec0359a5c 905 /*************************************************************************
shennongmin 10:9d4ec0359a5c 906 //Set up tcp or udp connection (multiple connection mode)
shennongmin 10:9d4ec0359a5c 907
shennongmin 10:9d4ec0359a5c 908 id: id number(0-4)
shennongmin 10:9d4ec0359a5c 909
shennongmin 10:9d4ec0359a5c 910 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 911 void ESP8266::closeMux(uint8_t id)
shennongmin 10:9d4ec0359a5c 912 {
shennongmin 10:9d4ec0359a5c 913 esp_uart.print("AT+CIPCLOSE=");
shennongmin 10:9d4ec0359a5c 914 esp_uart.println(String(id));
shennongmin 10:9d4ec0359a5c 915 String data;
shennongmin 10:9d4ec0359a5c 916 unsigned long start;
shennongmin 10:9d4ec0359a5c 917 start = millis();
shennongmin 10:9d4ec0359a5c 918 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 919 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 920 {
shennongmin 20:43451f36281e 921 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 922 data=data+a;
shennongmin 10:9d4ec0359a5c 923 }
shennongmin 10:9d4ec0359a5c 924 if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1)
shennongmin 10:9d4ec0359a5c 925 {
shennongmin 10:9d4ec0359a5c 926 break;
shennongmin 10:9d4ec0359a5c 927 }
shennongmin 10:9d4ec0359a5c 928 }
shennongmin 10:9d4ec0359a5c 929
shennongmin 10:9d4ec0359a5c 930 }
shennongmin 10:9d4ec0359a5c 931
shennongmin 10:9d4ec0359a5c 932 /*************************************************************************
shennongmin 10:9d4ec0359a5c 933 //show the current ip address
shennongmin 10:9d4ec0359a5c 934
shennongmin 10:9d4ec0359a5c 935 return: string of ip address
shennongmin 10:9d4ec0359a5c 936
shennongmin 10:9d4ec0359a5c 937 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 938 String ESP8266::showIP(void)
shennongmin 10:9d4ec0359a5c 939 {
shennongmin 10:9d4ec0359a5c 940 String data;
shennongmin 10:9d4ec0359a5c 941 unsigned long start;
shennongmin 14:40b1decf03f3 942
shennongmin 10:9d4ec0359a5c 943 for(int a=0; a<3;a++)
shennongmin 10:9d4ec0359a5c 944 {
shennongmin 10:9d4ec0359a5c 945 esp_uart.println("AT+CIFSR");
shennongmin 10:9d4ec0359a5c 946 start = millis();
shennongmin 10:9d4ec0359a5c 947 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 948 while(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 949 {
shennongmin 20:43451f36281e 950 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 951 data=data+a;
shennongmin 10:9d4ec0359a5c 952 }
shennongmin 10:9d4ec0359a5c 953 if (data.indexOf("AT+CIFSR")!=-1)
shennongmin 10:9d4ec0359a5c 954 {
shennongmin 10:9d4ec0359a5c 955 break;
shennongmin 10:9d4ec0359a5c 956 }
shennongmin 10:9d4ec0359a5c 957 }
shennongmin 10:9d4ec0359a5c 958 if(data.indexOf(".") != -1)
shennongmin 10:9d4ec0359a5c 959 {
shennongmin 10:9d4ec0359a5c 960 break;
shennongmin 10:9d4ec0359a5c 961 }
shennongmin 10:9d4ec0359a5c 962 data = "";
shennongmin 10:9d4ec0359a5c 963 }
shennongmin 14:40b1decf03f3 964
shennongmin 14:40b1decf03f3 965
shennongmin 10:9d4ec0359a5c 966 char head[4] = {0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 967 char tail[7] = {0x0D,0x0D,0x0A};
shennongmin 10:9d4ec0359a5c 968 data.replace("AT+CIFSR","");
shennongmin 10:9d4ec0359a5c 969 data.replace(tail,"");
shennongmin 10:9d4ec0359a5c 970 data.replace(head,"");
shennongmin 10:9d4ec0359a5c 971
shennongmin 10:9d4ec0359a5c 972 return data;
shennongmin 10:9d4ec0359a5c 973 }
shennongmin 10:9d4ec0359a5c 974
shennongmin 10:9d4ec0359a5c 975 /*************************************************************************
shennongmin 10:9d4ec0359a5c 976 ////set the parameter of server
shennongmin 10:9d4ec0359a5c 977
shennongmin 10:9d4ec0359a5c 978 mode:
shennongmin 10:9d4ec0359a5c 979 0 - close server mode
shennongmin 10:9d4ec0359a5c 980 1 - open server mode
shennongmin 10:9d4ec0359a5c 981
shennongmin 10:9d4ec0359a5c 982 port: <port>
shennongmin 10:9d4ec0359a5c 983
shennongmin 10:9d4ec0359a5c 984 return:
shennongmin 10:9d4ec0359a5c 985 true - successfully
shennongmin 10:9d4ec0359a5c 986 false - unsuccessfully
shennongmin 10:9d4ec0359a5c 987
shennongmin 10:9d4ec0359a5c 988 ***************************************************************************/
shennongmin 10:9d4ec0359a5c 989
shennongmin 10:9d4ec0359a5c 990 bool ESP8266::confServer(uint8_t mode, int port)
shennongmin 10:9d4ec0359a5c 991 {
shennongmin 10:9d4ec0359a5c 992 esp_uart.print("AT+CIPSERVER=");
shennongmin 10:9d4ec0359a5c 993 esp_uart.print(String(mode));
shennongmin 10:9d4ec0359a5c 994 esp_uart.print(",");
shennongmin 10:9d4ec0359a5c 995 esp_uart.println(String(port));
shennongmin 10:9d4ec0359a5c 996
shennongmin 10:9d4ec0359a5c 997 String data;
shennongmin 10:9d4ec0359a5c 998 unsigned long start;
shennongmin 10:9d4ec0359a5c 999 start = millis();
shennongmin 10:9d4ec0359a5c 1000 bool found = false;
shennongmin 10:9d4ec0359a5c 1001 while (millis()-start<3000) {
shennongmin 10:9d4ec0359a5c 1002 if(esp_uart.available()>0)
shennongmin 10:9d4ec0359a5c 1003 {
shennongmin 20:43451f36281e 1004 char a =esp_uart.readChr();
shennongmin 10:9d4ec0359a5c 1005 data=data+a;
shennongmin 10:9d4ec0359a5c 1006 }
shennongmin 10:9d4ec0359a5c 1007 if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1)
shennongmin 10:9d4ec0359a5c 1008 {
shennongmin 10:9d4ec0359a5c 1009 found = true;
shennongmin 10:9d4ec0359a5c 1010 break;
shennongmin 10:9d4ec0359a5c 1011 }
shennongmin 10:9d4ec0359a5c 1012 }
shennongmin 10:9d4ec0359a5c 1013 esp_uart.flush();
shennongmin 10:9d4ec0359a5c 1014 return found;
shennongmin 10:9d4ec0359a5c 1015 }