test

Dependencies:   BME280 SB1602E

Fork of MultiIoTBoardLib by Junichi Katsu

Committer:
jksoft
Date:
Fri Jul 28 02:23:25 2017 +0000
Revision:
0:bad9495b4215
First edition

Who changed what in which revision?

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