Class library for using the ESP8266 wifi module.
ESP8266.cpp@0:30dd9c0f7559, 2018-06-11 (annotated)
- Committer:
- grantphillips
- Date:
- Mon Jun 11 17:56:55 2018 +0000
- Revision:
- 0:30dd9c0f7559
V1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
grantphillips | 0:30dd9c0f7559 | 1 | #include "ESP8266.h" |
grantphillips | 0:30dd9c0f7559 | 2 | #include "mbed.h" |
grantphillips | 0:30dd9c0f7559 | 3 | |
grantphillips | 0:30dd9c0f7559 | 4 | /***************************** Public functions ******************************/ |
grantphillips | 0:30dd9c0f7559 | 5 | ESP8266::ESP8266(PinName Tx, PinName Rx, PinName Rst) : esp(Tx, Rx), _rst(Rst) { |
grantphillips | 0:30dd9c0f7559 | 6 | esp.baud(115200); |
grantphillips | 0:30dd9c0f7559 | 7 | mDEBUG = false; |
grantphillips | 0:30dd9c0f7559 | 8 | } |
grantphillips | 0:30dd9c0f7559 | 9 | |
grantphillips | 0:30dd9c0f7559 | 10 | bool ESP8266::startup(uint8_t mode) { |
grantphillips | 0:30dd9c0f7559 | 11 | |
grantphillips | 0:30dd9c0f7559 | 12 | if(mode < 1 || mode > 3) //only 3 valid modes |
grantphillips | 0:30dd9c0f7559 | 13 | return false; |
grantphillips | 0:30dd9c0f7559 | 14 | _rst=0; |
grantphillips | 0:30dd9c0f7559 | 15 | wait(1.0); |
grantphillips | 0:30dd9c0f7559 | 16 | _rst=1; |
grantphillips | 0:30dd9c0f7559 | 17 | wait(1.0); |
grantphillips | 0:30dd9c0f7559 | 18 | if(!kick()) //check if connected |
grantphillips | 0:30dd9c0f7559 | 19 | return(false); |
grantphillips | 0:30dd9c0f7559 | 20 | if(!reset()) //reset ESP8266 |
grantphillips | 0:30dd9c0f7559 | 21 | return(false); |
grantphillips | 0:30dd9c0f7559 | 22 | if(!setWiFiMode(mode)) //set the WiFi mode |
grantphillips | 0:30dd9c0f7559 | 23 | return(false); |
grantphillips | 0:30dd9c0f7559 | 24 | if(!setMux(true)) //set to multiple connections |
grantphillips | 0:30dd9c0f7559 | 25 | return false; |
grantphillips | 0:30dd9c0f7559 | 26 | |
grantphillips | 0:30dd9c0f7559 | 27 | rxtemp_idx=0; |
grantphillips | 0:30dd9c0f7559 | 28 | //rxmsg_idx= 0; |
grantphillips | 0:30dd9c0f7559 | 29 | new_rxmsg = false; |
grantphillips | 0:30dd9c0f7559 | 30 | rxpass1=false, rxpass2=false, rxpass3=false; |
grantphillips | 0:30dd9c0f7559 | 31 | |
grantphillips | 0:30dd9c0f7559 | 32 | return true; |
grantphillips | 0:30dd9c0f7559 | 33 | } |
grantphillips | 0:30dd9c0f7559 | 34 | |
grantphillips | 0:30dd9c0f7559 | 35 | void ESP8266::getVersion(char* version) { |
grantphillips | 0:30dd9c0f7559 | 36 | string ver; |
grantphillips | 0:30dd9c0f7559 | 37 | |
grantphillips | 0:30dd9c0f7559 | 38 | eATGMR(ver); |
grantphillips | 0:30dd9c0f7559 | 39 | strcpy(version, ver.c_str()); |
grantphillips | 0:30dd9c0f7559 | 40 | } |
grantphillips | 0:30dd9c0f7559 | 41 | |
grantphillips | 0:30dd9c0f7559 | 42 | void ESP8266::getAPList(char* list) { |
grantphillips | 0:30dd9c0f7559 | 43 | string lst; |
grantphillips | 0:30dd9c0f7559 | 44 | |
grantphillips | 0:30dd9c0f7559 | 45 | eATCWLAP(lst); |
grantphillips | 0:30dd9c0f7559 | 46 | strcpy(list, lst.c_str()); |
grantphillips | 0:30dd9c0f7559 | 47 | } |
grantphillips | 0:30dd9c0f7559 | 48 | |
grantphillips | 0:30dd9c0f7559 | 49 | bool ESP8266::setSoftAPParam(string ssid, string pwd, uint8_t chl, uint8_t ecn) { |
grantphillips | 0:30dd9c0f7559 | 50 | return sATCWSAP(ssid, pwd, chl, ecn); |
grantphillips | 0:30dd9c0f7559 | 51 | } |
grantphillips | 0:30dd9c0f7559 | 52 | |
grantphillips | 0:30dd9c0f7559 | 53 | bool ESP8266::DHCP(bool enabled, int mode) { |
grantphillips | 0:30dd9c0f7559 | 54 | return sATCWDHCP(enabled, mode); |
grantphillips | 0:30dd9c0f7559 | 55 | } |
grantphillips | 0:30dd9c0f7559 | 56 | |
grantphillips | 0:30dd9c0f7559 | 57 | bool ESP8266::joinAP(string ssid, string pwd) { |
grantphillips | 0:30dd9c0f7559 | 58 | return sATCWJAP(ssid, pwd); |
grantphillips | 0:30dd9c0f7559 | 59 | } |
grantphillips | 0:30dd9c0f7559 | 60 | |
grantphillips | 0:30dd9c0f7559 | 61 | void ESP8266::getLocalIP(char* LocalIP) { |
grantphillips | 0:30dd9c0f7559 | 62 | string lst; |
grantphillips | 0:30dd9c0f7559 | 63 | |
grantphillips | 0:30dd9c0f7559 | 64 | eATCIFSR(lst); |
grantphillips | 0:30dd9c0f7559 | 65 | strcpy(LocalIP, lst.c_str()); |
grantphillips | 0:30dd9c0f7559 | 66 | } |
grantphillips | 0:30dd9c0f7559 | 67 | |
grantphillips | 0:30dd9c0f7559 | 68 | bool ESP8266::setLocalIP(string LocalIP) { |
grantphillips | 0:30dd9c0f7559 | 69 | return sATCIPSTA(LocalIP); |
grantphillips | 0:30dd9c0f7559 | 70 | } |
grantphillips | 0:30dd9c0f7559 | 71 | |
grantphillips | 0:30dd9c0f7559 | 72 | bool ESP8266::setSoftAPIP(string LocalIP) { |
grantphillips | 0:30dd9c0f7559 | 73 | return sATCIPAP(LocalIP); |
grantphillips | 0:30dd9c0f7559 | 74 | } |
grantphillips | 0:30dd9c0f7559 | 75 | |
grantphillips | 0:30dd9c0f7559 | 76 | bool ESP8266::TCPServerStart(uint32_t port) { |
grantphillips | 0:30dd9c0f7559 | 77 | if (sATCIPSERVER(1, port)) { |
grantphillips | 0:30dd9c0f7559 | 78 | esp.attach(this,&ESP8266::RxInterrupt); |
grantphillips | 0:30dd9c0f7559 | 79 | return true; |
grantphillips | 0:30dd9c0f7559 | 80 | } |
grantphillips | 0:30dd9c0f7559 | 81 | return false; |
grantphillips | 0:30dd9c0f7559 | 82 | } |
grantphillips | 0:30dd9c0f7559 | 83 | |
grantphillips | 0:30dd9c0f7559 | 84 | bool ESP8266::TCPPacketReceived(void) { |
grantphillips | 0:30dd9c0f7559 | 85 | return new_rxmsg; |
grantphillips | 0:30dd9c0f7559 | 86 | } |
grantphillips | 0:30dd9c0f7559 | 87 | |
grantphillips | 0:30dd9c0f7559 | 88 | void ESP8266::TCPPacketRead(uint8_t *ID, uint32_t *LEN, char DATA[]) { |
grantphillips | 0:30dd9c0f7559 | 89 | *ID = packetID; |
grantphillips | 0:30dd9c0f7559 | 90 | *LEN = packetLen; |
grantphillips | 0:30dd9c0f7559 | 91 | strcpy(DATA, packetData); |
grantphillips | 0:30dd9c0f7559 | 92 | new_rxmsg=false; |
grantphillips | 0:30dd9c0f7559 | 93 | } |
grantphillips | 0:30dd9c0f7559 | 94 | |
grantphillips | 0:30dd9c0f7559 | 95 | bool ESP8266::TCPPacketSend(uint8_t ID, uint32_t LEN, char DATA[]) { |
grantphillips | 0:30dd9c0f7559 | 96 | bool ret; |
grantphillips | 0:30dd9c0f7559 | 97 | |
grantphillips | 0:30dd9c0f7559 | 98 | esp.attach(NULL); |
grantphillips | 0:30dd9c0f7559 | 99 | ret=sATCIPSENDMultiple(ID, DATA, LEN); |
grantphillips | 0:30dd9c0f7559 | 100 | esp.attach(this,&ESP8266::RxInterrupt); |
grantphillips | 0:30dd9c0f7559 | 101 | return ret; |
grantphillips | 0:30dd9c0f7559 | 102 | } |
grantphillips | 0:30dd9c0f7559 | 103 | |
grantphillips | 0:30dd9c0f7559 | 104 | /***************************** Private functions *****************************/ |
grantphillips | 0:30dd9c0f7559 | 105 | bool ESP8266::kick(void) { |
grantphillips | 0:30dd9c0f7559 | 106 | return eAT(); |
grantphillips | 0:30dd9c0f7559 | 107 | } |
grantphillips | 0:30dd9c0f7559 | 108 | |
grantphillips | 0:30dd9c0f7559 | 109 | bool ESP8266::reset(void) { |
grantphillips | 0:30dd9c0f7559 | 110 | unsigned long start; |
grantphillips | 0:30dd9c0f7559 | 111 | if (eATRST()) { |
grantphillips | 0:30dd9c0f7559 | 112 | wait_ms(2000); |
grantphillips | 0:30dd9c0f7559 | 113 | t.start(); |
grantphillips | 0:30dd9c0f7559 | 114 | start = t.read_ms(); |
grantphillips | 0:30dd9c0f7559 | 115 | while (t.read_ms() - start < 3000) { |
grantphillips | 0:30dd9c0f7559 | 116 | if (eAT()) { |
grantphillips | 0:30dd9c0f7559 | 117 | wait_ms(1500); /* Waiting for stable */ |
grantphillips | 0:30dd9c0f7559 | 118 | return true; |
grantphillips | 0:30dd9c0f7559 | 119 | } |
grantphillips | 0:30dd9c0f7559 | 120 | wait_ms(100); |
grantphillips | 0:30dd9c0f7559 | 121 | } |
grantphillips | 0:30dd9c0f7559 | 122 | } |
grantphillips | 0:30dd9c0f7559 | 123 | return false; |
grantphillips | 0:30dd9c0f7559 | 124 | } |
grantphillips | 0:30dd9c0f7559 | 125 | |
grantphillips | 0:30dd9c0f7559 | 126 | bool ESP8266::setWiFiMode(uint8_t mode) { |
grantphillips | 0:30dd9c0f7559 | 127 | switch(mode) { |
grantphillips | 0:30dd9c0f7559 | 128 | case 1: { |
grantphillips | 0:30dd9c0f7559 | 129 | if (sATCWMODE(1)) |
grantphillips | 0:30dd9c0f7559 | 130 | return true; |
grantphillips | 0:30dd9c0f7559 | 131 | else |
grantphillips | 0:30dd9c0f7559 | 132 | return false; |
grantphillips | 0:30dd9c0f7559 | 133 | } |
grantphillips | 0:30dd9c0f7559 | 134 | case 2: { |
grantphillips | 0:30dd9c0f7559 | 135 | if (sATCWMODE(2)) |
grantphillips | 0:30dd9c0f7559 | 136 | return true; |
grantphillips | 0:30dd9c0f7559 | 137 | else |
grantphillips | 0:30dd9c0f7559 | 138 | return false; |
grantphillips | 0:30dd9c0f7559 | 139 | } |
grantphillips | 0:30dd9c0f7559 | 140 | case 3: { |
grantphillips | 0:30dd9c0f7559 | 141 | if (sATCWMODE(3)) |
grantphillips | 0:30dd9c0f7559 | 142 | return true; |
grantphillips | 0:30dd9c0f7559 | 143 | else |
grantphillips | 0:30dd9c0f7559 | 144 | return false; |
grantphillips | 0:30dd9c0f7559 | 145 | } |
grantphillips | 0:30dd9c0f7559 | 146 | default: return false; |
grantphillips | 0:30dd9c0f7559 | 147 | } |
grantphillips | 0:30dd9c0f7559 | 148 | } |
grantphillips | 0:30dd9c0f7559 | 149 | |
grantphillips | 0:30dd9c0f7559 | 150 | bool ESP8266::setMux(bool onoff) { |
grantphillips | 0:30dd9c0f7559 | 151 | if(onoff) |
grantphillips | 0:30dd9c0f7559 | 152 | return sATCIPMUX(1); |
grantphillips | 0:30dd9c0f7559 | 153 | else |
grantphillips | 0:30dd9c0f7559 | 154 | return sATCIPMUX(0); |
grantphillips | 0:30dd9c0f7559 | 155 | } |
grantphillips | 0:30dd9c0f7559 | 156 | |
grantphillips | 0:30dd9c0f7559 | 157 | bool ESP8266::recvFind(string target, uint32_t timeout) { |
grantphillips | 0:30dd9c0f7559 | 158 | string data_tmp; |
grantphillips | 0:30dd9c0f7559 | 159 | data_tmp = recvString(target, timeout); |
grantphillips | 0:30dd9c0f7559 | 160 | if (data_tmp.find(target) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 161 | return true; |
grantphillips | 0:30dd9c0f7559 | 162 | } |
grantphillips | 0:30dd9c0f7559 | 163 | return false; |
grantphillips | 0:30dd9c0f7559 | 164 | } |
grantphillips | 0:30dd9c0f7559 | 165 | |
grantphillips | 0:30dd9c0f7559 | 166 | string ESP8266::recvString(string target, uint32_t timeout) { |
grantphillips | 0:30dd9c0f7559 | 167 | string data; |
grantphillips | 0:30dd9c0f7559 | 168 | char a; |
grantphillips | 0:30dd9c0f7559 | 169 | t.start(); |
grantphillips | 0:30dd9c0f7559 | 170 | unsigned long start = t.read_ms(); |
grantphillips | 0:30dd9c0f7559 | 171 | while (t.read_ms() - start < timeout) { |
grantphillips | 0:30dd9c0f7559 | 172 | while(esp.readable() > 0) { |
grantphillips | 0:30dd9c0f7559 | 173 | a = esp.getc(); |
grantphillips | 0:30dd9c0f7559 | 174 | data += a; |
grantphillips | 0:30dd9c0f7559 | 175 | } |
grantphillips | 0:30dd9c0f7559 | 176 | if (data.find(target) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 177 | break; |
grantphillips | 0:30dd9c0f7559 | 178 | } |
grantphillips | 0:30dd9c0f7559 | 179 | } |
grantphillips | 0:30dd9c0f7559 | 180 | t.stop(); |
grantphillips | 0:30dd9c0f7559 | 181 | |
grantphillips | 0:30dd9c0f7559 | 182 | return data; |
grantphillips | 0:30dd9c0f7559 | 183 | } |
grantphillips | 0:30dd9c0f7559 | 184 | |
grantphillips | 0:30dd9c0f7559 | 185 | string ESP8266::recvString(string target1, string target2, uint32_t timeout) { |
grantphillips | 0:30dd9c0f7559 | 186 | string data; |
grantphillips | 0:30dd9c0f7559 | 187 | char a; |
grantphillips | 0:30dd9c0f7559 | 188 | t.start(); |
grantphillips | 0:30dd9c0f7559 | 189 | unsigned long start = t.read_ms(); |
grantphillips | 0:30dd9c0f7559 | 190 | while (t.read_ms() - start < timeout) { |
grantphillips | 0:30dd9c0f7559 | 191 | while(esp.readable() > 0) { |
grantphillips | 0:30dd9c0f7559 | 192 | a = esp.getc(); |
grantphillips | 0:30dd9c0f7559 | 193 | data += a; |
grantphillips | 0:30dd9c0f7559 | 194 | } |
grantphillips | 0:30dd9c0f7559 | 195 | if (data.find(target1) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 196 | break; |
grantphillips | 0:30dd9c0f7559 | 197 | } else if (data.find(target2) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 198 | break; |
grantphillips | 0:30dd9c0f7559 | 199 | } |
grantphillips | 0:30dd9c0f7559 | 200 | } |
grantphillips | 0:30dd9c0f7559 | 201 | t.stop(); |
grantphillips | 0:30dd9c0f7559 | 202 | |
grantphillips | 0:30dd9c0f7559 | 203 | return data; |
grantphillips | 0:30dd9c0f7559 | 204 | } |
grantphillips | 0:30dd9c0f7559 | 205 | |
grantphillips | 0:30dd9c0f7559 | 206 | string ESP8266::recvString(string target1, string target2, string target3, uint32_t timeout) { |
grantphillips | 0:30dd9c0f7559 | 207 | string data; |
grantphillips | 0:30dd9c0f7559 | 208 | char a; |
grantphillips | 0:30dd9c0f7559 | 209 | t.start(); |
grantphillips | 0:30dd9c0f7559 | 210 | unsigned long start = t.read_ms(); |
grantphillips | 0:30dd9c0f7559 | 211 | while (t.read_ms() - start < timeout) { |
grantphillips | 0:30dd9c0f7559 | 212 | while(esp.readable() > 0) { |
grantphillips | 0:30dd9c0f7559 | 213 | a = esp.getc(); |
grantphillips | 0:30dd9c0f7559 | 214 | data += a; |
grantphillips | 0:30dd9c0f7559 | 215 | } |
grantphillips | 0:30dd9c0f7559 | 216 | if (data.find(target1) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 217 | break; |
grantphillips | 0:30dd9c0f7559 | 218 | } else if (data.find(target2) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 219 | break; |
grantphillips | 0:30dd9c0f7559 | 220 | } else if (data.find(target3) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 221 | break; |
grantphillips | 0:30dd9c0f7559 | 222 | } |
grantphillips | 0:30dd9c0f7559 | 223 | } |
grantphillips | 0:30dd9c0f7559 | 224 | t.stop(); |
grantphillips | 0:30dd9c0f7559 | 225 | |
grantphillips | 0:30dd9c0f7559 | 226 | return data; |
grantphillips | 0:30dd9c0f7559 | 227 | } |
grantphillips | 0:30dd9c0f7559 | 228 | |
grantphillips | 0:30dd9c0f7559 | 229 | bool ESP8266::recvFindAndFilter(string target, string begin, string end, string &data, uint32_t timeout) { |
grantphillips | 0:30dd9c0f7559 | 230 | string data_tmp; |
grantphillips | 0:30dd9c0f7559 | 231 | data_tmp = recvString(target, timeout); |
grantphillips | 0:30dd9c0f7559 | 232 | if (data_tmp.find(target) != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 233 | int32_t index1 = data_tmp.find(begin); |
grantphillips | 0:30dd9c0f7559 | 234 | int32_t index2 = data_tmp.find(end); |
grantphillips | 0:30dd9c0f7559 | 235 | if (index1 != std::string::npos && index2 != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 236 | index1 += begin.length(); |
grantphillips | 0:30dd9c0f7559 | 237 | data = data_tmp.substr(index1, index2-index1); |
grantphillips | 0:30dd9c0f7559 | 238 | return true; |
grantphillips | 0:30dd9c0f7559 | 239 | } |
grantphillips | 0:30dd9c0f7559 | 240 | } |
grantphillips | 0:30dd9c0f7559 | 241 | data = ""; |
grantphillips | 0:30dd9c0f7559 | 242 | return false; |
grantphillips | 0:30dd9c0f7559 | 243 | } |
grantphillips | 0:30dd9c0f7559 | 244 | |
grantphillips | 0:30dd9c0f7559 | 245 | |
grantphillips | 0:30dd9c0f7559 | 246 | |
grantphillips | 0:30dd9c0f7559 | 247 | |
grantphillips | 0:30dd9c0f7559 | 248 | /************************ Private AT Command Functions ***********************/ |
grantphillips | 0:30dd9c0f7559 | 249 | bool ESP8266::eAT(void) { |
grantphillips | 0:30dd9c0f7559 | 250 | esp.printf("AT\r\n"); |
grantphillips | 0:30dd9c0f7559 | 251 | return recvFind("OK", 2000); |
grantphillips | 0:30dd9c0f7559 | 252 | } |
grantphillips | 0:30dd9c0f7559 | 253 | |
grantphillips | 0:30dd9c0f7559 | 254 | bool ESP8266::eATRST(void) { |
grantphillips | 0:30dd9c0f7559 | 255 | esp.printf("AT+RST\r\n"); |
grantphillips | 0:30dd9c0f7559 | 256 | return recvFind("OK", 2000); |
grantphillips | 0:30dd9c0f7559 | 257 | } |
grantphillips | 0:30dd9c0f7559 | 258 | |
grantphillips | 0:30dd9c0f7559 | 259 | bool ESP8266::eATGMR(string &version) { |
grantphillips | 0:30dd9c0f7559 | 260 | esp.printf("AT+GMR\r\n"); |
grantphillips | 0:30dd9c0f7559 | 261 | return recvFindAndFilter("OK", "\r\r\n", "\r\nSDK", version); |
grantphillips | 0:30dd9c0f7559 | 262 | } |
grantphillips | 0:30dd9c0f7559 | 263 | |
grantphillips | 0:30dd9c0f7559 | 264 | bool ESP8266::sATCWMODE(uint8_t mode) { |
grantphillips | 0:30dd9c0f7559 | 265 | string data; |
grantphillips | 0:30dd9c0f7559 | 266 | esp.printf("AT+CWMODE=%d\r\n", mode); |
grantphillips | 0:30dd9c0f7559 | 267 | |
grantphillips | 0:30dd9c0f7559 | 268 | data = recvString("OK", "no change"); |
grantphillips | 0:30dd9c0f7559 | 269 | if (data.find("OK") != std::string::npos || data.find("no change") != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 270 | return true; |
grantphillips | 0:30dd9c0f7559 | 271 | } |
grantphillips | 0:30dd9c0f7559 | 272 | return false; |
grantphillips | 0:30dd9c0f7559 | 273 | } |
grantphillips | 0:30dd9c0f7559 | 274 | |
grantphillips | 0:30dd9c0f7559 | 275 | bool ESP8266::sATCIPMUX(uint8_t mode) |
grantphillips | 0:30dd9c0f7559 | 276 | { |
grantphillips | 0:30dd9c0f7559 | 277 | string data; |
grantphillips | 0:30dd9c0f7559 | 278 | esp.printf("AT+CIPMUX=%d\r\n",mode); |
grantphillips | 0:30dd9c0f7559 | 279 | |
grantphillips | 0:30dd9c0f7559 | 280 | data = recvString("OK", "Link is builded"); |
grantphillips | 0:30dd9c0f7559 | 281 | if (data.find("OK") != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 282 | return true; |
grantphillips | 0:30dd9c0f7559 | 283 | } |
grantphillips | 0:30dd9c0f7559 | 284 | return false; |
grantphillips | 0:30dd9c0f7559 | 285 | } |
grantphillips | 0:30dd9c0f7559 | 286 | |
grantphillips | 0:30dd9c0f7559 | 287 | bool ESP8266::eATCWLAP(string &list) { |
grantphillips | 0:30dd9c0f7559 | 288 | string data; |
grantphillips | 0:30dd9c0f7559 | 289 | esp.printf("AT+CWLAP\r\n"); |
grantphillips | 0:30dd9c0f7559 | 290 | return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list, 10000); |
grantphillips | 0:30dd9c0f7559 | 291 | } |
grantphillips | 0:30dd9c0f7559 | 292 | |
grantphillips | 0:30dd9c0f7559 | 293 | bool ESP8266::sATCWJAP(string ssid, string pwd) { |
grantphillips | 0:30dd9c0f7559 | 294 | string data; |
grantphillips | 0:30dd9c0f7559 | 295 | esp.printf("AT+CWJAP=\"%s\",\"%s\"\r\n", ssid.c_str(), pwd.c_str()); |
grantphillips | 0:30dd9c0f7559 | 296 | |
grantphillips | 0:30dd9c0f7559 | 297 | data = recvString("OK", "FAIL", 15000); |
grantphillips | 0:30dd9c0f7559 | 298 | if (data.find("OK") != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 299 | return true; |
grantphillips | 0:30dd9c0f7559 | 300 | } |
grantphillips | 0:30dd9c0f7559 | 301 | return false; |
grantphillips | 0:30dd9c0f7559 | 302 | } |
grantphillips | 0:30dd9c0f7559 | 303 | |
grantphillips | 0:30dd9c0f7559 | 304 | bool ESP8266::eATCIFSR(string &list) { |
grantphillips | 0:30dd9c0f7559 | 305 | esp.printf("AT+CIFSR\r\n"); |
grantphillips | 0:30dd9c0f7559 | 306 | return recvFindAndFilter("OK", "\r\r\n", "\r\n\r\nOK", list); |
grantphillips | 0:30dd9c0f7559 | 307 | } |
grantphillips | 0:30dd9c0f7559 | 308 | |
grantphillips | 0:30dd9c0f7559 | 309 | bool ESP8266::sATCIPSERVER(uint8_t mode, uint32_t port) { |
grantphillips | 0:30dd9c0f7559 | 310 | string data; |
grantphillips | 0:30dd9c0f7559 | 311 | if (mode) { |
grantphillips | 0:30dd9c0f7559 | 312 | esp.printf("AT+CIPSERVER=1,%d\r\n", port); |
grantphillips | 0:30dd9c0f7559 | 313 | |
grantphillips | 0:30dd9c0f7559 | 314 | data = recvString("OK", "no change"); |
grantphillips | 0:30dd9c0f7559 | 315 | if (data.find("OK") != std::string::npos || data.find("no change") != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 316 | return true; |
grantphillips | 0:30dd9c0f7559 | 317 | } |
grantphillips | 0:30dd9c0f7559 | 318 | return false; |
grantphillips | 0:30dd9c0f7559 | 319 | } else { |
grantphillips | 0:30dd9c0f7559 | 320 | esp.printf("AT+CIPSERVER=0\r\n"); |
grantphillips | 0:30dd9c0f7559 | 321 | return recvFind("\r\r\n"); |
grantphillips | 0:30dd9c0f7559 | 322 | } |
grantphillips | 0:30dd9c0f7559 | 323 | } |
grantphillips | 0:30dd9c0f7559 | 324 | |
grantphillips | 0:30dd9c0f7559 | 325 | bool ESP8266::sATCIPSENDMultiple(uint8_t mux_id, char buffer[], uint32_t len) { |
grantphillips | 0:30dd9c0f7559 | 326 | //esp.flush(); |
grantphillips | 0:30dd9c0f7559 | 327 | esp.printf("AT+CIPSEND=%d,%d\r\n", mux_id, len); |
grantphillips | 0:30dd9c0f7559 | 328 | if (recvFind(">", 5000)) { |
grantphillips | 0:30dd9c0f7559 | 329 | //esp.flush(); |
grantphillips | 0:30dd9c0f7559 | 330 | for (uint32_t i = 0; i < len; i++) { |
grantphillips | 0:30dd9c0f7559 | 331 | esp.printf("%c",buffer[i]); |
grantphillips | 0:30dd9c0f7559 | 332 | } |
grantphillips | 0:30dd9c0f7559 | 333 | return recvFind("SEND OK", 10000); |
grantphillips | 0:30dd9c0f7559 | 334 | } |
grantphillips | 0:30dd9c0f7559 | 335 | return false; |
grantphillips | 0:30dd9c0f7559 | 336 | } |
grantphillips | 0:30dd9c0f7559 | 337 | |
grantphillips | 0:30dd9c0f7559 | 338 | bool ESP8266::sATCWSAP(string ssid, string pwd, uint8_t chl, uint8_t ecn) |
grantphillips | 0:30dd9c0f7559 | 339 | { |
grantphillips | 0:30dd9c0f7559 | 340 | string data; |
grantphillips | 0:30dd9c0f7559 | 341 | esp.printf("AT+CWSAP=\"%s\",\"%s\",%d,%d\r\n", ssid.c_str(), pwd.c_str(), chl, ecn); |
grantphillips | 0:30dd9c0f7559 | 342 | |
grantphillips | 0:30dd9c0f7559 | 343 | data = recvString("OK", "ERROR", 5000); |
grantphillips | 0:30dd9c0f7559 | 344 | if (data.find("OK") != std::string::npos) { |
grantphillips | 0:30dd9c0f7559 | 345 | return true; |
grantphillips | 0:30dd9c0f7559 | 346 | } |
grantphillips | 0:30dd9c0f7559 | 347 | return false; |
grantphillips | 0:30dd9c0f7559 | 348 | } |
grantphillips | 0:30dd9c0f7559 | 349 | |
grantphillips | 0:30dd9c0f7559 | 350 | bool ESP8266::sATCWDHCP(bool enabled, int mode) { |
grantphillips | 0:30dd9c0f7559 | 351 | if(mode < 0 || mode > 2) { |
grantphillips | 0:30dd9c0f7559 | 352 | return false; |
grantphillips | 0:30dd9c0f7559 | 353 | } |
grantphillips | 0:30dd9c0f7559 | 354 | esp.printf("AT+CWDHCP=%d,%d\r\n", enabled?1:0, mode); |
grantphillips | 0:30dd9c0f7559 | 355 | return recvFind("OK", 2000); |
grantphillips | 0:30dd9c0f7559 | 356 | } |
grantphillips | 0:30dd9c0f7559 | 357 | |
grantphillips | 0:30dd9c0f7559 | 358 | bool ESP8266::sATCIPSTA(string ip) { |
grantphillips | 0:30dd9c0f7559 | 359 | esp.printf("AT+CIPSTA=\"%s\"\r\n", ip.c_str()); |
grantphillips | 0:30dd9c0f7559 | 360 | return recvFind("OK", 2000); |
grantphillips | 0:30dd9c0f7559 | 361 | } |
grantphillips | 0:30dd9c0f7559 | 362 | |
grantphillips | 0:30dd9c0f7559 | 363 | bool ESP8266::sATCIPAP(string ip) { |
grantphillips | 0:30dd9c0f7559 | 364 | esp.printf("AT+CIPAP=\"%s\"\r\n", ip.c_str()); |
grantphillips | 0:30dd9c0f7559 | 365 | return recvFind("OK", 2000); |
grantphillips | 0:30dd9c0f7559 | 366 | } |
grantphillips | 0:30dd9c0f7559 | 367 | |
grantphillips | 0:30dd9c0f7559 | 368 | /************************* Private Callback Functions *************************/ |
grantphillips | 0:30dd9c0f7559 | 369 | void ESP8266::RxInterrupt(void) |
grantphillips | 0:30dd9c0f7559 | 370 | { |
grantphillips | 0:30dd9c0f7559 | 371 | char c; |
grantphillips | 0:30dd9c0f7559 | 372 | |
grantphillips | 0:30dd9c0f7559 | 373 | c = esp.getc(); //read the incoming character |
grantphillips | 0:30dd9c0f7559 | 374 | if(c == '\n') { |
grantphillips | 0:30dd9c0f7559 | 375 | //rxmsg_idx = 0; |
grantphillips | 0:30dd9c0f7559 | 376 | rxpass1=false; |
grantphillips | 0:30dd9c0f7559 | 377 | rxpass2=false; |
grantphillips | 0:30dd9c0f7559 | 378 | rxpass3=false; |
grantphillips | 0:30dd9c0f7559 | 379 | rxtemp_idx=0; |
grantphillips | 0:30dd9c0f7559 | 380 | //new_rxmsg=false; |
grantphillips | 0:30dd9c0f7559 | 381 | } |
grantphillips | 0:30dd9c0f7559 | 382 | |
grantphillips | 0:30dd9c0f7559 | 383 | if(rxpass1 && rxpass2 && rxpass3) { |
grantphillips | 0:30dd9c0f7559 | 384 | if(rxtemp_idx < packetLen-1) { |
grantphillips | 0:30dd9c0f7559 | 385 | rxtemp[rxtemp_idx] = c; |
grantphillips | 0:30dd9c0f7559 | 386 | rxtemp_idx++; |
grantphillips | 0:30dd9c0f7559 | 387 | } |
grantphillips | 0:30dd9c0f7559 | 388 | else { |
grantphillips | 0:30dd9c0f7559 | 389 | rxtemp[rxtemp_idx] = c; |
grantphillips | 0:30dd9c0f7559 | 390 | rxtemp[rxtemp_idx+1] = '\0'; |
grantphillips | 0:30dd9c0f7559 | 391 | strcpy(packetData, rxtemp); |
grantphillips | 0:30dd9c0f7559 | 392 | //rxmsg_idx = 0; |
grantphillips | 0:30dd9c0f7559 | 393 | rxpass1=false; |
grantphillips | 0:30dd9c0f7559 | 394 | rxpass2=false; |
grantphillips | 0:30dd9c0f7559 | 395 | rxpass3=false; |
grantphillips | 0:30dd9c0f7559 | 396 | rxtemp_idx=0; |
grantphillips | 0:30dd9c0f7559 | 397 | new_rxmsg=true; |
grantphillips | 0:30dd9c0f7559 | 398 | } |
grantphillips | 0:30dd9c0f7559 | 399 | } |
grantphillips | 0:30dd9c0f7559 | 400 | else if(rxpass1 && rxpass2) { |
grantphillips | 0:30dd9c0f7559 | 401 | if(c != ':') { |
grantphillips | 0:30dd9c0f7559 | 402 | rxtemp[rxtemp_idx] = c; |
grantphillips | 0:30dd9c0f7559 | 403 | rxtemp_idx++; |
grantphillips | 0:30dd9c0f7559 | 404 | } |
grantphillips | 0:30dd9c0f7559 | 405 | else { |
grantphillips | 0:30dd9c0f7559 | 406 | rxtemp[rxtemp_idx] = '\0'; |
grantphillips | 0:30dd9c0f7559 | 407 | packetLen= atoi(rxtemp); |
grantphillips | 0:30dd9c0f7559 | 408 | rxtemp_idx=0; |
grantphillips | 0:30dd9c0f7559 | 409 | rxpass3=true; |
grantphillips | 0:30dd9c0f7559 | 410 | } |
grantphillips | 0:30dd9c0f7559 | 411 | } |
grantphillips | 0:30dd9c0f7559 | 412 | else if(rxpass1) { |
grantphillips | 0:30dd9c0f7559 | 413 | if(c != ',') { |
grantphillips | 0:30dd9c0f7559 | 414 | rxtemp[rxtemp_idx] = c; |
grantphillips | 0:30dd9c0f7559 | 415 | rxtemp_idx++; |
grantphillips | 0:30dd9c0f7559 | 416 | } |
grantphillips | 0:30dd9c0f7559 | 417 | else { |
grantphillips | 0:30dd9c0f7559 | 418 | rxtemp[rxtemp_idx] = '\0'; |
grantphillips | 0:30dd9c0f7559 | 419 | packetID = atoi(rxtemp); |
grantphillips | 0:30dd9c0f7559 | 420 | rxtemp_idx=0; |
grantphillips | 0:30dd9c0f7559 | 421 | rxpass2=true; |
grantphillips | 0:30dd9c0f7559 | 422 | } |
grantphillips | 0:30dd9c0f7559 | 423 | } |
grantphillips | 0:30dd9c0f7559 | 424 | else { |
grantphillips | 0:30dd9c0f7559 | 425 | rxtemp[rxtemp_idx] = c; |
grantphillips | 0:30dd9c0f7559 | 426 | rxtemp_idx++; |
grantphillips | 0:30dd9c0f7559 | 427 | |
grantphillips | 0:30dd9c0f7559 | 428 | if(rxtemp_idx == 6) { |
grantphillips | 0:30dd9c0f7559 | 429 | if(rxtemp[1] == '+' && rxtemp[2] == 'I' && rxtemp[3] == 'P' && rxtemp[4] == 'D' && rxtemp[5] == ',') { |
grantphillips | 0:30dd9c0f7559 | 430 | rxpass1 = true; |
grantphillips | 0:30dd9c0f7559 | 431 | } |
grantphillips | 0:30dd9c0f7559 | 432 | } |
grantphillips | 0:30dd9c0f7559 | 433 | /* |
grantphillips | 0:30dd9c0f7559 | 434 | rxmsg[rxmsg_idx] = c; |
grantphillips | 0:30dd9c0f7559 | 435 | rxmsg_idx++; |
grantphillips | 0:30dd9c0f7559 | 436 | |
grantphillips | 0:30dd9c0f7559 | 437 | if(rxmsg_idx == 6) { |
grantphillips | 0:30dd9c0f7559 | 438 | if(rxmsg[1] == '+' && rxmsg[2] == 'I' && rxmsg[3] == 'P' && rxmsg[4] == 'D' && rxmsg[5] == ',') { |
grantphillips | 0:30dd9c0f7559 | 439 | rxpass1 = true; |
grantphillips | 0:30dd9c0f7559 | 440 | } |
grantphillips | 0:30dd9c0f7559 | 441 | }*/ |
grantphillips | 0:30dd9c0f7559 | 442 | } |
grantphillips | 0:30dd9c0f7559 | 443 | } |