pengfei wu
/
ESP8266Program
For test
ESP8266.cpp@28:c25aff31dcc5, 2015-02-04 (annotated)
- Committer:
- shennongmin
- Date:
- Wed Feb 04 13:35:35 2015 +0000
- Revision:
- 28:c25aff31dcc5
- Parent:
- 27:dc7ad070c5b1
- Child:
- 29:75185b05d6a4
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 | 23:4cec7b9e3715 | 4 | ESP8266::ESP8266(PinName tx, PinName rx):m_esp_uart(tx, rx) { |
shennongmin | 10:9d4ec0359a5c | 5 | } |
shennongmin | 10:9d4ec0359a5c | 6 | |
shennongmin | 23:4cec7b9e3715 | 7 | int ESP8266::getChannelID(void) { |
shennongmin | 23:4cec7b9e3715 | 8 | return m_chl_id; |
shennongmin | 10:9d4ec0359a5c | 9 | } |
shennongmin | 10:9d4ec0359a5c | 10 | |
shennongmin | 18:37254b357abd | 11 | bool ESP8266::begin(void) |
shennongmin | 10:9d4ec0359a5c | 12 | { |
shennongmin | 28:c25aff31dcc5 | 13 | m_esp_uart.begin(9600); |
shennongmin | 23:4cec7b9e3715 | 14 | m_esp_uart.flush(); |
shennongmin | 23:4cec7b9e3715 | 15 | m_esp_uart.setTimeout(5000); |
shennongmin | 23:4cec7b9e3715 | 16 | m_esp_uart.println("AT+RST"); |
shennongmin | 23:4cec7b9e3715 | 17 | return m_esp_uart.find("eady"); |
shennongmin | 10:9d4ec0359a5c | 18 | } |
shennongmin | 10:9d4ec0359a5c | 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 | 23:4cec7b9e3715 | 98 | if (m_esp_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 | 23:4cec7b9e3715 | 103 | char c0 = m_esp_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 | 23:4cec7b9e3715 | 109 | if (m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 110 | { |
shennongmin | 23:4cec7b9e3715 | 111 | char c = m_esp_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 | 23:4cec7b9e3715 | 144 | m_chl_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 | |
shennongmin | 23:4cec7b9e3715 | 171 | void ESP8266::reset(void) |
shennongmin | 10:9d4ec0359a5c | 172 | { |
shennongmin | 23:4cec7b9e3715 | 173 | m_esp_uart.println("AT+RST"); |
shennongmin | 10:9d4ec0359a5c | 174 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 175 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 176 | while (millis()-start<5000) { |
shennongmin | 23:4cec7b9e3715 | 177 | if(m_esp_uart.find("eady")==true) |
shennongmin | 10:9d4ec0359a5c | 178 | { |
shennongmin | 10:9d4ec0359a5c | 179 | break; |
shennongmin | 10:9d4ec0359a5c | 180 | } |
shennongmin | 10:9d4ec0359a5c | 181 | } |
shennongmin | 10:9d4ec0359a5c | 182 | } |
shennongmin | 10:9d4ec0359a5c | 183 | |
shennongmin | 10:9d4ec0359a5c | 184 | |
shennongmin | 10:9d4ec0359a5c | 185 | String ESP8266::showMode() |
shennongmin | 10:9d4ec0359a5c | 186 | { |
shennongmin | 10:9d4ec0359a5c | 187 | String data; |
shennongmin | 23:4cec7b9e3715 | 188 | m_esp_uart.println("AT+CWMODE?"); |
shennongmin | 10:9d4ec0359a5c | 189 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 190 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 191 | while (millis()-start<2000) { |
shennongmin | 23:4cec7b9e3715 | 192 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 193 | { |
shennongmin | 23:4cec7b9e3715 | 194 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 195 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 196 | } |
shennongmin | 10:9d4ec0359a5c | 197 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 198 | { |
shennongmin | 10:9d4ec0359a5c | 199 | break; |
shennongmin | 10:9d4ec0359a5c | 200 | } |
shennongmin | 10:9d4ec0359a5c | 201 | } |
shennongmin | 10:9d4ec0359a5c | 202 | if(data.indexOf("1")!=-1) |
shennongmin | 10:9d4ec0359a5c | 203 | { |
shennongmin | 10:9d4ec0359a5c | 204 | return "Station"; |
shennongmin | 10:9d4ec0359a5c | 205 | }else if(data.indexOf("2")!=-1) |
shennongmin | 10:9d4ec0359a5c | 206 | { |
shennongmin | 10:9d4ec0359a5c | 207 | return "AP"; |
shennongmin | 10:9d4ec0359a5c | 208 | }else if(data.indexOf("3")!=-1) |
shennongmin | 10:9d4ec0359a5c | 209 | { |
shennongmin | 10:9d4ec0359a5c | 210 | return "AP+Station"; |
shennongmin | 10:9d4ec0359a5c | 211 | }else{ |
shennongmin | 10:9d4ec0359a5c | 212 | return "Invalid Mode!"; |
shennongmin | 10:9d4ec0359a5c | 213 | } |
shennongmin | 10:9d4ec0359a5c | 214 | } |
shennongmin | 10:9d4ec0359a5c | 215 | |
shennongmin | 17:c449356787f1 | 216 | bool ESP8266::confMode(uint8_t mode) |
shennongmin | 10:9d4ec0359a5c | 217 | { |
shennongmin | 10:9d4ec0359a5c | 218 | String data; |
shennongmin | 23:4cec7b9e3715 | 219 | m_esp_uart.print("AT+CWMODE="); |
shennongmin | 23:4cec7b9e3715 | 220 | m_esp_uart.println(String(mode)); |
shennongmin | 10:9d4ec0359a5c | 221 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 222 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 223 | while (millis()-start<2000) { |
shennongmin | 23:4cec7b9e3715 | 224 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 225 | { |
shennongmin | 23:4cec7b9e3715 | 226 | char mode =m_esp_uart.readChr(); |
shennongmin | 17:c449356787f1 | 227 | data=data+mode; |
shennongmin | 10:9d4ec0359a5c | 228 | } |
shennongmin | 10:9d4ec0359a5c | 229 | if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1) |
shennongmin | 10:9d4ec0359a5c | 230 | { |
shennongmin | 10:9d4ec0359a5c | 231 | return true; |
shennongmin | 10:9d4ec0359a5c | 232 | } |
shennongmin | 10:9d4ec0359a5c | 233 | if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1) |
shennongmin | 10:9d4ec0359a5c | 234 | { |
shennongmin | 10:9d4ec0359a5c | 235 | return false; |
shennongmin | 10:9d4ec0359a5c | 236 | } |
shennongmin | 10:9d4ec0359a5c | 237 | |
shennongmin | 10:9d4ec0359a5c | 238 | } |
shennongmin | 10:9d4ec0359a5c | 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 | 23:4cec7b9e3715 | 245 | m_esp_uart.flush(); |
shennongmin | 23:4cec7b9e3715 | 246 | m_esp_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 | 23:4cec7b9e3715 | 252 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 253 | { |
shennongmin | 23:4cec7b9e3715 | 254 | char a =m_esp_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 | 23:4cec7b9e3715 | 282 | m_esp_uart.flush(); |
shennongmin | 23:4cec7b9e3715 | 283 | m_esp_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 | 27:dc7ad070c5b1 | 289 | while(m_esp_uart.available() > 0) { |
shennongmin | 27:dc7ad070c5b1 | 290 | char a = m_esp_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 | 10:9d4ec0359a5c | 317 | |
shennongmin | 23:4cec7b9e3715 | 318 | m_esp_uart.print("AT+CWJAP="); |
shennongmin | 23:4cec7b9e3715 | 319 | m_esp_uart.print("\""); //"ssid" |
shennongmin | 23:4cec7b9e3715 | 320 | m_esp_uart.print(ssid); |
shennongmin | 23:4cec7b9e3715 | 321 | m_esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 322 | |
shennongmin | 23:4cec7b9e3715 | 323 | m_esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 324 | |
shennongmin | 23:4cec7b9e3715 | 325 | m_esp_uart.print("\""); //"pwd" |
shennongmin | 23:4cec7b9e3715 | 326 | m_esp_uart.print(pwd); |
shennongmin | 23:4cec7b9e3715 | 327 | m_esp_uart.println("\""); |
shennongmin | 10:9d4ec0359a5c | 328 | |
shennongmin | 10:9d4ec0359a5c | 329 | |
shennongmin | 10:9d4ec0359a5c | 330 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 331 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 332 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 333 | if(m_esp_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 | bool ESP8266::quitAP(void) |
shennongmin | 10:9d4ec0359a5c | 343 | { |
shennongmin | 23:4cec7b9e3715 | 344 | m_esp_uart.println("AT+CWQAP"); |
shennongmin | 10:9d4ec0359a5c | 345 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 346 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 347 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 348 | if(m_esp_uart.find("OK")==true) |
shennongmin | 10:9d4ec0359a5c | 349 | { |
shennongmin | 10:9d4ec0359a5c | 350 | return true; |
shennongmin | 10:9d4ec0359a5c | 351 | |
shennongmin | 10:9d4ec0359a5c | 352 | } |
shennongmin | 10:9d4ec0359a5c | 353 | } |
shennongmin | 10:9d4ec0359a5c | 354 | return false; |
shennongmin | 10:9d4ec0359a5c | 355 | |
shennongmin | 10:9d4ec0359a5c | 356 | } |
shennongmin | 10:9d4ec0359a5c | 357 | |
shennongmin | 10:9d4ec0359a5c | 358 | String ESP8266::showSAP() |
shennongmin | 10:9d4ec0359a5c | 359 | { |
shennongmin | 23:4cec7b9e3715 | 360 | m_esp_uart.println("AT+CWSAP?"); |
shennongmin | 10:9d4ec0359a5c | 361 | String data; |
shennongmin | 10:9d4ec0359a5c | 362 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 363 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 364 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 365 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 366 | { |
shennongmin | 23:4cec7b9e3715 | 367 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 368 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 369 | } |
shennongmin | 10:9d4ec0359a5c | 370 | if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 371 | { |
shennongmin | 10:9d4ec0359a5c | 372 | break; |
shennongmin | 10:9d4ec0359a5c | 373 | } |
shennongmin | 10:9d4ec0359a5c | 374 | } |
shennongmin | 10:9d4ec0359a5c | 375 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 376 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 377 | data.replace("AT+CWSAP?",""); |
shennongmin | 10:9d4ec0359a5c | 378 | data.replace("+CWSAP","mySAP"); |
shennongmin | 10:9d4ec0359a5c | 379 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 380 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 381 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 382 | |
shennongmin | 10:9d4ec0359a5c | 383 | return data; |
shennongmin | 10:9d4ec0359a5c | 384 | } |
shennongmin | 10:9d4ec0359a5c | 385 | |
shennongmin | 10:9d4ec0359a5c | 386 | bool ESP8266::confSAP(String ssid , String pwd , uint8_t chl , uint8_t ecn) |
shennongmin | 10:9d4ec0359a5c | 387 | { |
shennongmin | 23:4cec7b9e3715 | 388 | m_esp_uart.print("AT+CWSAP="); |
shennongmin | 23:4cec7b9e3715 | 389 | m_esp_uart.print("\""); //"ssid" |
shennongmin | 23:4cec7b9e3715 | 390 | m_esp_uart.print(ssid); |
shennongmin | 23:4cec7b9e3715 | 391 | m_esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 392 | |
shennongmin | 23:4cec7b9e3715 | 393 | m_esp_uart.print(","); |
shennongmin | 10:9d4ec0359a5c | 394 | |
shennongmin | 23:4cec7b9e3715 | 395 | m_esp_uart.print("\""); //"pwd" |
shennongmin | 23:4cec7b9e3715 | 396 | m_esp_uart.print(pwd); |
shennongmin | 23:4cec7b9e3715 | 397 | m_esp_uart.print("\""); |
shennongmin | 10:9d4ec0359a5c | 398 | |
shennongmin | 23:4cec7b9e3715 | 399 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 400 | m_esp_uart.print(String(chl)); |
shennongmin | 10:9d4ec0359a5c | 401 | |
shennongmin | 23:4cec7b9e3715 | 402 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 403 | m_esp_uart.println(String(ecn)); |
shennongmin | 10:9d4ec0359a5c | 404 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 405 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 406 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 407 | if(m_esp_uart.find("OK")==true ) |
shennongmin | 10:9d4ec0359a5c | 408 | { |
shennongmin | 10:9d4ec0359a5c | 409 | return true; |
shennongmin | 10:9d4ec0359a5c | 410 | } |
shennongmin | 10:9d4ec0359a5c | 411 | } |
shennongmin | 10:9d4ec0359a5c | 412 | |
shennongmin | 10:9d4ec0359a5c | 413 | return false; |
shennongmin | 10:9d4ec0359a5c | 414 | |
shennongmin | 10:9d4ec0359a5c | 415 | } |
shennongmin | 10:9d4ec0359a5c | 416 | |
shennongmin | 10:9d4ec0359a5c | 417 | |
shennongmin | 10:9d4ec0359a5c | 418 | String ESP8266::showStatus(void) |
shennongmin | 10:9d4ec0359a5c | 419 | { |
shennongmin | 23:4cec7b9e3715 | 420 | m_esp_uart.println("AT+CIPSTATUS"); |
shennongmin | 10:9d4ec0359a5c | 421 | String data; |
shennongmin | 10:9d4ec0359a5c | 422 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 423 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 424 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 425 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 426 | { |
shennongmin | 23:4cec7b9e3715 | 427 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 428 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 429 | } |
shennongmin | 10:9d4ec0359a5c | 430 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 431 | { |
shennongmin | 10:9d4ec0359a5c | 432 | break; |
shennongmin | 10:9d4ec0359a5c | 433 | } |
shennongmin | 10:9d4ec0359a5c | 434 | } |
shennongmin | 10:9d4ec0359a5c | 435 | |
shennongmin | 10:9d4ec0359a5c | 436 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 437 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 438 | data.replace("AT+CIPSTATUS",""); |
shennongmin | 10:9d4ec0359a5c | 439 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 440 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 441 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 442 | |
shennongmin | 10:9d4ec0359a5c | 443 | return data; |
shennongmin | 10:9d4ec0359a5c | 444 | } |
shennongmin | 10:9d4ec0359a5c | 445 | |
shennongmin | 10:9d4ec0359a5c | 446 | String ESP8266::showMux(void) |
shennongmin | 10:9d4ec0359a5c | 447 | { |
shennongmin | 10:9d4ec0359a5c | 448 | String data; |
shennongmin | 23:4cec7b9e3715 | 449 | m_esp_uart.println("AT+CIPMUX?"); |
shennongmin | 10:9d4ec0359a5c | 450 | |
shennongmin | 10:9d4ec0359a5c | 451 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 452 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 453 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 454 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 455 | { |
shennongmin | 23:4cec7b9e3715 | 456 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 457 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 458 | } |
shennongmin | 10:9d4ec0359a5c | 459 | if (data.indexOf("OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 460 | { |
shennongmin | 10:9d4ec0359a5c | 461 | break; |
shennongmin | 10:9d4ec0359a5c | 462 | } |
shennongmin | 10:9d4ec0359a5c | 463 | } |
shennongmin | 10:9d4ec0359a5c | 464 | char head[4] = {0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 465 | char tail[7] = {0x0D,0x0A,0x0D,0x0A}; |
shennongmin | 10:9d4ec0359a5c | 466 | data.replace("AT+CIPMUX?",""); |
shennongmin | 10:9d4ec0359a5c | 467 | data.replace("+CIPMUX","showMux"); |
shennongmin | 10:9d4ec0359a5c | 468 | data.replace("OK",""); |
shennongmin | 10:9d4ec0359a5c | 469 | data.replace(tail,""); |
shennongmin | 10:9d4ec0359a5c | 470 | data.replace(head,""); |
shennongmin | 10:9d4ec0359a5c | 471 | |
shennongmin | 10:9d4ec0359a5c | 472 | return data; |
shennongmin | 10:9d4ec0359a5c | 473 | } |
shennongmin | 10:9d4ec0359a5c | 474 | |
shennongmin | 17:c449356787f1 | 475 | bool ESP8266::confMux(int mux) |
shennongmin | 10:9d4ec0359a5c | 476 | { |
shennongmin | 23:4cec7b9e3715 | 477 | m_esp_uart.print("AT+CIPMUX="); |
shennongmin | 23:4cec7b9e3715 | 478 | m_esp_uart.println(mux); |
shennongmin | 10:9d4ec0359a5c | 479 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 480 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 481 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 482 | if(m_esp_uart.find("OK")==true ) |
shennongmin | 10:9d4ec0359a5c | 483 | { |
shennongmin | 10:9d4ec0359a5c | 484 | return true; |
shennongmin | 10:9d4ec0359a5c | 485 | } |
shennongmin | 10:9d4ec0359a5c | 486 | } |
shennongmin | 10:9d4ec0359a5c | 487 | |
shennongmin | 10:9d4ec0359a5c | 488 | return false; |
shennongmin | 10:9d4ec0359a5c | 489 | } |
shennongmin | 10:9d4ec0359a5c | 490 | |
shennongmin | 10:9d4ec0359a5c | 491 | bool ESP8266::newMux(uint8_t type, String addr, int port) |
shennongmin | 10:9d4ec0359a5c | 492 | |
shennongmin | 10:9d4ec0359a5c | 493 | { |
shennongmin | 10:9d4ec0359a5c | 494 | String data; |
shennongmin | 23:4cec7b9e3715 | 495 | m_esp_uart.flush(); |
shennongmin | 23:4cec7b9e3715 | 496 | m_esp_uart.print("AT+CIPSTART="); |
shennongmin | 28:c25aff31dcc5 | 497 | if(ESP8266_COMM_TCP == type) { |
shennongmin | 23:4cec7b9e3715 | 498 | m_esp_uart.print("\"TCP\""); |
shennongmin | 28:c25aff31dcc5 | 499 | } else if (ESP8266_COMM_UDP == type) { |
shennongmin | 23:4cec7b9e3715 | 500 | m_esp_uart.print("\"UDP\""); |
shennongmin | 10:9d4ec0359a5c | 501 | } |
shennongmin | 23:4cec7b9e3715 | 502 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 503 | m_esp_uart.print("\""); |
shennongmin | 23:4cec7b9e3715 | 504 | m_esp_uart.print(addr); |
shennongmin | 23:4cec7b9e3715 | 505 | m_esp_uart.print("\""); |
shennongmin | 23:4cec7b9e3715 | 506 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 507 | m_esp_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 508 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 509 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 510 | while (millis()-start<10000) { |
shennongmin | 23:4cec7b9e3715 | 511 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 512 | { |
shennongmin | 23:4cec7b9e3715 | 513 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 514 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 515 | } |
shennongmin | 10:9d4ec0359a5c | 516 | if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1) |
shennongmin | 10:9d4ec0359a5c | 517 | { |
shennongmin | 10:9d4ec0359a5c | 518 | return true; |
shennongmin | 10:9d4ec0359a5c | 519 | } |
shennongmin | 10:9d4ec0359a5c | 520 | } |
shennongmin | 10:9d4ec0359a5c | 521 | return false; |
shennongmin | 10:9d4ec0359a5c | 522 | } |
shennongmin | 10:9d4ec0359a5c | 523 | |
shennongmin | 10:9d4ec0359a5c | 524 | bool ESP8266::newMux( uint8_t id, uint8_t type, String addr, int port) |
shennongmin | 10:9d4ec0359a5c | 525 | { |
shennongmin | 23:4cec7b9e3715 | 526 | m_esp_uart.print("AT+CIPSTART="); |
shennongmin | 23:4cec7b9e3715 | 527 | m_esp_uart.print("\""); |
shennongmin | 23:4cec7b9e3715 | 528 | m_esp_uart.print(String(id)); |
shennongmin | 23:4cec7b9e3715 | 529 | m_esp_uart.print("\""); |
shennongmin | 28:c25aff31dcc5 | 530 | if(ESP8266_COMM_TCP == type) { |
shennongmin | 23:4cec7b9e3715 | 531 | m_esp_uart.print("\"TCP\""); |
shennongmin | 28:c25aff31dcc5 | 532 | } else if (ESP8266_COMM_UDP == type) { |
shennongmin | 23:4cec7b9e3715 | 533 | m_esp_uart.print("\"UDP\""); |
shennongmin | 10:9d4ec0359a5c | 534 | } |
shennongmin | 23:4cec7b9e3715 | 535 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 536 | m_esp_uart.print("\""); |
shennongmin | 23:4cec7b9e3715 | 537 | m_esp_uart.print(addr); |
shennongmin | 23:4cec7b9e3715 | 538 | m_esp_uart.print("\""); |
shennongmin | 23:4cec7b9e3715 | 539 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 540 | m_esp_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 541 | String data; |
shennongmin | 10:9d4ec0359a5c | 542 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 543 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 544 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 545 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 546 | { |
shennongmin | 23:4cec7b9e3715 | 547 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 548 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 549 | } |
shennongmin | 10:9d4ec0359a5c | 550 | if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 ) |
shennongmin | 10:9d4ec0359a5c | 551 | { |
shennongmin | 10:9d4ec0359a5c | 552 | return true; |
shennongmin | 10:9d4ec0359a5c | 553 | } |
shennongmin | 10:9d4ec0359a5c | 554 | } |
shennongmin | 10:9d4ec0359a5c | 555 | return false; |
shennongmin | 10:9d4ec0359a5c | 556 | |
shennongmin | 10:9d4ec0359a5c | 557 | |
shennongmin | 10:9d4ec0359a5c | 558 | } |
shennongmin | 10:9d4ec0359a5c | 559 | |
shennongmin | 23:4cec7b9e3715 | 560 | bool ESP8266::send(String str) |
shennongmin | 10:9d4ec0359a5c | 561 | { |
shennongmin | 28:c25aff31dcc5 | 562 | m_esp_uart.flush(); |
shennongmin | 23:4cec7b9e3715 | 563 | m_esp_uart.print("AT+CIPSEND="); |
shennongmin | 23:4cec7b9e3715 | 564 | m_esp_uart.println(str.length()); |
shennongmin | 10:9d4ec0359a5c | 565 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 566 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 567 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 568 | while (millis()-start<5000) { |
shennongmin | 23:4cec7b9e3715 | 569 | if(m_esp_uart.find(">")==true ) |
shennongmin | 10:9d4ec0359a5c | 570 | { |
shennongmin | 10:9d4ec0359a5c | 571 | found = true; |
shennongmin | 10:9d4ec0359a5c | 572 | break; |
shennongmin | 10:9d4ec0359a5c | 573 | } |
shennongmin | 10:9d4ec0359a5c | 574 | } |
shennongmin | 10:9d4ec0359a5c | 575 | if(found) |
shennongmin | 23:4cec7b9e3715 | 576 | m_esp_uart.print(str); |
shennongmin | 10:9d4ec0359a5c | 577 | else |
shennongmin | 10:9d4ec0359a5c | 578 | { |
shennongmin | 10:9d4ec0359a5c | 579 | closeMux(); |
shennongmin | 10:9d4ec0359a5c | 580 | return false; |
shennongmin | 10:9d4ec0359a5c | 581 | } |
shennongmin | 10:9d4ec0359a5c | 582 | |
shennongmin | 10:9d4ec0359a5c | 583 | |
shennongmin | 10:9d4ec0359a5c | 584 | String data; |
shennongmin | 10:9d4ec0359a5c | 585 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 586 | while (millis()-start<5000) { |
shennongmin | 23:4cec7b9e3715 | 587 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 588 | { |
shennongmin | 23:4cec7b9e3715 | 589 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 590 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 591 | } |
shennongmin | 10:9d4ec0359a5c | 592 | if (data.indexOf("SEND OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 593 | { |
shennongmin | 10:9d4ec0359a5c | 594 | return true; |
shennongmin | 10:9d4ec0359a5c | 595 | } |
shennongmin | 10:9d4ec0359a5c | 596 | } |
shennongmin | 10:9d4ec0359a5c | 597 | return false; |
shennongmin | 10:9d4ec0359a5c | 598 | } |
shennongmin | 10:9d4ec0359a5c | 599 | |
shennongmin | 23:4cec7b9e3715 | 600 | bool ESP8266::send(uint8_t id, String str) |
shennongmin | 10:9d4ec0359a5c | 601 | { |
shennongmin | 23:4cec7b9e3715 | 602 | m_esp_uart.print("AT+CIPSEND="); |
shennongmin | 10:9d4ec0359a5c | 603 | |
shennongmin | 23:4cec7b9e3715 | 604 | m_esp_uart.print(String(id)); |
shennongmin | 23:4cec7b9e3715 | 605 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 606 | m_esp_uart.println(str.length()); |
shennongmin | 10:9d4ec0359a5c | 607 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 608 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 609 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 610 | while (millis()-start<5000) { |
shennongmin | 23:4cec7b9e3715 | 611 | if(m_esp_uart.find(">")==true ) |
shennongmin | 10:9d4ec0359a5c | 612 | { |
shennongmin | 10:9d4ec0359a5c | 613 | found = true; |
shennongmin | 10:9d4ec0359a5c | 614 | break; |
shennongmin | 10:9d4ec0359a5c | 615 | } |
shennongmin | 10:9d4ec0359a5c | 616 | } |
shennongmin | 10:9d4ec0359a5c | 617 | if(found) |
shennongmin | 23:4cec7b9e3715 | 618 | m_esp_uart.print(str); |
shennongmin | 10:9d4ec0359a5c | 619 | else |
shennongmin | 10:9d4ec0359a5c | 620 | { |
shennongmin | 10:9d4ec0359a5c | 621 | closeMux(id); |
shennongmin | 10:9d4ec0359a5c | 622 | return false; |
shennongmin | 10:9d4ec0359a5c | 623 | } |
shennongmin | 10:9d4ec0359a5c | 624 | |
shennongmin | 10:9d4ec0359a5c | 625 | |
shennongmin | 10:9d4ec0359a5c | 626 | String data; |
shennongmin | 10:9d4ec0359a5c | 627 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 628 | while (millis()-start<5000) { |
shennongmin | 23:4cec7b9e3715 | 629 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 630 | { |
shennongmin | 23:4cec7b9e3715 | 631 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 632 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 633 | } |
shennongmin | 10:9d4ec0359a5c | 634 | if (data.indexOf("SEND OK")!=-1) |
shennongmin | 10:9d4ec0359a5c | 635 | { |
shennongmin | 10:9d4ec0359a5c | 636 | return true; |
shennongmin | 10:9d4ec0359a5c | 637 | } |
shennongmin | 10:9d4ec0359a5c | 638 | } |
shennongmin | 10:9d4ec0359a5c | 639 | return false; |
shennongmin | 10:9d4ec0359a5c | 640 | } |
shennongmin | 10:9d4ec0359a5c | 641 | |
shennongmin | 10:9d4ec0359a5c | 642 | |
shennongmin | 10:9d4ec0359a5c | 643 | void ESP8266::closeMux(void) |
shennongmin | 10:9d4ec0359a5c | 644 | { |
shennongmin | 23:4cec7b9e3715 | 645 | m_esp_uart.println("AT+CIPCLOSE"); |
shennongmin | 10:9d4ec0359a5c | 646 | |
shennongmin | 10:9d4ec0359a5c | 647 | String data; |
shennongmin | 10:9d4ec0359a5c | 648 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 649 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 650 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 651 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 652 | { |
shennongmin | 23:4cec7b9e3715 | 653 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 654 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 655 | } |
shennongmin | 10:9d4ec0359a5c | 656 | if (data.indexOf("Linked")!=-1 || data.indexOf("ERROR")!=-1 || data.indexOf("we must restart")!=-1) |
shennongmin | 10:9d4ec0359a5c | 657 | { |
shennongmin | 10:9d4ec0359a5c | 658 | break; |
shennongmin | 10:9d4ec0359a5c | 659 | } |
shennongmin | 10:9d4ec0359a5c | 660 | } |
shennongmin | 10:9d4ec0359a5c | 661 | } |
shennongmin | 10:9d4ec0359a5c | 662 | |
shennongmin | 10:9d4ec0359a5c | 663 | void ESP8266::closeMux(uint8_t id) |
shennongmin | 10:9d4ec0359a5c | 664 | { |
shennongmin | 23:4cec7b9e3715 | 665 | m_esp_uart.print("AT+CIPCLOSE="); |
shennongmin | 23:4cec7b9e3715 | 666 | m_esp_uart.println(String(id)); |
shennongmin | 10:9d4ec0359a5c | 667 | String data; |
shennongmin | 10:9d4ec0359a5c | 668 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 669 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 670 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 671 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 672 | { |
shennongmin | 23:4cec7b9e3715 | 673 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 674 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 675 | } |
shennongmin | 10:9d4ec0359a5c | 676 | if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1) |
shennongmin | 10:9d4ec0359a5c | 677 | { |
shennongmin | 10:9d4ec0359a5c | 678 | break; |
shennongmin | 10:9d4ec0359a5c | 679 | } |
shennongmin | 10:9d4ec0359a5c | 680 | } |
shennongmin | 10:9d4ec0359a5c | 681 | |
shennongmin | 10:9d4ec0359a5c | 682 | } |
shennongmin | 10:9d4ec0359a5c | 683 | |
shennongmin | 10:9d4ec0359a5c | 684 | String ESP8266::showIP(void) |
shennongmin | 10:9d4ec0359a5c | 685 | { |
shennongmin | 27:dc7ad070c5b1 | 686 | bool ret = false; |
shennongmin | 27:dc7ad070c5b1 | 687 | m_esp_uart.flush(); |
shennongmin | 27:dc7ad070c5b1 | 688 | m_esp_uart.println("AT+CIFSR"); |
shennongmin | 27:dc7ad070c5b1 | 689 | |
shennongmin | 10:9d4ec0359a5c | 690 | String data; |
shennongmin | 10:9d4ec0359a5c | 691 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 692 | start = millis(); |
shennongmin | 27:dc7ad070c5b1 | 693 | while (millis()-start < 3000) { |
shennongmin | 27:dc7ad070c5b1 | 694 | while(m_esp_uart.available() > 0) { |
shennongmin | 27:dc7ad070c5b1 | 695 | char a = m_esp_uart.readChr(); |
shennongmin | 27:dc7ad070c5b1 | 696 | data += a; |
shennongmin | 27:dc7ad070c5b1 | 697 | } |
shennongmin | 27:dc7ad070c5b1 | 698 | if (data.indexOf("OK") != -1) { |
shennongmin | 27:dc7ad070c5b1 | 699 | ret = true; |
shennongmin | 27:dc7ad070c5b1 | 700 | break; |
shennongmin | 27:dc7ad070c5b1 | 701 | } else if (data.indexOf("ERROR") != -1) { |
shennongmin | 27:dc7ad070c5b1 | 702 | ret = false; |
shennongmin | 27:dc7ad070c5b1 | 703 | break; |
shennongmin | 27:dc7ad070c5b1 | 704 | } |
shennongmin | 10:9d4ec0359a5c | 705 | } |
shennongmin | 27:dc7ad070c5b1 | 706 | |
shennongmin | 27:dc7ad070c5b1 | 707 | if (ret) { |
shennongmin | 27:dc7ad070c5b1 | 708 | int index1 = data.indexOf("\r\n"); |
shennongmin | 27:dc7ad070c5b1 | 709 | int index2 = data.indexOf("\r\n\r\nOK"); |
shennongmin | 27:dc7ad070c5b1 | 710 | if (index1 != -1 && index2 != -1) { |
shennongmin | 27:dc7ad070c5b1 | 711 | return data.substring(index1 + strlen("\r\n"), index2); |
shennongmin | 27:dc7ad070c5b1 | 712 | } else { |
shennongmin | 27:dc7ad070c5b1 | 713 | return "not found"; |
shennongmin | 27:dc7ad070c5b1 | 714 | } |
shennongmin | 27:dc7ad070c5b1 | 715 | } else { |
shennongmin | 27:dc7ad070c5b1 | 716 | return "null"; |
shennongmin | 27:dc7ad070c5b1 | 717 | } |
shennongmin | 10:9d4ec0359a5c | 718 | } |
shennongmin | 10:9d4ec0359a5c | 719 | |
shennongmin | 10:9d4ec0359a5c | 720 | bool ESP8266::confServer(uint8_t mode, int port) |
shennongmin | 10:9d4ec0359a5c | 721 | { |
shennongmin | 23:4cec7b9e3715 | 722 | m_esp_uart.print("AT+CIPSERVER="); |
shennongmin | 23:4cec7b9e3715 | 723 | m_esp_uart.print(String(mode)); |
shennongmin | 23:4cec7b9e3715 | 724 | m_esp_uart.print(","); |
shennongmin | 23:4cec7b9e3715 | 725 | m_esp_uart.println(String(port)); |
shennongmin | 10:9d4ec0359a5c | 726 | |
shennongmin | 10:9d4ec0359a5c | 727 | String data; |
shennongmin | 10:9d4ec0359a5c | 728 | unsigned long start; |
shennongmin | 10:9d4ec0359a5c | 729 | start = millis(); |
shennongmin | 10:9d4ec0359a5c | 730 | bool found = false; |
shennongmin | 10:9d4ec0359a5c | 731 | while (millis()-start<3000) { |
shennongmin | 23:4cec7b9e3715 | 732 | if(m_esp_uart.available()>0) |
shennongmin | 10:9d4ec0359a5c | 733 | { |
shennongmin | 23:4cec7b9e3715 | 734 | char a =m_esp_uart.readChr(); |
shennongmin | 10:9d4ec0359a5c | 735 | data=data+a; |
shennongmin | 10:9d4ec0359a5c | 736 | } |
shennongmin | 10:9d4ec0359a5c | 737 | if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1) |
shennongmin | 10:9d4ec0359a5c | 738 | { |
shennongmin | 10:9d4ec0359a5c | 739 | found = true; |
shennongmin | 10:9d4ec0359a5c | 740 | break; |
shennongmin | 10:9d4ec0359a5c | 741 | } |
shennongmin | 10:9d4ec0359a5c | 742 | } |
shennongmin | 23:4cec7b9e3715 | 743 | m_esp_uart.flush(); |
shennongmin | 10:9d4ec0359a5c | 744 | return found; |
shennongmin | 10:9d4ec0359a5c | 745 | } |