pengfei wu
/
ESP8266Program
For test
ESP8266.cpp@33:f721a46ef2f2, 2015-02-05 (annotated)
- Committer:
- shennongmin
- Date:
- Thu Feb 05 02:28:09 2015 +0000
- Revision:
- 33:f721a46ef2f2
- Parent:
- 30:46a560b71e61
- Child:
- 35:90be2bc2a492
fixed showIP bug.
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 | |
shennongmin | 29:75185b05d6a4 | 4 | ESP8266::ESP8266(PinName tx, PinName rx, int baud_rate):m_uart(tx, rx) { |
shennongmin | 29:75185b05d6a4 | 5 | m_uart.begin(baud_rate); |
shennongmin | 29:75185b05d6a4 | 6 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 7 | m_uart.setTimeout(1000); |
shennongmin | 10:9d4ec0359a5c | 8 | } |
shennongmin | 10:9d4ec0359a5c | 9 | |
shennongmin | 29:75185b05d6a4 | 10 | int ESP8266::getMuxID(void) { |
shennongmin | 29:75185b05d6a4 | 11 | return m_mux_id; |
shennongmin | 10:9d4ec0359a5c | 12 | } |
shennongmin | 10:9d4ec0359a5c | 13 | |
shennongmin | 29:75185b05d6a4 | 14 | bool ESP8266::reset(void) |
shennongmin | 29:75185b05d6a4 | 15 | { |
shennongmin | 33:f721a46ef2f2 | 16 | unsigned long start; |
shennongmin | 33:f721a46ef2f2 | 17 | m_uart.setTimeout(100); |
shennongmin | 33:f721a46ef2f2 | 18 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 19 | m_uart.println("AT+RST"); |
shennongmin | 33:f721a46ef2f2 | 20 | if (!m_uart.find("OK")) { |
shennongmin | 33:f721a46ef2f2 | 21 | return false; |
shennongmin | 33:f721a46ef2f2 | 22 | } |
shennongmin | 33:f721a46ef2f2 | 23 | |
shennongmin | 33:f721a46ef2f2 | 24 | delay(2000); |
shennongmin | 33:f721a46ef2f2 | 25 | |
shennongmin | 33:f721a46ef2f2 | 26 | start = millis(); |
shennongmin | 33:f721a46ef2f2 | 27 | while (millis() - start < 3000) { |
shennongmin | 33:f721a46ef2f2 | 28 | m_uart.flush(); |
shennongmin | 33:f721a46ef2f2 | 29 | m_uart.println("AT"); |
shennongmin | 33:f721a46ef2f2 | 30 | if (m_uart.find("OK")) { |
shennongmin | 33:f721a46ef2f2 | 31 | delay(500); |
shennongmin | 33:f721a46ef2f2 | 32 | return true; |
shennongmin | 33:f721a46ef2f2 | 33 | } |
shennongmin | 33:f721a46ef2f2 | 34 | delay(100); |
shennongmin | 33:f721a46ef2f2 | 35 | } |
shennongmin | 33:f721a46ef2f2 | 36 | return false; |
shennongmin | 29:75185b05d6a4 | 37 | } |
shennongmin | 10:9d4ec0359a5c | 38 | |
shennongmin | 30:46a560b71e61 | 39 | bool ESP8266::setStationMode(String ssid, String pwd) |
shennongmin | 10:9d4ec0359a5c | 40 | { |
shennongmin | 30:46a560b71e61 | 41 | if (!confMode(ESP8266_MODE_STA)) |
shennongmin | 30:46a560b71e61 | 42 | { |
shennongmin | 30:46a560b71e61 | 43 | return false; |
shennongmin | 10:9d4ec0359a5c | 44 | } |
shennongmin | 30:46a560b71e61 | 45 | return confJAP(ssid, pwd); |
shennongmin | 30:46a560b71e61 | 46 | } |
shennongmin | 30:46a560b71e61 | 47 | |
shennongmin | 30:46a560b71e61 | 48 | bool ESP8266::setSoftAPMode(String sap_ssid, String sap_pwd, |
shennongmin | 30:46a560b71e61 | 49 | ESP8266Channel chl, ESP8266Encrypstion ecn) |
shennongmin | 30:46a560b71e61 | 50 | { |
shennongmin | 30:46a560b71e61 | 51 | if (!confMode(ESP8266_MODE_SAP)) |
shennongmin | 10:9d4ec0359a5c | 52 | { |
shennongmin | 30:46a560b71e61 | 53 | return false; |
shennongmin | 10:9d4ec0359a5c | 54 | } |
shennongmin | 30:46a560b71e61 | 55 | reset(); |
shennongmin | 30:46a560b71e61 | 56 | return confSAP(sap_ssid, sap_pwd, chl, ecn); |
shennongmin | 30:46a560b71e61 | 57 | } |
shennongmin | 30:46a560b71e61 | 58 | |
shennongmin | 30:46a560b71e61 | 59 | bool ESP8266::setStationSoftAPMode(String ssid, String pwd, |
shennongmin | 30:46a560b71e61 | 60 | String sap_ssid, String sap_pwd , |
shennongmin | 30:46a560b71e61 | 61 | ESP8266Channel chl, ESP8266Encrypstion ecn) |
shennongmin | 30:46a560b71e61 | 62 | { |
shennongmin | 30:46a560b71e61 | 63 | if (!confMode(ESP8266_MODE_STA_SAP)) |
shennongmin | 10:9d4ec0359a5c | 64 | { |
shennongmin | 30:46a560b71e61 | 65 | return false; |
shennongmin | 10:9d4ec0359a5c | 66 | } |
shennongmin | 30:46a560b71e61 | 67 | reset(); |
shennongmin | 30:46a560b71e61 | 68 | if (confJAP(ssid, pwd) && confSAP(sap_ssid, sap_pwd, chl, ecn)) { |
shennongmin | 30:46a560b71e61 | 69 | return true; |
shennongmin | 30:46a560b71e61 | 70 | } else { |
shennongmin | 30:46a560b71e61 | 71 | return false; |
shennongmin | 30:46a560b71e61 | 72 | } |
shennongmin | 10:9d4ec0359a5c | 73 | } |
shennongmin | 10:9d4ec0359a5c | 74 | |
shennongmin | 10:9d4ec0359a5c | 75 | |
shennongmin | 17:c449356787f1 | 76 | bool ESP8266::ipConfig(uint8_t type, String addr, int port, uint8_t mux, uint8_t id) |
shennongmin | 10:9d4ec0359a5c | 77 | { |
shennongmin | 10:9d4ec0359a5c | 78 | bool result = false; |
shennongmin | 17:c449356787f1 | 79 | if (mux == 0 ) |
shennongmin | 10:9d4ec0359a5c | 80 | { |
shennongmin | 17:c449356787f1 | 81 | confMux(mux); |
shennongmin | 10:9d4ec0359a5c | 82 | |
shennongmin | 10:9d4ec0359a5c | 83 | long timeStart = millis(); |
shennongmin | 10:9d4ec0359a5c | 84 | while (1) |
shennongmin | 10:9d4ec0359a5c | 85 | { |
shennongmin | 10:9d4ec0359a5c | 86 | long time0 = millis(); |
shennongmin | 10:9d4ec0359a5c | 87 | if (time0 - timeStart > 5000) |
shennongmin | 10:9d4ec0359a5c | 88 | { |
shennongmin | 10:9d4ec0359a5c | 89 | break; |
shennongmin | 10:9d4ec0359a5c | 90 | } |
shennongmin | 10:9d4ec0359a5c | 91 | } |
shennongmin | 10:9d4ec0359a5c | 92 | result = newMux(type, addr, port); |
shennongmin | 10:9d4ec0359a5c | 93 | } |
shennongmin | 17:c449356787f1 | 94 | else if (mux == 1) |
shennongmin | 10:9d4ec0359a5c | 95 | { |
shennongmin | 17:c449356787f1 | 96 | confMux(mux); |
shennongmin | 10:9d4ec0359a5c | 97 | long timeStart = millis(); |
shennongmin | 10:9d4ec0359a5c | 98 | while (1) |
shennongmin | 10:9d4ec0359a5c | 99 | { |
shennongmin | 10:9d4ec0359a5c | 100 | long time0 = millis(); |
shennongmin | 10:9d4ec0359a5c | 101 | if (time0 - timeStart > 5000) |
shennongmin | 10:9d4ec0359a5c | 102 | { |
shennongmin | 10:9d4ec0359a5c | 103 | break; |
shennongmin | 10:9d4ec0359a5c | 104 | } |
shennongmin | 10:9d4ec0359a5c | 105 | } |
shennongmin | 10:9d4ec0359a5c | 106 | result = newMux(id, type, addr, port); |
shennongmin | 10:9d4ec0359a5c | 107 | } |
shennongmin | 10:9d4ec0359a5c | 108 | return result; |
shennongmin | 10:9d4ec0359a5c | 109 | } |
shennongmin | 10:9d4ec0359a5c | 110 | |
shennongmin | 10:9d4ec0359a5c | 111 | |
shennongmin | 23:4cec7b9e3715 | 112 | int ESP8266::recvData(char *buf) |
shennongmin | 10:9d4ec0359a5c | 113 | { |
shennongmin | 10:9d4ec0359a5c | 114 | String data = ""; |
shennongmin | 29:75185b05d6a4 | 115 | if (m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 116 | { |
shennongmin | 10:9d4ec0359a5c | 117 | |
shennongmin | 10:9d4ec0359a5c | 118 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 119 | start = millis(); |
shennongmin | 29:75185b05d6a4 | 120 | char c0 = m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 121 | if (c0 == '+') |
shennongmin | 10:9d4ec0359a5c | 122 | { |
shennongmin | 10:9d4ec0359a5c | 123 | |
shennongmin | 10:9d4ec0359a5c | 124 | while (millis()-start<5000) |
shennongmin | 10:9d4ec0359a5c | 125 | { |
shennongmin | 29:75185b05d6a4 | 126 | if (m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 127 | { |
shennongmin | 29:75185b05d6a4 | 128 | char c = m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 129 | data += c; |
shennongmin | 10:9d4ec0359a5c | 130 | } |
shennongmin | 10:9d4ec0359a5c | 131 | if (data.indexOf("\nOK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 132 | { |
shennongmin | 10:9d4ec0359a5c | 133 | break; |
shennongmin | 10:9d4ec0359a5c | 134 | } |
shennongmin | 10:9d4ec0359a5c | 135 | } |
shennongmin | 28:c25aff31dcc5 | 136 | |
shennongmin | 10:9d4ec0359a5c | 137 | int sLen = strlen(data.c_str()); |
shennongmin | 10:9d4ec0359a5c | 138 | int i,j; |
shennongmin | 10:9d4ec0359a5c | 139 | for (i = 0; i <= sLen; i++) |
shennongmin | 10:9d4ec0359a5c | 140 | { |
shennongmin | 10:9d4ec0359a5c | 141 | if (data[i] == ':') |
shennongmin | 10:9d4ec0359a5c | 142 | { |
shennongmin | 10:9d4ec0359a5c | 143 | break; |
shennongmin | 10:9d4ec0359a5c | 144 | } |
shennongmin | 10:9d4ec0359a5c | 145 | |
shennongmin | 10:9d4ec0359a5c | 146 | } |
shennongmin | 10:9d4ec0359a5c | 147 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 148 | for (j = 4; j <= i; j++) |
shennongmin | 10:9d4ec0359a5c | 149 | { |
shennongmin | 10:9d4ec0359a5c | 150 | if (data[j] == ',') |
shennongmin | 10:9d4ec0359a5c | 151 | { |
shennongmin | 10:9d4ec0359a5c | 152 | found = true; |
shennongmin | 10:9d4ec0359a5c | 153 | break; |
shennongmin | 10:9d4ec0359a5c | 154 | } |
shennongmin | 10:9d4ec0359a5c | 155 | |
shennongmin | 10:9d4ec0359a5c | 156 | } |
shennongmin | 10:9d4ec0359a5c | 157 | int iSize; |
shennongmin | 10:9d4ec0359a5c | 158 | if(found ==true) |
shennongmin | 10:9d4ec0359a5c | 159 | { |
shennongmin | 10:9d4ec0359a5c | 160 | String _id = data.substring(4, j); |
shennongmin | 29:75185b05d6a4 | 161 | m_mux_id = _id.toInt(); |
shennongmin | 10:9d4ec0359a5c | 162 | String _size = data.substring(j+1, i); |
shennongmin | 10:9d4ec0359a5c | 163 | iSize = _size.toInt(); |
shennongmin | 14:40b1decf03f3 | 164 | |
shennongmin | 10:9d4ec0359a5c | 165 | String str = data.substring(i+1, i+1+iSize); |
shennongmin | 10:9d4ec0359a5c | 166 | strcpy(buf, str.c_str()); |
shennongmin | 14:40b1decf03f3 | 167 | |
shennongmin | 10:9d4ec0359a5c | 168 | |
shennongmin | 10:9d4ec0359a5c | 169 | } |
shennongmin | 10:9d4ec0359a5c | 170 | else |
shennongmin | 10:9d4ec0359a5c | 171 | { |
shennongmin | 10:9d4ec0359a5c | 172 | String _size = data.substring(4, i); |
shennongmin | 10:9d4ec0359a5c | 173 | iSize = _size.toInt(); |
shennongmin | 14:40b1decf03f3 | 174 | |
shennongmin | 14:40b1decf03f3 | 175 | |
shennongmin | 10:9d4ec0359a5c | 176 | String str = data.substring(i+1, i+1+iSize); |
shennongmin | 10:9d4ec0359a5c | 177 | strcpy(buf, str.c_str()); |
shennongmin | 14:40b1decf03f3 | 178 | |
shennongmin | 10:9d4ec0359a5c | 179 | } |
shennongmin | 10:9d4ec0359a5c | 180 | return iSize; |
shennongmin | 10:9d4ec0359a5c | 181 | } |
shennongmin | 10:9d4ec0359a5c | 182 | } |
shennongmin | 10:9d4ec0359a5c | 183 | |
shennongmin | 10:9d4ec0359a5c | 184 | return 0; |
shennongmin | 10:9d4ec0359a5c | 185 | } |
shennongmin | 10:9d4ec0359a5c | 186 | |
shennongmin | 10:9d4ec0359a5c | 187 | String ESP8266::showMode() |
shennongmin | 10:9d4ec0359a5c | 188 | { |
shennongmin | 10:9d4ec0359a5c | 189 | String data; |
shennongmin | 29:75185b05d6a4 | 190 | m_uart.println("AT+CWMODE?"); |
shennongmin | 10:9d4ec0359a5c | 191 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 192 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 193 | while (millis()-start<2000) { |
shennongmin | 29:75185b05d6a4 | 194 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 195 | { |
shennongmin | 29:75185b05d6a4 | 196 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 197 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 198 | } |
shennongmin | 10:9d4ec0359a5c | 199 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 200 | { |
shennongmin | 10:9d4ec0359a5c | 201 | break; |
shennongmin | 10:9d4ec0359a5c | 202 | } |
shennongmin | 10:9d4ec0359a5c | 203 | } |
shennongmin | 10:9d4ec0359a5c | 204 | if(data.indexOf("1")!=-1) |
shennongmin | 10:9d4ec0359a5c | 205 | { |
shennongmin | 10:9d4ec0359a5c | 206 | return "Station"; |
shennongmin | 10:9d4ec0359a5c | 207 | }else if(data.indexOf("2")!=-1) |
shennongmin | 10:9d4ec0359a5c | 208 | { |
shennongmin | 10:9d4ec0359a5c | 209 | return "AP"; |
shennongmin | 10:9d4ec0359a5c | 210 | }else if(data.indexOf("3")!=-1) |
shennongmin | 10:9d4ec0359a5c | 211 | { |
shennongmin | 10:9d4ec0359a5c | 212 | return "AP+Station"; |
shennongmin | 10:9d4ec0359a5c | 213 | }else{ |
shennongmin | 10:9d4ec0359a5c | 214 | return "Invalid Mode!"; |
shennongmin | 10:9d4ec0359a5c | 215 | } |
shennongmin | 10:9d4ec0359a5c | 216 | } |
shennongmin | 10:9d4ec0359a5c | 217 | |
shennongmin | 17:c449356787f1 | 218 | bool ESP8266::confMode(uint8_t mode) |
shennongmin | 10:9d4ec0359a5c | 219 | { |
shennongmin | 10:9d4ec0359a5c | 220 | String data; |
shennongmin | 33:f721a46ef2f2 | 221 | m_uart.flush(); |
shennongmin | 33:f721a46ef2f2 | 222 | m_uart.print("AT+CWMODE="); |
shennongmin | 33:f721a46ef2f2 | 223 | m_uart.println(String(mode)); |
shennongmin | 33:f721a46ef2f2 | 224 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 225 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 226 | while (millis()-start<2000) { |
shennongmin | 33:f721a46ef2f2 | 227 | while(m_uart.available()>0) { |
shennongmin | 33:f721a46ef2f2 | 228 | char c = m_uart.readChr(); |
shennongmin | 33:f721a46ef2f2 | 229 | data += c; |
shennongmin | 33:f721a46ef2f2 | 230 | } |
shennongmin | 33:f721a46ef2f2 | 231 | if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1) |
shennongmin | 33:f721a46ef2f2 | 232 | { |
shennongmin | 33:f721a46ef2f2 | 233 | return true; |
shennongmin | 33:f721a46ef2f2 | 234 | } else if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1) |
shennongmin | 33:f721a46ef2f2 | 235 | { |
shennongmin | 33:f721a46ef2f2 | 236 | return false; |
shennongmin | 33:f721a46ef2f2 | 237 | } |
shennongmin | 33:f721a46ef2f2 | 238 | } |
shennongmin | 33:f721a46ef2f2 | 239 | return false; |
shennongmin | 10:9d4ec0359a5c | 240 | } |
shennongmin | 10:9d4ec0359a5c | 241 | |
shennongmin | 10:9d4ec0359a5c | 242 | String ESP8266::showAP(void) |
shennongmin | 10:9d4ec0359a5c | 243 | { |
shennongmin | 10:9d4ec0359a5c | 244 | String data; |
shennongmin | 29:75185b05d6a4 | 245 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 246 | m_uart.print("AT+CWLAP\r\n"); |
shennongmin | 10:9d4ec0359a5c | 247 | delay(5000); |
shennongmin | 10:9d4ec0359a5c | 248 | |
shennongmin | 10:9d4ec0359a5c | 249 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 250 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 251 | while (millis()-start<8000) { |
shennongmin | 29:75185b05d6a4 | 252 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 253 | { |
shennongmin | 29:75185b05d6a4 | 254 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 255 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 256 | } |
shennongmin | 10:9d4ec0359a5c | 257 | if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 258 | { |
shennongmin | 10:9d4ec0359a5c | 259 | break; |
shennongmin | 10:9d4ec0359a5c | 260 | } |
shennongmin | 10:9d4ec0359a5c | 261 | } |
shennongmin | 10:9d4ec0359a5c | 262 | if(data.indexOf("ERROR")!=-1) |
shennongmin | 10:9d4ec0359a5c | 263 | { |
shennongmin | 10:9d4ec0359a5c | 264 | return "ERROR"; |
shennongmin | 10:9d4ec0359a5c | 265 | } |
shennongmin | 10:9d4ec0359a5c | 266 | else{ |
shennongmin | 10:9d4ec0359a5c | 267 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 268 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 269 | data.replace("AT+CWLAP",""); |
shennongmin | 10:9d4ec0359a5c | 270 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 271 | data.replace("+CWLAP","ESP8266"); |
shennongmin | 10:9d4ec0359a5c | 272 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 273 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 274 | |
shennongmin | 10:9d4ec0359a5c | 275 | return data; |
shennongmin | 10:9d4ec0359a5c | 276 | } |
shennongmin | 10:9d4ec0359a5c | 277 | } |
shennongmin | 10:9d4ec0359a5c | 278 | |
shennongmin | 10:9d4ec0359a5c | 279 | String ESP8266::showJAP(void) |
shennongmin | 10:9d4ec0359a5c | 280 | { |
shennongmin | 27:dc7ad070c5b1 | 281 | bool ret = false; |
shennongmin | 29:75185b05d6a4 | 282 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 283 | m_uart.println("AT+CWJAP?"); |
shennongmin | 27:dc7ad070c5b1 | 284 | |
shennongmin | 27:dc7ad070c5b1 | 285 | String data; |
shennongmin | 27:dc7ad070c5b1 | 286 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 287 | start = millis(); |
shennongmin | 27:dc7ad070c5b1 | 288 | while (millis()-start < 3000) { |
shennongmin | 29:75185b05d6a4 | 289 | while(m_uart.available() > 0) { |
shennongmin | 29:75185b05d6a4 | 290 | char a = m_uart.readChr(); |
shennongmin | 27:dc7ad070c5b1 | 291 | data += a; |
shennongmin | 27:dc7ad070c5b1 | 292 | //printf("%c", a); |
shennongmin | 27:dc7ad070c5b1 | 293 | } |
shennongmin | 27:dc7ad070c5b1 | 294 | if (data.indexOf("OK") != -1) { |
shennongmin | 27:dc7ad070c5b1 | 295 | ret = true; |
shennongmin | 27:dc7ad070c5b1 | 296 | break; |
shennongmin | 27:dc7ad070c5b1 | 297 | } else if (data.indexOf("ERROR") != -1) { |
shennongmin | 27:dc7ad070c5b1 | 298 | ret = false; |
shennongmin | 27:dc7ad070c5b1 | 299 | break; |
shennongmin | 27:dc7ad070c5b1 | 300 | } |
shennongmin | 10:9d4ec0359a5c | 301 | } |
shennongmin | 27:dc7ad070c5b1 | 302 | if (ret) { |
shennongmin | 27:dc7ad070c5b1 | 303 | int index1 = data.indexOf(":\""); |
shennongmin | 27:dc7ad070c5b1 | 304 | int index2 = data.indexOf("\"\r\n"); |
shennongmin | 27:dc7ad070c5b1 | 305 | if (index1 != -1 && index2 != -1) { |
shennongmin | 27:dc7ad070c5b1 | 306 | return data.substring(index1 + 2, index2); |
shennongmin | 27:dc7ad070c5b1 | 307 | } else { |
shennongmin | 27:dc7ad070c5b1 | 308 | return "not found"; |
shennongmin | 27:dc7ad070c5b1 | 309 | } |
shennongmin | 27:dc7ad070c5b1 | 310 | } else { |
shennongmin | 27:dc7ad070c5b1 | 311 | return "null"; |
shennongmin | 27:dc7ad070c5b1 | 312 | } |
shennongmin | 10:9d4ec0359a5c | 313 | } |
shennongmin | 10:9d4ec0359a5c | 314 | |
shennongmin | 10:9d4ec0359a5c | 315 | bool ESP8266::confJAP(String ssid , String pwd) |
shennongmin | 10:9d4ec0359a5c | 316 | { |
shennongmin | 33:f721a46ef2f2 | 317 | bool ret = false; |
shennongmin | 33:f721a46ef2f2 | 318 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 319 | m_uart.print("AT+CWJAP="); |
shennongmin | 29:75185b05d6a4 | 320 | m_uart.print("\""); //"ssid" |
shennongmin | 29:75185b05d6a4 | 321 | m_uart.print(ssid); |
shennongmin | 29:75185b05d6a4 | 322 | m_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 323 | |
shennongmin | 29:75185b05d6a4 | 324 | m_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 325 | |
shennongmin | 29:75185b05d6a4 | 326 | m_uart.print("\""); //"pwd" |
shennongmin | 29:75185b05d6a4 | 327 | m_uart.print(pwd); |
shennongmin | 29:75185b05d6a4 | 328 | m_uart.println("\""); |
shennongmin | 10:9d4ec0359a5c | 329 | |
shennongmin | 33:f721a46ef2f2 | 330 | String data; |
shennongmin | 10:9d4ec0359a5c | 331 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 332 | start = millis(); |
shennongmin | 33:f721a46ef2f2 | 333 | while (millis() - start < 10000) { |
shennongmin | 33:f721a46ef2f2 | 334 | while (m_uart.available() > 0) { |
shennongmin | 33:f721a46ef2f2 | 335 | char c = m_uart.readChr(); |
shennongmin | 33:f721a46ef2f2 | 336 | data += c; |
shennongmin | 33:f721a46ef2f2 | 337 | } |
shennongmin | 33:f721a46ef2f2 | 338 | if(data.indexOf("OK") != -1) { |
shennongmin | 33:f721a46ef2f2 | 339 | ret = true; |
shennongmin | 33:f721a46ef2f2 | 340 | break; |
shennongmin | 33:f721a46ef2f2 | 341 | } else if (data.indexOf("ERROR") != -1) { |
shennongmin | 33:f721a46ef2f2 | 342 | ret = false; |
shennongmin | 33:f721a46ef2f2 | 343 | break; |
shennongmin | 10:9d4ec0359a5c | 344 | } |
shennongmin | 10:9d4ec0359a5c | 345 | } |
shennongmin | 33:f721a46ef2f2 | 346 | |
shennongmin | 33:f721a46ef2f2 | 347 | if (ret) { |
shennongmin | 33:f721a46ef2f2 | 348 | return true; |
shennongmin | 33:f721a46ef2f2 | 349 | } else { |
shennongmin | 33:f721a46ef2f2 | 350 | return false; |
shennongmin | 33:f721a46ef2f2 | 351 | } |
shennongmin | 10:9d4ec0359a5c | 352 | } |
shennongmin | 10:9d4ec0359a5c | 353 | |
shennongmin | 10:9d4ec0359a5c | 354 | bool ESP8266::quitAP(void) |
shennongmin | 10:9d4ec0359a5c | 355 | { |
shennongmin | 29:75185b05d6a4 | 356 | m_uart.println("AT+CWQAP"); |
shennongmin | 10:9d4ec0359a5c | 357 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 358 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 359 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 360 | if(m_uart.find("OK")==true) |
shennongmin | 10:9d4ec0359a5c | 361 | { |
shennongmin | 10:9d4ec0359a5c | 362 | return true; |
shennongmin | 10:9d4ec0359a5c | 363 | |
shennongmin | 10:9d4ec0359a5c | 364 | } |
shennongmin | 10:9d4ec0359a5c | 365 | } |
shennongmin | 10:9d4ec0359a5c | 366 | return false; |
shennongmin | 10:9d4ec0359a5c | 367 | |
shennongmin | 10:9d4ec0359a5c | 368 | } |
shennongmin | 10:9d4ec0359a5c | 369 | |
shennongmin | 10:9d4ec0359a5c | 370 | String ESP8266::showSAP() |
shennongmin | 10:9d4ec0359a5c | 371 | { |
shennongmin | 29:75185b05d6a4 | 372 | m_uart.println("AT+CWSAP?"); |
shennongmin | 10:9d4ec0359a5c | 373 | String data; |
shennongmin | 10:9d4ec0359a5c | 374 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 375 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 376 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 377 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 378 | { |
shennongmin | 29:75185b05d6a4 | 379 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 380 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 381 | } |
shennongmin | 10:9d4ec0359a5c | 382 | if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 383 | { |
shennongmin | 10:9d4ec0359a5c | 384 | break; |
shennongmin | 10:9d4ec0359a5c | 385 | } |
shennongmin | 10:9d4ec0359a5c | 386 | } |
shennongmin | 10:9d4ec0359a5c | 387 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 388 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 389 | data.replace("AT+CWSAP?",""); |
shennongmin | 10:9d4ec0359a5c | 390 | data.replace("+CWSAP","mySAP"); |
shennongmin | 10:9d4ec0359a5c | 391 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 392 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 393 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 394 | |
shennongmin | 10:9d4ec0359a5c | 395 | return data; |
shennongmin | 10:9d4ec0359a5c | 396 | } |
shennongmin | 10:9d4ec0359a5c | 397 | |
shennongmin | 10:9d4ec0359a5c | 398 | bool ESP8266::confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn) |
shennongmin | 10:9d4ec0359a5c | 399 | { |
shennongmin | 29:75185b05d6a4 | 400 | m_uart.print("AT+CWSAP="); |
shennongmin | 29:75185b05d6a4 | 401 | m_uart.print("\""); //"ssid" |
shennongmin | 29:75185b05d6a4 | 402 | m_uart.print(ssid); |
shennongmin | 29:75185b05d6a4 | 403 | m_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 404 | |
shennongmin | 29:75185b05d6a4 | 405 | m_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 406 | |
shennongmin | 29:75185b05d6a4 | 407 | m_uart.print("\""); //"pwd" |
shennongmin | 29:75185b05d6a4 | 408 | m_uart.print(pwd); |
shennongmin | 29:75185b05d6a4 | 409 | m_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 410 | |
shennongmin | 29:75185b05d6a4 | 411 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 412 | m_uart.print(String(chl)); |
shennongmin | 10:9d4ec0359a5c | 413 | |
shennongmin | 29:75185b05d6a4 | 414 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 415 | m_uart.println(String(ecn)); |
shennongmin | 10:9d4ec0359a5c | 416 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 417 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 418 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 419 | if(m_uart.find("OK")==true ) |
shennongmin | 10:9d4ec0359a5c | 420 | { |
shennongmin | 10:9d4ec0359a5c | 421 | return true; |
shennongmin | 10:9d4ec0359a5c | 422 | } |
shennongmin | 10:9d4ec0359a5c | 423 | } |
shennongmin | 10:9d4ec0359a5c | 424 | |
shennongmin | 10:9d4ec0359a5c | 425 | return false; |
shennongmin | 10:9d4ec0359a5c | 426 | |
shennongmin | 10:9d4ec0359a5c | 427 | } |
shennongmin | 10:9d4ec0359a5c | 428 | |
shennongmin | 10:9d4ec0359a5c | 429 | |
shennongmin | 10:9d4ec0359a5c | 430 | String ESP8266::showStatus(void) |
shennongmin | 10:9d4ec0359a5c | 431 | { |
shennongmin | 29:75185b05d6a4 | 432 | m_uart.println("AT+CIPSTATUS"); |
shennongmin | 10:9d4ec0359a5c | 433 | String data; |
shennongmin | 10:9d4ec0359a5c | 434 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 435 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 436 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 437 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 438 | { |
shennongmin | 29:75185b05d6a4 | 439 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 440 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 441 | } |
shennongmin | 10:9d4ec0359a5c | 442 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 443 | { |
shennongmin | 10:9d4ec0359a5c | 444 | break; |
shennongmin | 10:9d4ec0359a5c | 445 | } |
shennongmin | 10:9d4ec0359a5c | 446 | } |
shennongmin | 10:9d4ec0359a5c | 447 | |
shennongmin | 10:9d4ec0359a5c | 448 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 449 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 450 | data.replace("AT+CIPSTATUS",""); |
shennongmin | 10:9d4ec0359a5c | 451 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 452 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 453 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 454 | |
shennongmin | 10:9d4ec0359a5c | 455 | return data; |
shennongmin | 10:9d4ec0359a5c | 456 | } |
shennongmin | 10:9d4ec0359a5c | 457 | |
shennongmin | 10:9d4ec0359a5c | 458 | String ESP8266::showMux(void) |
shennongmin | 10:9d4ec0359a5c | 459 | { |
shennongmin | 10:9d4ec0359a5c | 460 | String data; |
shennongmin | 29:75185b05d6a4 | 461 | m_uart.println("AT+CIPMUX?"); |
shennongmin | 10:9d4ec0359a5c | 462 | |
shennongmin | 10:9d4ec0359a5c | 463 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 464 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 465 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 466 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 467 | { |
shennongmin | 29:75185b05d6a4 | 468 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 469 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 470 | } |
shennongmin | 10:9d4ec0359a5c | 471 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 472 | { |
shennongmin | 10:9d4ec0359a5c | 473 | break; |
shennongmin | 10:9d4ec0359a5c | 474 | } |
shennongmin | 10:9d4ec0359a5c | 475 | } |
shennongmin | 10:9d4ec0359a5c | 476 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 477 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 478 | data.replace("AT+CIPMUX?",""); |
shennongmin | 10:9d4ec0359a5c | 479 | data.replace("+CIPMUX","showMux"); |
shennongmin | 10:9d4ec0359a5c | 480 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 481 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 482 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 483 | |
shennongmin | 10:9d4ec0359a5c | 484 | return data; |
shennongmin | 10:9d4ec0359a5c | 485 | } |
shennongmin | 10:9d4ec0359a5c | 486 | |
shennongmin | 17:c449356787f1 | 487 | bool ESP8266::confMux(int mux) |
shennongmin | 10:9d4ec0359a5c | 488 | { |
shennongmin | 29:75185b05d6a4 | 489 | m_uart.print("AT+CIPMUX="); |
shennongmin | 29:75185b05d6a4 | 490 | m_uart.println(mux); |
shennongmin | 10:9d4ec0359a5c | 491 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 492 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 493 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 494 | if(m_uart.find("OK")==true ) |
shennongmin | 10:9d4ec0359a5c | 495 | { |
shennongmin | 10:9d4ec0359a5c | 496 | return true; |
shennongmin | 10:9d4ec0359a5c | 497 | } |
shennongmin | 10:9d4ec0359a5c | 498 | } |
shennongmin | 10:9d4ec0359a5c | 499 | |
shennongmin | 10:9d4ec0359a5c | 500 | return false; |
shennongmin | 10:9d4ec0359a5c | 501 | } |
shennongmin | 10:9d4ec0359a5c | 502 | |
shennongmin | 10:9d4ec0359a5c | 503 | bool ESP8266::newMux(uint8_t type, String addr, int port) |
shennongmin | 10:9d4ec0359a5c | 504 | |
shennongmin | 10:9d4ec0359a5c | 505 | { |
shennongmin | 10:9d4ec0359a5c | 506 | String data; |
shennongmin | 29:75185b05d6a4 | 507 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 508 | m_uart.print("AT+CIPSTART="); |
shennongmin | 28:c25aff31dcc5 | 509 | if(ESP8266_COMM_TCP == type) { |
shennongmin | 29:75185b05d6a4 | 510 | m_uart.print("\"TCP\""); |
shennongmin | 28:c25aff31dcc5 | 511 | } else if (ESP8266_COMM_UDP == type) { |
shennongmin | 29:75185b05d6a4 | 512 | m_uart.print("\"UDP\""); |
shennongmin | 10:9d4ec0359a5c | 513 | } |
shennongmin | 29:75185b05d6a4 | 514 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 515 | m_uart.print("\""); |
shennongmin | 29:75185b05d6a4 | 516 | m_uart.print(addr); |
shennongmin | 29:75185b05d6a4 | 517 | m_uart.print("\""); |
shennongmin | 29:75185b05d6a4 | 518 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 519 | m_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 520 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 521 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 522 | while (millis()-start<10000) { |
shennongmin | 29:75185b05d6a4 | 523 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 524 | { |
shennongmin | 29:75185b05d6a4 | 525 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 526 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 527 | } |
shennongmin | 10:9d4ec0359a5c | 528 | if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1) |
shennongmin | 10:9d4ec0359a5c | 529 | { |
shennongmin | 10:9d4ec0359a5c | 530 | return true; |
shennongmin | 10:9d4ec0359a5c | 531 | } |
shennongmin | 10:9d4ec0359a5c | 532 | } |
shennongmin | 10:9d4ec0359a5c | 533 | return false; |
shennongmin | 10:9d4ec0359a5c | 534 | } |
shennongmin | 10:9d4ec0359a5c | 535 | |
shennongmin | 10:9d4ec0359a5c | 536 | bool ESP8266::newMux( uint8_t id, uint8_t type, String addr, int port) |
shennongmin | 10:9d4ec0359a5c | 537 | { |
shennongmin | 29:75185b05d6a4 | 538 | m_uart.print("AT+CIPSTART="); |
shennongmin | 29:75185b05d6a4 | 539 | m_uart.print("\""); |
shennongmin | 29:75185b05d6a4 | 540 | m_uart.print(String(id)); |
shennongmin | 29:75185b05d6a4 | 541 | m_uart.print("\""); |
shennongmin | 28:c25aff31dcc5 | 542 | if(ESP8266_COMM_TCP == type) { |
shennongmin | 29:75185b05d6a4 | 543 | m_uart.print("\"TCP\""); |
shennongmin | 28:c25aff31dcc5 | 544 | } else if (ESP8266_COMM_UDP == type) { |
shennongmin | 29:75185b05d6a4 | 545 | m_uart.print("\"UDP\""); |
shennongmin | 10:9d4ec0359a5c | 546 | } |
shennongmin | 29:75185b05d6a4 | 547 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 548 | m_uart.print("\""); |
shennongmin | 29:75185b05d6a4 | 549 | m_uart.print(addr); |
shennongmin | 29:75185b05d6a4 | 550 | m_uart.print("\""); |
shennongmin | 29:75185b05d6a4 | 551 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 552 | m_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 553 | String data; |
shennongmin | 10:9d4ec0359a5c | 554 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 555 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 556 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 557 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 558 | { |
shennongmin | 29:75185b05d6a4 | 559 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 560 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 561 | } |
shennongmin | 10:9d4ec0359a5c | 562 | if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 563 | { |
shennongmin | 10:9d4ec0359a5c | 564 | return true; |
shennongmin | 10:9d4ec0359a5c | 565 | } |
shennongmin | 10:9d4ec0359a5c | 566 | } |
shennongmin | 10:9d4ec0359a5c | 567 | return false; |
shennongmin | 10:9d4ec0359a5c | 568 | |
shennongmin | 10:9d4ec0359a5c | 569 | |
shennongmin | 10:9d4ec0359a5c | 570 | } |
shennongmin | 10:9d4ec0359a5c | 571 | |
shennongmin | 23:4cec7b9e3715 | 572 | bool ESP8266::send(String str) |
shennongmin | 10:9d4ec0359a5c | 573 | { |
shennongmin | 29:75185b05d6a4 | 574 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 575 | m_uart.print("AT+CIPSEND="); |
shennongmin | 29:75185b05d6a4 | 576 | m_uart.println(str.length()); |
shennongmin | 10:9d4ec0359a5c | 577 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 578 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 579 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 580 | while (millis()-start<5000) { |
shennongmin | 29:75185b05d6a4 | 581 | if(m_uart.find(">")==true ) |
shennongmin | 10:9d4ec0359a5c | 582 | { |
shennongmin | 10:9d4ec0359a5c | 583 | found = true; |
shennongmin | 10:9d4ec0359a5c | 584 | break; |
shennongmin | 10:9d4ec0359a5c | 585 | } |
shennongmin | 10:9d4ec0359a5c | 586 | } |
shennongmin | 10:9d4ec0359a5c | 587 | if(found) |
shennongmin | 29:75185b05d6a4 | 588 | m_uart.print(str); |
shennongmin | 10:9d4ec0359a5c | 589 | else |
shennongmin | 10:9d4ec0359a5c | 590 | { |
shennongmin | 10:9d4ec0359a5c | 591 | closeMux(); |
shennongmin | 10:9d4ec0359a5c | 592 | return false; |
shennongmin | 10:9d4ec0359a5c | 593 | } |
shennongmin | 10:9d4ec0359a5c | 594 | |
shennongmin | 10:9d4ec0359a5c | 595 | |
shennongmin | 10:9d4ec0359a5c | 596 | String data; |
shennongmin | 10:9d4ec0359a5c | 597 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 598 | while (millis()-start<5000) { |
shennongmin | 29:75185b05d6a4 | 599 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 600 | { |
shennongmin | 29:75185b05d6a4 | 601 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 602 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 603 | } |
shennongmin | 10:9d4ec0359a5c | 604 | if (data.indexOf("SEND OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 605 | { |
shennongmin | 10:9d4ec0359a5c | 606 | return true; |
shennongmin | 10:9d4ec0359a5c | 607 | } |
shennongmin | 10:9d4ec0359a5c | 608 | } |
shennongmin | 10:9d4ec0359a5c | 609 | return false; |
shennongmin | 10:9d4ec0359a5c | 610 | } |
shennongmin | 10:9d4ec0359a5c | 611 | |
shennongmin | 23:4cec7b9e3715 | 612 | bool ESP8266::send(uint8_t id, String str) |
shennongmin | 10:9d4ec0359a5c | 613 | { |
shennongmin | 29:75185b05d6a4 | 614 | m_uart.print("AT+CIPSEND="); |
shennongmin | 10:9d4ec0359a5c | 615 | |
shennongmin | 29:75185b05d6a4 | 616 | m_uart.print(String(id)); |
shennongmin | 29:75185b05d6a4 | 617 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 618 | m_uart.println(str.length()); |
shennongmin | 10:9d4ec0359a5c | 619 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 620 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 621 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 622 | while (millis()-start<5000) { |
shennongmin | 29:75185b05d6a4 | 623 | if(m_uart.find(">")==true ) |
shennongmin | 10:9d4ec0359a5c | 624 | { |
shennongmin | 10:9d4ec0359a5c | 625 | found = true; |
shennongmin | 10:9d4ec0359a5c | 626 | break; |
shennongmin | 10:9d4ec0359a5c | 627 | } |
shennongmin | 10:9d4ec0359a5c | 628 | } |
shennongmin | 10:9d4ec0359a5c | 629 | if(found) |
shennongmin | 29:75185b05d6a4 | 630 | m_uart.print(str); |
shennongmin | 10:9d4ec0359a5c | 631 | else |
shennongmin | 10:9d4ec0359a5c | 632 | { |
shennongmin | 10:9d4ec0359a5c | 633 | closeMux(id); |
shennongmin | 10:9d4ec0359a5c | 634 | return false; |
shennongmin | 10:9d4ec0359a5c | 635 | } |
shennongmin | 10:9d4ec0359a5c | 636 | |
shennongmin | 10:9d4ec0359a5c | 637 | |
shennongmin | 10:9d4ec0359a5c | 638 | String data; |
shennongmin | 10:9d4ec0359a5c | 639 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 640 | while (millis()-start<5000) { |
shennongmin | 29:75185b05d6a4 | 641 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 642 | { |
shennongmin | 29:75185b05d6a4 | 643 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 644 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 645 | } |
shennongmin | 10:9d4ec0359a5c | 646 | if (data.indexOf("SEND OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 647 | { |
shennongmin | 10:9d4ec0359a5c | 648 | return true; |
shennongmin | 10:9d4ec0359a5c | 649 | } |
shennongmin | 10:9d4ec0359a5c | 650 | } |
shennongmin | 10:9d4ec0359a5c | 651 | return false; |
shennongmin | 10:9d4ec0359a5c | 652 | } |
shennongmin | 10:9d4ec0359a5c | 653 | |
shennongmin | 10:9d4ec0359a5c | 654 | |
shennongmin | 10:9d4ec0359a5c | 655 | void ESP8266::closeMux(void) |
shennongmin | 10:9d4ec0359a5c | 656 | { |
shennongmin | 29:75185b05d6a4 | 657 | m_uart.println("AT+CIPCLOSE"); |
shennongmin | 10:9d4ec0359a5c | 658 | |
shennongmin | 10:9d4ec0359a5c | 659 | String data; |
shennongmin | 10:9d4ec0359a5c | 660 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 661 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 662 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 663 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 664 | { |
shennongmin | 29:75185b05d6a4 | 665 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 666 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 667 | } |
shennongmin | 10:9d4ec0359a5c | 668 | if (data.indexOf("Linked")!=-1 || data.indexOf("ERROR")!=-1 || data.indexOf("we must restart")!=-1) |
shennongmin | 10:9d4ec0359a5c | 669 | { |
shennongmin | 10:9d4ec0359a5c | 670 | break; |
shennongmin | 10:9d4ec0359a5c | 671 | } |
shennongmin | 10:9d4ec0359a5c | 672 | } |
shennongmin | 10:9d4ec0359a5c | 673 | } |
shennongmin | 10:9d4ec0359a5c | 674 | |
shennongmin | 10:9d4ec0359a5c | 675 | void ESP8266::closeMux(uint8_t id) |
shennongmin | 10:9d4ec0359a5c | 676 | { |
shennongmin | 29:75185b05d6a4 | 677 | m_uart.print("AT+CIPCLOSE="); |
shennongmin | 29:75185b05d6a4 | 678 | m_uart.println(String(id)); |
shennongmin | 10:9d4ec0359a5c | 679 | String data; |
shennongmin | 10:9d4ec0359a5c | 680 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 681 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 682 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 683 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 684 | { |
shennongmin | 29:75185b05d6a4 | 685 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 686 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 687 | } |
shennongmin | 10:9d4ec0359a5c | 688 | if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1) |
shennongmin | 10:9d4ec0359a5c | 689 | { |
shennongmin | 10:9d4ec0359a5c | 690 | break; |
shennongmin | 10:9d4ec0359a5c | 691 | } |
shennongmin | 10:9d4ec0359a5c | 692 | } |
shennongmin | 10:9d4ec0359a5c | 693 | |
shennongmin | 10:9d4ec0359a5c | 694 | } |
shennongmin | 10:9d4ec0359a5c | 695 | |
shennongmin | 10:9d4ec0359a5c | 696 | String ESP8266::showIP(void) |
shennongmin | 10:9d4ec0359a5c | 697 | { |
shennongmin | 27:dc7ad070c5b1 | 698 | bool ret = false; |
shennongmin | 29:75185b05d6a4 | 699 | m_uart.flush(); |
shennongmin | 29:75185b05d6a4 | 700 | m_uart.println("AT+CIFSR"); |
shennongmin | 27:dc7ad070c5b1 | 701 | |
shennongmin | 10:9d4ec0359a5c | 702 | String data; |
shennongmin | 10:9d4ec0359a5c | 703 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 704 | start = millis(); |
shennongmin | 27:dc7ad070c5b1 | 705 | while (millis()-start < 3000) { |
shennongmin | 29:75185b05d6a4 | 706 | while(m_uart.available() > 0) { |
shennongmin | 29:75185b05d6a4 | 707 | char a = m_uart.readChr(); |
shennongmin | 27:dc7ad070c5b1 | 708 | data += a; |
shennongmin | 27:dc7ad070c5b1 | 709 | } |
shennongmin | 27:dc7ad070c5b1 | 710 | if (data.indexOf("OK") != -1) { |
shennongmin | 27:dc7ad070c5b1 | 711 | ret = true; |
shennongmin | 27:dc7ad070c5b1 | 712 | break; |
shennongmin | 27:dc7ad070c5b1 | 713 | } else if (data.indexOf("ERROR") != -1) { |
shennongmin | 27:dc7ad070c5b1 | 714 | ret = false; |
shennongmin | 27:dc7ad070c5b1 | 715 | break; |
shennongmin | 27:dc7ad070c5b1 | 716 | } |
shennongmin | 10:9d4ec0359a5c | 717 | } |
shennongmin | 33:f721a46ef2f2 | 718 | //printf("data = [%s]\r\n", data.c_str()); |
shennongmin | 27:dc7ad070c5b1 | 719 | if (ret) { |
shennongmin | 33:f721a46ef2f2 | 720 | int index1 = data.indexOf("AT+CIFSR\r\r\n"); |
shennongmin | 27:dc7ad070c5b1 | 721 | int index2 = data.indexOf("\r\n\r\nOK"); |
shennongmin | 27:dc7ad070c5b1 | 722 | if (index1 != -1 && index2 != -1) { |
shennongmin | 33:f721a46ef2f2 | 723 | return data.substring(index1 + strlen("AT+CIFSR\r\r\n"), index2); |
shennongmin | 27:dc7ad070c5b1 | 724 | } else { |
shennongmin | 27:dc7ad070c5b1 | 725 | return "not found"; |
shennongmin | 27:dc7ad070c5b1 | 726 | } |
shennongmin | 27:dc7ad070c5b1 | 727 | } else { |
shennongmin | 27:dc7ad070c5b1 | 728 | return "null"; |
shennongmin | 27:dc7ad070c5b1 | 729 | } |
shennongmin | 10:9d4ec0359a5c | 730 | } |
shennongmin | 10:9d4ec0359a5c | 731 | |
shennongmin | 10:9d4ec0359a5c | 732 | bool ESP8266::confServer(uint8_t mode, int port) |
shennongmin | 10:9d4ec0359a5c | 733 | { |
shennongmin | 29:75185b05d6a4 | 734 | m_uart.print("AT+CIPSERVER="); |
shennongmin | 29:75185b05d6a4 | 735 | m_uart.print(String(mode)); |
shennongmin | 29:75185b05d6a4 | 736 | m_uart.print(","); |
shennongmin | 29:75185b05d6a4 | 737 | m_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 738 | |
shennongmin | 10:9d4ec0359a5c | 739 | String data; |
shennongmin | 10:9d4ec0359a5c | 740 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 741 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 742 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 743 | while (millis()-start<3000) { |
shennongmin | 29:75185b05d6a4 | 744 | if(m_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 745 | { |
shennongmin | 29:75185b05d6a4 | 746 | char a =m_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 747 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 748 | } |
shennongmin | 10:9d4ec0359a5c | 749 | if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1) |
shennongmin | 10:9d4ec0359a5c | 750 | { |
shennongmin | 10:9d4ec0359a5c | 751 | found = true; |
shennongmin | 10:9d4ec0359a5c | 752 | break; |
shennongmin | 10:9d4ec0359a5c | 753 | } |
shennongmin | 10:9d4ec0359a5c | 754 | } |
shennongmin | 29:75185b05d6a4 | 755 | m_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 756 | return found; |
shennongmin | 10:9d4ec0359a5c | 757 | } |