Provide an easy-to-use way to manipulate ESP8266.

Dependencies:   ArduinoAPI

Dependents:   WeeESP8266_TCPClientMultiple WeeESP8266_TCPClientSingle WeeESP8266_TCPServer WeeESP8266_UDPClientMultiple ... more

Committer:
itead
Date:
Wed Feb 11 06:04:07 2015 +0000
Revision:
21:d1a7f48ab3ba
Parent:
17:60ab5d5686d3
Update docs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
itead 5:5fa655d7212c 1 /**
itead 5:5fa655d7212c 2 * @file ESP8266.cpp
itead 5:5fa655d7212c 3 * @brief The implementation of class ESP8266.
itead 5:5fa655d7212c 4 * @author Wu Pengfei<pengfei.wu@itead.cc>
itead 5:5fa655d7212c 5 * @date 2015.02
itead 5:5fa655d7212c 6 *
itead 5:5fa655d7212c 7 * @par Copyright:
itead 5:5fa655d7212c 8 * Copyright (c) 2015 ITEAD Intelligent Systems Co., Ltd. \n\n
itead 5:5fa655d7212c 9 * This program is free software; you can redistribute it and/or
itead 5:5fa655d7212c 10 * modify it under the terms of the GNU General Public License as
itead 5:5fa655d7212c 11 * published by the Free Software Foundation; either version 2 of
itead 5:5fa655d7212c 12 * the License, or (at your option) any later version. \n\n
itead 5:5fa655d7212c 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
itead 5:5fa655d7212c 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
itead 5:5fa655d7212c 15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
itead 5:5fa655d7212c 16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
itead 5:5fa655d7212c 17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
itead 5:5fa655d7212c 18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
itead 5:5fa655d7212c 19 * THE SOFTWARE.
itead 5:5fa655d7212c 20 */
itead 2:fb209f93f3f1 21 #include "ESP8266.h"
itead 15:5c5a85e984a8 22 #include "log.h"
itead 0:d3b93948c925 23
itead 5:5fa655d7212c 24 ESP8266::ESP8266(ArduinoSerial &uart): m_puart(&uart)
itead 5:5fa655d7212c 25 {
itead 5:5fa655d7212c 26 m_puart->begin(9600);
itead 5:5fa655d7212c 27 m_puart->flush();
itead 5:5fa655d7212c 28 }
itead 5:5fa655d7212c 29
itead 5:5fa655d7212c 30
itead 5:5fa655d7212c 31 bool ESP8266::kick(void)
itead 5:5fa655d7212c 32 {
itead 5:5fa655d7212c 33 return eAT();
itead 5:5fa655d7212c 34 }
itead 5:5fa655d7212c 35
itead 5:5fa655d7212c 36 bool ESP8266::restart(void)
itead 5:5fa655d7212c 37 {
itead 5:5fa655d7212c 38 unsigned long start;
itead 5:5fa655d7212c 39 if (eATRST()) {
itead 5:5fa655d7212c 40 delay(2000);
itead 5:5fa655d7212c 41 start = millis();
itead 5:5fa655d7212c 42 while (millis() - start < 3000) {
itead 5:5fa655d7212c 43 if (eAT()) {
itead 9:dec2cf52b744 44 delay(1500); /* Waiting for stable */
itead 5:5fa655d7212c 45 return true;
itead 5:5fa655d7212c 46 }
itead 5:5fa655d7212c 47 delay(100);
itead 5:5fa655d7212c 48 }
itead 5:5fa655d7212c 49 }
itead 5:5fa655d7212c 50 return false;
itead 5:5fa655d7212c 51 }
itead 5:5fa655d7212c 52
itead 5:5fa655d7212c 53 String ESP8266::getVersion(void)
itead 5:5fa655d7212c 54 {
itead 6:18eb2d9fb2ac 55 String version;
itead 6:18eb2d9fb2ac 56 eATGMR(version);
itead 6:18eb2d9fb2ac 57 return version;
itead 5:5fa655d7212c 58 }
itead 5:5fa655d7212c 59
itead 5:5fa655d7212c 60 bool ESP8266::setOprToStation(void)
itead 5:5fa655d7212c 61 {
itead 10:002615561b0b 62 uint8_t mode;
itead 10:002615561b0b 63 if (!qATCWMODE(&mode)) {
itead 10:002615561b0b 64 return false;
itead 10:002615561b0b 65 }
itead 10:002615561b0b 66 if (mode == 1) {
itead 5:5fa655d7212c 67 return true;
itead 10:002615561b0b 68 } else {
itead 10:002615561b0b 69 if (sATCWMODE(1) && restart()) {
itead 10:002615561b0b 70 return true;
itead 10:002615561b0b 71 } else {
itead 10:002615561b0b 72 return false;
itead 10:002615561b0b 73 }
itead 5:5fa655d7212c 74 }
itead 5:5fa655d7212c 75 }
itead 5:5fa655d7212c 76
itead 5:5fa655d7212c 77 bool ESP8266::setOprToSoftAP(void)
itead 5:5fa655d7212c 78 {
itead 10:002615561b0b 79 uint8_t mode;
itead 10:002615561b0b 80 if (!qATCWMODE(&mode)) {
itead 10:002615561b0b 81 return false;
itead 10:002615561b0b 82 }
itead 10:002615561b0b 83 if (mode == 2) {
itead 5:5fa655d7212c 84 return true;
itead 10:002615561b0b 85 } else {
itead 10:002615561b0b 86 if (sATCWMODE(2) && restart()) {
itead 10:002615561b0b 87 return true;
itead 10:002615561b0b 88 } else {
itead 10:002615561b0b 89 return false;
itead 10:002615561b0b 90 }
itead 5:5fa655d7212c 91 }
itead 5:5fa655d7212c 92 }
itead 5:5fa655d7212c 93
itead 5:5fa655d7212c 94 bool ESP8266::setOprToStationSoftAP(void)
itead 5:5fa655d7212c 95 {
itead 10:002615561b0b 96 uint8_t mode;
itead 10:002615561b0b 97 if (!qATCWMODE(&mode)) {
itead 10:002615561b0b 98 return false;
itead 10:002615561b0b 99 }
itead 10:002615561b0b 100 if (mode == 3) {
itead 5:5fa655d7212c 101 return true;
itead 10:002615561b0b 102 } else {
itead 10:002615561b0b 103 if (sATCWMODE(3) && restart()) {
itead 10:002615561b0b 104 return true;
itead 10:002615561b0b 105 } else {
itead 10:002615561b0b 106 return false;
itead 10:002615561b0b 107 }
itead 5:5fa655d7212c 108 }
itead 5:5fa655d7212c 109 }
itead 5:5fa655d7212c 110
itead 5:5fa655d7212c 111 String ESP8266::getAPList(void)
itead 5:5fa655d7212c 112 {
itead 9:dec2cf52b744 113 String list;
itead 9:dec2cf52b744 114 eATCWLAP(list);
itead 9:dec2cf52b744 115 return list;
itead 5:5fa655d7212c 116 }
itead 5:5fa655d7212c 117
itead 5:5fa655d7212c 118 bool ESP8266::joinAP(String ssid, String pwd)
itead 5:5fa655d7212c 119 {
itead 9:dec2cf52b744 120 return sATCWJAP(ssid, pwd);
itead 5:5fa655d7212c 121 }
itead 5:5fa655d7212c 122
itead 5:5fa655d7212c 123 bool ESP8266::leaveAP(void)
itead 5:5fa655d7212c 124 {
itead 9:dec2cf52b744 125 return eATCWQAP();
itead 5:5fa655d7212c 126 }
itead 5:5fa655d7212c 127
itead 5:5fa655d7212c 128 bool ESP8266::setSoftAPParam(String ssid, String pwd, uint8_t chl, uint8_t ecn)
itead 5:5fa655d7212c 129 {
itead 9:dec2cf52b744 130 return sATCWSAP(ssid, pwd, chl, ecn);
itead 5:5fa655d7212c 131 }
itead 5:5fa655d7212c 132
itead 5:5fa655d7212c 133 String ESP8266::getJoinedDeviceIP(void)
itead 5:5fa655d7212c 134 {
itead 9:dec2cf52b744 135 String list;
itead 9:dec2cf52b744 136 eATCWLIF(list);
itead 9:dec2cf52b744 137 return list;
itead 5:5fa655d7212c 138 }
itead 5:5fa655d7212c 139
itead 5:5fa655d7212c 140 String ESP8266::getIPStatus(void)
itead 5:5fa655d7212c 141 {
itead 9:dec2cf52b744 142 String list;
itead 9:dec2cf52b744 143 eATCIPSTATUS(list);
itead 9:dec2cf52b744 144 return list;
itead 5:5fa655d7212c 145 }
itead 5:5fa655d7212c 146
itead 5:5fa655d7212c 147 String ESP8266::getLocalIP(void)
itead 5:5fa655d7212c 148 {
itead 9:dec2cf52b744 149 String list;
itead 9:dec2cf52b744 150 eATCIFSR(list);
itead 9:dec2cf52b744 151 return list;
itead 5:5fa655d7212c 152 }
itead 5:5fa655d7212c 153
itead 5:5fa655d7212c 154 bool ESP8266::enableMUX(void)
itead 5:5fa655d7212c 155 {
itead 9:dec2cf52b744 156 return sATCIPMUX(1);
itead 5:5fa655d7212c 157 }
itead 5:5fa655d7212c 158
itead 5:5fa655d7212c 159 bool ESP8266::disableMUX(void)
itead 5:5fa655d7212c 160 {
itead 9:dec2cf52b744 161 return sATCIPMUX(0);
itead 5:5fa655d7212c 162 }
itead 5:5fa655d7212c 163
itead 5:5fa655d7212c 164 bool ESP8266::createTCP(String addr, uint32_t port)
itead 5:5fa655d7212c 165 {
itead 9:dec2cf52b744 166 return sATCIPSTARTSingle("TCP", addr, port);
itead 5:5fa655d7212c 167 }
itead 5:5fa655d7212c 168
itead 5:5fa655d7212c 169 bool ESP8266::releaseTCP(void)
itead 5:5fa655d7212c 170 {
itead 9:dec2cf52b744 171 return eATCIPCLOSESingle();
itead 5:5fa655d7212c 172 }
itead 5:5fa655d7212c 173
itead 5:5fa655d7212c 174 bool ESP8266::registerUDP(String addr, uint32_t port)
itead 5:5fa655d7212c 175 {
itead 9:dec2cf52b744 176 return sATCIPSTARTSingle("UDP", addr, port);
itead 5:5fa655d7212c 177 }
itead 5:5fa655d7212c 178
itead 5:5fa655d7212c 179 bool ESP8266::unregisterUDP(void)
itead 5:5fa655d7212c 180 {
itead 9:dec2cf52b744 181 return eATCIPCLOSESingle();
itead 5:5fa655d7212c 182 }
itead 5:5fa655d7212c 183
itead 5:5fa655d7212c 184 bool ESP8266::createTCP(uint8_t mux_id, String addr, uint32_t port)
itead 5:5fa655d7212c 185 {
itead 9:dec2cf52b744 186 return sATCIPSTARTMultiple(mux_id, "TCP", addr, port);
itead 5:5fa655d7212c 187 }
itead 5:5fa655d7212c 188
itead 5:5fa655d7212c 189 bool ESP8266::releaseTCP(uint8_t mux_id)
itead 5:5fa655d7212c 190 {
itead 9:dec2cf52b744 191 return sATCIPCLOSEMulitple(mux_id);
itead 5:5fa655d7212c 192 }
itead 5:5fa655d7212c 193
itead 5:5fa655d7212c 194 bool ESP8266::registerUDP(uint8_t mux_id, String addr, uint32_t port)
itead 5:5fa655d7212c 195 {
itead 9:dec2cf52b744 196 return sATCIPSTARTMultiple(mux_id, "UDP", addr, port);
itead 5:5fa655d7212c 197 }
itead 5:5fa655d7212c 198
itead 5:5fa655d7212c 199 bool ESP8266::unregisterUDP(uint8_t mux_id)
itead 5:5fa655d7212c 200 {
itead 9:dec2cf52b744 201 return sATCIPCLOSEMulitple(mux_id);
itead 5:5fa655d7212c 202 }
itead 5:5fa655d7212c 203
itead 5:5fa655d7212c 204 bool ESP8266::setTCPServerTimeout(uint32_t timeout)
itead 5:5fa655d7212c 205 {
itead 9:dec2cf52b744 206 return sATCIPSTO(timeout);
itead 5:5fa655d7212c 207 }
itead 5:5fa655d7212c 208
itead 5:5fa655d7212c 209 bool ESP8266::startTCPServer(uint32_t port)
itead 5:5fa655d7212c 210 {
itead 13:81f2548212e5 211 if (sATCIPSERVER(1, port)) {
itead 9:dec2cf52b744 212 return true;
itead 9:dec2cf52b744 213 }
itead 5:5fa655d7212c 214 return false;
itead 5:5fa655d7212c 215 }
itead 5:5fa655d7212c 216
itead 5:5fa655d7212c 217 bool ESP8266::stopTCPServer(void)
itead 5:5fa655d7212c 218 {
itead 9:dec2cf52b744 219 sATCIPSERVER(0);
itead 9:dec2cf52b744 220 restart();
itead 5:5fa655d7212c 221 return false;
itead 5:5fa655d7212c 222 }
itead 5:5fa655d7212c 223
itead 5:5fa655d7212c 224 bool ESP8266::send(const uint8_t *buffer, uint32_t len)
itead 5:5fa655d7212c 225 {
itead 9:dec2cf52b744 226 return sATCIPSENDSingle(buffer, len);
itead 9:dec2cf52b744 227 }
itead 9:dec2cf52b744 228
itead 9:dec2cf52b744 229 bool ESP8266::send(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
itead 9:dec2cf52b744 230 {
itead 9:dec2cf52b744 231 return sATCIPSENDMultiple(mux_id, buffer, len);
itead 9:dec2cf52b744 232 }
itead 9:dec2cf52b744 233
itead 9:dec2cf52b744 234 uint32_t ESP8266::recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
itead 9:dec2cf52b744 235 {
itead 9:dec2cf52b744 236 return recvPkg(buffer, buffer_size, NULL, timeout, NULL);
itead 5:5fa655d7212c 237 }
itead 5:5fa655d7212c 238
itead 9:dec2cf52b744 239 uint32_t ESP8266::recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
itead 5:5fa655d7212c 240 {
itead 9:dec2cf52b744 241 uint8_t id;
itead 9:dec2cf52b744 242 uint32_t ret;
itead 9:dec2cf52b744 243 ret = recvPkg(buffer, buffer_size, NULL, timeout, &id);
itead 9:dec2cf52b744 244 if (ret > 0 && id == mux_id) {
itead 9:dec2cf52b744 245 return ret;
itead 9:dec2cf52b744 246 }
itead 9:dec2cf52b744 247 return 0;
itead 9:dec2cf52b744 248 }
itead 9:dec2cf52b744 249
itead 9:dec2cf52b744 250 uint32_t ESP8266::recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
itead 9:dec2cf52b744 251 {
itead 9:dec2cf52b744 252 return recvPkg(buffer, buffer_size, NULL, timeout, coming_mux_id);
itead 5:5fa655d7212c 253 }
itead 5:5fa655d7212c 254
itead 9:dec2cf52b744 255 /*----------------------------------------------------------------------------*/
itead 5:5fa655d7212c 256
itead 15:5c5a85e984a8 257 uint32_t ESP8266::recvPkg(uint8_t *buffer, uint32_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id)
itead 5:5fa655d7212c 258 {
itead 9:dec2cf52b744 259 String data;
itead 9:dec2cf52b744 260 char a;
itead 9:dec2cf52b744 261 int32_t index_PIPDcomma = -1;
itead 9:dec2cf52b744 262 int32_t index_colon = -1; /* : */
itead 9:dec2cf52b744 263 int32_t index_comma = -1; /* , */
itead 15:5c5a85e984a8 264 int32_t len = -1;
itead 15:5c5a85e984a8 265 int8_t id = -1;
itead 9:dec2cf52b744 266 bool has_data = false;
itead 15:5c5a85e984a8 267 logDebug("start recv pkg");
itead 9:dec2cf52b744 268
itead 9:dec2cf52b744 269 unsigned long start = millis();
itead 9:dec2cf52b744 270 while (millis() - start < timeout) {
itead 9:dec2cf52b744 271 while(m_puart->available() > 0) {
itead 9:dec2cf52b744 272 a = m_puart->readChr();
itead 9:dec2cf52b744 273 data += a;
itead 9:dec2cf52b744 274 }
itead 9:dec2cf52b744 275
itead 9:dec2cf52b744 276 index_PIPDcomma = data.indexOf("+IPD,");
itead 9:dec2cf52b744 277 if (index_PIPDcomma != -1) {
itead 15:5c5a85e984a8 278 logDebug("index_PIPDcomma found = %d", index_PIPDcomma);
itead 9:dec2cf52b744 279 index_colon = data.indexOf(':', index_PIPDcomma + 5);
itead 9:dec2cf52b744 280 if (index_colon != -1) {
itead 15:5c5a85e984a8 281 logDebug("data1 = %s\r\n", data.c_str());
itead 15:5c5a85e984a8 282 logDebug("index_colon found = %d", index_colon);
itead 15:5c5a85e984a8 283
itead 9:dec2cf52b744 284 index_comma = data.indexOf(',', index_PIPDcomma + 5);
itead 15:5c5a85e984a8 285 logDebug("index_comma found = %d", index_comma);
itead 15:5c5a85e984a8 286
itead 15:5c5a85e984a8 287 if (index_comma != -1 && index_comma < index_colon) { /* +IPD,id,len:data */
itead 15:5c5a85e984a8 288 logDebug("id = %d", id);
itead 9:dec2cf52b744 289 id = data.substring(index_PIPDcomma + 5, index_comma).toInt();
itead 15:5c5a85e984a8 290 logDebug("id = %d", id);
itead 9:dec2cf52b744 291 if (id < 0 || id > 4) {
itead 9:dec2cf52b744 292 return 0;
itead 9:dec2cf52b744 293 }
itead 15:5c5a85e984a8 294 logDebug("len = %d", len);
itead 9:dec2cf52b744 295 len = data.substring(index_comma + 1, index_colon).toInt();
itead 15:5c5a85e984a8 296 logDebug("len = %d", len);
itead 9:dec2cf52b744 297 if (len <= 0) {
itead 9:dec2cf52b744 298 return 0;
itead 9:dec2cf52b744 299 }
itead 15:5c5a85e984a8 300 logDebug("has id");
itead 9:dec2cf52b744 301 } else { /* +IPD,len:data */
itead 9:dec2cf52b744 302 len = data.substring(index_PIPDcomma + 5, index_colon).toInt();
itead 15:5c5a85e984a8 303 logDebug("len = %d", len);
itead 9:dec2cf52b744 304 if (len <= 0) {
itead 9:dec2cf52b744 305 return 0;
itead 9:dec2cf52b744 306 }
itead 15:5c5a85e984a8 307 logDebug("no id");
itead 9:dec2cf52b744 308 }
itead 9:dec2cf52b744 309 has_data = true;
itead 15:5c5a85e984a8 310 logDebug("has_data true");
itead 9:dec2cf52b744 311 break;
itead 9:dec2cf52b744 312 }
itead 9:dec2cf52b744 313 }
itead 9:dec2cf52b744 314 }
itead 15:5c5a85e984a8 315 //logDebug("has_data = %u\r\n", has_data);
itead 15:5c5a85e984a8 316 //logDebug("data2 = %s\r\n", data.c_str());
itead 9:dec2cf52b744 317
itead 9:dec2cf52b744 318 if (has_data) {
itead 9:dec2cf52b744 319 start = millis();
itead 17:60ab5d5686d3 320 while (millis() - start < 3000) {
itead 9:dec2cf52b744 321 while(m_puart->available() > 0) {
itead 9:dec2cf52b744 322 a = m_puart->readChr();
itead 9:dec2cf52b744 323 data += a;
itead 9:dec2cf52b744 324 }
itead 15:5c5a85e984a8 325 //logDebug("data3 = %s\r\n", data.c_str());
itead 9:dec2cf52b744 326 if (data.length() >= index_colon + 1 + len) {
itead 9:dec2cf52b744 327 if (data_len) {
itead 9:dec2cf52b744 328 *data_len = len;
itead 9:dec2cf52b744 329 }
itead 9:dec2cf52b744 330 if (index_comma != -1 && coming_mux_id) {
itead 9:dec2cf52b744 331 *coming_mux_id = id;
itead 9:dec2cf52b744 332 }
itead 15:5c5a85e984a8 333 //logDebug("len = %d", len);
itead 15:5c5a85e984a8 334 //logDebug("buffer_size = %d", buffer_size);
itead 9:dec2cf52b744 335 uint32_t ret = len > buffer_size ? buffer_size : len;
itead 15:5c5a85e984a8 336
itead 9:dec2cf52b744 337 memcpy(buffer,
itead 9:dec2cf52b744 338 data.substring(index_colon + 1, index_colon + 1 + len).c_str(),
itead 9:dec2cf52b744 339 ret);
itead 15:5c5a85e984a8 340 logDebug("ret = %u\r\n", ret);
itead 9:dec2cf52b744 341 return ret;
itead 9:dec2cf52b744 342 }
itead 9:dec2cf52b744 343 }
itead 9:dec2cf52b744 344 }
itead 15:5c5a85e984a8 345 logDebug("end recv pkg");
itead 9:dec2cf52b744 346 return 0;
itead 5:5fa655d7212c 347 }
itead 5:5fa655d7212c 348
itead 6:18eb2d9fb2ac 349 String ESP8266::recvString(String target, uint32_t timeout)
itead 5:5fa655d7212c 350 {
itead 5:5fa655d7212c 351 String data;
itead 5:5fa655d7212c 352 char a;
itead 5:5fa655d7212c 353 unsigned long start = millis();
itead 5:5fa655d7212c 354 while (millis() - start < timeout) {
itead 5:5fa655d7212c 355 while(m_puart->available() > 0) {
itead 5:5fa655d7212c 356 a = m_puart->readChr();
itead 5:5fa655d7212c 357 data += a;
itead 5:5fa655d7212c 358 }
itead 5:5fa655d7212c 359 if (data.indexOf(target) != -1) {
itead 5:5fa655d7212c 360 break;
itead 5:5fa655d7212c 361 }
itead 5:5fa655d7212c 362 }
itead 5:5fa655d7212c 363 return data;
itead 5:5fa655d7212c 364 }
itead 5:5fa655d7212c 365
itead 6:18eb2d9fb2ac 366 String ESP8266::recvString(String target1, String target2, uint32_t timeout)
itead 5:5fa655d7212c 367 {
itead 5:5fa655d7212c 368 String data;
itead 5:5fa655d7212c 369 char a;
itead 5:5fa655d7212c 370 unsigned long start = millis();
itead 5:5fa655d7212c 371 while (millis() - start < timeout) {
itead 5:5fa655d7212c 372 while(m_puart->available() > 0) {
itead 5:5fa655d7212c 373 a = m_puart->readChr();
itead 5:5fa655d7212c 374 data += a;
itead 5:5fa655d7212c 375 }
itead 5:5fa655d7212c 376 if (data.indexOf(target1) != -1) {
itead 5:5fa655d7212c 377 break;
itead 5:5fa655d7212c 378 } else if (data.indexOf(target2) != -1) {
itead 5:5fa655d7212c 379 break;
itead 5:5fa655d7212c 380 }
itead 5:5fa655d7212c 381 }
itead 5:5fa655d7212c 382 return data;
itead 5:5fa655d7212c 383 }
itead 5:5fa655d7212c 384
itead 6:18eb2d9fb2ac 385 String ESP8266::recvString(String target1, String target2, String target3, uint32_t timeout)
itead 5:5fa655d7212c 386 {
itead 5:5fa655d7212c 387 String data;
itead 5:5fa655d7212c 388 char a;
itead 5:5fa655d7212c 389 unsigned long start = millis();
itead 5:5fa655d7212c 390 while (millis() - start < timeout) {
itead 5:5fa655d7212c 391 while(m_puart->available() > 0) {
itead 5:5fa655d7212c 392 a = m_puart->readChr();
itead 5:5fa655d7212c 393 data += a;
itead 5:5fa655d7212c 394 }
itead 5:5fa655d7212c 395 if (data.indexOf(target1) != -1) {
itead 5:5fa655d7212c 396 break;
itead 5:5fa655d7212c 397 } else if (data.indexOf(target2) != -1) {
itead 5:5fa655d7212c 398 break;
itead 5:5fa655d7212c 399 } else if (data.indexOf(target3) != -1) {
itead 5:5fa655d7212c 400 break;
itead 5:5fa655d7212c 401 }
itead 5:5fa655d7212c 402 }
itead 5:5fa655d7212c 403 return data;
itead 5:5fa655d7212c 404 }
itead 5:5fa655d7212c 405
itead 6:18eb2d9fb2ac 406 bool ESP8266::recvFind(String target, uint32_t timeout)
itead 6:18eb2d9fb2ac 407 {
itead 6:18eb2d9fb2ac 408 String data_tmp;
itead 6:18eb2d9fb2ac 409 data_tmp = recvString(target, timeout);
itead 6:18eb2d9fb2ac 410 if (data_tmp.indexOf(target) != -1) {
itead 6:18eb2d9fb2ac 411 return true;
itead 6:18eb2d9fb2ac 412 }
itead 6:18eb2d9fb2ac 413 return false;
itead 6:18eb2d9fb2ac 414 }
itead 6:18eb2d9fb2ac 415
itead 6:18eb2d9fb2ac 416 bool ESP8266::recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout)
itead 6:18eb2d9fb2ac 417 {
itead 6:18eb2d9fb2ac 418 String data_tmp;
itead 6:18eb2d9fb2ac 419 data_tmp = recvString(target, timeout);
itead 6:18eb2d9fb2ac 420 if (data_tmp.indexOf(target) != -1) {
itead 6:18eb2d9fb2ac 421 int32_t index1 = data_tmp.indexOf(begin);
itead 6:18eb2d9fb2ac 422 int32_t index2 = data_tmp.indexOf(end);
itead 6:18eb2d9fb2ac 423 if (index1 != -1 && index2 != -1) {
itead 6:18eb2d9fb2ac 424 index1 += begin.length();
itead 6:18eb2d9fb2ac 425 data = data_tmp.substring(index1, index2);
itead 6:18eb2d9fb2ac 426 return true;
itead 6:18eb2d9fb2ac 427 }
itead 6:18eb2d9fb2ac 428 }
itead 6:18eb2d9fb2ac 429 data = "";
itead 6:18eb2d9fb2ac 430 return false;
itead 6:18eb2d9fb2ac 431 }
itead 5:5fa655d7212c 432
itead 5:5fa655d7212c 433 bool ESP8266::eAT(void)
itead 5:5fa655d7212c 434 {
itead 5:5fa655d7212c 435 m_puart->flush();
itead 5:5fa655d7212c 436 m_puart->println("AT");
itead 6:18eb2d9fb2ac 437 return recvFind("OK");
itead 5:5fa655d7212c 438 }
itead 5:5fa655d7212c 439
itead 5:5fa655d7212c 440 bool ESP8266::eATRST(void)
itead 5:5fa655d7212c 441 {
itead 5:5fa655d7212c 442 m_puart->flush();
itead 5:5fa655d7212c 443 m_puart->println("AT+RST");
itead 6:18eb2d9fb2ac 444 return recvFind("OK");
itead 5:5fa655d7212c 445 }
itead 5:5fa655d7212c 446
itead 6:18eb2d9fb2ac 447 bool ESP8266::eATGMR(String &version)
itead 5:5fa655d7212c 448 {
itead 5:5fa655d7212c 449 m_puart->flush();
itead 5:5fa655d7212c 450 m_puart->println("AT+GMR");
itead 6:18eb2d9fb2ac 451 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", version);
itead 5:5fa655d7212c 452 }
itead 5:5fa655d7212c 453
itead 10:002615561b0b 454 bool ESP8266::qATCWMODE(uint8_t *mode)
itead 10:002615561b0b 455 {
itead 10:002615561b0b 456 String str_mode;
itead 10:002615561b0b 457 bool ret;
itead 10:002615561b0b 458 if (!mode) {
itead 10:002615561b0b 459 return false;
itead 10:002615561b0b 460 }
itead 10:002615561b0b 461 m_puart->flush();
itead 10:002615561b0b 462 m_puart->println("AT+CWMODE?");
itead 10:002615561b0b 463 ret = recvFindAndFilter("OK", "+CWMODE:", "\r\n\r\nOK", str_mode);
itead 10:002615561b0b 464 if (ret) {
itead 10:002615561b0b 465 *mode = (uint8_t)str_mode.toInt();
itead 10:002615561b0b 466 return true;
itead 10:002615561b0b 467 } else {
itead 10:002615561b0b 468 return false;
itead 10:002615561b0b 469 }
itead 10:002615561b0b 470 }
itead 5:5fa655d7212c 471
itead 5:5fa655d7212c 472 bool ESP8266::sATCWMODE(uint8_t mode)
itead 5:5fa655d7212c 473 {
itead 5:5fa655d7212c 474 String data;
itead 5:5fa655d7212c 475 m_puart->flush();
itead 5:5fa655d7212c 476 m_puart->print("AT+CWMODE=");
itead 5:5fa655d7212c 477 m_puart->println(mode);
itead 7:d54995c9e1d4 478
itead 7:d54995c9e1d4 479 data = recvString("OK", "no change");
itead 7:d54995c9e1d4 480 if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
itead 7:d54995c9e1d4 481 return true;
itead 7:d54995c9e1d4 482 }
itead 7:d54995c9e1d4 483 return false;
itead 6:18eb2d9fb2ac 484 }
itead 6:18eb2d9fb2ac 485
itead 6:18eb2d9fb2ac 486 bool ESP8266::sATCWJAP(String ssid, String pwd)
itead 6:18eb2d9fb2ac 487 {
itead 6:18eb2d9fb2ac 488 String data;
itead 6:18eb2d9fb2ac 489 m_puart->flush();
itead 6:18eb2d9fb2ac 490 m_puart->print("AT+CWJAP=\"");
itead 6:18eb2d9fb2ac 491 m_puart->print(ssid);
itead 6:18eb2d9fb2ac 492 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 493 m_puart->print(pwd);
itead 6:18eb2d9fb2ac 494 m_puart->println("\"");
itead 6:18eb2d9fb2ac 495
itead 6:18eb2d9fb2ac 496 data = recvString("OK", "FAIL", 10000);
itead 6:18eb2d9fb2ac 497 if (data.indexOf("OK") != -1) {
itead 6:18eb2d9fb2ac 498 return true;
itead 6:18eb2d9fb2ac 499 }
itead 6:18eb2d9fb2ac 500 return false;
itead 6:18eb2d9fb2ac 501 }
itead 6:18eb2d9fb2ac 502
itead 6:18eb2d9fb2ac 503 bool ESP8266::eATCWLAP(String &list)
itead 6:18eb2d9fb2ac 504 {
itead 6:18eb2d9fb2ac 505 String data;
itead 6:18eb2d9fb2ac 506 m_puart->flush();
itead 6:18eb2d9fb2ac 507 m_puart->println("AT+CWLAP");
itead 6:18eb2d9fb2ac 508 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list, 10000);
itead 6:18eb2d9fb2ac 509 }
itead 6:18eb2d9fb2ac 510
itead 6:18eb2d9fb2ac 511 bool ESP8266::eATCWQAP(void)
itead 6:18eb2d9fb2ac 512 {
itead 6:18eb2d9fb2ac 513 String data;
itead 6:18eb2d9fb2ac 514 m_puart->flush();
itead 6:18eb2d9fb2ac 515 m_puart->println("AT+CWQAP");
itead 6:18eb2d9fb2ac 516 return recvFind("OK");
itead 6:18eb2d9fb2ac 517 }
itead 6:18eb2d9fb2ac 518
itead 6:18eb2d9fb2ac 519 bool ESP8266::sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn)
itead 6:18eb2d9fb2ac 520 {
itead 6:18eb2d9fb2ac 521 String data;
itead 6:18eb2d9fb2ac 522 m_puart->flush();
itead 6:18eb2d9fb2ac 523 m_puart->print("AT+CWSAP=\"");
itead 6:18eb2d9fb2ac 524 m_puart->print(ssid);
itead 6:18eb2d9fb2ac 525 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 526 m_puart->print(pwd);
itead 6:18eb2d9fb2ac 527 m_puart->print("\",");
itead 6:18eb2d9fb2ac 528 m_puart->print(chl);
itead 6:18eb2d9fb2ac 529 m_puart->print(",");
itead 6:18eb2d9fb2ac 530 m_puart->println(ecn);
itead 6:18eb2d9fb2ac 531
itead 6:18eb2d9fb2ac 532 data = recvString("OK", "ERROR", 5000);
itead 5:5fa655d7212c 533 if (data.indexOf("OK") != -1) {
itead 5:5fa655d7212c 534 return true;
itead 5:5fa655d7212c 535 }
itead 5:5fa655d7212c 536 return false;
itead 5:5fa655d7212c 537 }
itead 5:5fa655d7212c 538
itead 6:18eb2d9fb2ac 539 bool ESP8266::eATCWLIF(String &list)
itead 6:18eb2d9fb2ac 540 {
itead 6:18eb2d9fb2ac 541 String data;
itead 6:18eb2d9fb2ac 542 m_puart->flush();
itead 6:18eb2d9fb2ac 543 m_puart->println("AT+CWLIF");
itead 6:18eb2d9fb2ac 544 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
itead 6:18eb2d9fb2ac 545 }
itead 6:18eb2d9fb2ac 546 bool ESP8266::eATCIPSTATUS(String &list)
itead 6:18eb2d9fb2ac 547 {
itead 6:18eb2d9fb2ac 548 String data;
itead 17:60ab5d5686d3 549 delay(100);
itead 6:18eb2d9fb2ac 550 m_puart->flush();
itead 6:18eb2d9fb2ac 551 m_puart->println("AT+CIPSTATUS");
itead 6:18eb2d9fb2ac 552 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
itead 6:18eb2d9fb2ac 553 }
itead 6:18eb2d9fb2ac 554 bool ESP8266::sATCIPSTARTSingle(String type, String addr, uint32_t port)
itead 6:18eb2d9fb2ac 555 {
itead 6:18eb2d9fb2ac 556 String data;
itead 6:18eb2d9fb2ac 557 m_puart->flush();
itead 6:18eb2d9fb2ac 558 m_puart->print("AT+CIPSTART=\"");
itead 6:18eb2d9fb2ac 559 m_puart->print(type);
itead 6:18eb2d9fb2ac 560 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 561 m_puart->print(addr);
itead 6:18eb2d9fb2ac 562 m_puart->print("\",");
itead 6:18eb2d9fb2ac 563 m_puart->println(port);
itead 6:18eb2d9fb2ac 564
itead 6:18eb2d9fb2ac 565 data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
itead 6:18eb2d9fb2ac 566 if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
itead 6:18eb2d9fb2ac 567 return true;
itead 6:18eb2d9fb2ac 568 }
itead 6:18eb2d9fb2ac 569 return false;
itead 6:18eb2d9fb2ac 570 }
itead 6:18eb2d9fb2ac 571 bool ESP8266::sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port)
itead 6:18eb2d9fb2ac 572 {
itead 6:18eb2d9fb2ac 573 String data;
itead 6:18eb2d9fb2ac 574 m_puart->flush();
itead 6:18eb2d9fb2ac 575 m_puart->print("AT+CIPSTART=");
itead 6:18eb2d9fb2ac 576 m_puart->print(mux_id);
itead 6:18eb2d9fb2ac 577 m_puart->print(",\"");
itead 6:18eb2d9fb2ac 578 m_puart->print(type);
itead 6:18eb2d9fb2ac 579 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 580 m_puart->print(addr);
itead 6:18eb2d9fb2ac 581 m_puart->print("\",");
itead 6:18eb2d9fb2ac 582 m_puart->println(port);
itead 6:18eb2d9fb2ac 583
itead 6:18eb2d9fb2ac 584 data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
itead 6:18eb2d9fb2ac 585 if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
itead 6:18eb2d9fb2ac 586 return true;
itead 6:18eb2d9fb2ac 587 }
itead 6:18eb2d9fb2ac 588 return false;
itead 6:18eb2d9fb2ac 589 }
itead 9:dec2cf52b744 590 bool ESP8266::sATCIPSENDSingle(const uint8_t *buffer, uint32_t len)
itead 6:18eb2d9fb2ac 591 {
itead 7:d54995c9e1d4 592 m_puart->flush();
itead 7:d54995c9e1d4 593 m_puart->print("AT+CIPSEND=");
itead 7:d54995c9e1d4 594 m_puart->println(len);
itead 14:6a9414577daa 595 if (recvFind(">", 5000)) {
itead 7:d54995c9e1d4 596 m_puart->flush();
itead 7:d54995c9e1d4 597 for (uint32_t i = 0; i < len; i++) {
itead 7:d54995c9e1d4 598 m_puart->write(buffer[i]);
itead 7:d54995c9e1d4 599 }
itead 14:6a9414577daa 600 return recvFind("SEND OK", 10000);
itead 7:d54995c9e1d4 601 }
itead 6:18eb2d9fb2ac 602 return false;
itead 6:18eb2d9fb2ac 603 }
itead 9:dec2cf52b744 604 bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
itead 6:18eb2d9fb2ac 605 {
itead 7:d54995c9e1d4 606 m_puart->flush();
itead 7:d54995c9e1d4 607 m_puart->print("AT+CIPSEND=");
itead 7:d54995c9e1d4 608 m_puart->print(mux_id);
itead 7:d54995c9e1d4 609 m_puart->print(",");
itead 7:d54995c9e1d4 610 m_puart->println(len);
itead 14:6a9414577daa 611 if (recvFind(">", 5000)) {
itead 7:d54995c9e1d4 612 m_puart->flush();
itead 7:d54995c9e1d4 613 for (uint32_t i = 0; i < len; i++) {
itead 7:d54995c9e1d4 614 m_puart->write(buffer[i]);
itead 7:d54995c9e1d4 615 }
itead 14:6a9414577daa 616 return recvFind("SEND OK", 10000);
itead 7:d54995c9e1d4 617 }
itead 6:18eb2d9fb2ac 618 return false;
itead 6:18eb2d9fb2ac 619 }
itead 6:18eb2d9fb2ac 620 bool ESP8266::sATCIPCLOSEMulitple(uint8_t mux_id)
itead 6:18eb2d9fb2ac 621 {
itead 7:d54995c9e1d4 622 String data;
itead 7:d54995c9e1d4 623 m_puart->flush();
itead 7:d54995c9e1d4 624 m_puart->print("AT+CIPCLOSE=");
itead 7:d54995c9e1d4 625 m_puart->println(mux_id);
itead 7:d54995c9e1d4 626
itead 14:6a9414577daa 627 data = recvString("OK", "Link is not", 5000);
itead 7:d54995c9e1d4 628 if (data.indexOf("OK") != -1 || data.indexOf("Link is not") != -1) {
itead 7:d54995c9e1d4 629 return true;
itead 7:d54995c9e1d4 630 }
itead 6:18eb2d9fb2ac 631 return false;
itead 6:18eb2d9fb2ac 632 }
itead 6:18eb2d9fb2ac 633 bool ESP8266::eATCIPCLOSESingle(void)
itead 6:18eb2d9fb2ac 634 {
itead 7:d54995c9e1d4 635 m_puart->flush();
itead 7:d54995c9e1d4 636 m_puart->println("AT+CIPCLOSE");
itead 14:6a9414577daa 637 return recvFind("OK", 5000);
itead 6:18eb2d9fb2ac 638 }
itead 6:18eb2d9fb2ac 639 bool ESP8266::eATCIFSR(String &list)
itead 6:18eb2d9fb2ac 640 {
itead 7:d54995c9e1d4 641 m_puart->flush();
itead 7:d54995c9e1d4 642 m_puart->println("AT+CIFSR");
itead 7:d54995c9e1d4 643 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
itead 6:18eb2d9fb2ac 644 }
itead 6:18eb2d9fb2ac 645 bool ESP8266::sATCIPMUX(uint8_t mode)
itead 6:18eb2d9fb2ac 646 {
itead 7:d54995c9e1d4 647 String data;
itead 7:d54995c9e1d4 648 m_puart->flush();
itead 7:d54995c9e1d4 649 m_puart->print("AT+CIPMUX=");
itead 7:d54995c9e1d4 650 m_puart->println(mode);
itead 7:d54995c9e1d4 651
itead 7:d54995c9e1d4 652 data = recvString("OK", "Link is builded");
itead 7:d54995c9e1d4 653 if (data.indexOf("OK") != -1) {
itead 7:d54995c9e1d4 654 return true;
itead 7:d54995c9e1d4 655 }
itead 6:18eb2d9fb2ac 656 return false;
itead 6:18eb2d9fb2ac 657 }
itead 6:18eb2d9fb2ac 658 bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port)
itead 6:18eb2d9fb2ac 659 {
itead 7:d54995c9e1d4 660 String data;
itead 7:d54995c9e1d4 661 if (mode) {
itead 7:d54995c9e1d4 662 m_puart->flush();
itead 7:d54995c9e1d4 663 m_puart->print("AT+CIPSERVER=1,");
itead 7:d54995c9e1d4 664 m_puart->println(port);
itead 7:d54995c9e1d4 665
itead 7:d54995c9e1d4 666 data = recvString("OK", "no change");
itead 7:d54995c9e1d4 667 if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
itead 7:d54995c9e1d4 668 return true;
itead 7:d54995c9e1d4 669 }
itead 7:d54995c9e1d4 670 return false;
itead 7:d54995c9e1d4 671 } else {
itead 7:d54995c9e1d4 672 m_puart->flush();
itead 7:d54995c9e1d4 673 m_puart->println("AT+CIPSERVER=0");
itead 8:60d15efb4a1f 674 return recvFind("\r\r\n");
itead 7:d54995c9e1d4 675 }
itead 6:18eb2d9fb2ac 676 }
itead 6:18eb2d9fb2ac 677 bool ESP8266::sATCIPSTO(uint32_t timeout)
itead 6:18eb2d9fb2ac 678 {
itead 7:d54995c9e1d4 679 m_puart->flush();
itead 7:d54995c9e1d4 680 m_puart->print("AT+CIPSTO=");
itead 7:d54995c9e1d4 681 m_puart->println(timeout);
itead 7:d54995c9e1d4 682 return recvFind("OK");
itead 6:18eb2d9fb2ac 683 }