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