now this shit works
Fork of ESP8266NodeMCUInterface by
ESP8266/ESP8266.cpp@13:41098c907200, 2014-11-30 (annotated)
- Committer:
- michaeljkoster
- Date:
- Sun Nov 30 21:14:09 2014 +0000
- Revision:
- 13:41098c907200
- Parent:
- Wifly/Wifly.cpp@12:c5f0eac67a8a
- Child:
- 14:4d1128f72e00
First commit function templates for lightweight UDP client interface to ESP8266
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:fb4494783863 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
samux | 1:fb4494783863 | 2 | * |
samux | 1:fb4494783863 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
samux | 1:fb4494783863 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
samux | 1:fb4494783863 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
samux | 1:fb4494783863 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
samux | 1:fb4494783863 | 7 | * furnished to do so, subject to the following conditions: |
samux | 1:fb4494783863 | 8 | * |
samux | 1:fb4494783863 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
samux | 1:fb4494783863 | 10 | * substantial portions of the Software. |
samux | 1:fb4494783863 | 11 | * |
samux | 1:fb4494783863 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
samux | 1:fb4494783863 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
samux | 1:fb4494783863 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
samux | 1:fb4494783863 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
samux | 1:fb4494783863 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
samux | 1:fb4494783863 | 17 | */ |
samux | 1:fb4494783863 | 18 | |
samux | 1:fb4494783863 | 19 | #include "mbed.h" |
michaeljkoster | 13:41098c907200 | 20 | #include "ESP8266.h" |
samux | 1:fb4494783863 | 21 | #include <string> |
samux | 1:fb4494783863 | 22 | #include <algorithm> |
samux | 1:fb4494783863 | 23 | |
samux | 1:fb4494783863 | 24 | //Debug is disabled by default |
screamer | 10:131675c17372 | 25 | #if (defined(DEBUG)) |
michaeljkoster | 13:41098c907200 | 26 | #define DBG(x, ...) std::printf("[ESP8266 : DBG]"x"\r\n", ##__VA_ARGS__); |
michaeljkoster | 13:41098c907200 | 27 | #define WARN(x, ...) std::printf("[ESP8266 : WARN]"x"\r\n", ##__VA_ARGS__); |
michaeljkoster | 13:41098c907200 | 28 | #define ERR(x, ...) std::printf("[ESP8266 : ERR]"x"\r\n", ##__VA_ARGS__); |
samux | 1:fb4494783863 | 29 | #else |
samux | 1:fb4494783863 | 30 | #define DBG(x, ...) |
samux | 1:fb4494783863 | 31 | #define WARN(x, ...) |
samux | 1:fb4494783863 | 32 | #define ERR(x, ...) |
samux | 1:fb4494783863 | 33 | #endif |
samux | 1:fb4494783863 | 34 | |
screamer | 10:131675c17372 | 35 | #if defined(DEBUG) |
michaeljkoster | 13:41098c907200 | 36 | #define INFO(x, ...) printf("[ESP8266 : INFO]"x"\r\n", ##__VA_ARGS__); |
samux | 1:fb4494783863 | 37 | #else |
samux | 1:fb4494783863 | 38 | #define INFO(x, ...) |
samux | 1:fb4494783863 | 39 | #endif |
samux | 1:fb4494783863 | 40 | |
samux | 1:fb4494783863 | 41 | #define MAX_TRY_JOIN 3 |
samux | 1:fb4494783863 | 42 | |
michaeljkoster | 12:c5f0eac67a8a | 43 | extern Serial pc; |
michaeljkoster | 12:c5f0eac67a8a | 44 | |
michaeljkoster | 13:41098c907200 | 45 | ESP8266 * ESP8266::inst; |
samux | 1:fb4494783863 | 46 | |
michaeljkoster | 13:41098c907200 | 47 | ESP8266::ESP8266( PinName tx, PinName rx, PinName _reset, const char * ssid, const char * phrase ): |
michaeljkoster | 13:41098c907200 | 48 | wifi(tx, rx), reset_pin(_reset), buf_ESP8266(256) |
samux | 1:fb4494783863 | 49 | { |
samux | 1:fb4494783863 | 50 | memset(&state, 0, sizeof(state)); |
samux | 1:fb4494783863 | 51 | |
samux | 1:fb4494783863 | 52 | // change all ' ' in '$' in the ssid and the passphrase |
samux | 1:fb4494783863 | 53 | strcpy(this->ssid, ssid); |
samux | 1:fb4494783863 | 54 | for (int i = 0; i < strlen(ssid); i++) { |
samux | 1:fb4494783863 | 55 | if (this->ssid[i] == ' ') |
samux | 1:fb4494783863 | 56 | this->ssid[i] = '$'; |
samux | 1:fb4494783863 | 57 | } |
samux | 1:fb4494783863 | 58 | strcpy(this->phrase, phrase); |
samux | 1:fb4494783863 | 59 | for (int i = 0; i < strlen(phrase); i++) { |
samux | 1:fb4494783863 | 60 | if (this->phrase[i] == ' ') |
samux | 1:fb4494783863 | 61 | this->phrase[i] = '$'; |
samux | 1:fb4494783863 | 62 | } |
samux | 1:fb4494783863 | 63 | |
samux | 1:fb4494783863 | 64 | inst = this; |
samux | 1:fb4494783863 | 65 | attach_rx(false); |
samux | 1:fb4494783863 | 66 | } |
samux | 1:fb4494783863 | 67 | |
michaeljkoster | 13:41098c907200 | 68 | bool ESP8266::join() |
samux | 1:fb4494783863 | 69 | { |
samux | 1:fb4494783863 | 70 | |
samux | 1:fb4494783863 | 71 | for (int i= 0; i < MAX_TRY_JOIN; i++) { |
samux | 2:8e54830d0df7 | 72 | |
samux | 1:fb4494783863 | 73 | //join the network |
samux | 1:fb4494783863 | 74 | state.associated = true; |
michaeljkoster | 13:41098c907200 | 75 | INFO("\r\nssid: %s\r\nphrase: %s\r\nsecurity: %s\r\n\r\n", this->ssid, this->phrase); |
samux | 1:fb4494783863 | 76 | return true; |
samux | 1:fb4494783863 | 77 | } |
samux | 1:fb4494783863 | 78 | return false; |
samux | 1:fb4494783863 | 79 | } |
samux | 1:fb4494783863 | 80 | |
michaeljkoster | 13:41098c907200 | 81 | bool ESP8266::connect() |
samux | 1:fb4494783863 | 82 | { |
michaeljkoster | 13:41098c907200 | 83 | return join(); |
samux | 1:fb4494783863 | 84 | } |
samux | 1:fb4494783863 | 85 | |
michaeljkoster | 13:41098c907200 | 86 | bool ESP8266::gethostbyname(const char * host, char * ip) |
samux | 1:fb4494783863 | 87 | { |
samux | 1:fb4494783863 | 88 | string h = host; |
samux | 1:fb4494783863 | 89 | int nb_digits = 0; |
samux | 1:fb4494783863 | 90 | |
samux | 1:fb4494783863 | 91 | // no dns needed |
samux | 1:fb4494783863 | 92 | int pos = h.find("."); |
samux | 1:fb4494783863 | 93 | if (pos != string::npos) { |
samux | 1:fb4494783863 | 94 | string sub = h.substr(0, h.find(".")); |
samux | 1:fb4494783863 | 95 | nb_digits = atoi(sub.c_str()); |
samux | 1:fb4494783863 | 96 | } |
samux | 1:fb4494783863 | 97 | //printf("substrL %s\r\n", sub.c_str()); |
samux | 1:fb4494783863 | 98 | if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { |
samux | 1:fb4494783863 | 99 | strcpy(ip, host); |
michaeljkoster | 13:41098c907200 | 100 | return true; |
samux | 1:fb4494783863 | 101 | } |
samux | 1:fb4494783863 | 102 | else { |
michaeljkoster | 13:41098c907200 | 103 | // dns needed, not currently available |
michaeljkoster | 13:41098c907200 | 104 | return false; |
michaeljkoster | 13:41098c907200 | 105 | } |
michaeljkoster | 13:41098c907200 | 106 | } |
samux | 1:fb4494783863 | 107 | |
michaeljkoster | 13:41098c907200 | 108 | void ESP8266::flush() |
michaeljkoster | 13:41098c907200 | 109 | { |
michaeljkoster | 13:41098c907200 | 110 | buf_ESP8266.flush(); |
michaeljkoster | 13:41098c907200 | 111 | } |
michaeljkoster | 13:41098c907200 | 112 | |
michaeljkoster | 13:41098c907200 | 113 | bool ESP8266::sendCommand(const char * cmd, const char * ack, char * res, int timeout) |
michaeljkoster | 13:41098c907200 | 114 | { |
samux | 1:fb4494783863 | 115 | return true; |
samux | 1:fb4494783863 | 116 | } |
samux | 1:fb4494783863 | 117 | |
michaeljkoster | 13:41098c907200 | 118 | bool ESP8266::close() |
samux | 1:fb4494783863 | 119 | { |
samux | 1:fb4494783863 | 120 | return true; |
samux | 1:fb4494783863 | 121 | } |
samux | 1:fb4494783863 | 122 | |
michaeljkoster | 13:41098c907200 | 123 | bool ESP8266::disconnect() |
samux | 1:fb4494783863 | 124 | { |
samux | 1:fb4494783863 | 125 | // if already disconnected, return |
samux | 1:fb4494783863 | 126 | if (!state.associated) |
samux | 1:fb4494783863 | 127 | return true; |
michaeljkoster | 13:41098c907200 | 128 | // send command to quit AP |
michaeljkoster | 13:41098c907200 | 129 | // |
samux | 1:fb4494783863 | 130 | state.associated = false; |
samux | 1:fb4494783863 | 131 | return true; |
samux | 1:fb4494783863 | 132 | |
samux | 1:fb4494783863 | 133 | } |
samux | 1:fb4494783863 | 134 | |
michaeljkoster | 13:41098c907200 | 135 | bool ESP8266::is_connected() |
samux | 1:fb4494783863 | 136 | { |
michaeljkoster | 13:41098c907200 | 137 | return true; |
samux | 1:fb4494783863 | 138 | } |
samux | 1:fb4494783863 | 139 | |
samux | 1:fb4494783863 | 140 | |
michaeljkoster | 13:41098c907200 | 141 | void ESP8266::reset() |
samux | 1:fb4494783863 | 142 | { |
samux | 1:fb4494783863 | 143 | reset_pin = 0; |
samux | 1:fb4494783863 | 144 | wait(0.2); |
samux | 1:fb4494783863 | 145 | reset_pin = 1; |
samux | 1:fb4494783863 | 146 | wait(0.2); |
samux | 1:fb4494783863 | 147 | } |
samux | 1:fb4494783863 | 148 | |
michaeljkoster | 13:41098c907200 | 149 | bool ESP8266::reboot() |
samux | 3:9aa05e19c62e | 150 | { |
samux | 3:9aa05e19c62e | 151 | return true; |
samux | 3:9aa05e19c62e | 152 | } |
samux | 3:9aa05e19c62e | 153 | |
michaeljkoster | 13:41098c907200 | 154 | int ESP8266::putc(char c) |
samux | 1:fb4494783863 | 155 | { |
samux | 1:fb4494783863 | 156 | while (!wifi.writeable()); |
samux | 1:fb4494783863 | 157 | return wifi.putc(c); |
samux | 1:fb4494783863 | 158 | } |
samux | 1:fb4494783863 | 159 | |
michaeljkoster | 13:41098c907200 | 160 | int ESP8266::readable() |
samux | 1:fb4494783863 | 161 | { |
michaeljkoster | 13:41098c907200 | 162 | return buf_ESP8266.available(); |
samux | 1:fb4494783863 | 163 | } |
samux | 1:fb4494783863 | 164 | |
michaeljkoster | 13:41098c907200 | 165 | int ESP8266::writeable() |
samux | 1:fb4494783863 | 166 | { |
samux | 1:fb4494783863 | 167 | return wifi.writeable(); |
samux | 1:fb4494783863 | 168 | } |
samux | 1:fb4494783863 | 169 | |
michaeljkoster | 13:41098c907200 | 170 | char ESP8266::getc() |
samux | 1:fb4494783863 | 171 | { |
samux | 1:fb4494783863 | 172 | char c; |
michaeljkoster | 13:41098c907200 | 173 | while (!buf_ESP8266.available()); |
michaeljkoster | 13:41098c907200 | 174 | buf_ESP8266.dequeue(&c); |
samux | 1:fb4494783863 | 175 | return c; |
samux | 1:fb4494783863 | 176 | } |
samux | 1:fb4494783863 | 177 | |
michaeljkoster | 13:41098c907200 | 178 | void ESP8266::handler_rx(void) |
samux | 1:fb4494783863 | 179 | { |
samux | 1:fb4494783863 | 180 | //read characters |
samux | 1:fb4494783863 | 181 | while (wifi.readable()) |
michaeljkoster | 13:41098c907200 | 182 | buf_ESP8266.queue(wifi.getc()); |
samux | 1:fb4494783863 | 183 | } |
samux | 1:fb4494783863 | 184 | |
michaeljkoster | 13:41098c907200 | 185 | void ESP8266::attach_rx(bool callback) |
samux | 1:fb4494783863 | 186 | { |
samux | 1:fb4494783863 | 187 | if (!callback) |
samux | 1:fb4494783863 | 188 | wifi.attach(NULL); |
samux | 1:fb4494783863 | 189 | else |
michaeljkoster | 13:41098c907200 | 190 | wifi.attach(this, &ESP8266::handler_rx); |
samux | 1:fb4494783863 | 191 | } |
samux | 1:fb4494783863 | 192 | |
samux | 1:fb4494783863 | 193 | |
michaeljkoster | 13:41098c907200 | 194 | int ESP8266::send(const char * str, int len, const char * ACK, char * res, int timeout) |
samux | 1:fb4494783863 | 195 | { |
samux | 1:fb4494783863 | 196 | char read; |
samux | 1:fb4494783863 | 197 | size_t found = string::npos; |
samux | 1:fb4494783863 | 198 | string checking; |
samux | 1:fb4494783863 | 199 | Timer tmr; |
samux | 1:fb4494783863 | 200 | int result = 0; |
samux | 1:fb4494783863 | 201 | |
samux | 1:fb4494783863 | 202 | DBG("will send: %s\r\n",str); |
samux | 1:fb4494783863 | 203 | |
samux | 1:fb4494783863 | 204 | attach_rx(false); |
samux | 1:fb4494783863 | 205 | |
samux | 1:fb4494783863 | 206 | //We flush the buffer |
samux | 1:fb4494783863 | 207 | while (wifi.readable()) |
samux | 1:fb4494783863 | 208 | wifi.getc(); |
samux | 1:fb4494783863 | 209 | |
samux | 1:fb4494783863 | 210 | if (!ACK || !strcmp(ACK, "NO")) { |
samux | 1:fb4494783863 | 211 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 212 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 213 | } else { |
samux | 1:fb4494783863 | 214 | //We flush the buffer |
samux | 1:fb4494783863 | 215 | while (wifi.readable()) |
samux | 1:fb4494783863 | 216 | wifi.getc(); |
samux | 1:fb4494783863 | 217 | |
samux | 1:fb4494783863 | 218 | tmr.start(); |
samux | 1:fb4494783863 | 219 | for (int i = 0; i < len; i++) |
samux | 1:fb4494783863 | 220 | result = (putc(str[i]) == str[i]) ? result + 1 : result; |
samux | 1:fb4494783863 | 221 | |
samux | 1:fb4494783863 | 222 | while (1) { |
samux | 1:fb4494783863 | 223 | if (tmr.read_ms() > timeout) { |
samux | 1:fb4494783863 | 224 | //We flush the buffer |
samux | 1:fb4494783863 | 225 | while (wifi.readable()) |
samux | 1:fb4494783863 | 226 | wifi.getc(); |
samux | 1:fb4494783863 | 227 | |
samux | 1:fb4494783863 | 228 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 229 | |
samux | 1:fb4494783863 | 230 | attach_rx(true); |
samux | 1:fb4494783863 | 231 | return -1; |
samux | 1:fb4494783863 | 232 | } else if (wifi.readable()) { |
samux | 1:fb4494783863 | 233 | read = wifi.getc(); |
samux | 1:fb4494783863 | 234 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 235 | checking += read; |
samux | 1:fb4494783863 | 236 | found = checking.find(ACK); |
samux | 1:fb4494783863 | 237 | if (found != string::npos) { |
samux | 1:fb4494783863 | 238 | wait(0.01); |
samux | 1:fb4494783863 | 239 | |
samux | 1:fb4494783863 | 240 | //We flush the buffer |
samux | 1:fb4494783863 | 241 | while (wifi.readable()) |
samux | 1:fb4494783863 | 242 | wifi.getc(); |
samux | 1:fb4494783863 | 243 | |
samux | 1:fb4494783863 | 244 | break; |
samux | 1:fb4494783863 | 245 | } |
samux | 1:fb4494783863 | 246 | } |
samux | 1:fb4494783863 | 247 | } |
samux | 1:fb4494783863 | 248 | } |
samux | 1:fb4494783863 | 249 | DBG("check: %s\r\n", checking.c_str()); |
samux | 1:fb4494783863 | 250 | |
samux | 1:fb4494783863 | 251 | attach_rx(true); |
samux | 1:fb4494783863 | 252 | return result; |
samux | 1:fb4494783863 | 253 | } |
samux | 1:fb4494783863 | 254 | |
samux | 1:fb4494783863 | 255 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 1:fb4494783863 | 256 | if ( res != NULL) { |
samux | 1:fb4494783863 | 257 | int i = 0; |
samux | 1:fb4494783863 | 258 | Timer timeout; |
samux | 1:fb4494783863 | 259 | timeout.start(); |
samux | 1:fb4494783863 | 260 | tmr.reset(); |
samux | 1:fb4494783863 | 261 | while (1) { |
samux | 1:fb4494783863 | 262 | if (timeout.read() > 2) { |
samux | 1:fb4494783863 | 263 | if (i == 0) { |
samux | 1:fb4494783863 | 264 | res = NULL; |
samux | 1:fb4494783863 | 265 | break; |
samux | 1:fb4494783863 | 266 | } |
samux | 1:fb4494783863 | 267 | res[i] = '\0'; |
samux | 1:fb4494783863 | 268 | DBG("user str 1: %s\r\n", res); |
samux | 1:fb4494783863 | 269 | |
samux | 1:fb4494783863 | 270 | break; |
samux | 1:fb4494783863 | 271 | } else { |
samux | 1:fb4494783863 | 272 | if (tmr.read_ms() > 300) { |
samux | 1:fb4494783863 | 273 | res[i] = '\0'; |
samux | 1:fb4494783863 | 274 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 275 | |
samux | 1:fb4494783863 | 276 | break; |
samux | 1:fb4494783863 | 277 | } |
samux | 1:fb4494783863 | 278 | if (wifi.readable()) { |
samux | 1:fb4494783863 | 279 | tmr.start(); |
samux | 1:fb4494783863 | 280 | read = wifi.getc(); |
samux | 1:fb4494783863 | 281 | |
samux | 1:fb4494783863 | 282 | // we drop \r and \n |
samux | 1:fb4494783863 | 283 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 284 | res[i++] = read; |
samux | 1:fb4494783863 | 285 | } |
samux | 1:fb4494783863 | 286 | } |
samux | 1:fb4494783863 | 287 | } |
samux | 1:fb4494783863 | 288 | } |
samux | 1:fb4494783863 | 289 | DBG("user str: %s\r\n", res); |
samux | 1:fb4494783863 | 290 | } |
samux | 1:fb4494783863 | 291 | |
samux | 1:fb4494783863 | 292 | //We flush the buffer |
samux | 1:fb4494783863 | 293 | while (wifi.readable()) |
samux | 1:fb4494783863 | 294 | wifi.getc(); |
samux | 1:fb4494783863 | 295 | |
samux | 1:fb4494783863 | 296 | attach_rx(true); |
samux | 1:fb4494783863 | 297 | DBG("result: %d\r\n", result) |
samux | 1:fb4494783863 | 298 | return result; |
samux | 1:fb4494783863 | 299 | } |