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