Class library for using the ESP8266 wifi module.

Committer:
grantphillips
Date:
Mon Jun 11 17:56:55 2018 +0000
Revision:
0:30dd9c0f7559
V1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
grantphillips 0:30dd9c0f7559 1 /* ESP8266 Library v1.0
grantphillips 0:30dd9c0f7559 2 * Copyright (c) 2017 Grant Phillips
grantphillips 0:30dd9c0f7559 3 * grant.phillips@nmmu.ac.za
grantphillips 0:30dd9c0f7559 4 *
grantphillips 0:30dd9c0f7559 5 * This library was adapted from the WeeESP8266 version by ITEAD STUDIO.
grantphillips 0:30dd9c0f7559 6 * (https://os.mbed.com/users/itead/code/WeeESP8266/)
grantphillips 0:30dd9c0f7559 7 *
grantphillips 0:30dd9c0f7559 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
grantphillips 0:30dd9c0f7559 9 * of this software and associated documentation files (the "Software"), to deal
grantphillips 0:30dd9c0f7559 10 * in the Software without restriction, including without limitation the rights
grantphillips 0:30dd9c0f7559 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
grantphillips 0:30dd9c0f7559 12 * copies of the Software, and to permit persons to whom the Software is
grantphillips 0:30dd9c0f7559 13 * furnished to do so, subject to the following conditions:
grantphillips 0:30dd9c0f7559 14 *
grantphillips 0:30dd9c0f7559 15 * The above copyright notice and this permission notice shall be included in
grantphillips 0:30dd9c0f7559 16 * all copies or substantial portions of the Software.
grantphillips 0:30dd9c0f7559 17 *
grantphillips 0:30dd9c0f7559 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
grantphillips 0:30dd9c0f7559 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
grantphillips 0:30dd9c0f7559 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
grantphillips 0:30dd9c0f7559 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
grantphillips 0:30dd9c0f7559 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
grantphillips 0:30dd9c0f7559 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
grantphillips 0:30dd9c0f7559 24 * THE SOFTWARE.
grantphillips 0:30dd9c0f7559 25 */
grantphillips 0:30dd9c0f7559 26
grantphillips 0:30dd9c0f7559 27 #ifndef ESP8266_H
grantphillips 0:30dd9c0f7559 28 #define ESP8266_H
grantphillips 0:30dd9c0f7559 29
grantphillips 0:30dd9c0f7559 30 #include "mbed.h"
grantphillips 0:30dd9c0f7559 31 #include <string>
grantphillips 0:30dd9c0f7559 32
grantphillips 0:30dd9c0f7559 33 /** Class library for using the ESP8266 wifi module.
grantphillips 0:30dd9c0f7559 34 */
grantphillips 0:30dd9c0f7559 35
grantphillips 0:30dd9c0f7559 36 class ESP8266 {
grantphillips 0:30dd9c0f7559 37 public:
grantphillips 0:30dd9c0f7559 38 /** Create a ESP8266 object connected to the specified pins.
grantphillips 0:30dd9c0f7559 39 * @param Tx Pin used to connect to ESP8266's Tx pin
grantphillips 0:30dd9c0f7559 40 * @param Rx Pin used to connect to ESP8266's Rx pin
grantphillips 0:30dd9c0f7559 41 */
grantphillips 0:30dd9c0f7559 42 ESP8266(PinName Tx, PinName Rx, PinName Rst);
grantphillips 0:30dd9c0f7559 43
grantphillips 0:30dd9c0f7559 44 /** Starts the ESP8266 in a specified mode.
grantphillips 0:30dd9c0f7559 45 * @param mode The WiFi mode the ESP8266 must be set to (1=Station, 2=SoftAP, 3=Station+SoftAP)
grantphillips 0:30dd9c0f7559 46 *
grantphillips 0:30dd9c0f7559 47 * @retval true - succesfull
grantphillips 0:30dd9c0f7559 48 * @retval false - not succesfull
grantphillips 0:30dd9c0f7559 49 */
grantphillips 0:30dd9c0f7559 50 bool startup(uint8_t mode);
grantphillips 0:30dd9c0f7559 51
grantphillips 0:30dd9c0f7559 52 /** Get the version of AT Command Set from the ESP8266.
grantphillips 0:30dd9c0f7559 53 *
grantphillips 0:30dd9c0f7559 54 * @param version String variable where version will be stored.
grantphillips 0:30dd9c0f7559 55 */
grantphillips 0:30dd9c0f7559 56 void getVersion(char* version);
grantphillips 0:30dd9c0f7559 57
grantphillips 0:30dd9c0f7559 58 /** Search available AP list and return it.
grantphillips 0:30dd9c0f7559 59 *
grantphillips 0:30dd9c0f7559 60 * @param list String variable where AP list will be stored.
grantphillips 0:30dd9c0f7559 61 * @note This method will occupy a lot of memeory(hundreds of Bytes to a couple of KBytes).
grantphillips 0:30dd9c0f7559 62 * Do not call this method unless you must and ensure that your board has enough memory left.
grantphillips 0:30dd9c0f7559 63 */
grantphillips 0:30dd9c0f7559 64 void getAPList(char* list);
grantphillips 0:30dd9c0f7559 65
grantphillips 0:30dd9c0f7559 66 /** Set SoftAP parameters.
grantphillips 0:30dd9c0f7559 67 *
grantphillips 0:30dd9c0f7559 68 * @param ssid SSID of SoftAP.
grantphillips 0:30dd9c0f7559 69 * @param pwd PASSWORD of SoftAP.
grantphillips 0:30dd9c0f7559 70 * @param chl the channel (1 - 13, default: 7).
grantphillips 0:30dd9c0f7559 71 * @param ecn the way of encrypstion (0 - OPEN, 1 - WEP,
grantphillips 0:30dd9c0f7559 72 * 2 - WPA_PSK, 3 - WPA2_PSK, 4 - WPA_WPA2_PSK, default: 4).
grantphillips 0:30dd9c0f7559 73 * @note This method should not be called when station mode.
grantphillips 0:30dd9c0f7559 74 */
grantphillips 0:30dd9c0f7559 75 bool setSoftAPParam(string ssid, string pwd, uint8_t chl = 7, uint8_t ecn = 4);
grantphillips 0:30dd9c0f7559 76
grantphillips 0:30dd9c0f7559 77 /** Enable/Disable DHCP
grantphillips 0:30dd9c0f7559 78 *
grantphillips 0:30dd9c0f7559 79 * @param enabled DHCP enabled when true
grantphillips 0:30dd9c0f7559 80 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
grantphillips 0:30dd9c0f7559 81 * @return true only if ESP8266 enables/disables DHCP successfully
grantphillips 0:30dd9c0f7559 82 */
grantphillips 0:30dd9c0f7559 83 bool DHCP(bool enabled, int mode);
grantphillips 0:30dd9c0f7559 84
grantphillips 0:30dd9c0f7559 85 /** Join in AP.
grantphillips 0:30dd9c0f7559 86 *
grantphillips 0:30dd9c0f7559 87 * @param ssid SSID of AP to join in.
grantphillips 0:30dd9c0f7559 88 * @param pwd Password of AP to join in.
grantphillips 0:30dd9c0f7559 89 * @retval true - success.
grantphillips 0:30dd9c0f7559 90 * @retval false - failure.
grantphillips 0:30dd9c0f7559 91 * @note This method will take a couple of seconds.
grantphillips 0:30dd9c0f7559 92 */
grantphillips 0:30dd9c0f7559 93 bool joinAP(string ssid, string pwd);
grantphillips 0:30dd9c0f7559 94
grantphillips 0:30dd9c0f7559 95 /** Get the local IP address of ESP8266.
grantphillips 0:30dd9c0f7559 96 *
grantphillips 0:30dd9c0f7559 97 * @param version String variable where version will be stored.
grantphillips 0:30dd9c0f7559 98 */
grantphillips 0:30dd9c0f7559 99 void getLocalIP(char* LocalIP);
grantphillips 0:30dd9c0f7559 100
grantphillips 0:30dd9c0f7559 101 /** Sets the IP Address of the ESP8266 Station.
grantphillips 0:30dd9c0f7559 102 *
grantphillips 0:30dd9c0f7559 103 * @param LocalIP The IP Address of the ESP8266 Station
grantphillips 0:30dd9c0f7559 104 * @retval true - success.
grantphillips 0:30dd9c0f7559 105 * @retval false - failure.
grantphillips 0:30dd9c0f7559 106 */
grantphillips 0:30dd9c0f7559 107 bool setLocalIP(string LocalIP);
grantphillips 0:30dd9c0f7559 108
grantphillips 0:30dd9c0f7559 109 /** Sets the IP Address of the ESP8266 SoftAP.
grantphillips 0:30dd9c0f7559 110 *
grantphillips 0:30dd9c0f7559 111 * @param LocalIP The IP Address of the ESP8266 SoftAP
grantphillips 0:30dd9c0f7559 112 * @retval true - success.
grantphillips 0:30dd9c0f7559 113 * @retval false - failure.
grantphillips 0:30dd9c0f7559 114 */
grantphillips 0:30dd9c0f7559 115 bool setSoftAPIP(string LocalIP);
grantphillips 0:30dd9c0f7559 116
grantphillips 0:30dd9c0f7559 117 /** Start TCP Server(Only in multiple mode).
grantphillips 0:30dd9c0f7559 118 *
grantphillips 0:30dd9c0f7559 119 * After started, only methods TCPPacketSend, TCPPacketRead, and TCPPacketArrived can be used.
grantphillips 0:30dd9c0f7559 120 * Once TCPServerStop is called, all other methods can be used again.
grantphillips 0:30dd9c0f7559 121 *
grantphillips 0:30dd9c0f7559 122 * @param port The port number to listen on (default: 333).
grantphillips 0:30dd9c0f7559 123 * @retval true - success.
grantphillips 0:30dd9c0f7559 124 * @retval false - failure.
grantphillips 0:30dd9c0f7559 125 */
grantphillips 0:30dd9c0f7559 126 bool TCPServerStart(uint32_t port = 333);
grantphillips 0:30dd9c0f7559 127
grantphillips 0:30dd9c0f7559 128 /** Indicates whether a new packet has arrived from a connected client.
grantphillips 0:30dd9c0f7559 129 *
grantphillips 0:30dd9c0f7559 130 * @retval true - A new packet has arrived.
grantphillips 0:30dd9c0f7559 131 * @retval false - No new packets have arrived.
grantphillips 0:30dd9c0f7559 132 */
grantphillips 0:30dd9c0f7559 133 bool TCPPacketReceived(void);
grantphillips 0:30dd9c0f7559 134
grantphillips 0:30dd9c0f7559 135 /** Reads the current received TCP packet
grantphillips 0:30dd9c0f7559 136 *
grantphillips 0:30dd9c0f7559 137 * @param ID Pointer for the ID of client for the new received packet.
grantphillips 0:30dd9c0f7559 138 * @param LEN Pointer for the length of the new received packet.
grantphillips 0:30dd9c0f7559 139 * @param DATA String pointer for the string of the new received packet.
grantphillips 0:30dd9c0f7559 140 */
grantphillips 0:30dd9c0f7559 141 void TCPPacketRead(uint8_t *ID, uint32_t *LEN, char DATA[]);
grantphillips 0:30dd9c0f7559 142
grantphillips 0:30dd9c0f7559 143 /** Send a data packet using TCP to a specified client.
grantphillips 0:30dd9c0f7559 144 *
grantphillips 0:30dd9c0f7559 145 * @param ID The identifier of client to send the packet to (available value: 0 - 4).
grantphillips 0:30dd9c0f7559 146 * @param LEN The length of packet to send.
grantphillips 0:30dd9c0f7559 147 * @param DATA The buffer of data to send.
grantphillips 0:30dd9c0f7559 148 * @retval true - success.
grantphillips 0:30dd9c0f7559 149 * @retval false - failure.
grantphillips 0:30dd9c0f7559 150 */
grantphillips 0:30dd9c0f7559 151 bool TCPPacketSend(uint8_t ID, uint32_t LEN, char DATA[]);
grantphillips 0:30dd9c0f7559 152
grantphillips 0:30dd9c0f7559 153
grantphillips 0:30dd9c0f7559 154 private:
grantphillips 0:30dd9c0f7559 155 Serial esp; //Serial object for connecting to ESP8266
grantphillips 0:30dd9c0f7559 156 DigitalOut _rst;
grantphillips 0:30dd9c0f7559 157 bool mDEBUG;
grantphillips 0:30dd9c0f7559 158 Timer t;
grantphillips 0:30dd9c0f7559 159
grantphillips 0:30dd9c0f7559 160 char packetData[1000];
grantphillips 0:30dd9c0f7559 161 int packetID;
grantphillips 0:30dd9c0f7559 162 int packetLen;
grantphillips 0:30dd9c0f7559 163 char rxtemp[1000];
grantphillips 0:30dd9c0f7559 164 int rxtemp_idx;
grantphillips 0:30dd9c0f7559 165 bool new_rxmsg; //flag to indicate if a new complete message was received
grantphillips 0:30dd9c0f7559 166 bool rxpass1, rxpass2, rxpass3;
grantphillips 0:30dd9c0f7559 167 void RxInterrupt(void); //Interrupt to receive data when TCP server is set
grantphillips 0:30dd9c0f7559 168
grantphillips 0:30dd9c0f7559 169 bool eAT(void);
grantphillips 0:30dd9c0f7559 170 bool eATRST(void);
grantphillips 0:30dd9c0f7559 171 bool eATGMR(string &version);
grantphillips 0:30dd9c0f7559 172 bool sATCWMODE(uint8_t mode);
grantphillips 0:30dd9c0f7559 173 bool sATCIPMUX(uint8_t mode);
grantphillips 0:30dd9c0f7559 174 bool eATCWLAP(string &list);
grantphillips 0:30dd9c0f7559 175 bool sATCWJAP(string ssid, string pwd);
grantphillips 0:30dd9c0f7559 176 bool eATCIFSR(string &list);
grantphillips 0:30dd9c0f7559 177 bool sATCIPSERVER(uint8_t mode, uint32_t port = 333);
grantphillips 0:30dd9c0f7559 178 bool sATCIPSENDMultiple(uint8_t mux_id, char buffer[], uint32_t len);
grantphillips 0:30dd9c0f7559 179 bool sATCWSAP(string ssid, string pwd, uint8_t chl, uint8_t ecn);
grantphillips 0:30dd9c0f7559 180 bool sATCWDHCP(bool enabled, int mode);
grantphillips 0:30dd9c0f7559 181 bool sATCIPSTA(string ip);
grantphillips 0:30dd9c0f7559 182 bool sATCIPAP(string ip);
grantphillips 0:30dd9c0f7559 183 /*
grantphillips 0:30dd9c0f7559 184
grantphillips 0:30dd9c0f7559 185 bool qATCWMODE(uint8_t *mode);
grantphillips 0:30dd9c0f7559 186
grantphillips 0:30dd9c0f7559 187
grantphillips 0:30dd9c0f7559 188
grantphillips 0:30dd9c0f7559 189 bool eATCWQAP(void);
grantphillips 0:30dd9c0f7559 190 bool sATCWSAP(string ssid, string pwd, uint8_t chl, uint8_t ecn);
grantphillips 0:30dd9c0f7559 191 bool eATCWLIF(string &list);
grantphillips 0:30dd9c0f7559 192
grantphillips 0:30dd9c0f7559 193 bool eATCIPSTATUS(string &list);
grantphillips 0:30dd9c0f7559 194 bool eATCIFSR(string &list);
grantphillips 0:30dd9c0f7559 195
grantphillips 0:30dd9c0f7559 196 */
grantphillips 0:30dd9c0f7559 197
grantphillips 0:30dd9c0f7559 198
grantphillips 0:30dd9c0f7559 199
grantphillips 0:30dd9c0f7559 200
grantphillips 0:30dd9c0f7559 201 /** Verify whether ESP8266 is connected correctly or not.
grantphillips 0:30dd9c0f7559 202 * @param None
grantphillips 0:30dd9c0f7559 203 *
grantphillips 0:30dd9c0f7559 204 * @retval true - connected.
grantphillips 0:30dd9c0f7559 205 * @retval false - not connected or found.
grantphillips 0:30dd9c0f7559 206 */
grantphillips 0:30dd9c0f7559 207 bool kick(void);
grantphillips 0:30dd9c0f7559 208
grantphillips 0:30dd9c0f7559 209 /** Restart ESP8266 by "AT+RST". This method will take 3 seconds or more.
grantphillips 0:30dd9c0f7559 210 *
grantphillips 0:30dd9c0f7559 211 * @retval true - success.
grantphillips 0:30dd9c0f7559 212 * @retval false - failure.
grantphillips 0:30dd9c0f7559 213 */
grantphillips 0:30dd9c0f7559 214 bool reset(void);
grantphillips 0:30dd9c0f7559 215
grantphillips 0:30dd9c0f7559 216 /** Sets the ESP8266's WiFi mode.
grantphillips 0:30dd9c0f7559 217 * @param mode The WiFi mode the ESP8266 must be set to (1=Station, 2=SoftAP, 3=Station+SoftAP)
grantphillips 0:30dd9c0f7559 218 *
grantphillips 0:30dd9c0f7559 219 * @retval true - success.
grantphillips 0:30dd9c0f7559 220 * @retval false - failure.
grantphillips 0:30dd9c0f7559 221 */
grantphillips 0:30dd9c0f7559 222 bool setWiFiMode(uint8_t mode);
grantphillips 0:30dd9c0f7559 223
grantphillips 0:30dd9c0f7559 224 /** Enable of disable multiple connections on the ESP8266
grantphillips 0:30dd9c0f7559 225 * @param onoff Value to Enable(=true) or Disable(=false) multiple connections.
grantphillips 0:30dd9c0f7559 226 *
grantphillips 0:30dd9c0f7559 227 * @retval true - success.
grantphillips 0:30dd9c0f7559 228 * @retval false - failure.
grantphillips 0:30dd9c0f7559 229 */
grantphillips 0:30dd9c0f7559 230 bool setMux(bool onoff);
grantphillips 0:30dd9c0f7559 231
grantphillips 0:30dd9c0f7559 232 /* Receive data from uart and search first target. Return true if target found, false for timeout. */
grantphillips 0:30dd9c0f7559 233 bool recvFind(string target, uint32_t timeout = 1000);
grantphillips 0:30dd9c0f7559 234
grantphillips 0:30dd9c0f7559 235 /* Receive data from uart. Return all received data if target found or timeout. */
grantphillips 0:30dd9c0f7559 236 string recvString(string target, uint32_t timeout = 1000);
grantphillips 0:30dd9c0f7559 237
grantphillips 0:30dd9c0f7559 238 /* Receive data from uart. Return all received data if one of target1 and target2 found or timeout. */
grantphillips 0:30dd9c0f7559 239 string recvString(string target1, string target2, uint32_t timeout = 1000);
grantphillips 0:30dd9c0f7559 240
grantphillips 0:30dd9c0f7559 241 /* Receive data from uart. Return all received data if one of target1, target2 and target3 found or timeout. */
grantphillips 0:30dd9c0f7559 242 string recvString(string target1, string target2, string target3, uint32_t timeout = 1000);
grantphillips 0:30dd9c0f7559 243
grantphillips 0:30dd9c0f7559 244 /* Receive data from uart and search first target and cut out the substring between begin and end(excluding begin and end self).
grantphillips 0:30dd9c0f7559 245 * Return true if target found, false for timeout. */
grantphillips 0:30dd9c0f7559 246 bool recvFindAndFilter(string target, string begin, string end, string &data, uint32_t timeout = 1000);
grantphillips 0:30dd9c0f7559 247
grantphillips 0:30dd9c0f7559 248
grantphillips 0:30dd9c0f7559 249 };
grantphillips 0:30dd9c0f7559 250
grantphillips 0:30dd9c0f7559 251 #endif