Simple IoT Board用のライブラリです。 ESP8266ライブラリの軽量化 送信のみのソフトシリアルライブラリを含んでいます。

Dependents:   SITB_HttpGetSample SITB_IFTTTSample SITB_INA226PRC AmbientExampleSITB ... more

Committer:
jksoft
Date:
Sun Nov 15 13:36:44 2015 +0000
Revision:
0:890c12951e96
??

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:890c12951e96 1 /* Copyright (C) 2012 mbed.org, MIT License
jksoft 0:890c12951e96 2 *
jksoft 0:890c12951e96 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jksoft 0:890c12951e96 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jksoft 0:890c12951e96 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jksoft 0:890c12951e96 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jksoft 0:890c12951e96 7 * furnished to do so, subject to the following conditions:
jksoft 0:890c12951e96 8 *
jksoft 0:890c12951e96 9 * The above copyright notice and this permission notice shall be included in all copies or
jksoft 0:890c12951e96 10 * substantial portions of the Software.
jksoft 0:890c12951e96 11 *
jksoft 0:890c12951e96 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jksoft 0:890c12951e96 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jksoft 0:890c12951e96 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jksoft 0:890c12951e96 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jksoft 0:890c12951e96 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jksoft 0:890c12951e96 17 */
jksoft 0:890c12951e96 18
jksoft 0:890c12951e96 19 #include "mbed.h"
jksoft 0:890c12951e96 20 #include "ESP8266.h"
jksoft 0:890c12951e96 21 #include "Endpoint.h"
jksoft 0:890c12951e96 22 #include "SoftSerialSendOnry.h"
jksoft 0:890c12951e96 23
jksoft 0:890c12951e96 24 extern SoftSerialSendOnry pc;
jksoft 0:890c12951e96 25
jksoft 0:890c12951e96 26 //#include <string>
jksoft 0:890c12951e96 27 //#include <algorithm>
jksoft 0:890c12951e96 28
jksoft 0:890c12951e96 29 //Debug is disabled by default
jksoft 0:890c12951e96 30 #if 0
jksoft 0:890c12951e96 31 #define DBG(x, ...) pc.printf("[ESP8266 : DBG]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
jksoft 0:890c12951e96 32 #define WARN(x, ...) pc.printf("[ESP8266 : WARN]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
jksoft 0:890c12951e96 33 #define ERR(x, ...) pc.printf("[ESP8266 : ERR]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
jksoft 0:890c12951e96 34 #else
jksoft 0:890c12951e96 35 #define DBG(x, ...) //wait_us(10);
jksoft 0:890c12951e96 36 #define WARN(x, ...) //wait_us(10);
jksoft 0:890c12951e96 37 #define ERR(x, ...)
jksoft 0:890c12951e96 38 #endif
jksoft 0:890c12951e96 39
jksoft 0:890c12951e96 40 #if 0
jksoft 0:890c12951e96 41 #define INFO(x, ...) printf("[ESP8266 : INFO]"x" \t[%s,%d]\r\n", ##__VA_ARGS__,__FILE__,__LINE__);
jksoft 0:890c12951e96 42 #else
jksoft 0:890c12951e96 43 #define INFO(x, ...)
jksoft 0:890c12951e96 44 #endif
jksoft 0:890c12951e96 45
jksoft 0:890c12951e96 46 #define ESP_MAX_TRY_JOIN 3
jksoft 0:890c12951e96 47 #define ESP_MAXID 4 // the largest possible ID Value (max num of sockets possible)
jksoft 0:890c12951e96 48
jksoft 0:890c12951e96 49 ESP8266 * ESP8266::inst;
jksoft 0:890c12951e96 50 char* ip = NULL;
jksoft 0:890c12951e96 51
jksoft 0:890c12951e96 52 ESP8266::ESP8266(PinName tx, PinName rx, PinName reset, const char *ssid, const char *phrase, uint32_t baud) :
jksoft 0:890c12951e96 53 wifi(tx, rx), reset_pin(reset), buf_ESP8266(ESP_MBUFFE_MAX)
jksoft 0:890c12951e96 54 {
jksoft 0:890c12951e96 55 INFO("Initializing ESP8266 object");
jksoft 0:890c12951e96 56 memset(&state, 0, sizeof(state));
jksoft 0:890c12951e96 57
jksoft 0:890c12951e96 58
jksoft 0:890c12951e96 59 strcpy(this->ssid, ssid);
jksoft 0:890c12951e96 60 strcpy(this->phrase, phrase);
jksoft 0:890c12951e96 61 inst = this;
jksoft 0:890c12951e96 62 attach_rx(false);
jksoft 0:890c12951e96 63
jksoft 0:890c12951e96 64 wifi.baud(baud); // initial baud rate of the ESP8266
jksoft 0:890c12951e96 65
jksoft 0:890c12951e96 66 state.associated = false;
jksoft 0:890c12951e96 67 state.cmdMode = false;
jksoft 0:890c12951e96 68 }
jksoft 0:890c12951e96 69
jksoft 0:890c12951e96 70 bool ESP8266::join()
jksoft 0:890c12951e96 71 {
jksoft 0:890c12951e96 72 char cmd[100];
jksoft 0:890c12951e96 73 sendCommand( "AT+CWMODE=1", "change", NULL, 1000);
jksoft 0:890c12951e96 74 //string cmd="AT+CWJAP=\""+(string)this->ssid+"\",\""+(string)this->phrase+"\"";
jksoft 0:890c12951e96 75 sprintf(cmd,"AT+CWJAP=\"%s\",\"%s\"",this->ssid,this->phrase);
jksoft 0:890c12951e96 76 if( sendCommand( cmd, "OK", NULL, 10000) ) {
jksoft 0:890c12951e96 77 // successfully joined the network
jksoft 0:890c12951e96 78 state.associated = true;
jksoft 0:890c12951e96 79 INFO("ssid: %s, phrase: %s", this->ssid, this->phrase);
jksoft 0:890c12951e96 80 return true;
jksoft 0:890c12951e96 81 }
jksoft 0:890c12951e96 82 return false;
jksoft 0:890c12951e96 83 }
jksoft 0:890c12951e96 84
jksoft 0:890c12951e96 85 bool ESP8266::connect()
jksoft 0:890c12951e96 86 {
jksoft 0:890c12951e96 87 sendCommand("AT+CWDHCP=1,1","OK",NULL,1000); // DHCP Enabled in Station Mode
jksoft 0:890c12951e96 88 return ESP8266::join();
jksoft 0:890c12951e96 89 }
jksoft 0:890c12951e96 90
jksoft 0:890c12951e96 91 bool ESP8266::is_connected()
jksoft 0:890c12951e96 92 {
jksoft 0:890c12951e96 93 return true;
jksoft 0:890c12951e96 94 }
jksoft 0:890c12951e96 95
jksoft 0:890c12951e96 96 bool ESP8266::start(bool type,char* ip, int port, int id)
jksoft 0:890c12951e96 97 {
jksoft 0:890c12951e96 98 char cmd[256];
jksoft 0:890c12951e96 99 // Error Check
jksoft 0:890c12951e96 100 if(id > ESP_MAXID) {
jksoft 0:890c12951e96 101 ERR("startUDPMulti: max id is: %d, id given is %d",ESP_MAXID,id);
jksoft 0:890c12951e96 102 return false;
jksoft 0:890c12951e96 103 }
jksoft 0:890c12951e96 104 // Single Connection Mode
jksoft 0:890c12951e96 105 if(id < 0) {
jksoft 0:890c12951e96 106 DBG("Start Single Connection Mode");
jksoft 0:890c12951e96 107 //char portstr[5];
jksoft 0:890c12951e96 108 bool check [3] = {0};
jksoft 0:890c12951e96 109 //sprintf(portstr, "%d", port);
jksoft 0:890c12951e96 110 switch(type) {
jksoft 0:890c12951e96 111 case ESP_UDP_TYPE : //UDP
jksoft 0:890c12951e96 112 sprintf(cmd,"AT+CIPSTART=\"UDP\",\"%s\",%d",ip,port);
jksoft 0:890c12951e96 113 check[0] = sendCommand(cmd, "OK", NULL, 10000);
jksoft 0:890c12951e96 114 //check[0] = sendCommand(( "AT+CIPSTART=\"UDP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
jksoft 0:890c12951e96 115 break;
jksoft 0:890c12951e96 116 case ESP_TCP_TYPE : //TCP
jksoft 0:890c12951e96 117 sprintf(cmd,"AT+CIPSTART=\"TCP\",\"%s\",%d",ip,port);
jksoft 0:890c12951e96 118 check[0] = sendCommand(cmd, "OK", NULL, 10000);
jksoft 0:890c12951e96 119 //check[0] = sendCommand(( "AT+CIPSTART=\"TCP\",\"" + (string) ip + "\"," + (string) portstr ).c_str(), "OK", NULL, 10000);
jksoft 0:890c12951e96 120 break;
jksoft 0:890c12951e96 121 default:
jksoft 0:890c12951e96 122 ERR("Default hit for starting connection, this shouldnt be possible!!");
jksoft 0:890c12951e96 123 break;
jksoft 0:890c12951e96 124 }
jksoft 0:890c12951e96 125 check[1] = sendCommand("AT+CIPMODE=1", "OK", NULL, 1000);// go into transparent mode
jksoft 0:890c12951e96 126 check[2] = sendCommand("AT+CIPSEND", ">", NULL, 1000);// go into transparent mode
jksoft 0:890c12951e96 127 // check that all commands were sucessful
jksoft 0:890c12951e96 128 if(check[0] and check[1] and check[2]) {
jksoft 0:890c12951e96 129 state.cmdMode = false;
jksoft 0:890c12951e96 130 return true;
jksoft 0:890c12951e96 131 } else {
jksoft 0:890c12951e96 132 ERR("startUDPTransparent Failed for ip:%s, port:%d",ip,port);
jksoft 0:890c12951e96 133 return false;
jksoft 0:890c12951e96 134 }
jksoft 0:890c12951e96 135 }
jksoft 0:890c12951e96 136 // Multi Connection Mode
jksoft 0:890c12951e96 137 else {
jksoft 0:890c12951e96 138 //TODO: impliment Multi Connection Mode
jksoft 0:890c12951e96 139 ERR("Not currently Supported!");
jksoft 0:890c12951e96 140 return false;
jksoft 0:890c12951e96 141 }
jksoft 0:890c12951e96 142 }
jksoft 0:890c12951e96 143
jksoft 0:890c12951e96 144 bool ESP8266::startUDP(char* ip, int port, int id, int length)
jksoft 0:890c12951e96 145 {
jksoft 0:890c12951e96 146 char cmd[256];
jksoft 0:890c12951e96 147 char portstr[5];
jksoft 0:890c12951e96 148 char idstr[1];
jksoft 0:890c12951e96 149 char lenstr[2];
jksoft 0:890c12951e96 150
jksoft 0:890c12951e96 151 sprintf(portstr, "%d", port);
jksoft 0:890c12951e96 152 sprintf(idstr, "%d", id);
jksoft 0:890c12951e96 153 sprintf(lenstr, "%d", length);
jksoft 0:890c12951e96 154
jksoft 0:890c12951e96 155 sendCommand("AT+CIPMUX=1", "OK", NULL, 1000);
jksoft 0:890c12951e96 156 sprintf(cmd,"AT+CIPSTART=%d,\"UDP\",\"%s\",%d,1112,0",id,ip,port);
jksoft 0:890c12951e96 157 sendCommand(cmd, "OK", NULL, 10000);
jksoft 0:890c12951e96 158 //sendCommand(( "AT+CIPSTART=" + string(idstr) + ",\"UDP\",\"" + (string) ip + "\"," + (string) portstr + ",1112,0").c_str(), "OK", NULL, 10000);
jksoft 0:890c12951e96 159 sprintf(cmd,"AT+CIPSEND=%d,%d",id,length);
jksoft 0:890c12951e96 160 sendCommand(cmd, ">", NULL, 1000);// go into transparent mode
jksoft 0:890c12951e96 161 //sendCommand(("AT+CIPSEND=" + (string)idstr + "," + (string)lenstr).c_str(), ">", NULL, 1000);// go into transparent mode
jksoft 0:890c12951e96 162 DBG("Data Mode\r\n");
jksoft 0:890c12951e96 163 state.cmdMode = false;
jksoft 0:890c12951e96 164
jksoft 0:890c12951e96 165 return true;
jksoft 0:890c12951e96 166 }
jksoft 0:890c12951e96 167
jksoft 0:890c12951e96 168 bool ESP8266::startTCPServer(int port)
jksoft 0:890c12951e96 169 {
jksoft 0:890c12951e96 170 char cmd[100];
jksoft 0:890c12951e96 171 bool command_results[3];
jksoft 0:890c12951e96 172 command_results[0]=sendCommand("AT+CWMODE=3", "OK", NULL, 1000);
jksoft 0:890c12951e96 173 command_results[1]=sendCommand("AT+CIPMUX=1", "OK", NULL, 1000);
jksoft 0:890c12951e96 174 if(port == 333){
jksoft 0:890c12951e96 175 command_results[2]=sendCommand("AT+CIPSERVER=1", "OK", NULL, 1000);
jksoft 0:890c12951e96 176 }
jksoft 0:890c12951e96 177 else{
jksoft 0:890c12951e96 178 sprintf(cmd,"AT+CIPSERVER=1,%d",port);
jksoft 0:890c12951e96 179 command_results[2]=sendCommand(cmd, "OK", NULL, 1000);
jksoft 0:890c12951e96 180 }
jksoft 0:890c12951e96 181 //sendCommand("AT+CIFSR", "OK", NULL, 1000);
jksoft 0:890c12951e96 182 DBG("Data Mode\r\n");
jksoft 0:890c12951e96 183 state.cmdMode = false;
jksoft 0:890c12951e96 184 if (command_results[0] and command_results[1] and command_results[2]){
jksoft 0:890c12951e96 185 return true;
jksoft 0:890c12951e96 186 }
jksoft 0:890c12951e96 187 else{
jksoft 0:890c12951e96 188 return false;
jksoft 0:890c12951e96 189 }
jksoft 0:890c12951e96 190 }
jksoft 0:890c12951e96 191
jksoft 0:890c12951e96 192 bool ESP8266::close()
jksoft 0:890c12951e96 193 {
jksoft 0:890c12951e96 194 wait(0.1f);
jksoft 0:890c12951e96 195 send("+++",3);
jksoft 0:890c12951e96 196 wait(1.0f);
jksoft 0:890c12951e96 197 state.cmdMode = true;
jksoft 0:890c12951e96 198 sendCommand("AT+CIPCLOSE","OK", NULL, 10000);
jksoft 0:890c12951e96 199 return true;
jksoft 0:890c12951e96 200 }
jksoft 0:890c12951e96 201
jksoft 0:890c12951e96 202 bool ESP8266::disconnect()
jksoft 0:890c12951e96 203 {
jksoft 0:890c12951e96 204 // if already disconnected, return
jksoft 0:890c12951e96 205 if (!state.associated)
jksoft 0:890c12951e96 206 return true;
jksoft 0:890c12951e96 207 // send command to quit AP
jksoft 0:890c12951e96 208 sendCommand("AT+CWQAP", "OK", NULL, 10000);
jksoft 0:890c12951e96 209 state.associated = false;
jksoft 0:890c12951e96 210 return true;
jksoft 0:890c12951e96 211 }
jksoft 0:890c12951e96 212
jksoft 0:890c12951e96 213 int ESP8266::strfind(const char *str,const char *chkstr,int pos)
jksoft 0:890c12951e96 214 {
jksoft 0:890c12951e96 215 if( (strlen(str)-pos) < strlen(chkstr) ) return(-1);
jksoft 0:890c12951e96 216
jksoft 0:890c12951e96 217
jksoft 0:890c12951e96 218 for(int i=pos;i<strlen(str);i++)
jksoft 0:890c12951e96 219 {
jksoft 0:890c12951e96 220 if(memcmp(chkstr,&str[i],strlen(chkstr))==0)
jksoft 0:890c12951e96 221 {
jksoft 0:890c12951e96 222 return(i);
jksoft 0:890c12951e96 223 }
jksoft 0:890c12951e96 224 }
jksoft 0:890c12951e96 225 return(-1);
jksoft 0:890c12951e96 226 }
jksoft 0:890c12951e96 227
jksoft 0:890c12951e96 228 char* ESP8266::substr(const char *str , char *outstr , int pos1 , int pos2 )
jksoft 0:890c12951e96 229 {
jksoft 0:890c12951e96 230 int size = pos2 - pos1;
jksoft 0:890c12951e96 231
jksoft 0:890c12951e96 232 memcpy(outstr , &str[pos1] , size );
jksoft 0:890c12951e96 233 outstr[size] = '\0';
jksoft 0:890c12951e96 234
jksoft 0:890c12951e96 235 return(outstr);
jksoft 0:890c12951e96 236 }
jksoft 0:890c12951e96 237
jksoft 0:890c12951e96 238 int ESP8266::strcount(const char *str , char countstr )
jksoft 0:890c12951e96 239 {
jksoft 0:890c12951e96 240 int ret = 0;
jksoft 0:890c12951e96 241
jksoft 0:890c12951e96 242 for(int i = 0 ; i < strlen(str) ; i++)
jksoft 0:890c12951e96 243 {
jksoft 0:890c12951e96 244 if( str[i] == countstr )
jksoft 0:890c12951e96 245 {
jksoft 0:890c12951e96 246 ret++;
jksoft 0:890c12951e96 247 }
jksoft 0:890c12951e96 248
jksoft 0:890c12951e96 249 }
jksoft 0:890c12951e96 250
jksoft 0:890c12951e96 251 return(ret);
jksoft 0:890c12951e96 252 }
jksoft 0:890c12951e96 253
jksoft 0:890c12951e96 254 /*
jksoft 0:890c12951e96 255 Assuming Returned data looks like this:
jksoft 0:890c12951e96 256 +CIFSR:STAIP,"192.168.11.2"
jksoft 0:890c12951e96 257 +CIFSR:STAMAC,"18:fe:34:9f:3a:f5"
jksoft 0:890c12951e96 258 grabbing IP from first set of quotation marks
jksoft 0:890c12951e96 259 */
jksoft 0:890c12951e96 260 char* ESP8266::getIPAddress()
jksoft 0:890c12951e96 261 {
jksoft 0:890c12951e96 262 char result[30] = {0};
jksoft 0:890c12951e96 263 char tmp[30] = {0};
jksoft 0:890c12951e96 264 int check = 0;
jksoft 0:890c12951e96 265 check = sendCommand("AT+CIFSR", NULL, result, 1000);
jksoft 0:890c12951e96 266 //pc.printf("\r\nReceivedInfo for IP Command is: %s\r\n",result);
jksoft 0:890c12951e96 267 ip = ipString;
jksoft 0:890c12951e96 268 if(check) {
jksoft 0:890c12951e96 269 // Success
jksoft 0:890c12951e96 270 uint8_t pos1 = 0, pos2 = 0;
jksoft 0:890c12951e96 271 //uint8_t pos3 = 0, pos4 = 0;
jksoft 0:890c12951e96 272 pos1 = strfind(result,"+CIFSR:STAIP",0);
jksoft 0:890c12951e96 273 //pos1 = resultString.find("+CIFSR:STAIP");
jksoft 0:890c12951e96 274 pos1 = strfind(result,"\"",pos1);
jksoft 0:890c12951e96 275 //pos1 = resultString.find('"',pos1);
jksoft 0:890c12951e96 276 pos2 = strfind(result,"\"",pos1+1);
jksoft 0:890c12951e96 277 //pos2 = resultString.find('"',pos1+1);
jksoft 0:890c12951e96 278 //pos3 = resultString.find('"',pos2+1); //would find mac address
jksoft 0:890c12951e96 279 //pos4 = resultString.find('"',pos3+1);
jksoft 0:890c12951e96 280 strncpy(ipString,substr(result,tmp,pos1,pos2),sizeof(ipString));
jksoft 0:890c12951e96 281 INFO("IP: %s",ipString);
jksoft 0:890c12951e96 282 ip = ipString;
jksoft 0:890c12951e96 283
jksoft 0:890c12951e96 284 } else {
jksoft 0:890c12951e96 285 // Failure
jksoft 0:890c12951e96 286 ERR("getIPAddress() failed");
jksoft 0:890c12951e96 287 ip = NULL;
jksoft 0:890c12951e96 288 }
jksoft 0:890c12951e96 289 return ip;
jksoft 0:890c12951e96 290 }
jksoft 0:890c12951e96 291
jksoft 0:890c12951e96 292 bool ESP8266::gethostbyname(const char * host, char * ip)
jksoft 0:890c12951e96 293 {
jksoft 0:890c12951e96 294 int nb_digits = 0;
jksoft 0:890c12951e96 295 char tmp[100];
jksoft 0:890c12951e96 296
jksoft 0:890c12951e96 297 strcpy(ip,host);
jksoft 0:890c12951e96 298
jksoft 0:890c12951e96 299 return true;
jksoft 0:890c12951e96 300
jksoft 0:890c12951e96 301 // no dns needed
jksoft 0:890c12951e96 302 int pos = strfind(host,".",0);
jksoft 0:890c12951e96 303 if (pos != -1) {
jksoft 0:890c12951e96 304 nb_digits = atoi(substr(host,tmp,0,pos));
jksoft 0:890c12951e96 305 }
jksoft 0:890c12951e96 306 //printf("substrL %s\r\n", sub.c_str());
jksoft 0:890c12951e96 307 if (strcount(host,'.') == 3 && nb_digits > 0) {
jksoft 0:890c12951e96 308 strcpy(ip, host);
jksoft 0:890c12951e96 309 return true;
jksoft 0:890c12951e96 310 } else {
jksoft 0:890c12951e96 311 // dns needed, not currently available
jksoft 0:890c12951e96 312 ERR("gethostbyname(): DNS Not currently available, only use IP Addresses!");
jksoft 0:890c12951e96 313 return false;
jksoft 0:890c12951e96 314 }
jksoft 0:890c12951e96 315 }
jksoft 0:890c12951e96 316
jksoft 0:890c12951e96 317 void ESP8266::reset()
jksoft 0:890c12951e96 318 {
jksoft 0:890c12951e96 319 reset_pin = 0;
jksoft 0:890c12951e96 320 wait_us(20);
jksoft 0:890c12951e96 321 reset_pin = 1;
jksoft 0:890c12951e96 322 //wait(1);
jksoft 0:890c12951e96 323 //reset_pin = !reset_pin
jksoft 0:890c12951e96 324 //send("+++",3);
jksoft 0:890c12951e96 325 wait(1);
jksoft 0:890c12951e96 326 state.cmdMode = true;
jksoft 0:890c12951e96 327 sendCommand("AT", "OK", NULL, 1000);
jksoft 0:890c12951e96 328 sendCommand("AT+RST", "ready", NULL, 10000);
jksoft 0:890c12951e96 329 state.associated = false;
jksoft 0:890c12951e96 330
jksoft 0:890c12951e96 331 }
jksoft 0:890c12951e96 332
jksoft 0:890c12951e96 333 bool ESP8266::reboot()
jksoft 0:890c12951e96 334 {
jksoft 0:890c12951e96 335 reset();
jksoft 0:890c12951e96 336 return true;
jksoft 0:890c12951e96 337 }
jksoft 0:890c12951e96 338
jksoft 0:890c12951e96 339 void ESP8266::handler_rx(void)
jksoft 0:890c12951e96 340 {
jksoft 0:890c12951e96 341 //read characters
jksoft 0:890c12951e96 342 char c;
jksoft 0:890c12951e96 343 while (wifi.readable()) {
jksoft 0:890c12951e96 344 c=wifi.getc();
jksoft 0:890c12951e96 345 buf_ESP8266.queue(c);
jksoft 0:890c12951e96 346 //if (state.cmdMode) pc.printf("%c",c); //debug echo, needs fast serial console to prevent UART overruns
jksoft 0:890c12951e96 347 }
jksoft 0:890c12951e96 348 }
jksoft 0:890c12951e96 349
jksoft 0:890c12951e96 350 void ESP8266::attach_rx(bool callback)
jksoft 0:890c12951e96 351 {
jksoft 0:890c12951e96 352 if (!callback) {
jksoft 0:890c12951e96 353 wifi.attach(NULL);
jksoft 0:890c12951e96 354 }
jksoft 0:890c12951e96 355 else {
jksoft 0:890c12951e96 356 wifi.attach(this, &ESP8266::handler_rx);
jksoft 0:890c12951e96 357 }
jksoft 0:890c12951e96 358 }
jksoft 0:890c12951e96 359
jksoft 0:890c12951e96 360 int ESP8266::readable()
jksoft 0:890c12951e96 361 {
jksoft 0:890c12951e96 362 return buf_ESP8266.available();
jksoft 0:890c12951e96 363 }
jksoft 0:890c12951e96 364
jksoft 0:890c12951e96 365 int ESP8266::writeable()
jksoft 0:890c12951e96 366 {
jksoft 0:890c12951e96 367 return wifi.writeable();
jksoft 0:890c12951e96 368 }
jksoft 0:890c12951e96 369
jksoft 0:890c12951e96 370 char ESP8266::getc()
jksoft 0:890c12951e96 371 {
jksoft 0:890c12951e96 372 char c=0;
jksoft 0:890c12951e96 373 while (!buf_ESP8266.available());
jksoft 0:890c12951e96 374 buf_ESP8266.dequeue(&c);
jksoft 0:890c12951e96 375 return c;
jksoft 0:890c12951e96 376 }
jksoft 0:890c12951e96 377
jksoft 0:890c12951e96 378 int ESP8266::putc(char c)
jksoft 0:890c12951e96 379 {
jksoft 0:890c12951e96 380 while (!wifi.writeable() || wifi.readable()); //wait for echoed command characters to be read first
jksoft 0:890c12951e96 381 return wifi.putc(c);
jksoft 0:890c12951e96 382 }
jksoft 0:890c12951e96 383
jksoft 0:890c12951e96 384 void ESP8266::flush()
jksoft 0:890c12951e96 385 {
jksoft 0:890c12951e96 386 buf_ESP8266.flush();
jksoft 0:890c12951e96 387 }
jksoft 0:890c12951e96 388
jksoft 0:890c12951e96 389 int ESP8266::send(const char * buf, int len)
jksoft 0:890c12951e96 390 {
jksoft 0:890c12951e96 391 //TODO: need to add handler for data > 2048B, this is the max packet size of the ESP8266.
jksoft 0:890c12951e96 392 if(len >= 2048){
jksoft 0:890c12951e96 393 WARN("send buffer >= 2048B, need to chunk this up to be less.");
jksoft 0:890c12951e96 394 }
jksoft 0:890c12951e96 395 const char* bufptr=buf;
jksoft 0:890c12951e96 396 for(int i=0; i<len; i++) {
jksoft 0:890c12951e96 397 putc((int)*bufptr++);
jksoft 0:890c12951e96 398 }
jksoft 0:890c12951e96 399 return len;
jksoft 0:890c12951e96 400 }
jksoft 0:890c12951e96 401
jksoft 0:890c12951e96 402 bool ESP8266::sendCommand(const char * cmd, const char * ACK, char * res, int timeout)
jksoft 0:890c12951e96 403 {
jksoft 0:890c12951e96 404 char read;
jksoft 0:890c12951e96 405 char checking[512] = "";
jksoft 0:890c12951e96 406 int checking_size = 0;
jksoft 0:890c12951e96 407 int fond = -1;
jksoft 0:890c12951e96 408 Timer tmr;
jksoft 0:890c12951e96 409 int result = 0;
jksoft 0:890c12951e96 410
jksoft 0:890c12951e96 411 DBG("sendCmd:\t %s",cmd);
jksoft 0:890c12951e96 412
jksoft 0:890c12951e96 413 attach_rx(true);
jksoft 0:890c12951e96 414
jksoft 0:890c12951e96 415 //We flush the buffer
jksoft 0:890c12951e96 416 while (readable())
jksoft 0:890c12951e96 417 getc();
jksoft 0:890c12951e96 418
jksoft 0:890c12951e96 419 if (!ACK || !strcmp(ACK, "NO")) {
jksoft 0:890c12951e96 420 for (int i = 0; i < strlen(cmd); i++) {
jksoft 0:890c12951e96 421 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
jksoft 0:890c12951e96 422 wait(0.005f); // prevents stuck recieve ready (?) need to let echoed character get read first
jksoft 0:890c12951e96 423 }
jksoft 0:890c12951e96 424 putc(13); //CR
jksoft 0:890c12951e96 425 wait(0.005f); // wait for echo
jksoft 0:890c12951e96 426 putc(10); //LF
jksoft 0:890c12951e96 427
jksoft 0:890c12951e96 428 } else {
jksoft 0:890c12951e96 429 //We flush the buffer
jksoft 0:890c12951e96 430 while (readable())
jksoft 0:890c12951e96 431 getc();
jksoft 0:890c12951e96 432
jksoft 0:890c12951e96 433 tmr.start();
jksoft 0:890c12951e96 434 for (int i = 0; i < strlen(cmd); i++) {
jksoft 0:890c12951e96 435 result = (putc(cmd[i]) == cmd[i]) ? result + 1 : result;
jksoft 0:890c12951e96 436 wait(.005); // wait for echo
jksoft 0:890c12951e96 437 }
jksoft 0:890c12951e96 438 putc(13); //CR
jksoft 0:890c12951e96 439 wait(0.005f); // wait for echo
jksoft 0:890c12951e96 440 putc(10); //LF
jksoft 0:890c12951e96 441
jksoft 0:890c12951e96 442 while (1) {
jksoft 0:890c12951e96 443 if (tmr.read_ms() > timeout) {
jksoft 0:890c12951e96 444 //We flush the buffer
jksoft 0:890c12951e96 445 while (readable())
jksoft 0:890c12951e96 446 getc();
jksoft 0:890c12951e96 447
jksoft 0:890c12951e96 448 DBG("check:\t %s", checking);
jksoft 0:890c12951e96 449
jksoft 0:890c12951e96 450 attach_rx(true);
jksoft 0:890c12951e96 451 return -1;
jksoft 0:890c12951e96 452 } else if (readable()) {
jksoft 0:890c12951e96 453 read = getc();
jksoft 0:890c12951e96 454 //pc.printf("%c",read); //debug echo
jksoft 0:890c12951e96 455 if ( read != '\r' && read != '\n') {
jksoft 0:890c12951e96 456 checking[checking_size] = read;
jksoft 0:890c12951e96 457 checking_size++;
jksoft 0:890c12951e96 458 checking[checking_size] = '\0';
jksoft 0:890c12951e96 459 fond = strfind(checking,ACK,0);
jksoft 0:890c12951e96 460 if (fond != -1) {
jksoft 0:890c12951e96 461 wait(0.01f);
jksoft 0:890c12951e96 462
jksoft 0:890c12951e96 463 //We flush the buffer
jksoft 0:890c12951e96 464 while (readable())
jksoft 0:890c12951e96 465 read = getc();
jksoft 0:890c12951e96 466 //printf("%c",read); //debug echo
jksoft 0:890c12951e96 467 break;
jksoft 0:890c12951e96 468 }
jksoft 0:890c12951e96 469 }
jksoft 0:890c12951e96 470 }
jksoft 0:890c12951e96 471 }
jksoft 0:890c12951e96 472 DBG("check: %s", checking);
jksoft 0:890c12951e96 473
jksoft 0:890c12951e96 474 attach_rx(true);
jksoft 0:890c12951e96 475 return result;
jksoft 0:890c12951e96 476 }
jksoft 0:890c12951e96 477
jksoft 0:890c12951e96 478 //the user wants the result from the command (ACK == NULL, res != NULL)
jksoft 0:890c12951e96 479 if (res != NULL) {
jksoft 0:890c12951e96 480 int i = 0;
jksoft 0:890c12951e96 481 Timer timeout;
jksoft 0:890c12951e96 482 timeout.start();
jksoft 0:890c12951e96 483 tmr.reset();
jksoft 0:890c12951e96 484 while (1) {
jksoft 0:890c12951e96 485 if (timeout.read() > 2) {
jksoft 0:890c12951e96 486 if (i == 0) {
jksoft 0:890c12951e96 487 res = NULL;
jksoft 0:890c12951e96 488 break;
jksoft 0:890c12951e96 489 }
jksoft 0:890c12951e96 490 res[i] = '\0';
jksoft 0:890c12951e96 491 DBG("user str 1: %s", res);
jksoft 0:890c12951e96 492
jksoft 0:890c12951e96 493 break;
jksoft 0:890c12951e96 494 } else {
jksoft 0:890c12951e96 495 if (tmr.read_ms() > 300) {
jksoft 0:890c12951e96 496 res[i] = '\0';
jksoft 0:890c12951e96 497 DBG("user str: %s", res);
jksoft 0:890c12951e96 498
jksoft 0:890c12951e96 499 break;
jksoft 0:890c12951e96 500 }
jksoft 0:890c12951e96 501 if (readable()) {
jksoft 0:890c12951e96 502 tmr.start();
jksoft 0:890c12951e96 503 read = getc();
jksoft 0:890c12951e96 504
jksoft 0:890c12951e96 505 // we drop \r and \n
jksoft 0:890c12951e96 506 if ( read != '\r' && read != '\n') {
jksoft 0:890c12951e96 507 res[i++] = read;
jksoft 0:890c12951e96 508 }
jksoft 0:890c12951e96 509 }
jksoft 0:890c12951e96 510 }
jksoft 0:890c12951e96 511 }
jksoft 0:890c12951e96 512 DBG("user str: %s", res);
jksoft 0:890c12951e96 513 }
jksoft 0:890c12951e96 514
jksoft 0:890c12951e96 515 //We flush the buffer
jksoft 0:890c12951e96 516 while (readable())
jksoft 0:890c12951e96 517 getc();
jksoft 0:890c12951e96 518
jksoft 0:890c12951e96 519 attach_rx(true);
jksoft 0:890c12951e96 520 DBG("result: %d", result)
jksoft 0:890c12951e96 521 return result;
jksoft 0:890c12951e96 522 }