A fork of the original interface for OS/2. Features a correctly-implemented recv (but retains the old behavior via recv2).
Dependents: weather_clock weather_clock
ESP8266/ESP8266.cpp@43:2e326d95fe2c, 2015-05-07 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu May 07 04:00:27 2015 +0000
- Revision:
- 43:2e326d95fe2c
- Parent:
- 42:3f62103a4f3c
added warning about sending messages > 2048 bytes
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" |
michaeljkoster | 16:3f0efaa57a12 | 21 | #include "Endpoint.h" |
samux | 1:fb4494783863 | 22 | #include <string> |
samux | 1:fb4494783863 | 23 | #include <algorithm> |
samux | 1:fb4494783863 | 24 | |
samux | 1:fb4494783863 | 25 | //Debug is disabled by default |
mbedAustin | 36:e1545c6c2cb3 | 26 | #if 1 |
mbedAustin | 42:3f62103a4f3c | 27 | #define DBG(x, ...) printf("[ESP8266 : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
mbedAustin | 42:3f62103a4f3c | 28 | #define WARN(x, ...) printf("[ESP8266 : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
mbedAustin | 42:3f62103a4f3c | 29 | #define ERR(x, ...) printf("[ESP8266 : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
samux | 1:fb4494783863 | 30 | #else |
samux | 1:fb4494783863 | 31 | #define DBG(x, ...) |
samux | 1:fb4494783863 | 32 | #define WARN(x, ...) |
samux | 1:fb4494783863 | 33 | #define ERR(x, ...) |
samux | 1:fb4494783863 | 34 | #endif |
samux | 1:fb4494783863 | 35 | |
mbedAustin | 36:e1545c6c2cb3 | 36 | #if 1 |
mbedAustin | 42:3f62103a4f3c | 37 | #define INFO(x, ...) printf("[ESP8266 : INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__); |
samux | 1:fb4494783863 | 38 | #else |
samux | 1:fb4494783863 | 39 | #define INFO(x, ...) |
samux | 1:fb4494783863 | 40 | #endif |
samux | 1:fb4494783863 | 41 | |
mbedAustin | 30:c035696b9397 | 42 | #define ESP_MAX_TRY_JOIN 3 |
mbedAustin | 30:c035696b9397 | 43 | #define ESP_MAXID 4 // the largest possible ID Value (max num of sockets possible) |
samux | 1:fb4494783863 | 44 | |
michaeljkoster | 13:41098c907200 | 45 | ESP8266 * ESP8266::inst; |
mbedAustin | 29:939372104145 | 46 | char* ip = NULL; |
samux | 1:fb4494783863 | 47 | |
sam_grove | 40:0a83315aea0a | 48 | ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, const char *ssid, const char *phrase, uint32_t baud) : |
sam_grove | 40:0a83315aea0a | 49 | wifi(tx, rx), reset_pin(reset), buf_ESP8266(ESP_MBUFFE_MAX) |
samux | 1:fb4494783863 | 50 | { |
mbedAustin | 36:e1545c6c2cb3 | 51 | INFO("Initializing ESP8266 object"); |
samux | 1:fb4494783863 | 52 | memset(&state, 0, sizeof(state)); |
samux | 1:fb4494783863 | 53 | |
michaeljkoster | 26:0d5bcb3903e2 | 54 | // change all ' ' in '$' in the ssid and the passphrase |
samux | 1:fb4494783863 | 55 | strcpy(this->ssid, ssid); |
samux | 1:fb4494783863 | 56 | for (int i = 0; i < strlen(ssid); i++) { |
samux | 1:fb4494783863 | 57 | if (this->ssid[i] == ' ') |
samux | 1:fb4494783863 | 58 | this->ssid[i] = '$'; |
samux | 1:fb4494783863 | 59 | } |
sam_grove | 40:0a83315aea0a | 60 | |
samux | 1:fb4494783863 | 61 | strcpy(this->phrase, phrase); |
samux | 1:fb4494783863 | 62 | for (int i = 0; i < strlen(phrase); i++) { |
samux | 1:fb4494783863 | 63 | if (this->phrase[i] == ' ') |
samux | 1:fb4494783863 | 64 | this->phrase[i] = '$'; |
samux | 1:fb4494783863 | 65 | } |
michaeljkoster | 26:0d5bcb3903e2 | 66 | |
samux | 1:fb4494783863 | 67 | inst = this; |
samux | 1:fb4494783863 | 68 | attach_rx(false); |
mbedAustin | 28:91e65e22e63a | 69 | |
mbedAustin | 28:91e65e22e63a | 70 | wifi.baud(baud); // initial baud rate of the ESP8266 |
mbedAustin | 28:91e65e22e63a | 71 | |
michaeljkoster | 22:c4360e61486a | 72 | state.associated = false; |
michaeljkoster | 22:c4360e61486a | 73 | state.cmdMode = false; |
samux | 1:fb4494783863 | 74 | } |
samux | 1:fb4494783863 | 75 | |
michaeljkoster | 13:41098c907200 | 76 | bool ESP8266::join() |
samux | 1:fb4494783863 | 77 | { |
michaeljkoster | 23:de9221771e96 | 78 | sendCommand( "AT+CWMODE=1", "change", NULL, 1000); |
michaeljkoster | 16:3f0efaa57a12 | 79 | string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\""; |
mbedAustin | 28:91e65e22e63a | 80 | if( sendCommand( cmd.c_str(), "OK", NULL, 10000) ) { |
michaeljkoster | 16:3f0efaa57a12 | 81 | // successfully joined the network |
samux | 1:fb4494783863 | 82 | state.associated = true; |
mbedAustin | 37:6887e61cf674 | 83 | INFO("ssid: %s, phrase: %s", this->ssid, this->phrase); |
samux | 1:fb4494783863 | 84 | return true; |
samux | 1:fb4494783863 | 85 | } |
samux | 1:fb4494783863 | 86 | return false; |
samux | 1:fb4494783863 | 87 | } |
samux | 1:fb4494783863 | 88 | |
michaeljkoster | 13:41098c907200 | 89 | bool ESP8266::connect() |
samux | 1:fb4494783863 | 90 | { |
mbedAustin | 28:91e65e22e63a | 91 | sendCommand("AT+CWDHCP=1,1","OK",NULL,1000); // DHCP Enabled in Station Mode |
mbedAustin | 28:91e65e22e63a | 92 | return ESP8266::join(); |
samux | 1:fb4494783863 | 93 | } |
samux | 1:fb4494783863 | 94 | |
michaeljkoster | 15:37a7a56a424f | 95 | bool ESP8266::is_connected() |
michaeljkoster | 15:37a7a56a424f | 96 | { |
michaeljkoster | 15:37a7a56a424f | 97 | return true; |
michaeljkoster | 15:37a7a56a424f | 98 | } |
michaeljkoster | 15:37a7a56a424f | 99 | |
mbedAustin | 30:c035696b9397 | 100 | bool ESP8266::start(bool type,char* ip, int port, int id) |
mbedAustin | 30:c035696b9397 | 101 | { |
mbedAustin | 30:c035696b9397 | 102 | // Error Check |
mbedAustin | 30:c035696b9397 | 103 | if(id > ESP_MAXID) { |
mbedAustin | 30:c035696b9397 | 104 | ERR("startUDPMulti: max id is: %d, id given is %d",ESP_MAXID,id); |
mbedAustin | 30:c035696b9397 | 105 | return false; |
mbedAustin | 30:c035696b9397 | 106 | } |
mbedAustin | 30:c035696b9397 | 107 | // Single Connection Mode |
mbedAustin | 30:c035696b9397 | 108 | if(id < 0) { |
mbedAustin | 30:c035696b9397 | 109 | DBG("Start Single Connection Mode"); |
mbedAustin | 30:c035696b9397 | 110 | char portstr[5]; |
mbedAustin | 30:c035696b9397 | 111 | char idstr[2]; |
mbedAustin | 30:c035696b9397 | 112 | bool check [3] = {0}; |
mbedAustin | 30:c035696b9397 | 113 | sprintf(idstr,"%d",id); |
mbedAustin | 30:c035696b9397 | 114 | sprintf(portstr, "%d", port); |
mbedAustin | 30:c035696b9397 | 115 | switch(type) { |
mbedAustin | 30:c035696b9397 | 116 | case ESP_UDP_TYPE : //UDP |
mbedAustin | 32:cf071dc33972 | 117 | check[0] = sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); |
mbedAustin | 30:c035696b9397 | 118 | break; |
mbedAustin | 30:c035696b9397 | 119 | case ESP_TCP_TYPE : //TCP |
mbedAustin | 32:cf071dc33972 | 120 | check[0] = sendCommand(( "AT+CIPSTART=\"TCP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); |
mbedAustin | 30:c035696b9397 | 121 | break; |
mbedAustin | 30:c035696b9397 | 122 | default: |
mbedAustin | 30:c035696b9397 | 123 | ERR("Default hit for starting connection, this shouldnt be possible!!"); |
mbedAustin | 30:c035696b9397 | 124 | break; |
mbedAustin | 30:c035696b9397 | 125 | } |
mbedAustin | 30:c035696b9397 | 126 | check[1] = sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode |
mbedAustin | 30:c035696b9397 | 127 | check[2] = sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode |
mbedAustin | 30:c035696b9397 | 128 | // check that all commands were sucessful |
mbedAustin | 32:cf071dc33972 | 129 | if(check[0] and check[1] and check[2]) { |
mbedAustin | 30:c035696b9397 | 130 | state.cmdMode = false; |
mbedAustin | 30:c035696b9397 | 131 | return true; |
mbedAustin | 30:c035696b9397 | 132 | } else { |
mbedAustin | 36:e1545c6c2cb3 | 133 | ERR("startUDPTransparent Failed for ip:%s, port:%d",ip,port); |
mbedAustin | 30:c035696b9397 | 134 | return false; |
mbedAustin | 30:c035696b9397 | 135 | } |
mbedAustin | 30:c035696b9397 | 136 | } |
mbedAustin | 30:c035696b9397 | 137 | // Multi Connection Mode |
mbedAustin | 30:c035696b9397 | 138 | else { |
mbedAustin | 30:c035696b9397 | 139 | //TODO: impliment Multi Connection Mode |
mbedAustin | 30:c035696b9397 | 140 | ERR("Not currently Supported!"); |
mbedAustin | 30:c035696b9397 | 141 | return false; |
mbedAustin | 32:cf071dc33972 | 142 | |
mbedAustin | 32:cf071dc33972 | 143 | // DBG("Start Multi Connection Mode"); |
mbedAustin | 32:cf071dc33972 | 144 | // char portstr[5]; |
mbedAustin | 32:cf071dc33972 | 145 | // char idstr[2]; |
mbedAustin | 32:cf071dc33972 | 146 | // bool check [3] = {0}; |
mbedAustin | 32:cf071dc33972 | 147 | // sprintf(idstr,"%d",id); |
mbedAustin | 32:cf071dc33972 | 148 | // sprintf(portstr, "%d", port); |
mbedAustin | 32:cf071dc33972 | 149 | // switch(type) { |
mbedAustin | 32:cf071dc33972 | 150 | // case ESP_UDP_TYPE : //UDP |
mbedAustin | 32:cf071dc33972 | 151 | // check[0] = sendCommand(( "AT+CIPSTART=" + (string) idstr + ",\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); |
mbedAustin | 32:cf071dc33972 | 152 | // break; |
mbedAustin | 32:cf071dc33972 | 153 | // case ESP_TCP_TYPE : //TCP |
mbedAustin | 32:cf071dc33972 | 154 | // check[0] = sendCommand(( "AT+CIPSTART=" + (string) idstr + ",\"TCP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); |
mbedAustin | 32:cf071dc33972 | 155 | // break; |
mbedAustin | 32:cf071dc33972 | 156 | // default: |
mbedAustin | 32:cf071dc33972 | 157 | // ERR("Default hit for starting connection, this shouldnt be possible!!"); |
mbedAustin | 32:cf071dc33972 | 158 | // break; |
mbedAustin | 32:cf071dc33972 | 159 | // } |
mbedAustin | 30:c035696b9397 | 160 | } |
mbedAustin | 30:c035696b9397 | 161 | } |
mbedAustin | 30:c035696b9397 | 162 | |
mbedAustin | 28:91e65e22e63a | 163 | bool ESP8266::startUDP(char* ip, int port) |
mbedAustin | 28:91e65e22e63a | 164 | { |
michaeljkoster | 17:d11fa4c3ac65 | 165 | char portstr[5]; |
michaeljkoster | 16:3f0efaa57a12 | 166 | sprintf(portstr, "%d", port); |
michaeljkoster | 18:60422852e99c | 167 | sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000); |
mbedAustin | 28:91e65e22e63a | 168 | sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode |
mbedAustin | 28:91e65e22e63a | 169 | sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode |
mbedAustin | 38:86e75901efc1 | 170 | //DBG("Data Mode"); |
michaeljkoster | 22:c4360e61486a | 171 | state.cmdMode = false; |
mbedAustin | 28:91e65e22e63a | 172 | |
michaeljkoster | 16:3f0efaa57a12 | 173 | return true; |
michaeljkoster | 16:3f0efaa57a12 | 174 | } |
michaeljkoster | 16:3f0efaa57a12 | 175 | |
michaeljkoster | 15:37a7a56a424f | 176 | bool ESP8266::close() |
michaeljkoster | 15:37a7a56a424f | 177 | { |
sam_grove | 40:0a83315aea0a | 178 | wait(0.1f); |
michaeljkoster | 17:d11fa4c3ac65 | 179 | send("+++",3); |
sam_grove | 40:0a83315aea0a | 180 | wait(1.0f); |
michaeljkoster | 22:c4360e61486a | 181 | state.cmdMode = true; |
michaeljkoster | 16:3f0efaa57a12 | 182 | sendCommand("AT+CIPCLOSE","OK", NULL, 10000); |
michaeljkoster | 15:37a7a56a424f | 183 | return true; |
michaeljkoster | 15:37a7a56a424f | 184 | } |
michaeljkoster | 15:37a7a56a424f | 185 | |
michaeljkoster | 15:37a7a56a424f | 186 | bool ESP8266::disconnect() |
michaeljkoster | 15:37a7a56a424f | 187 | { |
michaeljkoster | 15:37a7a56a424f | 188 | // if already disconnected, return |
michaeljkoster | 15:37a7a56a424f | 189 | if (!state.associated) |
michaeljkoster | 15:37a7a56a424f | 190 | return true; |
michaeljkoster | 15:37a7a56a424f | 191 | // send command to quit AP |
mbedAustin | 28:91e65e22e63a | 192 | sendCommand("AT+CWQAP", "OK", NULL, 10000); |
michaeljkoster | 15:37a7a56a424f | 193 | state.associated = false; |
michaeljkoster | 15:37a7a56a424f | 194 | return true; |
michaeljkoster | 15:37a7a56a424f | 195 | } |
michaeljkoster | 15:37a7a56a424f | 196 | |
mbedAustin | 28:91e65e22e63a | 197 | /* |
mbedAustin | 28:91e65e22e63a | 198 | Assuming Returned data looks like this: |
mbedAustin | 28:91e65e22e63a | 199 | +CIFSR:STAIP,"192.168.11.2" |
mbedAustin | 28:91e65e22e63a | 200 | +CIFSR:STAMAC,"18:fe:34:9f:3a:f5" |
mbedAustin | 28:91e65e22e63a | 201 | grabbing IP from first set of quotation marks |
mbedAustin | 28:91e65e22e63a | 202 | */ |
michaeljkoster | 15:37a7a56a424f | 203 | char* ESP8266::getIPAddress() |
michaeljkoster | 15:37a7a56a424f | 204 | { |
mbedAustin | 28:91e65e22e63a | 205 | char result[30] = {0}; |
mbedAustin | 28:91e65e22e63a | 206 | int check = 0; |
mbedAustin | 28:91e65e22e63a | 207 | check = sendCommand("AT+CIFSR", NULL, result, 1000); |
mbedAustin | 29:939372104145 | 208 | //pc.printf("\r\nReceivedInfo for IP Command is: %s\r\n",result); |
mbedAustin | 29:939372104145 | 209 | ip = ipString; |
mbedAustin | 29:939372104145 | 210 | if(check) { |
mbedAustin | 28:91e65e22e63a | 211 | // Success |
mbedAustin | 28:91e65e22e63a | 212 | string resultString(result); |
mbedAustin | 28:91e65e22e63a | 213 | uint8_t pos1 = 0, pos2 = 0; |
mbedAustin | 28:91e65e22e63a | 214 | //uint8_t pos3 = 0, pos4 = 0; |
mbedAustin | 28:91e65e22e63a | 215 | pos1 = resultString.find("+CIFSR:STAIP"); |
mbedAustin | 28:91e65e22e63a | 216 | pos1 = resultString.find('"',pos1); |
mbedAustin | 28:91e65e22e63a | 217 | pos2 = resultString.find('"',pos1+1); |
mbedAustin | 28:91e65e22e63a | 218 | //pos3 = resultString.find('"',pos2+1); //would find mac address |
mbedAustin | 28:91e65e22e63a | 219 | //pos4 = resultString.find('"',pos3+1); |
mbedAustin | 28:91e65e22e63a | 220 | strncpy(ipString,resultString.substr(pos1,pos2).c_str(),sizeof(ipString)); |
mbedAustin | 28:91e65e22e63a | 221 | ipString[pos2 - pos1 +1] = 0; // null terminate string correctly. |
mbedAustin | 42:3f62103a4f3c | 222 | INFO("IP: %s",ipString); |
mbedAustin | 29:939372104145 | 223 | ip = ipString; |
mbedAustin | 29:939372104145 | 224 | |
mbedAustin | 29:939372104145 | 225 | } else { |
mbedAustin | 28:91e65e22e63a | 226 | // Failure |
mbedAustin | 41:264902b160ea | 227 | ERR("getIPAddress() failed"); |
mbedAustin | 29:939372104145 | 228 | ip = NULL; |
mbedAustin | 28:91e65e22e63a | 229 | } |
mbedAustin | 29:939372104145 | 230 | return ip; |
michaeljkoster | 15:37a7a56a424f | 231 | } |
michaeljkoster | 15:37a7a56a424f | 232 | |
michaeljkoster | 13:41098c907200 | 233 | bool ESP8266::gethostbyname(const char * host, char * ip) |
samux | 1:fb4494783863 | 234 | { |
samux | 1:fb4494783863 | 235 | string h = host; |
samux | 1:fb4494783863 | 236 | int nb_digits = 0; |
samux | 1:fb4494783863 | 237 | |
samux | 1:fb4494783863 | 238 | // no dns needed |
samux | 1:fb4494783863 | 239 | int pos = h.find("."); |
samux | 1:fb4494783863 | 240 | if (pos != string::npos) { |
samux | 1:fb4494783863 | 241 | string sub = h.substr(0, h.find(".")); |
samux | 1:fb4494783863 | 242 | nb_digits = atoi(sub.c_str()); |
samux | 1:fb4494783863 | 243 | } |
samux | 1:fb4494783863 | 244 | //printf("substrL %s\r\n", sub.c_str()); |
samux | 1:fb4494783863 | 245 | if (count(h.begin(), h.end(), '.') == 3 && nb_digits > 0) { |
samux | 1:fb4494783863 | 246 | strcpy(ip, host); |
michaeljkoster | 13:41098c907200 | 247 | return true; |
mbedAustin | 28:91e65e22e63a | 248 | } else { |
michaeljkoster | 13:41098c907200 | 249 | // dns needed, not currently available |
mbedAustin | 30:c035696b9397 | 250 | ERR("gethostbyname(): DNS Not currently available, only use IP Addresses!"); |
michaeljkoster | 13:41098c907200 | 251 | return false; |
michaeljkoster | 13:41098c907200 | 252 | } |
michaeljkoster | 13:41098c907200 | 253 | } |
samux | 1:fb4494783863 | 254 | |
michaeljkoster | 13:41098c907200 | 255 | void ESP8266::reset() |
samux | 1:fb4494783863 | 256 | { |
samux | 1:fb4494783863 | 257 | reset_pin = 0; |
sam_grove | 40:0a83315aea0a | 258 | wait_us(20); |
samux | 1:fb4494783863 | 259 | reset_pin = 1; |
sam_grove | 40:0a83315aea0a | 260 | //wait(1); |
mbedAustin | 28:91e65e22e63a | 261 | |
michaeljkoster | 24:03585a13ff3b | 262 | //send("+++",3); |
michaeljkoster | 24:03585a13ff3b | 263 | //wait(1); |
michaeljkoster | 24:03585a13ff3b | 264 | state.cmdMode = true; |
michaeljkoster | 24:03585a13ff3b | 265 | sendCommand("AT", "OK", NULL, 1000); |
mbedAustin | 28:91e65e22e63a | 266 | sendCommand("AT+RST", "ready", NULL, 10000); |
michaeljkoster | 24:03585a13ff3b | 267 | state.associated = false; |
michaeljkoster | 24:03585a13ff3b | 268 | |
samux | 1:fb4494783863 | 269 | } |
samux | 1:fb4494783863 | 270 | |
michaeljkoster | 13:41098c907200 | 271 | bool ESP8266::reboot() |
samux | 3:9aa05e19c62e | 272 | { |
michaeljkoster | 16:3f0efaa57a12 | 273 | reset(); |
samux | 3:9aa05e19c62e | 274 | return true; |
samux | 3:9aa05e19c62e | 275 | } |
samux | 3:9aa05e19c62e | 276 | |
michaeljkoster | 15:37a7a56a424f | 277 | void ESP8266::handler_rx(void) |
samux | 1:fb4494783863 | 278 | { |
michaeljkoster | 15:37a7a56a424f | 279 | //read characters |
michaeljkoster | 16:3f0efaa57a12 | 280 | char c; |
mbedAustin | 28:91e65e22e63a | 281 | while (wifi.readable()) { |
michaeljkoster | 16:3f0efaa57a12 | 282 | c=wifi.getc(); |
michaeljkoster | 16:3f0efaa57a12 | 283 | buf_ESP8266.queue(c); |
michaeljkoster | 26:0d5bcb3903e2 | 284 | //if (state.cmdMode) pc.printf("%c",c); //debug echo, needs fast serial console to prevent UART overruns |
michaeljkoster | 16:3f0efaa57a12 | 285 | } |
michaeljkoster | 15:37a7a56a424f | 286 | } |
michaeljkoster | 15:37a7a56a424f | 287 | |
michaeljkoster | 15:37a7a56a424f | 288 | void ESP8266::attach_rx(bool callback) |
michaeljkoster | 15:37a7a56a424f | 289 | { |
sam_grove | 40:0a83315aea0a | 290 | if (!callback) { |
michaeljkoster | 15:37a7a56a424f | 291 | wifi.attach(NULL); |
sam_grove | 40:0a83315aea0a | 292 | } |
sam_grove | 40:0a83315aea0a | 293 | else { |
michaeljkoster | 15:37a7a56a424f | 294 | wifi.attach(this, &ESP8266::handler_rx); |
sam_grove | 40:0a83315aea0a | 295 | } |
samux | 1:fb4494783863 | 296 | } |
samux | 1:fb4494783863 | 297 | |
michaeljkoster | 13:41098c907200 | 298 | int ESP8266::readable() |
samux | 1:fb4494783863 | 299 | { |
michaeljkoster | 13:41098c907200 | 300 | return buf_ESP8266.available(); |
samux | 1:fb4494783863 | 301 | } |
samux | 1:fb4494783863 | 302 | |
michaeljkoster | 13:41098c907200 | 303 | int ESP8266::writeable() |
samux | 1:fb4494783863 | 304 | { |
samux | 1:fb4494783863 | 305 | return wifi.writeable(); |
samux | 1:fb4494783863 | 306 | } |
samux | 1:fb4494783863 | 307 | |
michaeljkoster | 13:41098c907200 | 308 | char ESP8266::getc() |
samux | 1:fb4494783863 | 309 | { |
michaeljkoster | 14:4d1128f72e00 | 310 | char c=0; |
michaeljkoster | 13:41098c907200 | 311 | while (!buf_ESP8266.available()); |
michaeljkoster | 13:41098c907200 | 312 | buf_ESP8266.dequeue(&c); |
samux | 1:fb4494783863 | 313 | return c; |
samux | 1:fb4494783863 | 314 | } |
samux | 1:fb4494783863 | 315 | |
michaeljkoster | 15:37a7a56a424f | 316 | int ESP8266::putc(char c) |
samux | 1:fb4494783863 | 317 | { |
michaeljkoster | 20:d764237405c2 | 318 | while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first |
michaeljkoster | 15:37a7a56a424f | 319 | return wifi.putc(c); |
samux | 1:fb4494783863 | 320 | } |
samux | 1:fb4494783863 | 321 | |
michaeljkoster | 15:37a7a56a424f | 322 | void ESP8266::flush() |
samux | 1:fb4494783863 | 323 | { |
michaeljkoster | 15:37a7a56a424f | 324 | buf_ESP8266.flush(); |
samux | 1:fb4494783863 | 325 | } |
samux | 1:fb4494783863 | 326 | |
michaeljkoster | 16:3f0efaa57a12 | 327 | int ESP8266::send(const char * buf, int len) |
michaeljkoster | 15:37a7a56a424f | 328 | { |
mbedAustin | 32:cf071dc33972 | 329 | //TODO: need to add handler for data > 2048B, this is the max packet size of the ESP8266. |
mbedAustin | 43:2e326d95fe2c | 330 | if(len >= 2048){ |
mbedAustin | 43:2e326d95fe2c | 331 | WARN("send buffer >= 2048B, need to chunk this up to be less."); |
mbedAustin | 43:2e326d95fe2c | 332 | } |
michaeljkoster | 16:3f0efaa57a12 | 333 | const char* bufptr=buf; |
mbedAustin | 28:91e65e22e63a | 334 | for(int i=0; i<len; i++) { |
michaeljkoster | 17:d11fa4c3ac65 | 335 | putc((int)*bufptr++); |
michaeljkoster | 18:60422852e99c | 336 | } |
michaeljkoster | 19:fb8d5bff2076 | 337 | return len; |
michaeljkoster | 15:37a7a56a424f | 338 | } |
samux | 1:fb4494783863 | 339 | |
michaeljkoster | 16:3f0efaa57a12 | 340 | bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout) |
samux | 1:fb4494783863 | 341 | { |
samux | 1:fb4494783863 | 342 | char read; |
samux | 1:fb4494783863 | 343 | size_t found = string::npos; |
sam_grove | 40:0a83315aea0a | 344 | string checking = ""; |
samux | 1:fb4494783863 | 345 | Timer tmr; |
samux | 1:fb4494783863 | 346 | int result = 0; |
samux | 1:fb4494783863 | 347 | |
mbedAustin | 42:3f62103a4f3c | 348 | DBG("sendCmd:\t %s",cmd); |
samux | 1:fb4494783863 | 349 | |
michaeljkoster | 17:d11fa4c3ac65 | 350 | attach_rx(true); |
samux | 1:fb4494783863 | 351 | |
samux | 1:fb4494783863 | 352 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 353 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 354 | getc(); |
mbedAustin | 28:91e65e22e63a | 355 | |
samux | 1:fb4494783863 | 356 | if (!ACK || !strcmp(ACK, "NO")) { |
mbedAustin | 28:91e65e22e63a | 357 | for (int i = 0; i < strlen(cmd); i++) { |
michaeljkoster | 16:3f0efaa57a12 | 358 | result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; |
sam_grove | 40:0a83315aea0a | 359 | wait(0.005f); // prevents stuck recieve ready (?) need to let echoed character get read first |
michaeljkoster | 16:3f0efaa57a12 | 360 | } |
michaeljkoster | 16:3f0efaa57a12 | 361 | putc(13); //CR |
sam_grove | 40:0a83315aea0a | 362 | wait(0.005f); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 363 | putc(10); //LF |
michaeljkoster | 16:3f0efaa57a12 | 364 | |
samux | 1:fb4494783863 | 365 | } else { |
samux | 1:fb4494783863 | 366 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 367 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 368 | getc(); |
samux | 1:fb4494783863 | 369 | |
samux | 1:fb4494783863 | 370 | tmr.start(); |
mbedAustin | 28:91e65e22e63a | 371 | for (int i = 0; i < strlen(cmd); i++) { |
michaeljkoster | 16:3f0efaa57a12 | 372 | result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result; |
michaeljkoster | 20:d764237405c2 | 373 | wait(.005); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 374 | } |
michaeljkoster | 16:3f0efaa57a12 | 375 | putc(13); //CR |
sam_grove | 40:0a83315aea0a | 376 | wait(0.005f); // wait for echo |
michaeljkoster | 16:3f0efaa57a12 | 377 | putc(10); //LF |
samux | 1:fb4494783863 | 378 | |
samux | 1:fb4494783863 | 379 | while (1) { |
samux | 1:fb4494783863 | 380 | if (tmr.read_ms() > timeout) { |
samux | 1:fb4494783863 | 381 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 382 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 383 | getc(); |
samux | 1:fb4494783863 | 384 | |
mbedAustin | 42:3f62103a4f3c | 385 | DBG("check:\t %s", checking.c_str()); |
samux | 1:fb4494783863 | 386 | |
samux | 1:fb4494783863 | 387 | attach_rx(true); |
samux | 1:fb4494783863 | 388 | return -1; |
michaeljkoster | 17:d11fa4c3ac65 | 389 | } else if (readable()) { |
michaeljkoster | 17:d11fa4c3ac65 | 390 | read = getc(); |
sam_grove | 40:0a83315aea0a | 391 | //printf("%c",read); //debug echo |
samux | 1:fb4494783863 | 392 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 393 | checking += read; |
samux | 1:fb4494783863 | 394 | found = checking.find(ACK); |
samux | 1:fb4494783863 | 395 | if (found != string::npos) { |
sam_grove | 40:0a83315aea0a | 396 | wait(0.01f); |
samux | 1:fb4494783863 | 397 | |
samux | 1:fb4494783863 | 398 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 399 | while (readable()) |
michaeljkoster | 26:0d5bcb3903e2 | 400 | read = getc(); |
sam_grove | 40:0a83315aea0a | 401 | //printf("%c",read); //debug echo |
samux | 1:fb4494783863 | 402 | break; |
samux | 1:fb4494783863 | 403 | } |
samux | 1:fb4494783863 | 404 | } |
samux | 1:fb4494783863 | 405 | } |
samux | 1:fb4494783863 | 406 | } |
mbedAustin | 36:e1545c6c2cb3 | 407 | DBG("check: %s", checking.c_str()); |
samux | 1:fb4494783863 | 408 | |
samux | 1:fb4494783863 | 409 | attach_rx(true); |
samux | 1:fb4494783863 | 410 | return result; |
samux | 1:fb4494783863 | 411 | } |
samux | 1:fb4494783863 | 412 | |
samux | 1:fb4494783863 | 413 | //the user wants the result from the command (ACK == NULL, res != NULL) |
samux | 1:fb4494783863 | 414 | if ( res != NULL) { |
samux | 1:fb4494783863 | 415 | int i = 0; |
samux | 1:fb4494783863 | 416 | Timer timeout; |
samux | 1:fb4494783863 | 417 | timeout.start(); |
samux | 1:fb4494783863 | 418 | tmr.reset(); |
samux | 1:fb4494783863 | 419 | while (1) { |
samux | 1:fb4494783863 | 420 | if (timeout.read() > 2) { |
samux | 1:fb4494783863 | 421 | if (i == 0) { |
samux | 1:fb4494783863 | 422 | res = NULL; |
samux | 1:fb4494783863 | 423 | break; |
samux | 1:fb4494783863 | 424 | } |
samux | 1:fb4494783863 | 425 | res[i] = '\0'; |
mbedAustin | 36:e1545c6c2cb3 | 426 | DBG("user str 1: %s", res); |
samux | 1:fb4494783863 | 427 | |
samux | 1:fb4494783863 | 428 | break; |
samux | 1:fb4494783863 | 429 | } else { |
samux | 1:fb4494783863 | 430 | if (tmr.read_ms() > 300) { |
samux | 1:fb4494783863 | 431 | res[i] = '\0'; |
mbedAustin | 36:e1545c6c2cb3 | 432 | DBG("user str: %s", res); |
samux | 1:fb4494783863 | 433 | |
samux | 1:fb4494783863 | 434 | break; |
samux | 1:fb4494783863 | 435 | } |
michaeljkoster | 17:d11fa4c3ac65 | 436 | if (readable()) { |
samux | 1:fb4494783863 | 437 | tmr.start(); |
michaeljkoster | 17:d11fa4c3ac65 | 438 | read = getc(); |
samux | 1:fb4494783863 | 439 | |
samux | 1:fb4494783863 | 440 | // we drop \r and \n |
samux | 1:fb4494783863 | 441 | if ( read != '\r' && read != '\n') { |
samux | 1:fb4494783863 | 442 | res[i++] = read; |
samux | 1:fb4494783863 | 443 | } |
samux | 1:fb4494783863 | 444 | } |
samux | 1:fb4494783863 | 445 | } |
samux | 1:fb4494783863 | 446 | } |
mbedAustin | 36:e1545c6c2cb3 | 447 | DBG("user str: %s", res); |
samux | 1:fb4494783863 | 448 | } |
samux | 1:fb4494783863 | 449 | |
samux | 1:fb4494783863 | 450 | //We flush the buffer |
michaeljkoster | 17:d11fa4c3ac65 | 451 | while (readable()) |
michaeljkoster | 17:d11fa4c3ac65 | 452 | getc(); |
samux | 1:fb4494783863 | 453 | |
samux | 1:fb4494783863 | 454 | attach_rx(true); |
mbedAustin | 36:e1545c6c2cb3 | 455 | DBG("result: %d", result) |
samux | 1:fb4494783863 | 456 | return result; |
michaeljkoster | 15:37a7a56a424f | 457 | } |