データの保存、更新、取得ができるWebサービス「milkcocoa」に接続し、データのプッシュ、送信、取得ができるライブラリを使ったサンプルです。ESP8266版 https://mlkcca.com/

Dependencies:   Milkcocoa mbed

Dependents:   M2m

Committer:
jksoft
Date:
Fri Dec 18 04:47:41 2015 +0000
Revision:
0:82d5445a9461
??

Who changed what in which revision?

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