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