Simpe IoT BoardにGrove温度センサを繋げてIFTTTにプッシュするプログラムです。

Dependencies:   mbed

Committer:
jksoft
Date:
Fri Nov 13 07:55:43 2015 +0000
Revision:
0:26b07836cf44
??

Who changed what in which revision?

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