For test

Dependencies:   mbed

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