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