Added monitoring feature of ESP8266's UART

Dependents:   ESP8266_W7500_Example DualNetworkInterface-Basic

Fork of ESP8266Interface by ESP8266

Committer:
michaeljkoster
Date:
Sun Nov 30 21:37:16 2014 +0000
Revision:
14:4d1128f72e00
Parent:
13:41098c907200
Child:
15:37a7a56a424f
Add getIPAddress

Who changed what in which revision?

UserRevisionLine numberNew 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
michaeljkoster 14:4d1128f72e00 140 char* ESP8266::getIPAddress()
michaeljkoster 14:4d1128f72e00 141 {
michaeljkoster 14:4d1128f72e00 142 return ipString;
michaeljkoster 14:4d1128f72e00 143 }
samux 1:fb4494783863 144
michaeljkoster 13:41098c907200 145 void ESP8266::reset()
samux 1:fb4494783863 146 {
samux 1:fb4494783863 147 reset_pin = 0;
samux 1:fb4494783863 148 wait(0.2);
samux 1:fb4494783863 149 reset_pin = 1;
samux 1:fb4494783863 150 wait(0.2);
samux 1:fb4494783863 151 }
samux 1:fb4494783863 152
michaeljkoster 13:41098c907200 153 bool ESP8266::reboot()
samux 3:9aa05e19c62e 154 {
samux 3:9aa05e19c62e 155 return true;
samux 3:9aa05e19c62e 156 }
samux 3:9aa05e19c62e 157
michaeljkoster 13:41098c907200 158 int ESP8266::putc(char c)
samux 1:fb4494783863 159 {
samux 1:fb4494783863 160 while (!wifi.writeable());
samux 1:fb4494783863 161 return wifi.putc(c);
samux 1:fb4494783863 162 }
samux 1:fb4494783863 163
michaeljkoster 13:41098c907200 164 int ESP8266::readable()
samux 1:fb4494783863 165 {
michaeljkoster 13:41098c907200 166 return buf_ESP8266.available();
samux 1:fb4494783863 167 }
samux 1:fb4494783863 168
michaeljkoster 13:41098c907200 169 int ESP8266::writeable()
samux 1:fb4494783863 170 {
samux 1:fb4494783863 171 return wifi.writeable();
samux 1:fb4494783863 172 }
samux 1:fb4494783863 173
michaeljkoster 13:41098c907200 174 char ESP8266::getc()
samux 1:fb4494783863 175 {
michaeljkoster 14:4d1128f72e00 176 char c=0;
michaeljkoster 13:41098c907200 177 while (!buf_ESP8266.available());
michaeljkoster 13:41098c907200 178 buf_ESP8266.dequeue(&c);
samux 1:fb4494783863 179 return c;
samux 1:fb4494783863 180 }
samux 1:fb4494783863 181
michaeljkoster 13:41098c907200 182 void ESP8266::handler_rx(void)
samux 1:fb4494783863 183 {
samux 1:fb4494783863 184 //read characters
samux 1:fb4494783863 185 while (wifi.readable())
michaeljkoster 13:41098c907200 186 buf_ESP8266.queue(wifi.getc());
samux 1:fb4494783863 187 }
samux 1:fb4494783863 188
michaeljkoster 13:41098c907200 189 void ESP8266::attach_rx(bool callback)
samux 1:fb4494783863 190 {
samux 1:fb4494783863 191 if (!callback)
samux 1:fb4494783863 192 wifi.attach(NULL);
samux 1:fb4494783863 193 else
michaeljkoster 13:41098c907200 194 wifi.attach(this, &ESP8266::handler_rx);
samux 1:fb4494783863 195 }
samux 1:fb4494783863 196
samux 1:fb4494783863 197
michaeljkoster 13:41098c907200 198 int ESP8266::send(const char * str, int len, const char * ACK, char * res, int timeout)
samux 1:fb4494783863 199 {
samux 1:fb4494783863 200 char read;
samux 1:fb4494783863 201 size_t found = string::npos;
samux 1:fb4494783863 202 string checking;
samux 1:fb4494783863 203 Timer tmr;
samux 1:fb4494783863 204 int result = 0;
samux 1:fb4494783863 205
samux 1:fb4494783863 206 DBG("will send: %s\r\n",str);
samux 1:fb4494783863 207
samux 1:fb4494783863 208 attach_rx(false);
samux 1:fb4494783863 209
samux 1:fb4494783863 210 //We flush the buffer
samux 1:fb4494783863 211 while (wifi.readable())
samux 1:fb4494783863 212 wifi.getc();
samux 1:fb4494783863 213
samux 1:fb4494783863 214 if (!ACK || !strcmp(ACK, "NO")) {
samux 1:fb4494783863 215 for (int i = 0; i < len; i++)
samux 1:fb4494783863 216 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 217 } else {
samux 1:fb4494783863 218 //We flush the buffer
samux 1:fb4494783863 219 while (wifi.readable())
samux 1:fb4494783863 220 wifi.getc();
samux 1:fb4494783863 221
samux 1:fb4494783863 222 tmr.start();
samux 1:fb4494783863 223 for (int i = 0; i < len; i++)
samux 1:fb4494783863 224 result = (putc(str[i]) == str[i]) ? result + 1 : result;
samux 1:fb4494783863 225
samux 1:fb4494783863 226 while (1) {
samux 1:fb4494783863 227 if (tmr.read_ms() > timeout) {
samux 1:fb4494783863 228 //We flush the buffer
samux 1:fb4494783863 229 while (wifi.readable())
samux 1:fb4494783863 230 wifi.getc();
samux 1:fb4494783863 231
samux 1:fb4494783863 232 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 233
samux 1:fb4494783863 234 attach_rx(true);
samux 1:fb4494783863 235 return -1;
samux 1:fb4494783863 236 } else if (wifi.readable()) {
samux 1:fb4494783863 237 read = wifi.getc();
samux 1:fb4494783863 238 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 239 checking += read;
samux 1:fb4494783863 240 found = checking.find(ACK);
samux 1:fb4494783863 241 if (found != string::npos) {
samux 1:fb4494783863 242 wait(0.01);
samux 1:fb4494783863 243
samux 1:fb4494783863 244 //We flush the buffer
samux 1:fb4494783863 245 while (wifi.readable())
samux 1:fb4494783863 246 wifi.getc();
samux 1:fb4494783863 247
samux 1:fb4494783863 248 break;
samux 1:fb4494783863 249 }
samux 1:fb4494783863 250 }
samux 1:fb4494783863 251 }
samux 1:fb4494783863 252 }
samux 1:fb4494783863 253 DBG("check: %s\r\n", checking.c_str());
samux 1:fb4494783863 254
samux 1:fb4494783863 255 attach_rx(true);
samux 1:fb4494783863 256 return result;
samux 1:fb4494783863 257 }
samux 1:fb4494783863 258
samux 1:fb4494783863 259 //the user wants the result from the command (ACK == NULL, res != NULL)
samux 1:fb4494783863 260 if ( res != NULL) {
samux 1:fb4494783863 261 int i = 0;
samux 1:fb4494783863 262 Timer timeout;
samux 1:fb4494783863 263 timeout.start();
samux 1:fb4494783863 264 tmr.reset();
samux 1:fb4494783863 265 while (1) {
samux 1:fb4494783863 266 if (timeout.read() > 2) {
samux 1:fb4494783863 267 if (i == 0) {
samux 1:fb4494783863 268 res = NULL;
samux 1:fb4494783863 269 break;
samux 1:fb4494783863 270 }
samux 1:fb4494783863 271 res[i] = '\0';
samux 1:fb4494783863 272 DBG("user str 1: %s\r\n", res);
samux 1:fb4494783863 273
samux 1:fb4494783863 274 break;
samux 1:fb4494783863 275 } else {
samux 1:fb4494783863 276 if (tmr.read_ms() > 300) {
samux 1:fb4494783863 277 res[i] = '\0';
samux 1:fb4494783863 278 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 279
samux 1:fb4494783863 280 break;
samux 1:fb4494783863 281 }
samux 1:fb4494783863 282 if (wifi.readable()) {
samux 1:fb4494783863 283 tmr.start();
samux 1:fb4494783863 284 read = wifi.getc();
samux 1:fb4494783863 285
samux 1:fb4494783863 286 // we drop \r and \n
samux 1:fb4494783863 287 if ( read != '\r' && read != '\n') {
samux 1:fb4494783863 288 res[i++] = read;
samux 1:fb4494783863 289 }
samux 1:fb4494783863 290 }
samux 1:fb4494783863 291 }
samux 1:fb4494783863 292 }
samux 1:fb4494783863 293 DBG("user str: %s\r\n", res);
samux 1:fb4494783863 294 }
samux 1:fb4494783863 295
samux 1:fb4494783863 296 //We flush the buffer
samux 1:fb4494783863 297 while (wifi.readable())
samux 1:fb4494783863 298 wifi.getc();
samux 1:fb4494783863 299
samux 1:fb4494783863 300 attach_rx(true);
samux 1:fb4494783863 301 DBG("result: %d\r\n", result)
samux 1:fb4494783863 302 return result;
samux 1:fb4494783863 303 }