Dopasowany do naszego firmware'u
Dependencies: ArduinoAPI
Fork of WeeESP8266 by
ESP8266.cpp@22:27be62d9d01f, 2015-04-27 (annotated)
- Committer:
- jakpor
- Date:
- Mon Apr 27 18:37:27 2015 +0000
- Revision:
- 22:27be62d9d01f
- Parent:
- 17:60ab5d5686d3
Wifi - 90% skuteczno?ci
Who changed what in which revision?
User | Revision | Line number | New 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 | |
jakpor | 22:27be62d9d01f | 30 | void ESP8266::clear_serial() |
jakpor | 22:27be62d9d01f | 31 | { |
jakpor | 22:27be62d9d01f | 32 | m_puart->flush(); |
jakpor | 22:27be62d9d01f | 33 | // 510 zer (długosc bufora) |
jakpor | 22:27be62d9d01f | 34 | m_puart->println("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); |
jakpor | 22:27be62d9d01f | 35 | } |
jakpor | 22:27be62d9d01f | 36 | |
itead | 5:5fa655d7212c | 37 | |
itead | 5:5fa655d7212c | 38 | bool ESP8266::kick(void) |
itead | 5:5fa655d7212c | 39 | { |
itead | 5:5fa655d7212c | 40 | return eAT(); |
itead | 5:5fa655d7212c | 41 | } |
itead | 5:5fa655d7212c | 42 | |
itead | 5:5fa655d7212c | 43 | bool ESP8266::restart(void) |
itead | 5:5fa655d7212c | 44 | { |
itead | 5:5fa655d7212c | 45 | unsigned long start; |
itead | 5:5fa655d7212c | 46 | if (eATRST()) { |
itead | 5:5fa655d7212c | 47 | delay(2000); |
itead | 5:5fa655d7212c | 48 | start = millis(); |
itead | 5:5fa655d7212c | 49 | while (millis() - start < 3000) { |
itead | 5:5fa655d7212c | 50 | if (eAT()) { |
itead | 9:dec2cf52b744 | 51 | delay(1500); /* Waiting for stable */ |
itead | 5:5fa655d7212c | 52 | return true; |
itead | 5:5fa655d7212c | 53 | } |
itead | 5:5fa655d7212c | 54 | delay(100); |
itead | 5:5fa655d7212c | 55 | } |
itead | 5:5fa655d7212c | 56 | } |
itead | 5:5fa655d7212c | 57 | return false; |
itead | 5:5fa655d7212c | 58 | } |
itead | 5:5fa655d7212c | 59 | |
itead | 5:5fa655d7212c | 60 | String ESP8266::getVersion(void) |
itead | 5:5fa655d7212c | 61 | { |
itead | 6:18eb2d9fb2ac | 62 | String version; |
itead | 6:18eb2d9fb2ac | 63 | eATGMR(version); |
itead | 6:18eb2d9fb2ac | 64 | return version; |
itead | 5:5fa655d7212c | 65 | } |
itead | 5:5fa655d7212c | 66 | |
itead | 5:5fa655d7212c | 67 | bool ESP8266::setOprToStation(void) |
itead | 5:5fa655d7212c | 68 | { |
itead | 10:002615561b0b | 69 | uint8_t mode; |
itead | 10:002615561b0b | 70 | if (!qATCWMODE(&mode)) { |
itead | 10:002615561b0b | 71 | return false; |
itead | 10:002615561b0b | 72 | } |
itead | 10:002615561b0b | 73 | if (mode == 1) { |
itead | 5:5fa655d7212c | 74 | return true; |
itead | 10:002615561b0b | 75 | } else { |
itead | 10:002615561b0b | 76 | if (sATCWMODE(1) && restart()) { |
itead | 10:002615561b0b | 77 | return true; |
itead | 10:002615561b0b | 78 | } else { |
itead | 10:002615561b0b | 79 | return false; |
itead | 10:002615561b0b | 80 | } |
itead | 5:5fa655d7212c | 81 | } |
itead | 5:5fa655d7212c | 82 | } |
itead | 5:5fa655d7212c | 83 | |
itead | 5:5fa655d7212c | 84 | bool ESP8266::setOprToSoftAP(void) |
itead | 5:5fa655d7212c | 85 | { |
itead | 10:002615561b0b | 86 | uint8_t mode; |
itead | 10:002615561b0b | 87 | if (!qATCWMODE(&mode)) { |
itead | 10:002615561b0b | 88 | return false; |
itead | 10:002615561b0b | 89 | } |
itead | 10:002615561b0b | 90 | if (mode == 2) { |
itead | 5:5fa655d7212c | 91 | return true; |
itead | 10:002615561b0b | 92 | } else { |
itead | 10:002615561b0b | 93 | if (sATCWMODE(2) && restart()) { |
itead | 10:002615561b0b | 94 | return true; |
itead | 10:002615561b0b | 95 | } else { |
itead | 10:002615561b0b | 96 | return false; |
itead | 10:002615561b0b | 97 | } |
itead | 5:5fa655d7212c | 98 | } |
itead | 5:5fa655d7212c | 99 | } |
itead | 5:5fa655d7212c | 100 | |
itead | 5:5fa655d7212c | 101 | bool ESP8266::setOprToStationSoftAP(void) |
itead | 5:5fa655d7212c | 102 | { |
itead | 10:002615561b0b | 103 | uint8_t mode; |
itead | 10:002615561b0b | 104 | if (!qATCWMODE(&mode)) { |
itead | 10:002615561b0b | 105 | return false; |
itead | 10:002615561b0b | 106 | } |
itead | 10:002615561b0b | 107 | if (mode == 3) { |
itead | 5:5fa655d7212c | 108 | return true; |
itead | 10:002615561b0b | 109 | } else { |
itead | 10:002615561b0b | 110 | if (sATCWMODE(3) && restart()) { |
itead | 10:002615561b0b | 111 | return true; |
itead | 10:002615561b0b | 112 | } else { |
itead | 10:002615561b0b | 113 | return false; |
itead | 10:002615561b0b | 114 | } |
itead | 5:5fa655d7212c | 115 | } |
itead | 5:5fa655d7212c | 116 | } |
itead | 5:5fa655d7212c | 117 | |
itead | 5:5fa655d7212c | 118 | String ESP8266::getAPList(void) |
itead | 5:5fa655d7212c | 119 | { |
itead | 9:dec2cf52b744 | 120 | String list; |
itead | 9:dec2cf52b744 | 121 | eATCWLAP(list); |
itead | 9:dec2cf52b744 | 122 | return list; |
itead | 5:5fa655d7212c | 123 | } |
itead | 5:5fa655d7212c | 124 | |
itead | 5:5fa655d7212c | 125 | bool ESP8266::joinAP(String ssid, String pwd) |
itead | 5:5fa655d7212c | 126 | { |
itead | 9:dec2cf52b744 | 127 | return sATCWJAP(ssid, pwd); |
itead | 5:5fa655d7212c | 128 | } |
itead | 5:5fa655d7212c | 129 | |
itead | 5:5fa655d7212c | 130 | bool ESP8266::leaveAP(void) |
itead | 5:5fa655d7212c | 131 | { |
itead | 9:dec2cf52b744 | 132 | return eATCWQAP(); |
itead | 5:5fa655d7212c | 133 | } |
itead | 5:5fa655d7212c | 134 | |
itead | 5:5fa655d7212c | 135 | bool ESP8266::setSoftAPParam(String ssid, String pwd, uint8_t chl, uint8_t ecn) |
itead | 5:5fa655d7212c | 136 | { |
itead | 9:dec2cf52b744 | 137 | return sATCWSAP(ssid, pwd, chl, ecn); |
itead | 5:5fa655d7212c | 138 | } |
itead | 5:5fa655d7212c | 139 | |
itead | 5:5fa655d7212c | 140 | String ESP8266::getJoinedDeviceIP(void) |
itead | 5:5fa655d7212c | 141 | { |
itead | 9:dec2cf52b744 | 142 | String list; |
itead | 9:dec2cf52b744 | 143 | eATCWLIF(list); |
itead | 9:dec2cf52b744 | 144 | return list; |
itead | 5:5fa655d7212c | 145 | } |
itead | 5:5fa655d7212c | 146 | |
jakpor | 22:27be62d9d01f | 147 | int ESP8266::getIPStatus(void) |
itead | 5:5fa655d7212c | 148 | { |
jakpor | 22:27be62d9d01f | 149 | String komenda = "AT+CIPSTATUS"; |
jakpor | 22:27be62d9d01f | 150 | |
jakpor | 22:27be62d9d01f | 151 | String data; |
jakpor | 22:27be62d9d01f | 152 | char a; |
jakpor | 22:27be62d9d01f | 153 | unsigned long start; |
jakpor | 22:27be62d9d01f | 154 | m_puart->flush(); |
jakpor | 22:27be62d9d01f | 155 | clear_serial(); |
jakpor | 22:27be62d9d01f | 156 | m_puart -> println(komenda); |
jakpor | 22:27be62d9d01f | 157 | start = millis(); |
jakpor | 22:27be62d9d01f | 158 | while (millis() - start < 1000) { |
jakpor | 22:27be62d9d01f | 159 | while(m_puart ->available() > 0) { |
jakpor | 22:27be62d9d01f | 160 | a = m_puart ->readChr(); |
jakpor | 22:27be62d9d01f | 161 | data +=a; |
jakpor | 22:27be62d9d01f | 162 | } |
jakpor | 22:27be62d9d01f | 163 | if (data.indexOf("OK") != -1) { |
jakpor | 22:27be62d9d01f | 164 | break; |
jakpor | 22:27be62d9d01f | 165 | } |
jakpor | 22:27be62d9d01f | 166 | if (data.indexOf("ERROR") != -1 ) { |
jakpor | 22:27be62d9d01f | 167 | return -1; //jak zle to powtarzam; |
jakpor | 22:27be62d9d01f | 168 | } |
jakpor | 22:27be62d9d01f | 169 | } |
jakpor | 22:27be62d9d01f | 170 | char num; |
jakpor | 22:27be62d9d01f | 171 | num = data[21]; |
jakpor | 22:27be62d9d01f | 172 | return num-48; |
jakpor | 22:27be62d9d01f | 173 | // String list; |
jakpor | 22:27be62d9d01f | 174 | // eATCIPSTATUS(list); |
jakpor | 22:27be62d9d01f | 175 | // return list; |
itead | 5:5fa655d7212c | 176 | } |
itead | 5:5fa655d7212c | 177 | |
itead | 5:5fa655d7212c | 178 | String ESP8266::getLocalIP(void) |
itead | 5:5fa655d7212c | 179 | { |
itead | 9:dec2cf52b744 | 180 | String list; |
itead | 9:dec2cf52b744 | 181 | eATCIFSR(list); |
itead | 9:dec2cf52b744 | 182 | return list; |
itead | 5:5fa655d7212c | 183 | } |
itead | 5:5fa655d7212c | 184 | |
itead | 5:5fa655d7212c | 185 | bool ESP8266::enableMUX(void) |
itead | 5:5fa655d7212c | 186 | { |
itead | 9:dec2cf52b744 | 187 | return sATCIPMUX(1); |
itead | 5:5fa655d7212c | 188 | } |
itead | 5:5fa655d7212c | 189 | |
itead | 5:5fa655d7212c | 190 | bool ESP8266::disableMUX(void) |
itead | 5:5fa655d7212c | 191 | { |
itead | 9:dec2cf52b744 | 192 | return sATCIPMUX(0); |
itead | 5:5fa655d7212c | 193 | } |
itead | 5:5fa655d7212c | 194 | |
itead | 5:5fa655d7212c | 195 | bool ESP8266::createTCP(String addr, uint32_t port) |
itead | 5:5fa655d7212c | 196 | { |
itead | 9:dec2cf52b744 | 197 | return sATCIPSTARTSingle("TCP", addr, port); |
itead | 5:5fa655d7212c | 198 | } |
itead | 5:5fa655d7212c | 199 | |
itead | 5:5fa655d7212c | 200 | bool ESP8266::releaseTCP(void) |
itead | 5:5fa655d7212c | 201 | { |
itead | 9:dec2cf52b744 | 202 | return eATCIPCLOSESingle(); |
itead | 5:5fa655d7212c | 203 | } |
itead | 5:5fa655d7212c | 204 | |
itead | 5:5fa655d7212c | 205 | bool ESP8266::registerUDP(String addr, uint32_t port) |
itead | 5:5fa655d7212c | 206 | { |
itead | 9:dec2cf52b744 | 207 | return sATCIPSTARTSingle("UDP", addr, port); |
itead | 5:5fa655d7212c | 208 | } |
itead | 5:5fa655d7212c | 209 | |
itead | 5:5fa655d7212c | 210 | bool ESP8266::unregisterUDP(void) |
itead | 5:5fa655d7212c | 211 | { |
itead | 9:dec2cf52b744 | 212 | return eATCIPCLOSESingle(); |
itead | 5:5fa655d7212c | 213 | } |
itead | 5:5fa655d7212c | 214 | |
itead | 5:5fa655d7212c | 215 | bool ESP8266::createTCP(uint8_t mux_id, String addr, uint32_t port) |
itead | 5:5fa655d7212c | 216 | { |
itead | 9:dec2cf52b744 | 217 | return sATCIPSTARTMultiple(mux_id, "TCP", addr, port); |
itead | 5:5fa655d7212c | 218 | } |
itead | 5:5fa655d7212c | 219 | |
itead | 5:5fa655d7212c | 220 | bool ESP8266::releaseTCP(uint8_t mux_id) |
itead | 5:5fa655d7212c | 221 | { |
itead | 9:dec2cf52b744 | 222 | return sATCIPCLOSEMulitple(mux_id); |
itead | 5:5fa655d7212c | 223 | } |
itead | 5:5fa655d7212c | 224 | |
itead | 5:5fa655d7212c | 225 | bool ESP8266::registerUDP(uint8_t mux_id, String addr, uint32_t port) |
itead | 5:5fa655d7212c | 226 | { |
itead | 9:dec2cf52b744 | 227 | return sATCIPSTARTMultiple(mux_id, "UDP", addr, port); |
itead | 5:5fa655d7212c | 228 | } |
itead | 5:5fa655d7212c | 229 | |
itead | 5:5fa655d7212c | 230 | bool ESP8266::unregisterUDP(uint8_t mux_id) |
itead | 5:5fa655d7212c | 231 | { |
itead | 9:dec2cf52b744 | 232 | return sATCIPCLOSEMulitple(mux_id); |
itead | 5:5fa655d7212c | 233 | } |
itead | 5:5fa655d7212c | 234 | |
itead | 5:5fa655d7212c | 235 | bool ESP8266::setTCPServerTimeout(uint32_t timeout) |
itead | 5:5fa655d7212c | 236 | { |
itead | 9:dec2cf52b744 | 237 | return sATCIPSTO(timeout); |
itead | 5:5fa655d7212c | 238 | } |
itead | 5:5fa655d7212c | 239 | |
itead | 5:5fa655d7212c | 240 | bool ESP8266::startTCPServer(uint32_t port) |
itead | 5:5fa655d7212c | 241 | { |
itead | 13:81f2548212e5 | 242 | if (sATCIPSERVER(1, port)) { |
itead | 9:dec2cf52b744 | 243 | return true; |
itead | 9:dec2cf52b744 | 244 | } |
itead | 5:5fa655d7212c | 245 | return false; |
itead | 5:5fa655d7212c | 246 | } |
itead | 5:5fa655d7212c | 247 | |
itead | 5:5fa655d7212c | 248 | bool ESP8266::stopTCPServer(void) |
itead | 5:5fa655d7212c | 249 | { |
itead | 9:dec2cf52b744 | 250 | sATCIPSERVER(0); |
itead | 9:dec2cf52b744 | 251 | restart(); |
itead | 5:5fa655d7212c | 252 | return false; |
itead | 5:5fa655d7212c | 253 | } |
itead | 5:5fa655d7212c | 254 | |
itead | 5:5fa655d7212c | 255 | bool ESP8266::send(const uint8_t *buffer, uint32_t len) |
itead | 5:5fa655d7212c | 256 | { |
itead | 9:dec2cf52b744 | 257 | return sATCIPSENDSingle(buffer, len); |
itead | 9:dec2cf52b744 | 258 | } |
itead | 9:dec2cf52b744 | 259 | |
itead | 9:dec2cf52b744 | 260 | bool ESP8266::send(uint8_t mux_id, const uint8_t *buffer, uint32_t len) |
itead | 9:dec2cf52b744 | 261 | { |
itead | 9:dec2cf52b744 | 262 | return sATCIPSENDMultiple(mux_id, buffer, len); |
itead | 9:dec2cf52b744 | 263 | } |
itead | 9:dec2cf52b744 | 264 | |
itead | 9:dec2cf52b744 | 265 | uint32_t ESP8266::recv(uint8_t *buffer, uint32_t buffer_size, uint32_t timeout) |
itead | 9:dec2cf52b744 | 266 | { |
itead | 9:dec2cf52b744 | 267 | return recvPkg(buffer, buffer_size, NULL, timeout, NULL); |
itead | 5:5fa655d7212c | 268 | } |
itead | 5:5fa655d7212c | 269 | |
itead | 9:dec2cf52b744 | 270 | uint32_t ESP8266::recv(uint8_t mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout) |
itead | 5:5fa655d7212c | 271 | { |
itead | 9:dec2cf52b744 | 272 | uint8_t id; |
itead | 9:dec2cf52b744 | 273 | uint32_t ret; |
itead | 9:dec2cf52b744 | 274 | ret = recvPkg(buffer, buffer_size, NULL, timeout, &id); |
itead | 9:dec2cf52b744 | 275 | if (ret > 0 && id == mux_id) { |
itead | 9:dec2cf52b744 | 276 | return ret; |
itead | 9:dec2cf52b744 | 277 | } |
itead | 9:dec2cf52b744 | 278 | return 0; |
itead | 9:dec2cf52b744 | 279 | } |
itead | 9:dec2cf52b744 | 280 | |
itead | 9:dec2cf52b744 | 281 | uint32_t ESP8266::recv(uint8_t *coming_mux_id, uint8_t *buffer, uint32_t buffer_size, uint32_t timeout) |
itead | 9:dec2cf52b744 | 282 | { |
itead | 9:dec2cf52b744 | 283 | return recvPkg(buffer, buffer_size, NULL, timeout, coming_mux_id); |
itead | 5:5fa655d7212c | 284 | } |
itead | 5:5fa655d7212c | 285 | |
itead | 9:dec2cf52b744 | 286 | /*----------------------------------------------------------------------------*/ |
itead | 5:5fa655d7212c | 287 | |
itead | 15:5c5a85e984a8 | 288 | 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 | 289 | { |
itead | 9:dec2cf52b744 | 290 | String data; |
itead | 9:dec2cf52b744 | 291 | char a; |
itead | 9:dec2cf52b744 | 292 | int32_t index_PIPDcomma = -1; |
itead | 9:dec2cf52b744 | 293 | int32_t index_colon = -1; /* : */ |
itead | 9:dec2cf52b744 | 294 | int32_t index_comma = -1; /* , */ |
itead | 15:5c5a85e984a8 | 295 | int32_t len = -1; |
itead | 15:5c5a85e984a8 | 296 | int8_t id = -1; |
itead | 9:dec2cf52b744 | 297 | bool has_data = false; |
itead | 15:5c5a85e984a8 | 298 | logDebug("start recv pkg"); |
itead | 9:dec2cf52b744 | 299 | |
itead | 9:dec2cf52b744 | 300 | unsigned long start = millis(); |
itead | 9:dec2cf52b744 | 301 | while (millis() - start < timeout) { |
itead | 9:dec2cf52b744 | 302 | while(m_puart->available() > 0) { |
itead | 9:dec2cf52b744 | 303 | a = m_puart->readChr(); |
itead | 9:dec2cf52b744 | 304 | data += a; |
itead | 9:dec2cf52b744 | 305 | } |
itead | 9:dec2cf52b744 | 306 | |
itead | 9:dec2cf52b744 | 307 | index_PIPDcomma = data.indexOf("+IPD,"); |
itead | 9:dec2cf52b744 | 308 | if (index_PIPDcomma != -1) { |
itead | 15:5c5a85e984a8 | 309 | logDebug("index_PIPDcomma found = %d", index_PIPDcomma); |
itead | 9:dec2cf52b744 | 310 | index_colon = data.indexOf(':', index_PIPDcomma + 5); |
itead | 9:dec2cf52b744 | 311 | if (index_colon != -1) { |
itead | 15:5c5a85e984a8 | 312 | logDebug("data1 = %s\r\n", data.c_str()); |
itead | 15:5c5a85e984a8 | 313 | logDebug("index_colon found = %d", index_colon); |
itead | 15:5c5a85e984a8 | 314 | |
itead | 9:dec2cf52b744 | 315 | index_comma = data.indexOf(',', index_PIPDcomma + 5); |
itead | 15:5c5a85e984a8 | 316 | logDebug("index_comma found = %d", index_comma); |
itead | 15:5c5a85e984a8 | 317 | |
itead | 15:5c5a85e984a8 | 318 | if (index_comma != -1 && index_comma < index_colon) { /* +IPD,id,len:data */ |
itead | 15:5c5a85e984a8 | 319 | logDebug("id = %d", id); |
itead | 9:dec2cf52b744 | 320 | id = data.substring(index_PIPDcomma + 5, index_comma).toInt(); |
itead | 15:5c5a85e984a8 | 321 | logDebug("id = %d", id); |
itead | 9:dec2cf52b744 | 322 | if (id < 0 || id > 4) { |
itead | 9:dec2cf52b744 | 323 | return 0; |
itead | 9:dec2cf52b744 | 324 | } |
itead | 15:5c5a85e984a8 | 325 | logDebug("len = %d", len); |
itead | 9:dec2cf52b744 | 326 | len = data.substring(index_comma + 1, index_colon).toInt(); |
itead | 15:5c5a85e984a8 | 327 | logDebug("len = %d", len); |
itead | 9:dec2cf52b744 | 328 | if (len <= 0) { |
itead | 9:dec2cf52b744 | 329 | return 0; |
itead | 9:dec2cf52b744 | 330 | } |
itead | 15:5c5a85e984a8 | 331 | logDebug("has id"); |
itead | 9:dec2cf52b744 | 332 | } else { /* +IPD,len:data */ |
itead | 9:dec2cf52b744 | 333 | len = data.substring(index_PIPDcomma + 5, index_colon).toInt(); |
itead | 15:5c5a85e984a8 | 334 | logDebug("len = %d", len); |
itead | 9:dec2cf52b744 | 335 | if (len <= 0) { |
itead | 9:dec2cf52b744 | 336 | return 0; |
itead | 9:dec2cf52b744 | 337 | } |
itead | 15:5c5a85e984a8 | 338 | logDebug("no id"); |
itead | 9:dec2cf52b744 | 339 | } |
itead | 9:dec2cf52b744 | 340 | has_data = true; |
itead | 15:5c5a85e984a8 | 341 | logDebug("has_data true"); |
itead | 9:dec2cf52b744 | 342 | break; |
itead | 9:dec2cf52b744 | 343 | } |
itead | 9:dec2cf52b744 | 344 | } |
itead | 9:dec2cf52b744 | 345 | } |
itead | 15:5c5a85e984a8 | 346 | //logDebug("has_data = %u\r\n", has_data); |
itead | 15:5c5a85e984a8 | 347 | //logDebug("data2 = %s\r\n", data.c_str()); |
itead | 9:dec2cf52b744 | 348 | |
itead | 9:dec2cf52b744 | 349 | if (has_data) { |
itead | 9:dec2cf52b744 | 350 | start = millis(); |
itead | 17:60ab5d5686d3 | 351 | while (millis() - start < 3000) { |
itead | 9:dec2cf52b744 | 352 | while(m_puart->available() > 0) { |
itead | 9:dec2cf52b744 | 353 | a = m_puart->readChr(); |
itead | 9:dec2cf52b744 | 354 | data += a; |
itead | 9:dec2cf52b744 | 355 | } |
itead | 15:5c5a85e984a8 | 356 | //logDebug("data3 = %s\r\n", data.c_str()); |
itead | 9:dec2cf52b744 | 357 | if (data.length() >= index_colon + 1 + len) { |
itead | 9:dec2cf52b744 | 358 | if (data_len) { |
itead | 9:dec2cf52b744 | 359 | *data_len = len; |
itead | 9:dec2cf52b744 | 360 | } |
itead | 9:dec2cf52b744 | 361 | if (index_comma != -1 && coming_mux_id) { |
itead | 9:dec2cf52b744 | 362 | *coming_mux_id = id; |
itead | 9:dec2cf52b744 | 363 | } |
itead | 15:5c5a85e984a8 | 364 | //logDebug("len = %d", len); |
itead | 15:5c5a85e984a8 | 365 | //logDebug("buffer_size = %d", buffer_size); |
itead | 9:dec2cf52b744 | 366 | uint32_t ret = len > buffer_size ? buffer_size : len; |
itead | 15:5c5a85e984a8 | 367 | |
itead | 9:dec2cf52b744 | 368 | memcpy(buffer, |
itead | 9:dec2cf52b744 | 369 | data.substring(index_colon + 1, index_colon + 1 + len).c_str(), |
itead | 9:dec2cf52b744 | 370 | ret); |
itead | 15:5c5a85e984a8 | 371 | logDebug("ret = %u\r\n", ret); |
itead | 9:dec2cf52b744 | 372 | return ret; |
itead | 9:dec2cf52b744 | 373 | } |
itead | 9:dec2cf52b744 | 374 | } |
itead | 9:dec2cf52b744 | 375 | } |
itead | 15:5c5a85e984a8 | 376 | logDebug("end recv pkg"); |
itead | 9:dec2cf52b744 | 377 | return 0; |
itead | 5:5fa655d7212c | 378 | } |
itead | 5:5fa655d7212c | 379 | |
itead | 6:18eb2d9fb2ac | 380 | String ESP8266::recvString(String target, uint32_t timeout) |
itead | 5:5fa655d7212c | 381 | { |
itead | 5:5fa655d7212c | 382 | String data; |
itead | 5:5fa655d7212c | 383 | char a; |
itead | 5:5fa655d7212c | 384 | unsigned long start = millis(); |
itead | 5:5fa655d7212c | 385 | while (millis() - start < timeout) { |
itead | 5:5fa655d7212c | 386 | while(m_puart->available() > 0) { |
itead | 5:5fa655d7212c | 387 | a = m_puart->readChr(); |
itead | 5:5fa655d7212c | 388 | data += a; |
itead | 5:5fa655d7212c | 389 | } |
itead | 5:5fa655d7212c | 390 | if (data.indexOf(target) != -1) { |
itead | 5:5fa655d7212c | 391 | break; |
itead | 5:5fa655d7212c | 392 | } |
itead | 5:5fa655d7212c | 393 | } |
itead | 5:5fa655d7212c | 394 | return data; |
itead | 5:5fa655d7212c | 395 | } |
itead | 5:5fa655d7212c | 396 | |
itead | 6:18eb2d9fb2ac | 397 | String ESP8266::recvString(String target1, String target2, uint32_t timeout) |
itead | 5:5fa655d7212c | 398 | { |
itead | 5:5fa655d7212c | 399 | String data; |
itead | 5:5fa655d7212c | 400 | char a; |
itead | 5:5fa655d7212c | 401 | unsigned long start = millis(); |
itead | 5:5fa655d7212c | 402 | while (millis() - start < timeout) { |
itead | 5:5fa655d7212c | 403 | while(m_puart->available() > 0) { |
itead | 5:5fa655d7212c | 404 | a = m_puart->readChr(); |
itead | 5:5fa655d7212c | 405 | data += a; |
itead | 5:5fa655d7212c | 406 | } |
itead | 5:5fa655d7212c | 407 | if (data.indexOf(target1) != -1) { |
itead | 5:5fa655d7212c | 408 | break; |
itead | 5:5fa655d7212c | 409 | } else if (data.indexOf(target2) != -1) { |
itead | 5:5fa655d7212c | 410 | break; |
itead | 5:5fa655d7212c | 411 | } |
itead | 5:5fa655d7212c | 412 | } |
itead | 5:5fa655d7212c | 413 | return data; |
itead | 5:5fa655d7212c | 414 | } |
itead | 5:5fa655d7212c | 415 | |
itead | 6:18eb2d9fb2ac | 416 | String ESP8266::recvString(String target1, String target2, String target3, uint32_t timeout) |
itead | 5:5fa655d7212c | 417 | { |
itead | 5:5fa655d7212c | 418 | String data; |
itead | 5:5fa655d7212c | 419 | char a; |
itead | 5:5fa655d7212c | 420 | unsigned long start = millis(); |
itead | 5:5fa655d7212c | 421 | while (millis() - start < timeout) { |
itead | 5:5fa655d7212c | 422 | while(m_puart->available() > 0) { |
itead | 5:5fa655d7212c | 423 | a = m_puart->readChr(); |
itead | 5:5fa655d7212c | 424 | data += a; |
itead | 5:5fa655d7212c | 425 | } |
itead | 5:5fa655d7212c | 426 | if (data.indexOf(target1) != -1) { |
itead | 5:5fa655d7212c | 427 | break; |
itead | 5:5fa655d7212c | 428 | } else if (data.indexOf(target2) != -1) { |
itead | 5:5fa655d7212c | 429 | break; |
itead | 5:5fa655d7212c | 430 | } else if (data.indexOf(target3) != -1) { |
itead | 5:5fa655d7212c | 431 | break; |
itead | 5:5fa655d7212c | 432 | } |
itead | 5:5fa655d7212c | 433 | } |
itead | 5:5fa655d7212c | 434 | return data; |
itead | 5:5fa655d7212c | 435 | } |
itead | 5:5fa655d7212c | 436 | |
itead | 6:18eb2d9fb2ac | 437 | bool ESP8266::recvFind(String target, uint32_t timeout) |
itead | 6:18eb2d9fb2ac | 438 | { |
itead | 6:18eb2d9fb2ac | 439 | String data_tmp; |
itead | 6:18eb2d9fb2ac | 440 | data_tmp = recvString(target, timeout); |
itead | 6:18eb2d9fb2ac | 441 | if (data_tmp.indexOf(target) != -1) { |
itead | 6:18eb2d9fb2ac | 442 | return true; |
itead | 6:18eb2d9fb2ac | 443 | } |
itead | 6:18eb2d9fb2ac | 444 | return false; |
itead | 6:18eb2d9fb2ac | 445 | } |
itead | 6:18eb2d9fb2ac | 446 | |
itead | 6:18eb2d9fb2ac | 447 | bool ESP8266::recvFindAndFilter(String target, String begin, String end, String &data, uint32_t timeout) |
itead | 6:18eb2d9fb2ac | 448 | { |
itead | 6:18eb2d9fb2ac | 449 | String data_tmp; |
itead | 6:18eb2d9fb2ac | 450 | data_tmp = recvString(target, timeout); |
itead | 6:18eb2d9fb2ac | 451 | if (data_tmp.indexOf(target) != -1) { |
itead | 6:18eb2d9fb2ac | 452 | int32_t index1 = data_tmp.indexOf(begin); |
itead | 6:18eb2d9fb2ac | 453 | int32_t index2 = data_tmp.indexOf(end); |
itead | 6:18eb2d9fb2ac | 454 | if (index1 != -1 && index2 != -1) { |
itead | 6:18eb2d9fb2ac | 455 | index1 += begin.length(); |
itead | 6:18eb2d9fb2ac | 456 | data = data_tmp.substring(index1, index2); |
itead | 6:18eb2d9fb2ac | 457 | return true; |
itead | 6:18eb2d9fb2ac | 458 | } |
itead | 6:18eb2d9fb2ac | 459 | } |
itead | 6:18eb2d9fb2ac | 460 | data = ""; |
itead | 6:18eb2d9fb2ac | 461 | return false; |
itead | 6:18eb2d9fb2ac | 462 | } |
itead | 5:5fa655d7212c | 463 | |
itead | 5:5fa655d7212c | 464 | bool ESP8266::eAT(void) |
itead | 5:5fa655d7212c | 465 | { |
itead | 5:5fa655d7212c | 466 | m_puart->flush(); |
itead | 5:5fa655d7212c | 467 | m_puart->println("AT"); |
itead | 6:18eb2d9fb2ac | 468 | return recvFind("OK"); |
itead | 5:5fa655d7212c | 469 | } |
itead | 5:5fa655d7212c | 470 | |
itead | 5:5fa655d7212c | 471 | bool ESP8266::eATRST(void) |
itead | 5:5fa655d7212c | 472 | { |
itead | 5:5fa655d7212c | 473 | m_puart->flush(); |
itead | 5:5fa655d7212c | 474 | m_puart->println("AT+RST"); |
itead | 6:18eb2d9fb2ac | 475 | return recvFind("OK"); |
itead | 5:5fa655d7212c | 476 | } |
itead | 5:5fa655d7212c | 477 | |
itead | 6:18eb2d9fb2ac | 478 | bool ESP8266::eATGMR(String &version) |
itead | 5:5fa655d7212c | 479 | { |
itead | 5:5fa655d7212c | 480 | m_puart->flush(); |
itead | 5:5fa655d7212c | 481 | m_puart->println("AT+GMR"); |
itead | 6:18eb2d9fb2ac | 482 | return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", version); |
itead | 5:5fa655d7212c | 483 | } |
itead | 5:5fa655d7212c | 484 | |
itead | 10:002615561b0b | 485 | bool ESP8266::qATCWMODE(uint8_t *mode) |
itead | 10:002615561b0b | 486 | { |
itead | 10:002615561b0b | 487 | String str_mode; |
itead | 10:002615561b0b | 488 | bool ret; |
itead | 10:002615561b0b | 489 | if (!mode) { |
itead | 10:002615561b0b | 490 | return false; |
itead | 10:002615561b0b | 491 | } |
itead | 10:002615561b0b | 492 | m_puart->flush(); |
itead | 10:002615561b0b | 493 | m_puart->println("AT+CWMODE?"); |
itead | 10:002615561b0b | 494 | ret = recvFindAndFilter("OK", "+CWMODE:", "\r\n\r\nOK", str_mode); |
itead | 10:002615561b0b | 495 | if (ret) { |
itead | 10:002615561b0b | 496 | *mode = (uint8_t)str_mode.toInt(); |
itead | 10:002615561b0b | 497 | return true; |
itead | 10:002615561b0b | 498 | } else { |
itead | 10:002615561b0b | 499 | return false; |
itead | 10:002615561b0b | 500 | } |
itead | 10:002615561b0b | 501 | } |
itead | 5:5fa655d7212c | 502 | |
itead | 5:5fa655d7212c | 503 | bool ESP8266::sATCWMODE(uint8_t mode) |
itead | 5:5fa655d7212c | 504 | { |
itead | 5:5fa655d7212c | 505 | String data; |
itead | 5:5fa655d7212c | 506 | m_puart->flush(); |
itead | 5:5fa655d7212c | 507 | m_puart->print("AT+CWMODE="); |
itead | 5:5fa655d7212c | 508 | m_puart->println(mode); |
itead | 7:d54995c9e1d4 | 509 | |
itead | 7:d54995c9e1d4 | 510 | data = recvString("OK", "no change"); |
itead | 7:d54995c9e1d4 | 511 | if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) { |
itead | 7:d54995c9e1d4 | 512 | return true; |
itead | 7:d54995c9e1d4 | 513 | } |
itead | 7:d54995c9e1d4 | 514 | return false; |
itead | 6:18eb2d9fb2ac | 515 | } |
itead | 6:18eb2d9fb2ac | 516 | |
itead | 6:18eb2d9fb2ac | 517 | bool ESP8266::sATCWJAP(String ssid, String pwd) |
itead | 6:18eb2d9fb2ac | 518 | { |
itead | 6:18eb2d9fb2ac | 519 | String data; |
jakpor | 22:27be62d9d01f | 520 | String komunikat = ""; |
jakpor | 22:27be62d9d01f | 521 | komunikat += "AT+CWJAP=\""; |
jakpor | 22:27be62d9d01f | 522 | komunikat += ssid; |
jakpor | 22:27be62d9d01f | 523 | komunikat += "\",\""; |
jakpor | 22:27be62d9d01f | 524 | komunikat += pwd; |
jakpor | 22:27be62d9d01f | 525 | komunikat += "\""; |
jakpor | 22:27be62d9d01f | 526 | komunikat+="\r\n"; |
jakpor | 22:27be62d9d01f | 527 | // |
jakpor | 22:27be62d9d01f | 528 | // for(int i = 0; i<=komunikat.length(); i++){ |
jakpor | 22:27be62d9d01f | 529 | // m_puart->printf("%c",komunikat[i]); |
jakpor | 22:27be62d9d01f | 530 | // m_puart->flush(); |
jakpor | 22:27be62d9d01f | 531 | // } |
jakpor | 22:27be62d9d01f | 532 | |
jakpor | 22:27be62d9d01f | 533 | // m_puart->print("AT+CWJAP=\""); |
jakpor | 22:27be62d9d01f | 534 | // m_puart->print(ssid); |
jakpor | 22:27be62d9d01f | 535 | // m_puart->print("\",\""); |
jakpor | 22:27be62d9d01f | 536 | // m_puart->print(pwd); |
jakpor | 22:27be62d9d01f | 537 | |
itead | 6:18eb2d9fb2ac | 538 | m_puart->flush(); |
jakpor | 22:27be62d9d01f | 539 | clear_serial(); |
jakpor | 22:27be62d9d01f | 540 | m_puart->println(komunikat); |
itead | 6:18eb2d9fb2ac | 541 | |
jakpor | 22:27be62d9d01f | 542 | data = recvString("OK", "FAIL", 20000); |
itead | 6:18eb2d9fb2ac | 543 | if (data.indexOf("OK") != -1) { |
itead | 6:18eb2d9fb2ac | 544 | return true; |
itead | 6:18eb2d9fb2ac | 545 | } |
itead | 6:18eb2d9fb2ac | 546 | return false; |
itead | 6:18eb2d9fb2ac | 547 | } |
itead | 6:18eb2d9fb2ac | 548 | |
itead | 6:18eb2d9fb2ac | 549 | bool ESP8266::eATCWLAP(String &list) |
itead | 6:18eb2d9fb2ac | 550 | { |
itead | 6:18eb2d9fb2ac | 551 | String data; |
itead | 6:18eb2d9fb2ac | 552 | m_puart->flush(); |
itead | 6:18eb2d9fb2ac | 553 | m_puart->println("AT+CWLAP"); |
itead | 6:18eb2d9fb2ac | 554 | return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list, 10000); |
itead | 6:18eb2d9fb2ac | 555 | } |
itead | 6:18eb2d9fb2ac | 556 | |
itead | 6:18eb2d9fb2ac | 557 | bool ESP8266::eATCWQAP(void) |
itead | 6:18eb2d9fb2ac | 558 | { |
itead | 6:18eb2d9fb2ac | 559 | String data; |
itead | 6:18eb2d9fb2ac | 560 | m_puart->flush(); |
itead | 6:18eb2d9fb2ac | 561 | m_puart->println("AT+CWQAP"); |
itead | 6:18eb2d9fb2ac | 562 | return recvFind("OK"); |
itead | 6:18eb2d9fb2ac | 563 | } |
itead | 6:18eb2d9fb2ac | 564 | |
itead | 6:18eb2d9fb2ac | 565 | bool ESP8266::sATCWSAP(String ssid, String pwd, uint8_t chl, uint8_t ecn) |
itead | 6:18eb2d9fb2ac | 566 | { |
itead | 6:18eb2d9fb2ac | 567 | String data; |
itead | 6:18eb2d9fb2ac | 568 | m_puart->flush(); |
itead | 6:18eb2d9fb2ac | 569 | m_puart->print("AT+CWSAP=\""); |
itead | 6:18eb2d9fb2ac | 570 | m_puart->print(ssid); |
itead | 6:18eb2d9fb2ac | 571 | m_puart->print("\",\""); |
itead | 6:18eb2d9fb2ac | 572 | m_puart->print(pwd); |
itead | 6:18eb2d9fb2ac | 573 | m_puart->print("\","); |
itead | 6:18eb2d9fb2ac | 574 | m_puart->print(chl); |
itead | 6:18eb2d9fb2ac | 575 | m_puart->print(","); |
itead | 6:18eb2d9fb2ac | 576 | m_puart->println(ecn); |
itead | 6:18eb2d9fb2ac | 577 | |
itead | 6:18eb2d9fb2ac | 578 | data = recvString("OK", "ERROR", 5000); |
itead | 5:5fa655d7212c | 579 | if (data.indexOf("OK") != -1) { |
itead | 5:5fa655d7212c | 580 | return true; |
itead | 5:5fa655d7212c | 581 | } |
itead | 5:5fa655d7212c | 582 | return false; |
itead | 5:5fa655d7212c | 583 | } |
itead | 5:5fa655d7212c | 584 | |
itead | 6:18eb2d9fb2ac | 585 | bool ESP8266::eATCWLIF(String &list) |
itead | 6:18eb2d9fb2ac | 586 | { |
itead | 6:18eb2d9fb2ac | 587 | String data; |
itead | 6:18eb2d9fb2ac | 588 | m_puart->flush(); |
itead | 6:18eb2d9fb2ac | 589 | m_puart->println("AT+CWLIF"); |
itead | 6:18eb2d9fb2ac | 590 | return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list); |
itead | 6:18eb2d9fb2ac | 591 | } |
itead | 6:18eb2d9fb2ac | 592 | bool ESP8266::eATCIPSTATUS(String &list) |
itead | 6:18eb2d9fb2ac | 593 | { |
itead | 6:18eb2d9fb2ac | 594 | String data; |
itead | 17:60ab5d5686d3 | 595 | delay(100); |
itead | 6:18eb2d9fb2ac | 596 | m_puart->flush(); |
itead | 6:18eb2d9fb2ac | 597 | m_puart->println("AT+CIPSTATUS"); |
itead | 6:18eb2d9fb2ac | 598 | return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list); |
itead | 6:18eb2d9fb2ac | 599 | } |
itead | 6:18eb2d9fb2ac | 600 | bool ESP8266::sATCIPSTARTSingle(String type, String addr, uint32_t port) |
itead | 6:18eb2d9fb2ac | 601 | { |
itead | 6:18eb2d9fb2ac | 602 | String data; |
itead | 6:18eb2d9fb2ac | 603 | m_puart->flush(); |
itead | 6:18eb2d9fb2ac | 604 | m_puart->print("AT+CIPSTART=\""); |
itead | 6:18eb2d9fb2ac | 605 | m_puart->print(type); |
itead | 6:18eb2d9fb2ac | 606 | m_puart->print("\",\""); |
itead | 6:18eb2d9fb2ac | 607 | m_puart->print(addr); |
itead | 6:18eb2d9fb2ac | 608 | m_puart->print("\","); |
itead | 6:18eb2d9fb2ac | 609 | m_puart->println(port); |
itead | 6:18eb2d9fb2ac | 610 | |
itead | 6:18eb2d9fb2ac | 611 | data = recvString("OK", "ERROR", "ALREADY CONNECT", 10000); |
itead | 6:18eb2d9fb2ac | 612 | if (data.indexOf("OK") != -1 || data.indexOf("ALREADY CONNECT") != -1) { |
itead | 6:18eb2d9fb2ac | 613 | return true; |
itead | 6:18eb2d9fb2ac | 614 | } |
itead | 6:18eb2d9fb2ac | 615 | return false; |
itead | 6:18eb2d9fb2ac | 616 | } |
itead | 6:18eb2d9fb2ac | 617 | bool ESP8266::sATCIPSTARTMultiple(uint8_t mux_id, String type, String addr, uint32_t port) |
itead | 6:18eb2d9fb2ac | 618 | { |
itead | 6:18eb2d9fb2ac | 619 | String data; |
itead | 6:18eb2d9fb2ac | 620 | m_puart->flush(); |
jakpor | 22:27be62d9d01f | 621 | String komunikat = ""; |
jakpor | 22:27be62d9d01f | 622 | komunikat += "AT+CIPSTART="; |
jakpor | 22:27be62d9d01f | 623 | komunikat += mux_id; |
jakpor | 22:27be62d9d01f | 624 | komunikat += ",\""; |
jakpor | 22:27be62d9d01f | 625 | komunikat += type; |
jakpor | 22:27be62d9d01f | 626 | komunikat += "\",\""; |
jakpor | 22:27be62d9d01f | 627 | komunikat += addr; |
jakpor | 22:27be62d9d01f | 628 | komunikat += "\","; |
jakpor | 22:27be62d9d01f | 629 | komunikat += port; |
jakpor | 22:27be62d9d01f | 630 | komunikat+="\r\n"; |
jakpor | 22:27be62d9d01f | 631 | |
jakpor | 22:27be62d9d01f | 632 | m_puart->flush(); |
jakpor | 22:27be62d9d01f | 633 | clear_serial(); |
jakpor | 22:27be62d9d01f | 634 | m_puart->println(komunikat); |
jakpor | 22:27be62d9d01f | 635 | |
jakpor | 22:27be62d9d01f | 636 | |
jakpor | 22:27be62d9d01f | 637 | data = recvString("OK", "ERROR", "CONNECT", 10000); |
jakpor | 22:27be62d9d01f | 638 | if ((data.indexOf("OK") != -1) ||(data.indexOf("CONNECT")!= -1)|| (data.indexOf("ALREADY CONNECT") != -1)) { |
itead | 6:18eb2d9fb2ac | 639 | return true; |
itead | 6:18eb2d9fb2ac | 640 | } |
itead | 6:18eb2d9fb2ac | 641 | return false; |
itead | 6:18eb2d9fb2ac | 642 | } |
itead | 9:dec2cf52b744 | 643 | bool ESP8266::sATCIPSENDSingle(const uint8_t *buffer, uint32_t len) |
itead | 6:18eb2d9fb2ac | 644 | { |
itead | 7:d54995c9e1d4 | 645 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 646 | m_puart->print("AT+CIPSEND="); |
itead | 7:d54995c9e1d4 | 647 | m_puart->println(len); |
itead | 14:6a9414577daa | 648 | if (recvFind(">", 5000)) { |
itead | 7:d54995c9e1d4 | 649 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 650 | for (uint32_t i = 0; i < len; i++) { |
itead | 7:d54995c9e1d4 | 651 | m_puart->write(buffer[i]); |
itead | 7:d54995c9e1d4 | 652 | } |
itead | 14:6a9414577daa | 653 | return recvFind("SEND OK", 10000); |
itead | 7:d54995c9e1d4 | 654 | } |
itead | 6:18eb2d9fb2ac | 655 | return false; |
itead | 6:18eb2d9fb2ac | 656 | } |
itead | 9:dec2cf52b744 | 657 | bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, const uint8_t *buffer, uint32_t len) |
itead | 6:18eb2d9fb2ac | 658 | { |
itead | 7:d54995c9e1d4 | 659 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 660 | m_puart->print("AT+CIPSEND="); |
itead | 7:d54995c9e1d4 | 661 | m_puart->print(mux_id); |
itead | 7:d54995c9e1d4 | 662 | m_puart->print(","); |
itead | 7:d54995c9e1d4 | 663 | m_puart->println(len); |
itead | 14:6a9414577daa | 664 | if (recvFind(">", 5000)) { |
itead | 7:d54995c9e1d4 | 665 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 666 | for (uint32_t i = 0; i < len; i++) { |
itead | 7:d54995c9e1d4 | 667 | m_puart->write(buffer[i]); |
itead | 7:d54995c9e1d4 | 668 | } |
itead | 14:6a9414577daa | 669 | return recvFind("SEND OK", 10000); |
itead | 7:d54995c9e1d4 | 670 | } |
itead | 6:18eb2d9fb2ac | 671 | return false; |
itead | 6:18eb2d9fb2ac | 672 | } |
itead | 6:18eb2d9fb2ac | 673 | bool ESP8266::sATCIPCLOSEMulitple(uint8_t mux_id) |
itead | 6:18eb2d9fb2ac | 674 | { |
itead | 7:d54995c9e1d4 | 675 | String data; |
itead | 7:d54995c9e1d4 | 676 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 677 | m_puart->print("AT+CIPCLOSE="); |
itead | 7:d54995c9e1d4 | 678 | m_puart->println(mux_id); |
itead | 7:d54995c9e1d4 | 679 | |
itead | 14:6a9414577daa | 680 | data = recvString("OK", "Link is not", 5000); |
itead | 7:d54995c9e1d4 | 681 | if (data.indexOf("OK") != -1 || data.indexOf("Link is not") != -1) { |
itead | 7:d54995c9e1d4 | 682 | return true; |
itead | 7:d54995c9e1d4 | 683 | } |
itead | 6:18eb2d9fb2ac | 684 | return false; |
itead | 6:18eb2d9fb2ac | 685 | } |
itead | 6:18eb2d9fb2ac | 686 | bool ESP8266::eATCIPCLOSESingle(void) |
itead | 6:18eb2d9fb2ac | 687 | { |
itead | 7:d54995c9e1d4 | 688 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 689 | m_puart->println("AT+CIPCLOSE"); |
itead | 14:6a9414577daa | 690 | return recvFind("OK", 5000); |
itead | 6:18eb2d9fb2ac | 691 | } |
itead | 6:18eb2d9fb2ac | 692 | bool ESP8266::eATCIFSR(String &list) |
itead | 6:18eb2d9fb2ac | 693 | { |
itead | 7:d54995c9e1d4 | 694 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 695 | m_puart->println("AT+CIFSR"); |
itead | 7:d54995c9e1d4 | 696 | return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list); |
itead | 6:18eb2d9fb2ac | 697 | } |
itead | 6:18eb2d9fb2ac | 698 | bool ESP8266::sATCIPMUX(uint8_t mode) |
itead | 6:18eb2d9fb2ac | 699 | { |
itead | 7:d54995c9e1d4 | 700 | String data; |
itead | 7:d54995c9e1d4 | 701 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 702 | m_puart->print("AT+CIPMUX="); |
itead | 7:d54995c9e1d4 | 703 | m_puart->println(mode); |
itead | 7:d54995c9e1d4 | 704 | |
itead | 7:d54995c9e1d4 | 705 | data = recvString("OK", "Link is builded"); |
itead | 7:d54995c9e1d4 | 706 | if (data.indexOf("OK") != -1) { |
itead | 7:d54995c9e1d4 | 707 | return true; |
itead | 7:d54995c9e1d4 | 708 | } |
itead | 6:18eb2d9fb2ac | 709 | return false; |
itead | 6:18eb2d9fb2ac | 710 | } |
itead | 6:18eb2d9fb2ac | 711 | bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port) |
itead | 6:18eb2d9fb2ac | 712 | { |
itead | 7:d54995c9e1d4 | 713 | String data; |
itead | 7:d54995c9e1d4 | 714 | if (mode) { |
itead | 7:d54995c9e1d4 | 715 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 716 | m_puart->print("AT+CIPSERVER=1,"); |
itead | 7:d54995c9e1d4 | 717 | m_puart->println(port); |
itead | 7:d54995c9e1d4 | 718 | |
jakpor | 22:27be62d9d01f | 719 | data = recvString("OK", "no change",1000); |
itead | 7:d54995c9e1d4 | 720 | if (data.indexOf("OK") != -1 || data.indexOf("no change") != -1) { |
itead | 7:d54995c9e1d4 | 721 | return true; |
itead | 7:d54995c9e1d4 | 722 | } |
itead | 7:d54995c9e1d4 | 723 | return false; |
itead | 7:d54995c9e1d4 | 724 | } else { |
itead | 7:d54995c9e1d4 | 725 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 726 | m_puart->println("AT+CIPSERVER=0"); |
itead | 8:60d15efb4a1f | 727 | return recvFind("\r\r\n"); |
itead | 7:d54995c9e1d4 | 728 | } |
itead | 6:18eb2d9fb2ac | 729 | } |
itead | 6:18eb2d9fb2ac | 730 | bool ESP8266::sATCIPSTO(uint32_t timeout) |
itead | 6:18eb2d9fb2ac | 731 | { |
itead | 7:d54995c9e1d4 | 732 | m_puart->flush(); |
itead | 7:d54995c9e1d4 | 733 | m_puart->print("AT+CIPSTO="); |
itead | 7:d54995c9e1d4 | 734 | m_puart->println(timeout); |
itead | 7:d54995c9e1d4 | 735 | return recvFind("OK"); |
itead | 6:18eb2d9fb2ac | 736 | } |