Tuned for OS2 to reduce flash code size for smaller MCU's. Added further functions since 2018 v1.7 firmware. Improved faster large web page sending. Built in fast NTP Client time and RTC setting function. ATParser message handling improvements and updated.

Dependencies:  

Dependents:   ESP8266-NTP-Test

Committer:
star297
Date:
Mon Feb 11 00:05:12 2019 +0000
Revision:
0:64d65feb280c
Child:
2:072248e430d0
Initial commit, OS2 version. Latest ESP8266 firmware integration + functions added- RSSI, Fast NTP Client set RTC, Fast web page send with error handling, added more AT command functions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:64d65feb280c 1
star297 0:64d65feb280c 2 #ifndef ESP8266_H
star297 0:64d65feb280c 3 #define ESP8266_H
star297 0:64d65feb280c 4
star297 0:64d65feb280c 5 #include "ATParser.h"
star297 0:64d65feb280c 6
star297 0:64d65feb280c 7
star297 0:64d65feb280c 8 class ESP8266
star297 0:64d65feb280c 9 {
star297 0:64d65feb280c 10 public:
star297 0:64d65feb280c 11 ESP8266(PinName tx, PinName rx, bool debug=false);
star297 0:64d65feb280c 12
star297 0:64d65feb280c 13 /**
star297 0:64d65feb280c 14 * Startup the ESP8266
star297 0:64d65feb280c 15 *
star297 0:64d65feb280c 16 * @param mode of WIFI 1-client, 2-host, 3-both
star297 0:64d65feb280c 17 * @return true only if ESP8266 was setup correctly
star297 0:64d65feb280c 18 */
star297 0:64d65feb280c 19 bool startup(int mode);
star297 0:64d65feb280c 20
star297 0:64d65feb280c 21 /**
star297 0:64d65feb280c 22 * Start the ESP8266 in server mode
star297 0:64d65feb280c 23 *
star297 0:64d65feb280c 24 * @param mode of WIFI 1-client, 2-host, 3-both
star297 0:64d65feb280c 25 * @param port
star297 0:64d65feb280c 26 * @param timeout
star297 0:64d65feb280c 27 * @return true only if ESP8266 was setup correctly
star297 0:64d65feb280c 28 */
star297 0:64d65feb280c 29 bool startServer(int mode,int port,int timeout);
star297 0:64d65feb280c 30
star297 0:64d65feb280c 31 /**
star297 0:64d65feb280c 32 * Reset ESP8266
star297 0:64d65feb280c 33 *
star297 0:64d65feb280c 34 * @return true only if ESP8266 resets successfully
star297 0:64d65feb280c 35 */
star297 0:64d65feb280c 36 bool reset(void);
star297 0:64d65feb280c 37
star297 0:64d65feb280c 38 /**
star297 0:64d65feb280c 39 * Get ESP8266 firmware version.
star297 0:64d65feb280c 40 *
star297 0:64d65feb280c 41 * @return firmware version.
star297 0:64d65feb280c 42 */
star297 0:64d65feb280c 43 const char *getFirmware(void);
star297 0:64d65feb280c 44
star297 0:64d65feb280c 45 /**
star297 0:64d65feb280c 46 * Enable/Disable DHCP
star297 0:64d65feb280c 47 *
star297 0:64d65feb280c 48 * @param enabled DHCP enabled when true
star297 0:64d65feb280c 49 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
star297 0:64d65feb280c 50 * @return true only if ESP8266 enables/disables DHCP successfully
star297 0:64d65feb280c 51 */
star297 0:64d65feb280c 52
star297 0:64d65feb280c 53 bool dhcp(bool enabled, int mode);
star297 0:64d65feb280c 54
star297 0:64d65feb280c 55 /**
star297 0:64d65feb280c 56 * Connect ESP8266 to AP
star297 0:64d65feb280c 57 *
star297 0:64d65feb280c 58 * @param ap the name of the AP
star297 0:64d65feb280c 59 * @param passPhrase the password of AP
star297 0:64d65feb280c 60 * @return true only if ESP8266 is connected successfully
star297 0:64d65feb280c 61 */
star297 0:64d65feb280c 62 bool connect(const char *ap, const char *passPhrase);
star297 0:64d65feb280c 63
star297 0:64d65feb280c 64 /**
star297 0:64d65feb280c 65 * Disconnect ESP8266 from AP
star297 0:64d65feb280c 66 *
star297 0:64d65feb280c 67 * @return true only if ESP8266 is disconnected successfully
star297 0:64d65feb280c 68 */
star297 0:64d65feb280c 69 bool disconnect(void);
star297 0:64d65feb280c 70
star297 0:64d65feb280c 71 /**
star297 0:64d65feb280c 72 * Get the IP address of ESP8266
star297 0:64d65feb280c 73 *
star297 0:64d65feb280c 74 * @return null-teriminated IP address or null if no IP address is assigned
star297 0:64d65feb280c 75 */
star297 0:64d65feb280c 76 const char *getIPAddress(void);
star297 0:64d65feb280c 77
star297 0:64d65feb280c 78 /**
star297 0:64d65feb280c 79 * Get the MAC address of ESP8266
star297 0:64d65feb280c 80 *
star297 0:64d65feb280c 81 * @return null-terminated MAC address or null if no MAC address is assigned
star297 0:64d65feb280c 82 */
star297 0:64d65feb280c 83 const char *getMACAddress(void);
star297 0:64d65feb280c 84
star297 0:64d65feb280c 85 /**
star297 0:64d65feb280c 86 * Check if ESP8266 is conenected
star297 0:64d65feb280c 87 *
star297 0:64d65feb280c 88 * @return true only if the chip has an IP address
star297 0:64d65feb280c 89 */
star297 0:64d65feb280c 90 bool isConnected(void);
star297 0:64d65feb280c 91
star297 0:64d65feb280c 92 /**
star297 0:64d65feb280c 93 * Open a socketed connection
star297 0:64d65feb280c 94 *
star297 0:64d65feb280c 95 * @param type the type of socket to open "UDP" or "TCP"
star297 0:64d65feb280c 96 * @param id id to give the new socket, valid 0-4
star297 0:64d65feb280c 97 * @param port port to open connection with
star297 0:64d65feb280c 98 * @param addr the IP address of the destination
star297 0:64d65feb280c 99 * @return true only if socket opened successfully
star297 0:64d65feb280c 100 */
star297 0:64d65feb280c 101 bool open(const char *type, int id, const char* addr, int port);
star297 0:64d65feb280c 102
star297 0:64d65feb280c 103 /**
star297 0:64d65feb280c 104 * Sends data to an open socket
star297 0:64d65feb280c 105 *
star297 0:64d65feb280c 106 * @param id id of socket to send to
star297 0:64d65feb280c 107 * @param data data to be sent
star297 0:64d65feb280c 108 * @param amount amount of data to be sent - max 2048
star297 0:64d65feb280c 109 * @return true only if data sent successfully
star297 0:64d65feb280c 110 */
star297 0:64d65feb280c 111 bool send(int id, const void *data, uint32_t amount);
star297 0:64d65feb280c 112
star297 0:64d65feb280c 113 /**
star297 0:64d65feb280c 114 * Sends larger web page data to a connection
star297 0:64d65feb280c 115 * more controlled with comms failure recovery
star297 0:64d65feb280c 116 *
star297 0:64d65feb280c 117 * @param id id of socket to send to
star297 0:64d65feb280c 118 * @param data web page data to be sent
star297 0:64d65feb280c 119 * @param amount amount of data to be sent.
star297 0:64d65feb280c 120 * No theoretical limit, use for large data pages.
star297 0:64d65feb280c 121 * Sends in 2048kbit chunks.
star297 0:64d65feb280c 122 * @return true only if data sent successfully
star297 0:64d65feb280c 123 */
star297 0:64d65feb280c 124 bool sendWebPage(int id, const char* page, uint32_t amount);
star297 0:64d65feb280c 125
star297 0:64d65feb280c 126 /**
star297 0:64d65feb280c 127 * Receives data from an open socket
star297 0:64d65feb280c 128 *
star297 0:64d65feb280c 129 * @param id id to receive from
star297 0:64d65feb280c 130 * @param data placeholder for returned information
star297 0:64d65feb280c 131 * @param amount number of bytes to be received
star297 0:64d65feb280c 132 * @return the number of bytes received
star297 0:64d65feb280c 133 */
star297 0:64d65feb280c 134 int32_t recv(int id, void *data, uint32_t amount);
star297 0:64d65feb280c 135
star297 0:64d65feb280c 136 /**
star297 0:64d65feb280c 137 * Receives data from an open web socket
star297 0:64d65feb280c 138 *
star297 0:64d65feb280c 139 * @param id id to receive from
star297 0:64d65feb280c 140 * @param data placed in 4 containers
star297 0:64d65feb280c 141 * @param char requestType;
star297 0:64d65feb280c 142 * @param char request;
star297 0:64d65feb280c 143 * @param int linkId;
star297 0:64d65feb280c 144 * @param int ipdLen;
star297 0:64d65feb280c 145 * @return the number of bytes received
star297 0:64d65feb280c 146 */
star297 0:64d65feb280c 147 const char *recvWebRequest(void);
star297 0:64d65feb280c 148
star297 0:64d65feb280c 149 /**
star297 0:64d65feb280c 150 * Closes a socket
star297 0:64d65feb280c 151 *
star297 0:64d65feb280c 152 * @param id id of socket to close, valid only 0-4
star297 0:64d65feb280c 153 * @return true only if socket is closed successfully
star297 0:64d65feb280c 154 */
star297 0:64d65feb280c 155 bool close(int id);
star297 0:64d65feb280c 156
star297 0:64d65feb280c 157 /* Return RSSI for active connection
star297 0:64d65feb280c 158 *
star297 0:64d65feb280c 159 * @return Measured RSSI
star297 0:64d65feb280c 160 */
star297 0:64d65feb280c 161 int8_t getRSSI();
star297 0:64d65feb280c 162
star297 0:64d65feb280c 163 /**
star297 0:64d65feb280c 164 * Gets NTP time in seconds since 1970.
star297 0:64d65feb280c 165 *
star297 0:64d65feb280c 166 * @param NTPpool url for server e.g. "1.nl.pool.ntp.org"
star297 0:64d65feb280c 167 * @param tzoffset +/- seconds offset for required time zone e.g. 3600 to add one hour
star297 0:64d65feb280c 168 * @param setRTC If true, Set's MCU internal RTC to the received seconds since 1970 including offset
star297 0:64d65feb280c 169 * @return seconds since 1970
star297 0:64d65feb280c 170 */
star297 0:64d65feb280c 171 int32_t getNTP(char * NTPpool, int tzoffset, int setRTC);
star297 0:64d65feb280c 172
star297 0:64d65feb280c 173
star297 0:64d65feb280c 174 /**
star297 0:64d65feb280c 175 * Allows timeout to be changed between commands
star297 0:64d65feb280c 176 *
star297 0:64d65feb280c 177 * @param timeout_ms timeout of the connection
star297 0:64d65feb280c 178 */
star297 0:64d65feb280c 179 void setTimeout(uint32_t timeout_ms);
star297 0:64d65feb280c 180
star297 0:64d65feb280c 181 /**
star297 0:64d65feb280c 182 * Checks if data is available
star297 0:64d65feb280c 183 */
star297 0:64d65feb280c 184 bool readable();
star297 0:64d65feb280c 185
star297 0:64d65feb280c 186 /**
star297 0:64d65feb280c 187 * Checks if data can be written
star297 0:64d65feb280c 188 */
star297 0:64d65feb280c 189 bool writeable();
star297 0:64d65feb280c 190
star297 0:64d65feb280c 191 char requestType[6];
star297 0:64d65feb280c 192 char request[50];
star297 0:64d65feb280c 193 int linkId;
star297 0:64d65feb280c 194 int ipdLen;
star297 0:64d65feb280c 195 int _con_status;
star297 0:64d65feb280c 196 char _ssid[32];
star297 0:64d65feb280c 197 char _APIP_buffer[16];
star297 0:64d65feb280c 198 char _APMAC_buffer[18];
star297 0:64d65feb280c 199 char _STAIP_buffer[16];
star297 0:64d65feb280c 200 char _STAMAC_buffer[18];
star297 0:64d65feb280c 201 char _firmware[200];
star297 0:64d65feb280c 202
star297 0:64d65feb280c 203
star297 0:64d65feb280c 204 private:
star297 0:64d65feb280c 205 BufferedSerial _serial;
star297 0:64d65feb280c 206 ATParser _parser;
star297 0:64d65feb280c 207
star297 0:64d65feb280c 208 };
star297 0:64d65feb280c 209
star297 0:64d65feb280c 210 #endif