pengfei wu
/
ESP8266Program
For test
ESP8266.cpp@10:9d4ec0359a5c, 2015-02-02 (annotated)
- Committer:
- shennongmin
- Date:
- Mon Feb 02 09:46:16 2015 +0000
- Revision:
- 10:9d4ec0359a5c
- Child:
- 11:7bd1b2a67b1a
Style remained
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shennongmin | 10:9d4ec0359a5c | 1 | #include "ESP8266.h" |
shennongmin | 10:9d4ec0359a5c | 2 | |
shennongmin | 10:9d4ec0359a5c | 3 | #define ESP8266_DEBUG |
shennongmin | 10:9d4ec0359a5c | 4 | |
shennongmin | 10:9d4ec0359a5c | 5 | #ifdef ESP8266_DEBUG |
shennongmin | 10:9d4ec0359a5c | 6 | #define DBG(message) printf(message) |
shennongmin | 10:9d4ec0359a5c | 7 | #else |
shennongmin | 10:9d4ec0359a5c | 8 | #define DBG(message) do{}while(0) |
shennongmin | 10:9d4ec0359a5c | 9 | #endif |
shennongmin | 10:9d4ec0359a5c | 10 | |
shennongmin | 10:9d4ec0359a5c | 11 | |
shennongmin | 10:9d4ec0359a5c | 12 | ESP8266::ESP8266(PinName tx, PinName rx):esp_uart(tx, rx) { |
shennongmin | 10:9d4ec0359a5c | 13 | |
shennongmin | 10:9d4ec0359a5c | 14 | } |
shennongmin | 10:9d4ec0359a5c | 15 | |
shennongmin | 10:9d4ec0359a5c | 16 | int ESP8266::get_chlID(void) { |
shennongmin | 10:9d4ec0359a5c | 17 | return chlID; |
shennongmin | 10:9d4ec0359a5c | 18 | } |
shennongmin | 10:9d4ec0359a5c | 19 | |
shennongmin | 10:9d4ec0359a5c | 20 | void ESP8266::begin(void) |
shennongmin | 10:9d4ec0359a5c | 21 | { |
shennongmin | 10:9d4ec0359a5c | 22 | bool result = false; |
shennongmin | 10:9d4ec0359a5c | 23 | esp_uart.begin(9600); //The default baud rate of ESP8266 is 115200 |
shennongmin | 10:9d4ec0359a5c | 24 | |
shennongmin | 10:9d4ec0359a5c | 25 | esp_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 26 | esp_uart.setTimeout(5000); |
shennongmin | 10:9d4ec0359a5c | 27 | esp_uart.println("AT+RST"); |
shennongmin | 10:9d4ec0359a5c | 28 | DBG("AT+RST\r\n"); |
shennongmin | 10:9d4ec0359a5c | 29 | result = esp_uart.find("eady"); |
shennongmin | 10:9d4ec0359a5c | 30 | if(result) |
shennongmin | 10:9d4ec0359a5c | 31 | DBG("Module is ready\r\n"); |
shennongmin | 10:9d4ec0359a5c | 32 | else |
shennongmin | 10:9d4ec0359a5c | 33 | { |
shennongmin | 10:9d4ec0359a5c | 34 | DBG("Module have no response\r\n"); |
shennongmin | 10:9d4ec0359a5c | 35 | while(1); |
shennongmin | 10:9d4ec0359a5c | 36 | } |
shennongmin | 10:9d4ec0359a5c | 37 | |
shennongmin | 10:9d4ec0359a5c | 38 | } |
shennongmin | 10:9d4ec0359a5c | 39 | |
shennongmin | 10:9d4ec0359a5c | 40 | |
shennongmin | 10:9d4ec0359a5c | 41 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 42 | //Initialize port |
shennongmin | 10:9d4ec0359a5c | 43 | |
shennongmin | 10:9d4ec0359a5c | 44 | mode: setting operation mode |
shennongmin | 10:9d4ec0359a5c | 45 | |
shennongmin | 10:9d4ec0359a5c | 46 | chl: channel number |
shennongmin | 10:9d4ec0359a5c | 47 | ecn: encryption |
shennongmin | 10:9d4ec0359a5c | 48 | |
shennongmin | 10:9d4ec0359a5c | 49 | return: |
shennongmin | 10:9d4ec0359a5c | 50 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 51 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 52 | |
shennongmin | 10:9d4ec0359a5c | 53 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 54 | bool ESP8266::Initialize(uint8_t mode, String ssid, String pwd, uint8_t chl, uint8_t ecn) |
shennongmin | 10:9d4ec0359a5c | 55 | { |
shennongmin | 10:9d4ec0359a5c | 56 | if (mode == ESP8266_MODE_STA) |
shennongmin | 10:9d4ec0359a5c | 57 | { |
shennongmin | 10:9d4ec0359a5c | 58 | bool b = confMode(mode); |
shennongmin | 10:9d4ec0359a5c | 59 | if (!b) |
shennongmin | 10:9d4ec0359a5c | 60 | { |
shennongmin | 10:9d4ec0359a5c | 61 | return false; |
shennongmin | 10:9d4ec0359a5c | 62 | } |
shennongmin | 10:9d4ec0359a5c | 63 | Reset(); |
shennongmin | 10:9d4ec0359a5c | 64 | confJAP(ssid, pwd); |
shennongmin | 10:9d4ec0359a5c | 65 | } |
shennongmin | 10:9d4ec0359a5c | 66 | else if (mode == ESP8266_MODE_SAP) |
shennongmin | 10:9d4ec0359a5c | 67 | { |
shennongmin | 10:9d4ec0359a5c | 68 | bool b = confMode(mode); |
shennongmin | 10:9d4ec0359a5c | 69 | if (!b) |
shennongmin | 10:9d4ec0359a5c | 70 | { |
shennongmin | 10:9d4ec0359a5c | 71 | return false; |
shennongmin | 10:9d4ec0359a5c | 72 | } |
shennongmin | 10:9d4ec0359a5c | 73 | Reset(); |
shennongmin | 10:9d4ec0359a5c | 74 | confSAP(ssid, pwd, chl, ecn); |
shennongmin | 10:9d4ec0359a5c | 75 | } |
shennongmin | 10:9d4ec0359a5c | 76 | else if (mode == ESP8266_MODE_STA_SAP) |
shennongmin | 10:9d4ec0359a5c | 77 | { |
shennongmin | 10:9d4ec0359a5c | 78 | bool b = confMode(mode); |
shennongmin | 10:9d4ec0359a5c | 79 | if (!b) |
shennongmin | 10:9d4ec0359a5c | 80 | { |
shennongmin | 10:9d4ec0359a5c | 81 | return false; |
shennongmin | 10:9d4ec0359a5c | 82 | } |
shennongmin | 10:9d4ec0359a5c | 83 | Reset(); |
shennongmin | 10:9d4ec0359a5c | 84 | confJAP(ssid, pwd); |
shennongmin | 10:9d4ec0359a5c | 85 | confSAP(ssid, pwd, chl, ecn); |
shennongmin | 10:9d4ec0359a5c | 86 | } |
shennongmin | 10:9d4ec0359a5c | 87 | |
shennongmin | 10:9d4ec0359a5c | 88 | return true; |
shennongmin | 10:9d4ec0359a5c | 89 | } |
shennongmin | 10:9d4ec0359a5c | 90 | |
shennongmin | 10:9d4ec0359a5c | 91 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 92 | //Set up tcp or udp connection |
shennongmin | 10:9d4ec0359a5c | 93 | |
shennongmin | 10:9d4ec0359a5c | 94 | type: tcp or udp |
shennongmin | 10:9d4ec0359a5c | 95 | |
shennongmin | 10:9d4ec0359a5c | 96 | addr: ip address |
shennongmin | 10:9d4ec0359a5c | 97 | |
shennongmin | 10:9d4ec0359a5c | 98 | port: port number |
shennongmin | 10:9d4ec0359a5c | 99 | |
shennongmin | 10:9d4ec0359a5c | 100 | a: set multiple connection |
shennongmin | 10:9d4ec0359a5c | 101 | 0 for sigle connection |
shennongmin | 10:9d4ec0359a5c | 102 | 1 for multiple connection |
shennongmin | 10:9d4ec0359a5c | 103 | |
shennongmin | 10:9d4ec0359a5c | 104 | id: id number(0-4) |
shennongmin | 10:9d4ec0359a5c | 105 | |
shennongmin | 10:9d4ec0359a5c | 106 | return: |
shennongmin | 10:9d4ec0359a5c | 107 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 108 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 109 | |
shennongmin | 10:9d4ec0359a5c | 110 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 111 | bool ESP8266::ipConfig(uint8_t type, String addr, int port, uint8_t a, uint8_t id) |
shennongmin | 10:9d4ec0359a5c | 112 | { |
shennongmin | 10:9d4ec0359a5c | 113 | bool result = false; |
shennongmin | 10:9d4ec0359a5c | 114 | if (a == 0 ) |
shennongmin | 10:9d4ec0359a5c | 115 | { |
shennongmin | 10:9d4ec0359a5c | 116 | confMux(a); |
shennongmin | 10:9d4ec0359a5c | 117 | |
shennongmin | 10:9d4ec0359a5c | 118 | long timeStart = millis(); |
shennongmin | 10:9d4ec0359a5c | 119 | while (1) |
shennongmin | 10:9d4ec0359a5c | 120 | { |
shennongmin | 10:9d4ec0359a5c | 121 | long time0 = millis(); |
shennongmin | 10:9d4ec0359a5c | 122 | if (time0 - timeStart > 5000) |
shennongmin | 10:9d4ec0359a5c | 123 | { |
shennongmin | 10:9d4ec0359a5c | 124 | break; |
shennongmin | 10:9d4ec0359a5c | 125 | } |
shennongmin | 10:9d4ec0359a5c | 126 | } |
shennongmin | 10:9d4ec0359a5c | 127 | result = newMux(type, addr, port); |
shennongmin | 10:9d4ec0359a5c | 128 | } |
shennongmin | 10:9d4ec0359a5c | 129 | else if (a == 1) |
shennongmin | 10:9d4ec0359a5c | 130 | { |
shennongmin | 10:9d4ec0359a5c | 131 | confMux(a); |
shennongmin | 10:9d4ec0359a5c | 132 | long timeStart = millis(); |
shennongmin | 10:9d4ec0359a5c | 133 | while (1) |
shennongmin | 10:9d4ec0359a5c | 134 | { |
shennongmin | 10:9d4ec0359a5c | 135 | long time0 = millis(); |
shennongmin | 10:9d4ec0359a5c | 136 | if (time0 - timeStart > 5000) |
shennongmin | 10:9d4ec0359a5c | 137 | { |
shennongmin | 10:9d4ec0359a5c | 138 | break; |
shennongmin | 10:9d4ec0359a5c | 139 | } |
shennongmin | 10:9d4ec0359a5c | 140 | } |
shennongmin | 10:9d4ec0359a5c | 141 | result = newMux(id, type, addr, port); |
shennongmin | 10:9d4ec0359a5c | 142 | } |
shennongmin | 10:9d4ec0359a5c | 143 | return result; |
shennongmin | 10:9d4ec0359a5c | 144 | } |
shennongmin | 10:9d4ec0359a5c | 145 | |
shennongmin | 10:9d4ec0359a5c | 146 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 147 | //receive message from wifi |
shennongmin | 10:9d4ec0359a5c | 148 | |
shennongmin | 10:9d4ec0359a5c | 149 | buf: buffer for receiving data |
shennongmin | 10:9d4ec0359a5c | 150 | |
shennongmin | 10:9d4ec0359a5c | 151 | chlID: <id>(0-4) |
shennongmin | 10:9d4ec0359a5c | 152 | |
shennongmin | 10:9d4ec0359a5c | 153 | return: size of the buffer |
shennongmin | 10:9d4ec0359a5c | 154 | |
shennongmin | 10:9d4ec0359a5c | 155 | |
shennongmin | 10:9d4ec0359a5c | 156 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 157 | int ESP8266::ReceiveMessage(char *buf) |
shennongmin | 10:9d4ec0359a5c | 158 | { |
shennongmin | 10:9d4ec0359a5c | 159 | //+IPD,<len>:<data> |
shennongmin | 10:9d4ec0359a5c | 160 | //+IPD,<id>,<len>:<data> |
shennongmin | 10:9d4ec0359a5c | 161 | String data = ""; |
shennongmin | 10:9d4ec0359a5c | 162 | if (esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 163 | { |
shennongmin | 10:9d4ec0359a5c | 164 | |
shennongmin | 10:9d4ec0359a5c | 165 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 166 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 167 | char c0 = esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 168 | if (c0 == '+') |
shennongmin | 10:9d4ec0359a5c | 169 | { |
shennongmin | 10:9d4ec0359a5c | 170 | |
shennongmin | 10:9d4ec0359a5c | 171 | while (millis()-start<5000) |
shennongmin | 10:9d4ec0359a5c | 172 | { |
shennongmin | 10:9d4ec0359a5c | 173 | if (esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 174 | { |
shennongmin | 10:9d4ec0359a5c | 175 | char c = esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 176 | data += c; |
shennongmin | 10:9d4ec0359a5c | 177 | } |
shennongmin | 10:9d4ec0359a5c | 178 | if (data.indexOf("\nOK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 179 | { |
shennongmin | 10:9d4ec0359a5c | 180 | break; |
shennongmin | 10:9d4ec0359a5c | 181 | } |
shennongmin | 10:9d4ec0359a5c | 182 | } |
shennongmin | 10:9d4ec0359a5c | 183 | //Serial.println(data); |
shennongmin | 10:9d4ec0359a5c | 184 | int sLen = strlen(data.c_str()); |
shennongmin | 10:9d4ec0359a5c | 185 | int i,j; |
shennongmin | 10:9d4ec0359a5c | 186 | for (i = 0; i <= sLen; i++) |
shennongmin | 10:9d4ec0359a5c | 187 | { |
shennongmin | 10:9d4ec0359a5c | 188 | if (data[i] == ':') |
shennongmin | 10:9d4ec0359a5c | 189 | { |
shennongmin | 10:9d4ec0359a5c | 190 | break; |
shennongmin | 10:9d4ec0359a5c | 191 | } |
shennongmin | 10:9d4ec0359a5c | 192 | |
shennongmin | 10:9d4ec0359a5c | 193 | } |
shennongmin | 10:9d4ec0359a5c | 194 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 195 | for (j = 4; j <= i; j++) |
shennongmin | 10:9d4ec0359a5c | 196 | { |
shennongmin | 10:9d4ec0359a5c | 197 | if (data[j] == ',') |
shennongmin | 10:9d4ec0359a5c | 198 | { |
shennongmin | 10:9d4ec0359a5c | 199 | found = true; |
shennongmin | 10:9d4ec0359a5c | 200 | break; |
shennongmin | 10:9d4ec0359a5c | 201 | } |
shennongmin | 10:9d4ec0359a5c | 202 | |
shennongmin | 10:9d4ec0359a5c | 203 | } |
shennongmin | 10:9d4ec0359a5c | 204 | int iSize; |
shennongmin | 10:9d4ec0359a5c | 205 | //DBG(data); |
shennongmin | 10:9d4ec0359a5c | 206 | //DBG("\r\n"); |
shennongmin | 10:9d4ec0359a5c | 207 | if(found ==true) |
shennongmin | 10:9d4ec0359a5c | 208 | { |
shennongmin | 10:9d4ec0359a5c | 209 | String _id = data.substring(4, j); |
shennongmin | 10:9d4ec0359a5c | 210 | chlID = _id.toInt(); |
shennongmin | 10:9d4ec0359a5c | 211 | String _size = data.substring(j+1, i); |
shennongmin | 10:9d4ec0359a5c | 212 | iSize = _size.toInt(); |
shennongmin | 10:9d4ec0359a5c | 213 | //DBG(_size); |
shennongmin | 10:9d4ec0359a5c | 214 | String str = data.substring(i+1, i+1+iSize); |
shennongmin | 10:9d4ec0359a5c | 215 | strcpy(buf, str.c_str()); |
shennongmin | 10:9d4ec0359a5c | 216 | //DBG(str); |
shennongmin | 10:9d4ec0359a5c | 217 | |
shennongmin | 10:9d4ec0359a5c | 218 | } |
shennongmin | 10:9d4ec0359a5c | 219 | else |
shennongmin | 10:9d4ec0359a5c | 220 | { |
shennongmin | 10:9d4ec0359a5c | 221 | String _size = data.substring(4, i); |
shennongmin | 10:9d4ec0359a5c | 222 | iSize = _size.toInt(); |
shennongmin | 10:9d4ec0359a5c | 223 | //DBG(iSize); |
shennongmin | 10:9d4ec0359a5c | 224 | //DBG("\r\n"); |
shennongmin | 10:9d4ec0359a5c | 225 | String str = data.substring(i+1, i+1+iSize); |
shennongmin | 10:9d4ec0359a5c | 226 | strcpy(buf, str.c_str()); |
shennongmin | 10:9d4ec0359a5c | 227 | //DBG(str); |
shennongmin | 10:9d4ec0359a5c | 228 | } |
shennongmin | 10:9d4ec0359a5c | 229 | return iSize; |
shennongmin | 10:9d4ec0359a5c | 230 | } |
shennongmin | 10:9d4ec0359a5c | 231 | } |
shennongmin | 10:9d4ec0359a5c | 232 | |
shennongmin | 10:9d4ec0359a5c | 233 | return 0; |
shennongmin | 10:9d4ec0359a5c | 234 | } |
shennongmin | 10:9d4ec0359a5c | 235 | |
shennongmin | 10:9d4ec0359a5c | 236 | ////////////////////////////////////////////////////////////////////////// |
shennongmin | 10:9d4ec0359a5c | 237 | |
shennongmin | 10:9d4ec0359a5c | 238 | |
shennongmin | 10:9d4ec0359a5c | 239 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 240 | //reboot the wifi module |
shennongmin | 10:9d4ec0359a5c | 241 | |
shennongmin | 10:9d4ec0359a5c | 242 | |
shennongmin | 10:9d4ec0359a5c | 243 | |
shennongmin | 10:9d4ec0359a5c | 244 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 245 | void ESP8266::Reset(void) |
shennongmin | 10:9d4ec0359a5c | 246 | { |
shennongmin | 10:9d4ec0359a5c | 247 | esp_uart.println("AT+RST"); |
shennongmin | 10:9d4ec0359a5c | 248 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 249 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 250 | while (millis()-start<5000) { |
shennongmin | 10:9d4ec0359a5c | 251 | if(esp_uart.find("ready")==true) |
shennongmin | 10:9d4ec0359a5c | 252 | { |
shennongmin | 10:9d4ec0359a5c | 253 | DBG("reboot wifi is OK\r\n"); |
shennongmin | 10:9d4ec0359a5c | 254 | break; |
shennongmin | 10:9d4ec0359a5c | 255 | } |
shennongmin | 10:9d4ec0359a5c | 256 | } |
shennongmin | 10:9d4ec0359a5c | 257 | } |
shennongmin | 10:9d4ec0359a5c | 258 | |
shennongmin | 10:9d4ec0359a5c | 259 | /********************************************* |
shennongmin | 10:9d4ec0359a5c | 260 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 261 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 262 | ESP8266 Function Commands |
shennongmin | 10:9d4ec0359a5c | 263 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 264 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 265 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 266 | */ |
shennongmin | 10:9d4ec0359a5c | 267 | |
shennongmin | 10:9d4ec0359a5c | 268 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 269 | //inquire the current mode of wifi module |
shennongmin | 10:9d4ec0359a5c | 270 | |
shennongmin | 10:9d4ec0359a5c | 271 | return: string of current mode |
shennongmin | 10:9d4ec0359a5c | 272 | Station |
shennongmin | 10:9d4ec0359a5c | 273 | AP |
shennongmin | 10:9d4ec0359a5c | 274 | AP+Station |
shennongmin | 10:9d4ec0359a5c | 275 | |
shennongmin | 10:9d4ec0359a5c | 276 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 277 | String ESP8266::showMode() |
shennongmin | 10:9d4ec0359a5c | 278 | { |
shennongmin | 10:9d4ec0359a5c | 279 | String data; |
shennongmin | 10:9d4ec0359a5c | 280 | esp_uart.println("AT+CWMODE?"); |
shennongmin | 10:9d4ec0359a5c | 281 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 282 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 283 | while (millis()-start<2000) { |
shennongmin | 10:9d4ec0359a5c | 284 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 285 | { |
shennongmin | 10:9d4ec0359a5c | 286 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 287 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 288 | } |
shennongmin | 10:9d4ec0359a5c | 289 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 290 | { |
shennongmin | 10:9d4ec0359a5c | 291 | break; |
shennongmin | 10:9d4ec0359a5c | 292 | } |
shennongmin | 10:9d4ec0359a5c | 293 | } |
shennongmin | 10:9d4ec0359a5c | 294 | if(data.indexOf("1")!=-1) |
shennongmin | 10:9d4ec0359a5c | 295 | { |
shennongmin | 10:9d4ec0359a5c | 296 | return "Station"; |
shennongmin | 10:9d4ec0359a5c | 297 | }else if(data.indexOf("2")!=-1) |
shennongmin | 10:9d4ec0359a5c | 298 | { |
shennongmin | 10:9d4ec0359a5c | 299 | return "AP"; |
shennongmin | 10:9d4ec0359a5c | 300 | }else if(data.indexOf("3")!=-1) |
shennongmin | 10:9d4ec0359a5c | 301 | { |
shennongmin | 10:9d4ec0359a5c | 302 | return "AP+Station"; |
shennongmin | 10:9d4ec0359a5c | 303 | }else{ |
shennongmin | 10:9d4ec0359a5c | 304 | return "Invalid Mode!"; |
shennongmin | 10:9d4ec0359a5c | 305 | } |
shennongmin | 10:9d4ec0359a5c | 306 | } |
shennongmin | 10:9d4ec0359a5c | 307 | |
shennongmin | 10:9d4ec0359a5c | 308 | |
shennongmin | 10:9d4ec0359a5c | 309 | |
shennongmin | 10:9d4ec0359a5c | 310 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 311 | //configure the operation mode |
shennongmin | 10:9d4ec0359a5c | 312 | |
shennongmin | 10:9d4ec0359a5c | 313 | a: |
shennongmin | 10:9d4ec0359a5c | 314 | 1 - Station |
shennongmin | 10:9d4ec0359a5c | 315 | 2 - AP |
shennongmin | 10:9d4ec0359a5c | 316 | 3 - AP+Station |
shennongmin | 10:9d4ec0359a5c | 317 | |
shennongmin | 10:9d4ec0359a5c | 318 | return: |
shennongmin | 10:9d4ec0359a5c | 319 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 320 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 321 | |
shennongmin | 10:9d4ec0359a5c | 322 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 323 | |
shennongmin | 10:9d4ec0359a5c | 324 | bool ESP8266::confMode(uint8_t a) |
shennongmin | 10:9d4ec0359a5c | 325 | { |
shennongmin | 10:9d4ec0359a5c | 326 | String data; |
shennongmin | 10:9d4ec0359a5c | 327 | esp_uart.print("AT+CWMODE="); |
shennongmin | 10:9d4ec0359a5c | 328 | esp_uart.println(String(a)); |
shennongmin | 10:9d4ec0359a5c | 329 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 330 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 331 | while (millis()-start<2000) { |
shennongmin | 10:9d4ec0359a5c | 332 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 333 | { |
shennongmin | 10:9d4ec0359a5c | 334 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 335 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 336 | } |
shennongmin | 10:9d4ec0359a5c | 337 | if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1) |
shennongmin | 10:9d4ec0359a5c | 338 | { |
shennongmin | 10:9d4ec0359a5c | 339 | return true; |
shennongmin | 10:9d4ec0359a5c | 340 | } |
shennongmin | 10:9d4ec0359a5c | 341 | if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1) |
shennongmin | 10:9d4ec0359a5c | 342 | { |
shennongmin | 10:9d4ec0359a5c | 343 | return false; |
shennongmin | 10:9d4ec0359a5c | 344 | } |
shennongmin | 10:9d4ec0359a5c | 345 | |
shennongmin | 10:9d4ec0359a5c | 346 | } |
shennongmin | 10:9d4ec0359a5c | 347 | return false; |
shennongmin | 10:9d4ec0359a5c | 348 | } |
shennongmin | 10:9d4ec0359a5c | 349 | |
shennongmin | 10:9d4ec0359a5c | 350 | |
shennongmin | 10:9d4ec0359a5c | 351 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 352 | //show the list of wifi hotspot |
shennongmin | 10:9d4ec0359a5c | 353 | |
shennongmin | 10:9d4ec0359a5c | 354 | return: string of wifi information |
shennongmin | 10:9d4ec0359a5c | 355 | encryption,SSID,RSSI |
shennongmin | 10:9d4ec0359a5c | 356 | |
shennongmin | 10:9d4ec0359a5c | 357 | |
shennongmin | 10:9d4ec0359a5c | 358 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 359 | |
shennongmin | 10:9d4ec0359a5c | 360 | String ESP8266::showAP(void) |
shennongmin | 10:9d4ec0359a5c | 361 | { |
shennongmin | 10:9d4ec0359a5c | 362 | String data; |
shennongmin | 10:9d4ec0359a5c | 363 | esp_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 364 | esp_uart.print("AT+CWLAP\r\n"); |
shennongmin | 10:9d4ec0359a5c | 365 | delay(5000); |
shennongmin | 10:9d4ec0359a5c | 366 | |
shennongmin | 10:9d4ec0359a5c | 367 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 368 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 369 | while (millis()-start<8000) { |
shennongmin | 10:9d4ec0359a5c | 370 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 371 | { |
shennongmin | 10:9d4ec0359a5c | 372 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 373 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 374 | } |
shennongmin | 10:9d4ec0359a5c | 375 | if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 376 | { |
shennongmin | 10:9d4ec0359a5c | 377 | break; |
shennongmin | 10:9d4ec0359a5c | 378 | } |
shennongmin | 10:9d4ec0359a5c | 379 | } |
shennongmin | 10:9d4ec0359a5c | 380 | if(data.indexOf("ERROR")!=-1) |
shennongmin | 10:9d4ec0359a5c | 381 | { |
shennongmin | 10:9d4ec0359a5c | 382 | return "ERROR"; |
shennongmin | 10:9d4ec0359a5c | 383 | } |
shennongmin | 10:9d4ec0359a5c | 384 | else{ |
shennongmin | 10:9d4ec0359a5c | 385 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 386 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 387 | data.replace("AT+CWLAP",""); |
shennongmin | 10:9d4ec0359a5c | 388 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 389 | data.replace("+CWLAP","ESP8266"); |
shennongmin | 10:9d4ec0359a5c | 390 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 391 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 392 | |
shennongmin | 10:9d4ec0359a5c | 393 | return data; |
shennongmin | 10:9d4ec0359a5c | 394 | } |
shennongmin | 10:9d4ec0359a5c | 395 | } |
shennongmin | 10:9d4ec0359a5c | 396 | |
shennongmin | 10:9d4ec0359a5c | 397 | |
shennongmin | 10:9d4ec0359a5c | 398 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 399 | //show the name of current wifi access port |
shennongmin | 10:9d4ec0359a5c | 400 | |
shennongmin | 10:9d4ec0359a5c | 401 | return: string of access port name |
shennongmin | 10:9d4ec0359a5c | 402 | AP:<SSID> |
shennongmin | 10:9d4ec0359a5c | 403 | |
shennongmin | 10:9d4ec0359a5c | 404 | |
shennongmin | 10:9d4ec0359a5c | 405 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 406 | String ESP8266::showJAP(void) |
shennongmin | 10:9d4ec0359a5c | 407 | { |
shennongmin | 10:9d4ec0359a5c | 408 | esp_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 409 | esp_uart.println("AT+CWJAP?"); |
shennongmin | 10:9d4ec0359a5c | 410 | String data; |
shennongmin | 10:9d4ec0359a5c | 411 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 412 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 413 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 414 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 415 | { |
shennongmin | 10:9d4ec0359a5c | 416 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 417 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 418 | } |
shennongmin | 10:9d4ec0359a5c | 419 | if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 420 | { |
shennongmin | 10:9d4ec0359a5c | 421 | break; |
shennongmin | 10:9d4ec0359a5c | 422 | } |
shennongmin | 10:9d4ec0359a5c | 423 | } |
shennongmin | 10:9d4ec0359a5c | 424 | //DBG(("data = [%s]\r\n", data.c_str())); |
shennongmin | 10:9d4ec0359a5c | 425 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 426 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 427 | data.replace("AT+CWJAP?",""); |
shennongmin | 10:9d4ec0359a5c | 428 | data.replace("+CWJAP","AP"); |
shennongmin | 10:9d4ec0359a5c | 429 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 430 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 431 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 432 | |
shennongmin | 10:9d4ec0359a5c | 433 | return data; |
shennongmin | 10:9d4ec0359a5c | 434 | } |
shennongmin | 10:9d4ec0359a5c | 435 | |
shennongmin | 10:9d4ec0359a5c | 436 | |
shennongmin | 10:9d4ec0359a5c | 437 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 438 | //configure the SSID and password of the access port |
shennongmin | 10:9d4ec0359a5c | 439 | |
shennongmin | 10:9d4ec0359a5c | 440 | return: |
shennongmin | 10:9d4ec0359a5c | 441 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 442 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 443 | |
shennongmin | 10:9d4ec0359a5c | 444 | |
shennongmin | 10:9d4ec0359a5c | 445 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 446 | bool ESP8266::confJAP(String ssid , String pwd) |
shennongmin | 10:9d4ec0359a5c | 447 | { |
shennongmin | 10:9d4ec0359a5c | 448 | |
shennongmin | 10:9d4ec0359a5c | 449 | esp_uart.print("AT+CWJAP="); |
shennongmin | 10:9d4ec0359a5c | 450 | esp_uart.print("\""); //"ssid" |
shennongmin | 10:9d4ec0359a5c | 451 | esp_uart.print(ssid); |
shennongmin | 10:9d4ec0359a5c | 452 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 453 | |
shennongmin | 10:9d4ec0359a5c | 454 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 455 | |
shennongmin | 10:9d4ec0359a5c | 456 | esp_uart.print("\""); //"pwd" |
shennongmin | 10:9d4ec0359a5c | 457 | esp_uart.print(pwd); |
shennongmin | 10:9d4ec0359a5c | 458 | esp_uart.println("\""); |
shennongmin | 10:9d4ec0359a5c | 459 | |
shennongmin | 10:9d4ec0359a5c | 460 | |
shennongmin | 10:9d4ec0359a5c | 461 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 462 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 463 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 464 | if(esp_uart.find("OK")==true) |
shennongmin | 10:9d4ec0359a5c | 465 | { |
shennongmin | 10:9d4ec0359a5c | 466 | return true; |
shennongmin | 10:9d4ec0359a5c | 467 | |
shennongmin | 10:9d4ec0359a5c | 468 | } |
shennongmin | 10:9d4ec0359a5c | 469 | } |
shennongmin | 10:9d4ec0359a5c | 470 | return false; |
shennongmin | 10:9d4ec0359a5c | 471 | } |
shennongmin | 10:9d4ec0359a5c | 472 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 473 | //quite the access port |
shennongmin | 10:9d4ec0359a5c | 474 | |
shennongmin | 10:9d4ec0359a5c | 475 | return: |
shennongmin | 10:9d4ec0359a5c | 476 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 477 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 478 | |
shennongmin | 10:9d4ec0359a5c | 479 | |
shennongmin | 10:9d4ec0359a5c | 480 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 481 | |
shennongmin | 10:9d4ec0359a5c | 482 | bool ESP8266::quitAP(void) |
shennongmin | 10:9d4ec0359a5c | 483 | { |
shennongmin | 10:9d4ec0359a5c | 484 | esp_uart.println("AT+CWQAP"); |
shennongmin | 10:9d4ec0359a5c | 485 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 486 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 487 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 488 | if(esp_uart.find("OK")==true) |
shennongmin | 10:9d4ec0359a5c | 489 | { |
shennongmin | 10:9d4ec0359a5c | 490 | return true; |
shennongmin | 10:9d4ec0359a5c | 491 | |
shennongmin | 10:9d4ec0359a5c | 492 | } |
shennongmin | 10:9d4ec0359a5c | 493 | } |
shennongmin | 10:9d4ec0359a5c | 494 | return false; |
shennongmin | 10:9d4ec0359a5c | 495 | |
shennongmin | 10:9d4ec0359a5c | 496 | } |
shennongmin | 10:9d4ec0359a5c | 497 | |
shennongmin | 10:9d4ec0359a5c | 498 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 499 | //show the parameter of ssid, password, channel, encryption in AP mode |
shennongmin | 10:9d4ec0359a5c | 500 | |
shennongmin | 10:9d4ec0359a5c | 501 | return: |
shennongmin | 10:9d4ec0359a5c | 502 | mySAP:<SSID>,<password>,<channel>,<encryption> |
shennongmin | 10:9d4ec0359a5c | 503 | |
shennongmin | 10:9d4ec0359a5c | 504 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 505 | String ESP8266::showSAP() |
shennongmin | 10:9d4ec0359a5c | 506 | { |
shennongmin | 10:9d4ec0359a5c | 507 | esp_uart.println("AT+CWSAP?"); |
shennongmin | 10:9d4ec0359a5c | 508 | String data; |
shennongmin | 10:9d4ec0359a5c | 509 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 510 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 511 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 512 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 513 | { |
shennongmin | 10:9d4ec0359a5c | 514 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 515 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 516 | } |
shennongmin | 10:9d4ec0359a5c | 517 | if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 518 | { |
shennongmin | 10:9d4ec0359a5c | 519 | break; |
shennongmin | 10:9d4ec0359a5c | 520 | } |
shennongmin | 10:9d4ec0359a5c | 521 | } |
shennongmin | 10:9d4ec0359a5c | 522 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 523 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 524 | data.replace("AT+CWSAP?",""); |
shennongmin | 10:9d4ec0359a5c | 525 | data.replace("+CWSAP","mySAP"); |
shennongmin | 10:9d4ec0359a5c | 526 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 527 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 528 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 529 | |
shennongmin | 10:9d4ec0359a5c | 530 | return data; |
shennongmin | 10:9d4ec0359a5c | 531 | } |
shennongmin | 10:9d4ec0359a5c | 532 | |
shennongmin | 10:9d4ec0359a5c | 533 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 534 | //configure the parameter of ssid, password, channel, encryption in AP mode |
shennongmin | 10:9d4ec0359a5c | 535 | |
shennongmin | 10:9d4ec0359a5c | 536 | return: |
shennongmin | 10:9d4ec0359a5c | 537 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 538 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 539 | |
shennongmin | 10:9d4ec0359a5c | 540 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 541 | |
shennongmin | 10:9d4ec0359a5c | 542 | bool ESP8266::confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn) |
shennongmin | 10:9d4ec0359a5c | 543 | { |
shennongmin | 10:9d4ec0359a5c | 544 | esp_uart.print("AT+CWSAP="); |
shennongmin | 10:9d4ec0359a5c | 545 | esp_uart.print("\""); //"ssid" |
shennongmin | 10:9d4ec0359a5c | 546 | esp_uart.print(ssid); |
shennongmin | 10:9d4ec0359a5c | 547 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 548 | |
shennongmin | 10:9d4ec0359a5c | 549 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 550 | |
shennongmin | 10:9d4ec0359a5c | 551 | esp_uart.print("\""); //"pwd" |
shennongmin | 10:9d4ec0359a5c | 552 | esp_uart.print(pwd); |
shennongmin | 10:9d4ec0359a5c | 553 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 554 | |
shennongmin | 10:9d4ec0359a5c | 555 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 556 | esp_uart.print(String(chl)); |
shennongmin | 10:9d4ec0359a5c | 557 | |
shennongmin | 10:9d4ec0359a5c | 558 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 559 | esp_uart.println(String(ecn)); |
shennongmin | 10:9d4ec0359a5c | 560 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 561 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 562 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 563 | if(esp_uart.find("OK")==true ) |
shennongmin | 10:9d4ec0359a5c | 564 | { |
shennongmin | 10:9d4ec0359a5c | 565 | return true; |
shennongmin | 10:9d4ec0359a5c | 566 | } |
shennongmin | 10:9d4ec0359a5c | 567 | } |
shennongmin | 10:9d4ec0359a5c | 568 | |
shennongmin | 10:9d4ec0359a5c | 569 | return false; |
shennongmin | 10:9d4ec0359a5c | 570 | |
shennongmin | 10:9d4ec0359a5c | 571 | } |
shennongmin | 10:9d4ec0359a5c | 572 | |
shennongmin | 10:9d4ec0359a5c | 573 | |
shennongmin | 10:9d4ec0359a5c | 574 | /********************************************* |
shennongmin | 10:9d4ec0359a5c | 575 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 576 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 577 | TPC/IP Function Command |
shennongmin | 10:9d4ec0359a5c | 578 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 579 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 580 | ********************************************* |
shennongmin | 10:9d4ec0359a5c | 581 | */ |
shennongmin | 10:9d4ec0359a5c | 582 | |
shennongmin | 10:9d4ec0359a5c | 583 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 584 | //inquire the connection status |
shennongmin | 10:9d4ec0359a5c | 585 | |
shennongmin | 10:9d4ec0359a5c | 586 | return: string of connection status |
shennongmin | 10:9d4ec0359a5c | 587 | <ID> 0-4 |
shennongmin | 10:9d4ec0359a5c | 588 | <type> tcp or udp |
shennongmin | 10:9d4ec0359a5c | 589 | <addr> ip |
shennongmin | 10:9d4ec0359a5c | 590 | <port> port number |
shennongmin | 10:9d4ec0359a5c | 591 | |
shennongmin | 10:9d4ec0359a5c | 592 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 593 | |
shennongmin | 10:9d4ec0359a5c | 594 | String ESP8266::showStatus(void) |
shennongmin | 10:9d4ec0359a5c | 595 | { |
shennongmin | 10:9d4ec0359a5c | 596 | esp_uart.println("AT+CIPSTATUS"); |
shennongmin | 10:9d4ec0359a5c | 597 | String data; |
shennongmin | 10:9d4ec0359a5c | 598 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 599 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 600 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 601 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 602 | { |
shennongmin | 10:9d4ec0359a5c | 603 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 604 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 605 | } |
shennongmin | 10:9d4ec0359a5c | 606 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 607 | { |
shennongmin | 10:9d4ec0359a5c | 608 | break; |
shennongmin | 10:9d4ec0359a5c | 609 | } |
shennongmin | 10:9d4ec0359a5c | 610 | } |
shennongmin | 10:9d4ec0359a5c | 611 | |
shennongmin | 10:9d4ec0359a5c | 612 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 613 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 614 | data.replace("AT+CIPSTATUS",""); |
shennongmin | 10:9d4ec0359a5c | 615 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 616 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 617 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 618 | |
shennongmin | 10:9d4ec0359a5c | 619 | return data; |
shennongmin | 10:9d4ec0359a5c | 620 | } |
shennongmin | 10:9d4ec0359a5c | 621 | |
shennongmin | 10:9d4ec0359a5c | 622 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 623 | //show the current connection mode(sigle or multiple) |
shennongmin | 10:9d4ec0359a5c | 624 | |
shennongmin | 10:9d4ec0359a5c | 625 | return: string of connection mode |
shennongmin | 10:9d4ec0359a5c | 626 | 0 - sigle |
shennongmin | 10:9d4ec0359a5c | 627 | 1 - multiple |
shennongmin | 10:9d4ec0359a5c | 628 | |
shennongmin | 10:9d4ec0359a5c | 629 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 630 | String ESP8266::showMux(void) |
shennongmin | 10:9d4ec0359a5c | 631 | { |
shennongmin | 10:9d4ec0359a5c | 632 | String data; |
shennongmin | 10:9d4ec0359a5c | 633 | esp_uart.println("AT+CIPMUX?"); |
shennongmin | 10:9d4ec0359a5c | 634 | |
shennongmin | 10:9d4ec0359a5c | 635 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 636 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 637 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 638 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 639 | { |
shennongmin | 10:9d4ec0359a5c | 640 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 641 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 642 | } |
shennongmin | 10:9d4ec0359a5c | 643 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 644 | { |
shennongmin | 10:9d4ec0359a5c | 645 | break; |
shennongmin | 10:9d4ec0359a5c | 646 | } |
shennongmin | 10:9d4ec0359a5c | 647 | } |
shennongmin | 10:9d4ec0359a5c | 648 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 649 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 650 | data.replace("AT+CIPMUX?",""); |
shennongmin | 10:9d4ec0359a5c | 651 | data.replace("+CIPMUX","showMux"); |
shennongmin | 10:9d4ec0359a5c | 652 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 653 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 654 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 655 | |
shennongmin | 10:9d4ec0359a5c | 656 | return data; |
shennongmin | 10:9d4ec0359a5c | 657 | } |
shennongmin | 10:9d4ec0359a5c | 658 | |
shennongmin | 10:9d4ec0359a5c | 659 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 660 | //configure the current connection mode(sigle or multiple) |
shennongmin | 10:9d4ec0359a5c | 661 | |
shennongmin | 10:9d4ec0359a5c | 662 | a: connection mode |
shennongmin | 10:9d4ec0359a5c | 663 | 0 - sigle |
shennongmin | 10:9d4ec0359a5c | 664 | 1 - multiple |
shennongmin | 10:9d4ec0359a5c | 665 | |
shennongmin | 10:9d4ec0359a5c | 666 | return: |
shennongmin | 10:9d4ec0359a5c | 667 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 668 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 669 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 670 | bool ESP8266::confMux(int a) |
shennongmin | 10:9d4ec0359a5c | 671 | { |
shennongmin | 10:9d4ec0359a5c | 672 | esp_uart.print("AT+CIPMUX="); |
shennongmin | 10:9d4ec0359a5c | 673 | esp_uart.println(a); |
shennongmin | 10:9d4ec0359a5c | 674 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 675 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 676 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 677 | if(esp_uart.find("OK")==true ) |
shennongmin | 10:9d4ec0359a5c | 678 | { |
shennongmin | 10:9d4ec0359a5c | 679 | return true; |
shennongmin | 10:9d4ec0359a5c | 680 | } |
shennongmin | 10:9d4ec0359a5c | 681 | } |
shennongmin | 10:9d4ec0359a5c | 682 | |
shennongmin | 10:9d4ec0359a5c | 683 | return false; |
shennongmin | 10:9d4ec0359a5c | 684 | } |
shennongmin | 10:9d4ec0359a5c | 685 | |
shennongmin | 10:9d4ec0359a5c | 686 | |
shennongmin | 10:9d4ec0359a5c | 687 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 688 | //Set up tcp or udp connection (signle connection mode) |
shennongmin | 10:9d4ec0359a5c | 689 | |
shennongmin | 10:9d4ec0359a5c | 690 | type: tcp or udp |
shennongmin | 10:9d4ec0359a5c | 691 | |
shennongmin | 10:9d4ec0359a5c | 692 | addr: ip address |
shennongmin | 10:9d4ec0359a5c | 693 | |
shennongmin | 10:9d4ec0359a5c | 694 | port: port number |
shennongmin | 10:9d4ec0359a5c | 695 | |
shennongmin | 10:9d4ec0359a5c | 696 | |
shennongmin | 10:9d4ec0359a5c | 697 | return: |
shennongmin | 10:9d4ec0359a5c | 698 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 699 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 700 | |
shennongmin | 10:9d4ec0359a5c | 701 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 702 | bool ESP8266::newMux(uint8_t type, String addr, int port) |
shennongmin | 10:9d4ec0359a5c | 703 | |
shennongmin | 10:9d4ec0359a5c | 704 | { |
shennongmin | 10:9d4ec0359a5c | 705 | String data; |
shennongmin | 10:9d4ec0359a5c | 706 | esp_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 707 | esp_uart.print("AT+CIPSTART="); |
shennongmin | 10:9d4ec0359a5c | 708 | if(type>0) |
shennongmin | 10:9d4ec0359a5c | 709 | { |
shennongmin | 10:9d4ec0359a5c | 710 | esp_uart.print("\"TCP\""); |
shennongmin | 10:9d4ec0359a5c | 711 | }else |
shennongmin | 10:9d4ec0359a5c | 712 | { |
shennongmin | 10:9d4ec0359a5c | 713 | esp_uart.print("\"UDP\""); |
shennongmin | 10:9d4ec0359a5c | 714 | } |
shennongmin | 10:9d4ec0359a5c | 715 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 716 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 717 | esp_uart.print(addr); |
shennongmin | 10:9d4ec0359a5c | 718 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 719 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 720 | // esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 721 | esp_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 722 | // esp_uart.println("\""); |
shennongmin | 10:9d4ec0359a5c | 723 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 724 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 725 | while (millis()-start<10000) { |
shennongmin | 10:9d4ec0359a5c | 726 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 727 | { |
shennongmin | 10:9d4ec0359a5c | 728 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 729 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 730 | } |
shennongmin | 10:9d4ec0359a5c | 731 | if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1) |
shennongmin | 10:9d4ec0359a5c | 732 | { |
shennongmin | 10:9d4ec0359a5c | 733 | return true; |
shennongmin | 10:9d4ec0359a5c | 734 | } |
shennongmin | 10:9d4ec0359a5c | 735 | } |
shennongmin | 10:9d4ec0359a5c | 736 | return false; |
shennongmin | 10:9d4ec0359a5c | 737 | } |
shennongmin | 10:9d4ec0359a5c | 738 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 739 | //Set up tcp or udp connection (multiple connection mode) |
shennongmin | 10:9d4ec0359a5c | 740 | |
shennongmin | 10:9d4ec0359a5c | 741 | type: tcp or udp |
shennongmin | 10:9d4ec0359a5c | 742 | |
shennongmin | 10:9d4ec0359a5c | 743 | addr: ip address |
shennongmin | 10:9d4ec0359a5c | 744 | |
shennongmin | 10:9d4ec0359a5c | 745 | port: port number |
shennongmin | 10:9d4ec0359a5c | 746 | |
shennongmin | 10:9d4ec0359a5c | 747 | id: id number(0-4) |
shennongmin | 10:9d4ec0359a5c | 748 | |
shennongmin | 10:9d4ec0359a5c | 749 | return: |
shennongmin | 10:9d4ec0359a5c | 750 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 751 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 752 | |
shennongmin | 10:9d4ec0359a5c | 753 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 754 | bool ESP8266::newMux( uint8_t id, uint8_t type, String addr, int port) |
shennongmin | 10:9d4ec0359a5c | 755 | |
shennongmin | 10:9d4ec0359a5c | 756 | { |
shennongmin | 10:9d4ec0359a5c | 757 | |
shennongmin | 10:9d4ec0359a5c | 758 | esp_uart.print("AT+CIPSTART="); |
shennongmin | 10:9d4ec0359a5c | 759 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 760 | esp_uart.print(String(id)); |
shennongmin | 10:9d4ec0359a5c | 761 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 762 | if(type>0) |
shennongmin | 10:9d4ec0359a5c | 763 | { |
shennongmin | 10:9d4ec0359a5c | 764 | esp_uart.print("\"TCP\""); |
shennongmin | 10:9d4ec0359a5c | 765 | } |
shennongmin | 10:9d4ec0359a5c | 766 | else |
shennongmin | 10:9d4ec0359a5c | 767 | { |
shennongmin | 10:9d4ec0359a5c | 768 | esp_uart.print("\"UDP\""); |
shennongmin | 10:9d4ec0359a5c | 769 | } |
shennongmin | 10:9d4ec0359a5c | 770 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 771 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 772 | esp_uart.print(addr); |
shennongmin | 10:9d4ec0359a5c | 773 | esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 774 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 775 | // esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 776 | esp_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 777 | // esp_uart.println("\""); |
shennongmin | 10:9d4ec0359a5c | 778 | String data; |
shennongmin | 10:9d4ec0359a5c | 779 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 780 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 781 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 782 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 783 | { |
shennongmin | 10:9d4ec0359a5c | 784 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 785 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 786 | } |
shennongmin | 10:9d4ec0359a5c | 787 | if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 788 | { |
shennongmin | 10:9d4ec0359a5c | 789 | return true; |
shennongmin | 10:9d4ec0359a5c | 790 | } |
shennongmin | 10:9d4ec0359a5c | 791 | } |
shennongmin | 10:9d4ec0359a5c | 792 | return false; |
shennongmin | 10:9d4ec0359a5c | 793 | |
shennongmin | 10:9d4ec0359a5c | 794 | |
shennongmin | 10:9d4ec0359a5c | 795 | } |
shennongmin | 10:9d4ec0359a5c | 796 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 797 | //send data in sigle connection mode |
shennongmin | 10:9d4ec0359a5c | 798 | |
shennongmin | 10:9d4ec0359a5c | 799 | str: string of message |
shennongmin | 10:9d4ec0359a5c | 800 | |
shennongmin | 10:9d4ec0359a5c | 801 | return: |
shennongmin | 10:9d4ec0359a5c | 802 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 803 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 804 | |
shennongmin | 10:9d4ec0359a5c | 805 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 806 | bool ESP8266::Send(String str) |
shennongmin | 10:9d4ec0359a5c | 807 | { |
shennongmin | 10:9d4ec0359a5c | 808 | esp_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 809 | esp_uart.print("AT+CIPSEND="); |
shennongmin | 10:9d4ec0359a5c | 810 | // esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 811 | esp_uart.println(str.length()); |
shennongmin | 10:9d4ec0359a5c | 812 | // esp_uart.println("\""); |
shennongmin | 10:9d4ec0359a5c | 813 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 814 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 815 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 816 | while (millis()-start<5000) { |
shennongmin | 10:9d4ec0359a5c | 817 | if(esp_uart.find(">")==true ) |
shennongmin | 10:9d4ec0359a5c | 818 | { |
shennongmin | 10:9d4ec0359a5c | 819 | found = true; |
shennongmin | 10:9d4ec0359a5c | 820 | break; |
shennongmin | 10:9d4ec0359a5c | 821 | } |
shennongmin | 10:9d4ec0359a5c | 822 | } |
shennongmin | 10:9d4ec0359a5c | 823 | if(found) |
shennongmin | 10:9d4ec0359a5c | 824 | esp_uart.print(str); |
shennongmin | 10:9d4ec0359a5c | 825 | else |
shennongmin | 10:9d4ec0359a5c | 826 | { |
shennongmin | 10:9d4ec0359a5c | 827 | closeMux(); |
shennongmin | 10:9d4ec0359a5c | 828 | return false; |
shennongmin | 10:9d4ec0359a5c | 829 | } |
shennongmin | 10:9d4ec0359a5c | 830 | |
shennongmin | 10:9d4ec0359a5c | 831 | |
shennongmin | 10:9d4ec0359a5c | 832 | String data; |
shennongmin | 10:9d4ec0359a5c | 833 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 834 | while (millis()-start<5000) { |
shennongmin | 10:9d4ec0359a5c | 835 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 836 | { |
shennongmin | 10:9d4ec0359a5c | 837 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 838 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 839 | } |
shennongmin | 10:9d4ec0359a5c | 840 | if (data.indexOf("SEND OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 841 | { |
shennongmin | 10:9d4ec0359a5c | 842 | return true; |
shennongmin | 10:9d4ec0359a5c | 843 | } |
shennongmin | 10:9d4ec0359a5c | 844 | } |
shennongmin | 10:9d4ec0359a5c | 845 | return false; |
shennongmin | 10:9d4ec0359a5c | 846 | } |
shennongmin | 10:9d4ec0359a5c | 847 | |
shennongmin | 10:9d4ec0359a5c | 848 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 849 | //send data in multiple connection mode |
shennongmin | 10:9d4ec0359a5c | 850 | |
shennongmin | 10:9d4ec0359a5c | 851 | id: <id>(0-4) |
shennongmin | 10:9d4ec0359a5c | 852 | |
shennongmin | 10:9d4ec0359a5c | 853 | str: string of message |
shennongmin | 10:9d4ec0359a5c | 854 | |
shennongmin | 10:9d4ec0359a5c | 855 | return: |
shennongmin | 10:9d4ec0359a5c | 856 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 857 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 858 | |
shennongmin | 10:9d4ec0359a5c | 859 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 860 | bool ESP8266::Send(uint8_t id, String str) |
shennongmin | 10:9d4ec0359a5c | 861 | { |
shennongmin | 10:9d4ec0359a5c | 862 | esp_uart.print("AT+CIPSEND="); |
shennongmin | 10:9d4ec0359a5c | 863 | |
shennongmin | 10:9d4ec0359a5c | 864 | esp_uart.print(String(id)); |
shennongmin | 10:9d4ec0359a5c | 865 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 866 | esp_uart.println(str.length()); |
shennongmin | 10:9d4ec0359a5c | 867 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 868 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 869 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 870 | while (millis()-start<5000) { |
shennongmin | 10:9d4ec0359a5c | 871 | if(esp_uart.find(">")==true ) |
shennongmin | 10:9d4ec0359a5c | 872 | { |
shennongmin | 10:9d4ec0359a5c | 873 | found = true; |
shennongmin | 10:9d4ec0359a5c | 874 | break; |
shennongmin | 10:9d4ec0359a5c | 875 | } |
shennongmin | 10:9d4ec0359a5c | 876 | } |
shennongmin | 10:9d4ec0359a5c | 877 | if(found) |
shennongmin | 10:9d4ec0359a5c | 878 | esp_uart.print(str); |
shennongmin | 10:9d4ec0359a5c | 879 | else |
shennongmin | 10:9d4ec0359a5c | 880 | { |
shennongmin | 10:9d4ec0359a5c | 881 | closeMux(id); |
shennongmin | 10:9d4ec0359a5c | 882 | return false; |
shennongmin | 10:9d4ec0359a5c | 883 | } |
shennongmin | 10:9d4ec0359a5c | 884 | |
shennongmin | 10:9d4ec0359a5c | 885 | |
shennongmin | 10:9d4ec0359a5c | 886 | String data; |
shennongmin | 10:9d4ec0359a5c | 887 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 888 | while (millis()-start<5000) { |
shennongmin | 10:9d4ec0359a5c | 889 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 890 | { |
shennongmin | 10:9d4ec0359a5c | 891 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 892 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 893 | } |
shennongmin | 10:9d4ec0359a5c | 894 | if (data.indexOf("SEND OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 895 | { |
shennongmin | 10:9d4ec0359a5c | 896 | return true; |
shennongmin | 10:9d4ec0359a5c | 897 | } |
shennongmin | 10:9d4ec0359a5c | 898 | } |
shennongmin | 10:9d4ec0359a5c | 899 | return false; |
shennongmin | 10:9d4ec0359a5c | 900 | } |
shennongmin | 10:9d4ec0359a5c | 901 | |
shennongmin | 10:9d4ec0359a5c | 902 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 903 | //Close up tcp or udp connection (sigle connection mode) |
shennongmin | 10:9d4ec0359a5c | 904 | |
shennongmin | 10:9d4ec0359a5c | 905 | |
shennongmin | 10:9d4ec0359a5c | 906 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 907 | void ESP8266::closeMux(void) |
shennongmin | 10:9d4ec0359a5c | 908 | { |
shennongmin | 10:9d4ec0359a5c | 909 | esp_uart.println("AT+CIPCLOSE"); |
shennongmin | 10:9d4ec0359a5c | 910 | |
shennongmin | 10:9d4ec0359a5c | 911 | String data; |
shennongmin | 10:9d4ec0359a5c | 912 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 913 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 914 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 915 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 916 | { |
shennongmin | 10:9d4ec0359a5c | 917 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 918 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 919 | } |
shennongmin | 10:9d4ec0359a5c | 920 | if (data.indexOf("Linked")!=-1 || data.indexOf("ERROR")!=-1 || data.indexOf("we must restart")!=-1) |
shennongmin | 10:9d4ec0359a5c | 921 | { |
shennongmin | 10:9d4ec0359a5c | 922 | break; |
shennongmin | 10:9d4ec0359a5c | 923 | } |
shennongmin | 10:9d4ec0359a5c | 924 | } |
shennongmin | 10:9d4ec0359a5c | 925 | } |
shennongmin | 10:9d4ec0359a5c | 926 | |
shennongmin | 10:9d4ec0359a5c | 927 | |
shennongmin | 10:9d4ec0359a5c | 928 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 929 | //Set up tcp or udp connection (multiple connection mode) |
shennongmin | 10:9d4ec0359a5c | 930 | |
shennongmin | 10:9d4ec0359a5c | 931 | id: id number(0-4) |
shennongmin | 10:9d4ec0359a5c | 932 | |
shennongmin | 10:9d4ec0359a5c | 933 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 934 | void ESP8266::closeMux(uint8_t id) |
shennongmin | 10:9d4ec0359a5c | 935 | { |
shennongmin | 10:9d4ec0359a5c | 936 | esp_uart.print("AT+CIPCLOSE="); |
shennongmin | 10:9d4ec0359a5c | 937 | esp_uart.println(String(id)); |
shennongmin | 10:9d4ec0359a5c | 938 | String data; |
shennongmin | 10:9d4ec0359a5c | 939 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 940 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 941 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 942 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 943 | { |
shennongmin | 10:9d4ec0359a5c | 944 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 945 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 946 | } |
shennongmin | 10:9d4ec0359a5c | 947 | if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1) |
shennongmin | 10:9d4ec0359a5c | 948 | { |
shennongmin | 10:9d4ec0359a5c | 949 | break; |
shennongmin | 10:9d4ec0359a5c | 950 | } |
shennongmin | 10:9d4ec0359a5c | 951 | } |
shennongmin | 10:9d4ec0359a5c | 952 | |
shennongmin | 10:9d4ec0359a5c | 953 | } |
shennongmin | 10:9d4ec0359a5c | 954 | |
shennongmin | 10:9d4ec0359a5c | 955 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 956 | //show the current ip address |
shennongmin | 10:9d4ec0359a5c | 957 | |
shennongmin | 10:9d4ec0359a5c | 958 | return: string of ip address |
shennongmin | 10:9d4ec0359a5c | 959 | |
shennongmin | 10:9d4ec0359a5c | 960 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 961 | String ESP8266::showIP(void) |
shennongmin | 10:9d4ec0359a5c | 962 | { |
shennongmin | 10:9d4ec0359a5c | 963 | String data; |
shennongmin | 10:9d4ec0359a5c | 964 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 965 | //DBG("AT+CIFSR\r\n"); |
shennongmin | 10:9d4ec0359a5c | 966 | for(int a=0; a<3;a++) |
shennongmin | 10:9d4ec0359a5c | 967 | { |
shennongmin | 10:9d4ec0359a5c | 968 | esp_uart.println("AT+CIFSR"); |
shennongmin | 10:9d4ec0359a5c | 969 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 970 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 971 | while(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 972 | { |
shennongmin | 10:9d4ec0359a5c | 973 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 974 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 975 | } |
shennongmin | 10:9d4ec0359a5c | 976 | if (data.indexOf("AT+CIFSR")!=-1) |
shennongmin | 10:9d4ec0359a5c | 977 | { |
shennongmin | 10:9d4ec0359a5c | 978 | break; |
shennongmin | 10:9d4ec0359a5c | 979 | } |
shennongmin | 10:9d4ec0359a5c | 980 | } |
shennongmin | 10:9d4ec0359a5c | 981 | if(data.indexOf(".") != -1) |
shennongmin | 10:9d4ec0359a5c | 982 | { |
shennongmin | 10:9d4ec0359a5c | 983 | break; |
shennongmin | 10:9d4ec0359a5c | 984 | } |
shennongmin | 10:9d4ec0359a5c | 985 | data = ""; |
shennongmin | 10:9d4ec0359a5c | 986 | } |
shennongmin | 10:9d4ec0359a5c | 987 | //DBG(data); |
shennongmin | 10:9d4ec0359a5c | 988 | //DBG("\r\n"); |
shennongmin | 10:9d4ec0359a5c | 989 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 990 | char tail[7] = {0x0D,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 991 | data.replace("AT+CIFSR",""); |
shennongmin | 10:9d4ec0359a5c | 992 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 993 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 994 | |
shennongmin | 10:9d4ec0359a5c | 995 | return data; |
shennongmin | 10:9d4ec0359a5c | 996 | } |
shennongmin | 10:9d4ec0359a5c | 997 | |
shennongmin | 10:9d4ec0359a5c | 998 | /************************************************************************* |
shennongmin | 10:9d4ec0359a5c | 999 | ////set the parameter of server |
shennongmin | 10:9d4ec0359a5c | 1000 | |
shennongmin | 10:9d4ec0359a5c | 1001 | mode: |
shennongmin | 10:9d4ec0359a5c | 1002 | 0 - close server mode |
shennongmin | 10:9d4ec0359a5c | 1003 | 1 - open server mode |
shennongmin | 10:9d4ec0359a5c | 1004 | |
shennongmin | 10:9d4ec0359a5c | 1005 | port: <port> |
shennongmin | 10:9d4ec0359a5c | 1006 | |
shennongmin | 10:9d4ec0359a5c | 1007 | return: |
shennongmin | 10:9d4ec0359a5c | 1008 | true - successfully |
shennongmin | 10:9d4ec0359a5c | 1009 | false - unsuccessfully |
shennongmin | 10:9d4ec0359a5c | 1010 | |
shennongmin | 10:9d4ec0359a5c | 1011 | ***************************************************************************/ |
shennongmin | 10:9d4ec0359a5c | 1012 | |
shennongmin | 10:9d4ec0359a5c | 1013 | bool ESP8266::confServer(uint8_t mode, int port) |
shennongmin | 10:9d4ec0359a5c | 1014 | { |
shennongmin | 10:9d4ec0359a5c | 1015 | esp_uart.print("AT+CIPSERVER="); |
shennongmin | 10:9d4ec0359a5c | 1016 | esp_uart.print(String(mode)); |
shennongmin | 10:9d4ec0359a5c | 1017 | esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 1018 | esp_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 1019 | |
shennongmin | 10:9d4ec0359a5c | 1020 | String data; |
shennongmin | 10:9d4ec0359a5c | 1021 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 1022 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 1023 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 1024 | while (millis()-start<3000) { |
shennongmin | 10:9d4ec0359a5c | 1025 | if(esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 1026 | { |
shennongmin | 10:9d4ec0359a5c | 1027 | char a =esp_uart.read_char(); |
shennongmin | 10:9d4ec0359a5c | 1028 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 1029 | } |
shennongmin | 10:9d4ec0359a5c | 1030 | if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1) |
shennongmin | 10:9d4ec0359a5c | 1031 | { |
shennongmin | 10:9d4ec0359a5c | 1032 | found = true; |
shennongmin | 10:9d4ec0359a5c | 1033 | break; |
shennongmin | 10:9d4ec0359a5c | 1034 | } |
shennongmin | 10:9d4ec0359a5c | 1035 | } |
shennongmin | 10:9d4ec0359a5c | 1036 | esp_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 1037 | return found; |
shennongmin | 10:9d4ec0359a5c | 1038 | } |