Dopasowany do naszego firmware'u

Dependencies:   ArduinoAPI

Fork of WeeESP8266 by ITEAD STUDIO

Committer:
itead
Date:
Sat Feb 07 05:24:08 2015 +0000
Revision:
9:dec2cf52b744
Parent:
8:60d15efb4a1f
Child:
10:002615561b0b
All code done!

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 5:5fa655d7212c 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 m_puart->setTimeout(1000);
itead 5:5fa655d7212c 29 logDebug("Create Instance ok");
itead 5:5fa655d7212c 30 }
itead 5:5fa655d7212c 31
itead 5:5fa655d7212c 32
itead 5:5fa655d7212c 33 bool ESP8266::kick(void)
itead 5:5fa655d7212c 34 {
itead 5:5fa655d7212c 35 return eAT();
itead 5:5fa655d7212c 36 }
itead 5:5fa655d7212c 37
itead 5:5fa655d7212c 38 bool ESP8266::restart(void)
itead 5:5fa655d7212c 39 {
itead 5:5fa655d7212c 40 unsigned long start;
itead 5:5fa655d7212c 41 if (eATRST()) {
itead 5:5fa655d7212c 42 delay(2000);
itead 5:5fa655d7212c 43 start = millis();
itead 5:5fa655d7212c 44 while (millis() - start < 3000) {
itead 5:5fa655d7212c 45 if (eAT()) {
itead 9:dec2cf52b744 46 delay(1500); /* Waiting for stable */
itead 5:5fa655d7212c 47 return true;
itead 5:5fa655d7212c 48 }
itead 5:5fa655d7212c 49 delay(100);
itead 5:5fa655d7212c 50 }
itead 5:5fa655d7212c 51 }
itead 5:5fa655d7212c 52 return false;
itead 5:5fa655d7212c 53 }
itead 5:5fa655d7212c 54
itead 5:5fa655d7212c 55 String ESP8266::getVersion(void)
itead 5:5fa655d7212c 56 {
itead 6:18eb2d9fb2ac 57 String version;
itead 6:18eb2d9fb2ac 58 eATGMR(version);
itead 6:18eb2d9fb2ac 59 return version;
itead 5:5fa655d7212c 60 }
itead 5:5fa655d7212c 61
itead 5:5fa655d7212c 62 bool ESP8266::setOprToStation(void)
itead 5:5fa655d7212c 63 {
itead 5:5fa655d7212c 64 if (sATCWMODE(1) && restart()) {
itead 5:5fa655d7212c 65 return true;
itead 5:5fa655d7212c 66 }
itead 5:5fa655d7212c 67 return false;
itead 5:5fa655d7212c 68 }
itead 5:5fa655d7212c 69
itead 5:5fa655d7212c 70 bool ESP8266::setOprToSoftAP(void)
itead 5:5fa655d7212c 71 {
itead 5:5fa655d7212c 72 if (sATCWMODE(2) && restart()) {
itead 5:5fa655d7212c 73 return true;
itead 5:5fa655d7212c 74 }
itead 5:5fa655d7212c 75 return false;
itead 5:5fa655d7212c 76 }
itead 5:5fa655d7212c 77
itead 5:5fa655d7212c 78 bool ESP8266::setOprToStationSoftAP(void)
itead 5:5fa655d7212c 79 {
itead 5:5fa655d7212c 80 if (sATCWMODE(3) && restart()) {
itead 5:5fa655d7212c 81 return true;
itead 5:5fa655d7212c 82 }
itead 5:5fa655d7212c 83 return false;
itead 5:5fa655d7212c 84 }
itead 5:5fa655d7212c 85
itead 5:5fa655d7212c 86 String ESP8266::getAPList(void)
itead 5:5fa655d7212c 87 {
itead 9:dec2cf52b744 88 String list;
itead 9:dec2cf52b744 89 eATCWLAP(list);
itead 9:dec2cf52b744 90 return list;
itead 5:5fa655d7212c 91 }
itead 5:5fa655d7212c 92
itead 5:5fa655d7212c 93 bool ESP8266::joinAP(String ssid, String pwd)
itead 5:5fa655d7212c 94 {
itead 9:dec2cf52b744 95 return sATCWJAP(ssid, pwd);
itead 5:5fa655d7212c 96 }
itead 5:5fa655d7212c 97
itead 5:5fa655d7212c 98 bool ESP8266::leaveAP(void)
itead 5:5fa655d7212c 99 {
itead 9:dec2cf52b744 100 return eATCWQAP();
itead 5:5fa655d7212c 101 }
itead 5:5fa655d7212c 102
itead 5:5fa655d7212c 103 bool ESP8266::setSoftAPParam(String ssid, String pwd, uint8_t chl, uint8_t ecn)
itead 5:5fa655d7212c 104 {
itead 9:dec2cf52b744 105 return sATCWSAP(ssid, pwd, chl, ecn);
itead 5:5fa655d7212c 106 }
itead 5:5fa655d7212c 107
itead 5:5fa655d7212c 108 String ESP8266::getJoinedDeviceIP(void)
itead 5:5fa655d7212c 109 {
itead 9:dec2cf52b744 110 String list;
itead 9:dec2cf52b744 111 eATCWLIF(list);
itead 9:dec2cf52b744 112 return list;
itead 5:5fa655d7212c 113 }
itead 5:5fa655d7212c 114
itead 5:5fa655d7212c 115 String ESP8266::getIPStatus(void)
itead 5:5fa655d7212c 116 {
itead 9:dec2cf52b744 117 String list;
itead 9:dec2cf52b744 118 eATCIPSTATUS(list);
itead 9:dec2cf52b744 119 return list;
itead 5:5fa655d7212c 120 }
itead 5:5fa655d7212c 121
itead 5:5fa655d7212c 122 String ESP8266::getLocalIP(void)
itead 5:5fa655d7212c 123 {
itead 9:dec2cf52b744 124 String list;
itead 9:dec2cf52b744 125 eATCIFSR(list);
itead 9:dec2cf52b744 126 return list;
itead 5:5fa655d7212c 127 }
itead 5:5fa655d7212c 128
itead 5:5fa655d7212c 129 bool ESP8266::enableMUX(void)
itead 5:5fa655d7212c 130 {
itead 9:dec2cf52b744 131 return sATCIPMUX(1);
itead 5:5fa655d7212c 132 }
itead 5:5fa655d7212c 133
itead 5:5fa655d7212c 134 bool ESP8266::disableMUX(void)
itead 5:5fa655d7212c 135 {
itead 9:dec2cf52b744 136 return sATCIPMUX(0);
itead 5:5fa655d7212c 137 }
itead 5:5fa655d7212c 138
itead 5:5fa655d7212c 139 bool ESP8266::createTCP(String addr, uint32_t port)
itead 5:5fa655d7212c 140 {
itead 9:dec2cf52b744 141 return sATCIPSTARTSingle("TCP", addr, port);
itead 5:5fa655d7212c 142 }
itead 5:5fa655d7212c 143
itead 5:5fa655d7212c 144 bool ESP8266::releaseTCP(void)
itead 5:5fa655d7212c 145 {
itead 9:dec2cf52b744 146 return eATCIPCLOSESingle();
itead 5:5fa655d7212c 147 }
itead 5:5fa655d7212c 148
itead 5:5fa655d7212c 149 bool ESP8266::registerUDP(String addr, uint32_t port)
itead 5:5fa655d7212c 150 {
itead 9:dec2cf52b744 151 return sATCIPSTARTSingle("UDP", addr, port);
itead 5:5fa655d7212c 152 }
itead 5:5fa655d7212c 153
itead 5:5fa655d7212c 154 bool ESP8266::unregisterUDP(void)
itead 5:5fa655d7212c 155 {
itead 9:dec2cf52b744 156 return eATCIPCLOSESingle();
itead 5:5fa655d7212c 157 }
itead 5:5fa655d7212c 158
itead 5:5fa655d7212c 159 bool ESP8266::createTCP(uint8_t mux_id, String addr, uint32_t port)
itead 5:5fa655d7212c 160 {
itead 9:dec2cf52b744 161 return sATCIPSTARTMultiple(mux_id, "TCP", addr, port);
itead 5:5fa655d7212c 162 }
itead 5:5fa655d7212c 163
itead 5:5fa655d7212c 164 bool ESP8266::releaseTCP(uint8_t mux_id)
itead 5:5fa655d7212c 165 {
itead 9:dec2cf52b744 166 return sATCIPCLOSEMulitple(mux_id);
itead 5:5fa655d7212c 167 }
itead 5:5fa655d7212c 168
itead 5:5fa655d7212c 169 bool ESP8266::registerUDP(uint8_t mux_id, String addr, uint32_t port)
itead 5:5fa655d7212c 170 {
itead 9:dec2cf52b744 171 return sATCIPSTARTMultiple(mux_id, "UDP", addr, port);
itead 5:5fa655d7212c 172 }
itead 5:5fa655d7212c 173
itead 5:5fa655d7212c 174 bool ESP8266::unregisterUDP(uint8_t mux_id)
itead 5:5fa655d7212c 175 {
itead 9:dec2cf52b744 176 return sATCIPCLOSEMulitple(mux_id);
itead 5:5fa655d7212c 177 }
itead 5:5fa655d7212c 178
itead 5:5fa655d7212c 179 bool ESP8266::setTCPServerTimeout(uint32_t timeout)
itead 5:5fa655d7212c 180 {
itead 9:dec2cf52b744 181 return sATCIPSTO(timeout);
itead 5:5fa655d7212c 182 }
itead 5:5fa655d7212c 183
itead 5:5fa655d7212c 184 bool ESP8266::startTCPServer(uint32_t port)
itead 5:5fa655d7212c 185 {
itead 9:dec2cf52b744 186 if (enableMUX() && sATCIPSERVER(1, port)) {
itead 9:dec2cf52b744 187 return true;
itead 9:dec2cf52b744 188 }
itead 5:5fa655d7212c 189 return false;
itead 5:5fa655d7212c 190 }
itead 5:5fa655d7212c 191
itead 5:5fa655d7212c 192 bool ESP8266::stopTCPServer(void)
itead 5:5fa655d7212c 193 {
itead 9:dec2cf52b744 194 sATCIPSERVER(0);
itead 9:dec2cf52b744 195 restart();
itead 5:5fa655d7212c 196 return false;
itead 5:5fa655d7212c 197 }
itead 5:5fa655d7212c 198
itead 5:5fa655d7212c 199 bool ESP8266::send(const uint8_t *buffer, uint32_t len)
itead 5:5fa655d7212c 200 {
itead 9:dec2cf52b744 201 return sATCIPSENDSingle(buffer, len);
itead 9:dec2cf52b744 202 }
itead 9:dec2cf52b744 203
itead 9:dec2cf52b744 204 bool ESP8266::send(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
itead 9:dec2cf52b744 205 {
itead 9:dec2cf52b744 206 return sATCIPSENDMultiple(mux_id, buffer, len);
itead 9:dec2cf52b744 207 }
itead 9:dec2cf52b744 208
itead 9:dec2cf52b744 209 uint32_t ESP8266::recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
itead 9:dec2cf52b744 210 {
itead 9:dec2cf52b744 211 return recvPkg(buffer, buffer_size, NULL, timeout, NULL);
itead 5:5fa655d7212c 212 }
itead 5:5fa655d7212c 213
itead 9:dec2cf52b744 214 uint32_t ESP8266::recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
itead 5:5fa655d7212c 215 {
itead 9:dec2cf52b744 216 uint8_t id;
itead 9:dec2cf52b744 217 uint32_t ret;
itead 9:dec2cf52b744 218 ret = recvPkg(buffer, buffer_size, NULL, timeout, &id);
itead 9:dec2cf52b744 219 if (ret > 0 && id == mux_id) {
itead 9:dec2cf52b744 220 return ret;
itead 9:dec2cf52b744 221 }
itead 9:dec2cf52b744 222 return 0;
itead 9:dec2cf52b744 223 }
itead 9:dec2cf52b744 224
itead 9:dec2cf52b744 225 uint32_t ESP8266::recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout)
itead 9:dec2cf52b744 226 {
itead 9:dec2cf52b744 227 return recvPkg(buffer, buffer_size, NULL, timeout, coming_mux_id);
itead 5:5fa655d7212c 228 }
itead 5:5fa655d7212c 229
itead 9:dec2cf52b744 230 /*----------------------------------------------------------------------------*/
itead 5:5fa655d7212c 231
itead 9:dec2cf52b744 232 uint32_t ESP8266::recvPkg(uint8_t *buffer, uint8_t buffer_size, uint32_t *data_len, uint32_t timeout, uint8_t *coming_mux_id)
itead 5:5fa655d7212c 233 {
itead 9:dec2cf52b744 234 String data;
itead 9:dec2cf52b744 235 char a;
itead 9:dec2cf52b744 236 int32_t index_PIPDcomma = -1;
itead 9:dec2cf52b744 237 int32_t index_colon = -1; /* : */
itead 9:dec2cf52b744 238 int32_t index_comma = -1; /* , */
itead 9:dec2cf52b744 239 int8_t id;
itead 9:dec2cf52b744 240 int32_t len;
itead 9:dec2cf52b744 241 bool has_data = false;
itead 9:dec2cf52b744 242
itead 9:dec2cf52b744 243 unsigned long start = millis();
itead 9:dec2cf52b744 244 while (millis() - start < timeout) {
itead 9:dec2cf52b744 245 while(m_puart->available() > 0) {
itead 9:dec2cf52b744 246 a = m_puart->readChr();
itead 9:dec2cf52b744 247 data += a;
itead 9:dec2cf52b744 248 }
itead 9:dec2cf52b744 249
itead 9:dec2cf52b744 250 index_PIPDcomma = data.indexOf("+IPD,");
itead 9:dec2cf52b744 251 if (index_PIPDcomma != -1) {
itead 9:dec2cf52b744 252 index_colon = data.indexOf(':', index_PIPDcomma + 5);
itead 9:dec2cf52b744 253 if (index_colon != -1) {
itead 9:dec2cf52b744 254 index_comma = data.indexOf(',', index_PIPDcomma + 5);
itead 9:dec2cf52b744 255 if (index_comma != -1) { /* +IPD,id,len:data */
itead 9:dec2cf52b744 256 id = data.substring(index_PIPDcomma + 5, index_comma).toInt();
itead 9:dec2cf52b744 257 if (id < 0 || id > 4) {
itead 9:dec2cf52b744 258 return 0;
itead 9:dec2cf52b744 259 }
itead 9:dec2cf52b744 260 len = data.substring(index_comma + 1, index_colon).toInt();
itead 9:dec2cf52b744 261 if (len <= 0) {
itead 9:dec2cf52b744 262 return 0;
itead 9:dec2cf52b744 263 }
itead 9:dec2cf52b744 264
itead 9:dec2cf52b744 265 } else { /* +IPD,len:data */
itead 9:dec2cf52b744 266 len = data.substring(index_PIPDcomma + 5, index_colon).toInt();
itead 9:dec2cf52b744 267 if (len <= 0) {
itead 9:dec2cf52b744 268 return 0;
itead 9:dec2cf52b744 269 }
itead 9:dec2cf52b744 270 }
itead 9:dec2cf52b744 271 has_data = true;
itead 9:dec2cf52b744 272 break;
itead 9:dec2cf52b744 273 }
itead 9:dec2cf52b744 274 }
itead 9:dec2cf52b744 275 }
itead 9:dec2cf52b744 276
itead 9:dec2cf52b744 277 if (has_data) {
itead 9:dec2cf52b744 278 start = millis();
itead 9:dec2cf52b744 279 while (millis() - start < timeout) {
itead 9:dec2cf52b744 280 while(m_puart->available() > 0) {
itead 9:dec2cf52b744 281 a = m_puart->readChr();
itead 9:dec2cf52b744 282 data += a;
itead 9:dec2cf52b744 283 }
itead 9:dec2cf52b744 284 if (data.length() >= index_colon + 1 + len) {
itead 9:dec2cf52b744 285 if (data_len) {
itead 9:dec2cf52b744 286 *data_len = len;
itead 9:dec2cf52b744 287 }
itead 9:dec2cf52b744 288 if (index_comma != -1 && coming_mux_id) {
itead 9:dec2cf52b744 289 *coming_mux_id = id;
itead 9:dec2cf52b744 290 }
itead 9:dec2cf52b744 291 uint32_t ret = len > buffer_size ? buffer_size : len;
itead 9:dec2cf52b744 292 memcpy(buffer,
itead 9:dec2cf52b744 293 data.substring(index_colon + 1, index_colon + 1 + len).c_str(),
itead 9:dec2cf52b744 294 ret);
itead 9:dec2cf52b744 295 return ret;
itead 9:dec2cf52b744 296 }
itead 9:dec2cf52b744 297 }
itead 9:dec2cf52b744 298 }
itead 9:dec2cf52b744 299
itead 9:dec2cf52b744 300 return 0;
itead 5:5fa655d7212c 301 }
itead 5:5fa655d7212c 302
itead 6:18eb2d9fb2ac 303 String ESP8266::recvString(String target, uint32_t timeout)
itead 5:5fa655d7212c 304 {
itead 5:5fa655d7212c 305 String data;
itead 5:5fa655d7212c 306 char a;
itead 5:5fa655d7212c 307 unsigned long start = millis();
itead 5:5fa655d7212c 308 while (millis() - start < timeout) {
itead 5:5fa655d7212c 309 while(m_puart->available() > 0) {
itead 5:5fa655d7212c 310 a = m_puart->readChr();
itead 5:5fa655d7212c 311 data += a;
itead 5:5fa655d7212c 312 }
itead 5:5fa655d7212c 313 if (data.indexOf(target) != -1) {
itead 5:5fa655d7212c 314 break;
itead 5:5fa655d7212c 315 }
itead 5:5fa655d7212c 316 }
itead 5:5fa655d7212c 317 return data;
itead 5:5fa655d7212c 318 }
itead 5:5fa655d7212c 319
itead 6:18eb2d9fb2ac 320 String ESP8266::recvString(String target1, String target2, uint32_t timeout)
itead 5:5fa655d7212c 321 {
itead 5:5fa655d7212c 322 String data;
itead 5:5fa655d7212c 323 char a;
itead 5:5fa655d7212c 324 unsigned long start = millis();
itead 5:5fa655d7212c 325 while (millis() - start < timeout) {
itead 5:5fa655d7212c 326 while(m_puart->available() > 0) {
itead 5:5fa655d7212c 327 a = m_puart->readChr();
itead 5:5fa655d7212c 328 data += a;
itead 5:5fa655d7212c 329 }
itead 5:5fa655d7212c 330 if (data.indexOf(target1) != -1) {
itead 5:5fa655d7212c 331 break;
itead 5:5fa655d7212c 332 } else if (data.indexOf(target2) != -1) {
itead 5:5fa655d7212c 333 break;
itead 5:5fa655d7212c 334 }
itead 5:5fa655d7212c 335 }
itead 5:5fa655d7212c 336 return data;
itead 5:5fa655d7212c 337 }
itead 5:5fa655d7212c 338
itead 6:18eb2d9fb2ac 339 String ESP8266::recvString(String target1, String target2, String target3, uint32_t timeout)
itead 5:5fa655d7212c 340 {
itead 5:5fa655d7212c 341 String data;
itead 5:5fa655d7212c 342 char a;
itead 5:5fa655d7212c 343 unsigned long start = millis();
itead 5:5fa655d7212c 344 while (millis() - start < timeout) {
itead 5:5fa655d7212c 345 while(m_puart->available() > 0) {
itead 5:5fa655d7212c 346 a = m_puart->readChr();
itead 5:5fa655d7212c 347 data += a;
itead 5:5fa655d7212c 348 }
itead 5:5fa655d7212c 349 if (data.indexOf(target1) != -1) {
itead 5:5fa655d7212c 350 break;
itead 5:5fa655d7212c 351 } else if (data.indexOf(target2) != -1) {
itead 5:5fa655d7212c 352 break;
itead 5:5fa655d7212c 353 } else if (data.indexOf(target3) != -1) {
itead 5:5fa655d7212c 354 break;
itead 5:5fa655d7212c 355 }
itead 5:5fa655d7212c 356 }
itead 5:5fa655d7212c 357 return data;
itead 5:5fa655d7212c 358 }
itead 5:5fa655d7212c 359
itead 6:18eb2d9fb2ac 360 bool ESP8266::recvFind(String target, uint32_t timeout)
itead 6:18eb2d9fb2ac 361 {
itead 6:18eb2d9fb2ac 362 String data_tmp;
itead 6:18eb2d9fb2ac 363 data_tmp = recvString(target, timeout);
itead 6:18eb2d9fb2ac 364 if (data_tmp.indexOf(target) != -1) {
itead 6:18eb2d9fb2ac 365 return true;
itead 6:18eb2d9fb2ac 366 }
itead 6:18eb2d9fb2ac 367 return false;
itead 6:18eb2d9fb2ac 368 }
itead 6:18eb2d9fb2ac 369
itead 6:18eb2d9fb2ac 370 bool ESP8266::recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout)
itead 6:18eb2d9fb2ac 371 {
itead 6:18eb2d9fb2ac 372 String data_tmp;
itead 6:18eb2d9fb2ac 373 data_tmp = recvString(target, timeout);
itead 6:18eb2d9fb2ac 374 if (data_tmp.indexOf(target) != -1) {
itead 6:18eb2d9fb2ac 375 int32_t index1 = data_tmp.indexOf(begin);
itead 6:18eb2d9fb2ac 376 int32_t index2 = data_tmp.indexOf(end);
itead 6:18eb2d9fb2ac 377 if (index1 != -1 && index2 != -1) {
itead 6:18eb2d9fb2ac 378 index1 += begin.length();
itead 6:18eb2d9fb2ac 379 data = data_tmp.substring(index1, index2);
itead 6:18eb2d9fb2ac 380 return true;
itead 6:18eb2d9fb2ac 381 }
itead 6:18eb2d9fb2ac 382 }
itead 6:18eb2d9fb2ac 383 data = "";
itead 6:18eb2d9fb2ac 384 return false;
itead 6:18eb2d9fb2ac 385 }
itead 5:5fa655d7212c 386
itead 5:5fa655d7212c 387 bool ESP8266::eAT(void)
itead 5:5fa655d7212c 388 {
itead 5:5fa655d7212c 389 m_puart->flush();
itead 5:5fa655d7212c 390 m_puart->println("AT");
itead 6:18eb2d9fb2ac 391 return recvFind("OK");
itead 5:5fa655d7212c 392 }
itead 5:5fa655d7212c 393
itead 5:5fa655d7212c 394 bool ESP8266::eATRST(void)
itead 5:5fa655d7212c 395 {
itead 5:5fa655d7212c 396 m_puart->flush();
itead 5:5fa655d7212c 397 m_puart->println("AT+RST");
itead 6:18eb2d9fb2ac 398 return recvFind("OK");
itead 5:5fa655d7212c 399 }
itead 5:5fa655d7212c 400
itead 6:18eb2d9fb2ac 401 bool ESP8266::eATGMR(String &version)
itead 5:5fa655d7212c 402 {
itead 5:5fa655d7212c 403 m_puart->flush();
itead 5:5fa655d7212c 404 m_puart->println("AT+GMR");
itead 6:18eb2d9fb2ac 405 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", version);
itead 5:5fa655d7212c 406 }
itead 5:5fa655d7212c 407
itead 5:5fa655d7212c 408
itead 5:5fa655d7212c 409 bool ESP8266::sATCWMODE(uint8_t mode)
itead 5:5fa655d7212c 410 {
itead 5:5fa655d7212c 411 String data;
itead 5:5fa655d7212c 412 m_puart->flush();
itead 5:5fa655d7212c 413 m_puart->print("AT+CWMODE=");
itead 5:5fa655d7212c 414 m_puart->println(mode);
itead 7:d54995c9e1d4 415
itead 7:d54995c9e1d4 416 data = recvString("OK", "no change");
itead 7:d54995c9e1d4 417 if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
itead 7:d54995c9e1d4 418 return true;
itead 7:d54995c9e1d4 419 }
itead 7:d54995c9e1d4 420 return false;
itead 6:18eb2d9fb2ac 421 }
itead 6:18eb2d9fb2ac 422
itead 6:18eb2d9fb2ac 423 bool ESP8266::sATCWJAP(String ssid, String pwd)
itead 6:18eb2d9fb2ac 424 {
itead 6:18eb2d9fb2ac 425 String data;
itead 6:18eb2d9fb2ac 426 m_puart->flush();
itead 6:18eb2d9fb2ac 427 m_puart->print("AT+CWJAP=\"");
itead 6:18eb2d9fb2ac 428 m_puart->print(ssid);
itead 6:18eb2d9fb2ac 429 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 430 m_puart->print(pwd);
itead 6:18eb2d9fb2ac 431 m_puart->println("\"");
itead 6:18eb2d9fb2ac 432
itead 6:18eb2d9fb2ac 433 data = recvString("OK", "FAIL", 10000);
itead 6:18eb2d9fb2ac 434 if (data.indexOf("OK") != -1) {
itead 6:18eb2d9fb2ac 435 return true;
itead 6:18eb2d9fb2ac 436 }
itead 6:18eb2d9fb2ac 437 return false;
itead 6:18eb2d9fb2ac 438 }
itead 6:18eb2d9fb2ac 439
itead 6:18eb2d9fb2ac 440 bool ESP8266::eATCWLAP(String &list)
itead 6:18eb2d9fb2ac 441 {
itead 6:18eb2d9fb2ac 442 String data;
itead 6:18eb2d9fb2ac 443 m_puart->flush();
itead 6:18eb2d9fb2ac 444 m_puart->println("AT+CWLAP");
itead 6:18eb2d9fb2ac 445 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list, 10000);
itead 6:18eb2d9fb2ac 446 }
itead 6:18eb2d9fb2ac 447
itead 6:18eb2d9fb2ac 448 bool ESP8266::eATCWQAP(void)
itead 6:18eb2d9fb2ac 449 {
itead 6:18eb2d9fb2ac 450 String data;
itead 6:18eb2d9fb2ac 451 m_puart->flush();
itead 6:18eb2d9fb2ac 452 m_puart->println("AT+CWQAP");
itead 6:18eb2d9fb2ac 453 return recvFind("OK");
itead 6:18eb2d9fb2ac 454 }
itead 6:18eb2d9fb2ac 455
itead 6:18eb2d9fb2ac 456 bool ESP8266::sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn)
itead 6:18eb2d9fb2ac 457 {
itead 6:18eb2d9fb2ac 458 String data;
itead 6:18eb2d9fb2ac 459 m_puart->flush();
itead 6:18eb2d9fb2ac 460 m_puart->print("AT+CWSAP=\"");
itead 6:18eb2d9fb2ac 461 m_puart->print(ssid);
itead 6:18eb2d9fb2ac 462 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 463 m_puart->print(pwd);
itead 6:18eb2d9fb2ac 464 m_puart->print("\",");
itead 6:18eb2d9fb2ac 465 m_puart->print(chl);
itead 6:18eb2d9fb2ac 466 m_puart->print(",");
itead 6:18eb2d9fb2ac 467 m_puart->println(ecn);
itead 6:18eb2d9fb2ac 468
itead 6:18eb2d9fb2ac 469 data = recvString("OK", "ERROR", 5000);
itead 5:5fa655d7212c 470 if (data.indexOf("OK") != -1) {
itead 5:5fa655d7212c 471 return true;
itead 5:5fa655d7212c 472 }
itead 5:5fa655d7212c 473 return false;
itead 5:5fa655d7212c 474 }
itead 5:5fa655d7212c 475
itead 6:18eb2d9fb2ac 476 bool ESP8266::eATCWLIF(String &list)
itead 6:18eb2d9fb2ac 477 {
itead 6:18eb2d9fb2ac 478 String data;
itead 6:18eb2d9fb2ac 479 m_puart->flush();
itead 6:18eb2d9fb2ac 480 m_puart->println("AT+CWLIF");
itead 6:18eb2d9fb2ac 481 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
itead 6:18eb2d9fb2ac 482 }
itead 6:18eb2d9fb2ac 483 bool ESP8266::eATCIPSTATUS(String &list)
itead 6:18eb2d9fb2ac 484 {
itead 6:18eb2d9fb2ac 485 String data;
itead 6:18eb2d9fb2ac 486 m_puart->flush();
itead 6:18eb2d9fb2ac 487 m_puart->println("AT+CIPSTATUS");
itead 6:18eb2d9fb2ac 488 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
itead 6:18eb2d9fb2ac 489 }
itead 6:18eb2d9fb2ac 490 bool ESP8266::sATCIPSTARTSingle(String type, String addr, uint32_t port)
itead 6:18eb2d9fb2ac 491 {
itead 6:18eb2d9fb2ac 492 String data;
itead 6:18eb2d9fb2ac 493 m_puart->flush();
itead 6:18eb2d9fb2ac 494 m_puart->print("AT+CIPSTART=\"");
itead 6:18eb2d9fb2ac 495 m_puart->print(type);
itead 6:18eb2d9fb2ac 496 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 497 m_puart->print(addr);
itead 6:18eb2d9fb2ac 498 m_puart->print("\",");
itead 6:18eb2d9fb2ac 499 m_puart->println(port);
itead 6:18eb2d9fb2ac 500
itead 6:18eb2d9fb2ac 501 data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
itead 6:18eb2d9fb2ac 502 if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
itead 6:18eb2d9fb2ac 503 return true;
itead 6:18eb2d9fb2ac 504 }
itead 6:18eb2d9fb2ac 505 return false;
itead 6:18eb2d9fb2ac 506 }
itead 6:18eb2d9fb2ac 507 bool ESP8266::sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port)
itead 6:18eb2d9fb2ac 508 {
itead 6:18eb2d9fb2ac 509 String data;
itead 6:18eb2d9fb2ac 510 m_puart->flush();
itead 6:18eb2d9fb2ac 511 m_puart->print("AT+CIPSTART=");
itead 6:18eb2d9fb2ac 512 m_puart->print(mux_id);
itead 6:18eb2d9fb2ac 513 m_puart->print(",\"");
itead 6:18eb2d9fb2ac 514 m_puart->print(type);
itead 6:18eb2d9fb2ac 515 m_puart->print("\",\"");
itead 6:18eb2d9fb2ac 516 m_puart->print(addr);
itead 6:18eb2d9fb2ac 517 m_puart->print("\",");
itead 6:18eb2d9fb2ac 518 m_puart->println(port);
itead 6:18eb2d9fb2ac 519
itead 6:18eb2d9fb2ac 520 data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000);
itead 6:18eb2d9fb2ac 521 if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) {
itead 6:18eb2d9fb2ac 522 return true;
itead 6:18eb2d9fb2ac 523 }
itead 6:18eb2d9fb2ac 524 return false;
itead 6:18eb2d9fb2ac 525 }
itead 9:dec2cf52b744 526 bool ESP8266::sATCIPSENDSingle(const uint8_t *buffer, uint32_t len)
itead 6:18eb2d9fb2ac 527 {
itead 7:d54995c9e1d4 528 m_puart->flush();
itead 7:d54995c9e1d4 529 m_puart->print("AT+CIPSEND=");
itead 7:d54995c9e1d4 530 m_puart->println(len);
itead 7:d54995c9e1d4 531 if (recvFind(">")) {
itead 7:d54995c9e1d4 532 m_puart->flush();
itead 7:d54995c9e1d4 533 for (uint32_t i = 0; i < len; i++) {
itead 7:d54995c9e1d4 534 m_puart->write(buffer[i]);
itead 7:d54995c9e1d4 535 }
itead 7:d54995c9e1d4 536 return recvFind("SEND OK", 5000);
itead 7:d54995c9e1d4 537 }
itead 6:18eb2d9fb2ac 538 return false;
itead 6:18eb2d9fb2ac 539 }
itead 9:dec2cf52b744 540 bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len)
itead 6:18eb2d9fb2ac 541 {
itead 7:d54995c9e1d4 542 m_puart->flush();
itead 7:d54995c9e1d4 543 m_puart->print("AT+CIPSEND=");
itead 7:d54995c9e1d4 544 m_puart->print(mux_id);
itead 7:d54995c9e1d4 545 m_puart->print(",");
itead 7:d54995c9e1d4 546 m_puart->println(len);
itead 7:d54995c9e1d4 547 if (recvFind(">")) {
itead 7:d54995c9e1d4 548 m_puart->flush();
itead 7:d54995c9e1d4 549 for (uint32_t i = 0; i < len; i++) {
itead 7:d54995c9e1d4 550 m_puart->write(buffer[i]);
itead 7:d54995c9e1d4 551 }
itead 7:d54995c9e1d4 552 return recvFind("SEND OK", 5000);
itead 7:d54995c9e1d4 553 }
itead 6:18eb2d9fb2ac 554 return false;
itead 6:18eb2d9fb2ac 555 }
itead 6:18eb2d9fb2ac 556 bool ESP8266::sATCIPCLOSEMulitple(uint8_t mux_id)
itead 6:18eb2d9fb2ac 557 {
itead 7:d54995c9e1d4 558 String data;
itead 7:d54995c9e1d4 559 m_puart->flush();
itead 7:d54995c9e1d4 560 m_puart->print("AT+CIPCLOSE=");
itead 7:d54995c9e1d4 561 m_puart->println(mux_id);
itead 7:d54995c9e1d4 562
itead 7:d54995c9e1d4 563 data = recvString("OK", "Link is not", 3000);
itead 7:d54995c9e1d4 564 if (data.indexOf("OK") != -1 || data.indexOf("Link is not") != -1) {
itead 7:d54995c9e1d4 565 return true;
itead 7:d54995c9e1d4 566 }
itead 6:18eb2d9fb2ac 567 return false;
itead 6:18eb2d9fb2ac 568 }
itead 6:18eb2d9fb2ac 569 bool ESP8266::eATCIPCLOSESingle(void)
itead 6:18eb2d9fb2ac 570 {
itead 7:d54995c9e1d4 571 m_puart->flush();
itead 7:d54995c9e1d4 572 m_puart->println("AT+CIPCLOSE");
itead 7:d54995c9e1d4 573 return recvFind("OK", 3000);
itead 6:18eb2d9fb2ac 574 }
itead 6:18eb2d9fb2ac 575 bool ESP8266::eATCIFSR(String &list)
itead 6:18eb2d9fb2ac 576 {
itead 7:d54995c9e1d4 577 m_puart->flush();
itead 7:d54995c9e1d4 578 m_puart->println("AT+CIFSR");
itead 7:d54995c9e1d4 579 return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list);
itead 6:18eb2d9fb2ac 580 }
itead 6:18eb2d9fb2ac 581 bool ESP8266::sATCIPMUX(uint8_t mode)
itead 6:18eb2d9fb2ac 582 {
itead 7:d54995c9e1d4 583 String data;
itead 7:d54995c9e1d4 584 m_puart->flush();
itead 7:d54995c9e1d4 585 m_puart->print("AT+CIPMUX=");
itead 7:d54995c9e1d4 586 m_puart->println(mode);
itead 7:d54995c9e1d4 587
itead 7:d54995c9e1d4 588 data = recvString("OK", "Link is builded");
itead 7:d54995c9e1d4 589 if (data.indexOf("OK") != -1) {
itead 7:d54995c9e1d4 590 return true;
itead 7:d54995c9e1d4 591 }
itead 6:18eb2d9fb2ac 592 return false;
itead 6:18eb2d9fb2ac 593 }
itead 6:18eb2d9fb2ac 594 bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port)
itead 6:18eb2d9fb2ac 595 {
itead 7:d54995c9e1d4 596 String data;
itead 7:d54995c9e1d4 597 if (mode) {
itead 7:d54995c9e1d4 598 m_puart->flush();
itead 7:d54995c9e1d4 599 m_puart->print("AT+CIPSERVER=1,");
itead 7:d54995c9e1d4 600 m_puart->println(port);
itead 7:d54995c9e1d4 601
itead 7:d54995c9e1d4 602 data = recvString("OK", "no change");
itead 7:d54995c9e1d4 603 if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) {
itead 7:d54995c9e1d4 604 return true;
itead 7:d54995c9e1d4 605 }
itead 7:d54995c9e1d4 606 return false;
itead 7:d54995c9e1d4 607 } else {
itead 7:d54995c9e1d4 608 m_puart->flush();
itead 7:d54995c9e1d4 609 m_puart->println("AT+CIPSERVER=0");
itead 8:60d15efb4a1f 610 return recvFind("\r\r\n");
itead 7:d54995c9e1d4 611 }
itead 6:18eb2d9fb2ac 612 }
itead 6:18eb2d9fb2ac 613 bool ESP8266::sATCIPSTO(uint32_t timeout)
itead 6:18eb2d9fb2ac 614 {
itead 7:d54995c9e1d4 615 m_puart->flush();
itead 7:d54995c9e1d4 616 m_puart->print("AT+CIPSTO=");
itead 7:d54995c9e1d4 617 m_puart->println(timeout);
itead 7:d54995c9e1d4 618 return recvFind("OK");
itead 6:18eb2d9fb2ac 619 }