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