Alex Leung
/
HealthTracker
Test version
ESP8266.h@0:4be500de690c, 2018-03-20 (annotated)
- Committer:
- a2824256
- Date:
- Tue Mar 20 02:09:21 2018 +0000
- Revision:
- 0:4be500de690c
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
a2824256 | 0:4be500de690c | 1 | /* ESP8266Interface Example |
a2824256 | 0:4be500de690c | 2 | * Copyright (c) 2015 ARM Limited |
a2824256 | 0:4be500de690c | 3 | * |
a2824256 | 0:4be500de690c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
a2824256 | 0:4be500de690c | 5 | * you may not use this file except in compliance with the License. |
a2824256 | 0:4be500de690c | 6 | * You may obtain a copy of the License at |
a2824256 | 0:4be500de690c | 7 | * |
a2824256 | 0:4be500de690c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
a2824256 | 0:4be500de690c | 9 | * |
a2824256 | 0:4be500de690c | 10 | * Unless required by applicable law or agreed to in writing, software |
a2824256 | 0:4be500de690c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
a2824256 | 0:4be500de690c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
a2824256 | 0:4be500de690c | 13 | * See the License for the specific language governing permissions and |
a2824256 | 0:4be500de690c | 14 | * limitations under the License. |
a2824256 | 0:4be500de690c | 15 | */ |
a2824256 | 0:4be500de690c | 16 | |
a2824256 | 0:4be500de690c | 17 | #ifndef ESP8266_H |
a2824256 | 0:4be500de690c | 18 | #define ESP8266_H |
a2824256 | 0:4be500de690c | 19 | |
a2824256 | 0:4be500de690c | 20 | #include "ATCmdParser.h" |
a2824256 | 0:4be500de690c | 21 | #include "nsapi_types.h" |
a2824256 | 0:4be500de690c | 22 | #include "rtos.h" |
a2824256 | 0:4be500de690c | 23 | |
a2824256 | 0:4be500de690c | 24 | /** ESP8266Interface class. |
a2824256 | 0:4be500de690c | 25 | This is an interface to a ESP8266 radio. |
a2824256 | 0:4be500de690c | 26 | */ |
a2824256 | 0:4be500de690c | 27 | class ESP8266 |
a2824256 | 0:4be500de690c | 28 | { |
a2824256 | 0:4be500de690c | 29 | public: |
a2824256 | 0:4be500de690c | 30 | ESP8266(PinName tx, PinName rx, bool debug=false); |
a2824256 | 0:4be500de690c | 31 | |
a2824256 | 0:4be500de690c | 32 | /** |
a2824256 | 0:4be500de690c | 33 | * Check firmware version of ESP8266 |
a2824256 | 0:4be500de690c | 34 | * |
a2824256 | 0:4be500de690c | 35 | * @return integer firmware version or -1 if firmware query command gives outdated response |
a2824256 | 0:4be500de690c | 36 | */ |
a2824256 | 0:4be500de690c | 37 | int get_firmware_version(void); |
a2824256 | 0:4be500de690c | 38 | |
a2824256 | 0:4be500de690c | 39 | /** |
a2824256 | 0:4be500de690c | 40 | * Startup the ESP8266 |
a2824256 | 0:4be500de690c | 41 | * |
a2824256 | 0:4be500de690c | 42 | * @param mode mode of WIFI 1-client, 2-host, 3-both |
a2824256 | 0:4be500de690c | 43 | * @return true only if ESP8266 was setup correctly |
a2824256 | 0:4be500de690c | 44 | */ |
a2824256 | 0:4be500de690c | 45 | bool startup(int mode); |
a2824256 | 0:4be500de690c | 46 | |
a2824256 | 0:4be500de690c | 47 | /** |
a2824256 | 0:4be500de690c | 48 | * Reset ESP8266 |
a2824256 | 0:4be500de690c | 49 | * |
a2824256 | 0:4be500de690c | 50 | * @return true only if ESP8266 resets successfully |
a2824256 | 0:4be500de690c | 51 | */ |
a2824256 | 0:4be500de690c | 52 | bool reset(void); |
a2824256 | 0:4be500de690c | 53 | |
a2824256 | 0:4be500de690c | 54 | /** |
a2824256 | 0:4be500de690c | 55 | * Enable/Disable DHCP |
a2824256 | 0:4be500de690c | 56 | * |
a2824256 | 0:4be500de690c | 57 | * @param enabled DHCP enabled when true |
a2824256 | 0:4be500de690c | 58 | * @param mode mode of DHCP 0-softAP, 1-station, 2-both |
a2824256 | 0:4be500de690c | 59 | * @return true only if ESP8266 enables/disables DHCP successfully |
a2824256 | 0:4be500de690c | 60 | */ |
a2824256 | 0:4be500de690c | 61 | bool dhcp(bool enabled, int mode); |
a2824256 | 0:4be500de690c | 62 | |
a2824256 | 0:4be500de690c | 63 | /** |
a2824256 | 0:4be500de690c | 64 | * Connect ESP8266 to AP |
a2824256 | 0:4be500de690c | 65 | * |
a2824256 | 0:4be500de690c | 66 | * @param ap the name of the AP |
a2824256 | 0:4be500de690c | 67 | * @param passPhrase the password of AP |
a2824256 | 0:4be500de690c | 68 | * @return NSAPI_ERROR_OK only if ESP8266 is connected successfully |
a2824256 | 0:4be500de690c | 69 | */ |
a2824256 | 0:4be500de690c | 70 | nsapi_error_t connect(const char *ap, const char *passPhrase); |
a2824256 | 0:4be500de690c | 71 | |
a2824256 | 0:4be500de690c | 72 | /** |
a2824256 | 0:4be500de690c | 73 | * Disconnect ESP8266 from AP |
a2824256 | 0:4be500de690c | 74 | * |
a2824256 | 0:4be500de690c | 75 | * @return true only if ESP8266 is disconnected successfully |
a2824256 | 0:4be500de690c | 76 | */ |
a2824256 | 0:4be500de690c | 77 | bool disconnect(void); |
a2824256 | 0:4be500de690c | 78 | |
a2824256 | 0:4be500de690c | 79 | /** |
a2824256 | 0:4be500de690c | 80 | * Get the IP address of ESP8266 |
a2824256 | 0:4be500de690c | 81 | * |
a2824256 | 0:4be500de690c | 82 | * @return null-teriminated IP address or null if no IP address is assigned |
a2824256 | 0:4be500de690c | 83 | */ |
a2824256 | 0:4be500de690c | 84 | const char *getIPAddress(void); |
a2824256 | 0:4be500de690c | 85 | |
a2824256 | 0:4be500de690c | 86 | /** |
a2824256 | 0:4be500de690c | 87 | * Get the MAC address of ESP8266 |
a2824256 | 0:4be500de690c | 88 | * |
a2824256 | 0:4be500de690c | 89 | * @return null-terminated MAC address or null if no MAC address is assigned |
a2824256 | 0:4be500de690c | 90 | */ |
a2824256 | 0:4be500de690c | 91 | const char *getMACAddress(void); |
a2824256 | 0:4be500de690c | 92 | |
a2824256 | 0:4be500de690c | 93 | /** Get the local gateway |
a2824256 | 0:4be500de690c | 94 | * |
a2824256 | 0:4be500de690c | 95 | * @return Null-terminated representation of the local gateway |
a2824256 | 0:4be500de690c | 96 | * or null if no network mask has been recieved |
a2824256 | 0:4be500de690c | 97 | */ |
a2824256 | 0:4be500de690c | 98 | const char *getGateway(); |
a2824256 | 0:4be500de690c | 99 | |
a2824256 | 0:4be500de690c | 100 | /** Get the local network mask |
a2824256 | 0:4be500de690c | 101 | * |
a2824256 | 0:4be500de690c | 102 | * @return Null-terminated representation of the local network mask |
a2824256 | 0:4be500de690c | 103 | * or null if no network mask has been recieved |
a2824256 | 0:4be500de690c | 104 | */ |
a2824256 | 0:4be500de690c | 105 | const char *getNetmask(); |
a2824256 | 0:4be500de690c | 106 | |
a2824256 | 0:4be500de690c | 107 | /* Return RSSI for active connection |
a2824256 | 0:4be500de690c | 108 | * |
a2824256 | 0:4be500de690c | 109 | * @return Measured RSSI |
a2824256 | 0:4be500de690c | 110 | */ |
a2824256 | 0:4be500de690c | 111 | int8_t getRSSI(); |
a2824256 | 0:4be500de690c | 112 | |
a2824256 | 0:4be500de690c | 113 | /** Scan for available networks |
a2824256 | 0:4be500de690c | 114 | * |
a2824256 | 0:4be500de690c | 115 | * @param ap Pointer to allocated array to store discovered AP |
a2824256 | 0:4be500de690c | 116 | * @param limit Size of allocated @a res array, or 0 to only count available AP |
a2824256 | 0:4be500de690c | 117 | * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error |
a2824256 | 0:4be500de690c | 118 | * see @a nsapi_error |
a2824256 | 0:4be500de690c | 119 | */ |
a2824256 | 0:4be500de690c | 120 | int scan(WiFiAccessPoint *res, unsigned limit); |
a2824256 | 0:4be500de690c | 121 | |
a2824256 | 0:4be500de690c | 122 | /**Perform a dns query |
a2824256 | 0:4be500de690c | 123 | * |
a2824256 | 0:4be500de690c | 124 | * @param name Hostname to resolve |
a2824256 | 0:4be500de690c | 125 | * @param ip Buffer to store IP address |
a2824256 | 0:4be500de690c | 126 | * @return 0 true on success, false on failure |
a2824256 | 0:4be500de690c | 127 | */ |
a2824256 | 0:4be500de690c | 128 | bool dns_lookup(const char *name, char *ip); |
a2824256 | 0:4be500de690c | 129 | |
a2824256 | 0:4be500de690c | 130 | /** |
a2824256 | 0:4be500de690c | 131 | * Open a socketed connection |
a2824256 | 0:4be500de690c | 132 | * |
a2824256 | 0:4be500de690c | 133 | * @param type the type of socket to open "UDP" or "TCP" |
a2824256 | 0:4be500de690c | 134 | * @param id id to give the new socket, valid 0-4 |
a2824256 | 0:4be500de690c | 135 | * @param port port to open connection with |
a2824256 | 0:4be500de690c | 136 | * @param addr the IP address of the destination |
a2824256 | 0:4be500de690c | 137 | * @param port the port on the destination |
a2824256 | 0:4be500de690c | 138 | * @param local_port UDP socket's local port, zero means any |
a2824256 | 0:4be500de690c | 139 | * @return true only if socket opened successfully |
a2824256 | 0:4be500de690c | 140 | */ |
a2824256 | 0:4be500de690c | 141 | bool open_udp(int id, const char* addr, int port, int local_port = 0); |
a2824256 | 0:4be500de690c | 142 | |
a2824256 | 0:4be500de690c | 143 | /** |
a2824256 | 0:4be500de690c | 144 | * Open a socketed connection |
a2824256 | 0:4be500de690c | 145 | * |
a2824256 | 0:4be500de690c | 146 | * @param type the type of socket to open "UDP" or "TCP" |
a2824256 | 0:4be500de690c | 147 | * @param id id to give the new socket, valid 0-4 |
a2824256 | 0:4be500de690c | 148 | * @param port port to open connection with |
a2824256 | 0:4be500de690c | 149 | * @param addr the IP address of the destination |
a2824256 | 0:4be500de690c | 150 | * @param port the port on the destination |
a2824256 | 0:4be500de690c | 151 | * @param tcp_keepalive TCP connection's keep alive time, zero means disabled |
a2824256 | 0:4be500de690c | 152 | * @return true only if socket opened successfully |
a2824256 | 0:4be500de690c | 153 | */ |
a2824256 | 0:4be500de690c | 154 | bool open_tcp(int id, const char* addr, int port, int keepalive = 0); |
a2824256 | 0:4be500de690c | 155 | |
a2824256 | 0:4be500de690c | 156 | /** |
a2824256 | 0:4be500de690c | 157 | * Sends data to an open socket |
a2824256 | 0:4be500de690c | 158 | * |
a2824256 | 0:4be500de690c | 159 | * @param id id of socket to send to |
a2824256 | 0:4be500de690c | 160 | * @param data data to be sent |
a2824256 | 0:4be500de690c | 161 | * @param amount amount of data to be sent - max 1024 |
a2824256 | 0:4be500de690c | 162 | * @return NSAPI_ERROR_OK in success, negative error code in failure |
a2824256 | 0:4be500de690c | 163 | */ |
a2824256 | 0:4be500de690c | 164 | nsapi_error_t send(int id, const void *data, uint32_t amount); |
a2824256 | 0:4be500de690c | 165 | |
a2824256 | 0:4be500de690c | 166 | /** |
a2824256 | 0:4be500de690c | 167 | * Receives datagram from an open UDP socket |
a2824256 | 0:4be500de690c | 168 | * |
a2824256 | 0:4be500de690c | 169 | * @param id id to receive from |
a2824256 | 0:4be500de690c | 170 | * @param data placeholder for returned information |
a2824256 | 0:4be500de690c | 171 | * @param amount number of bytes to be received |
a2824256 | 0:4be500de690c | 172 | * @return the number of bytes received |
a2824256 | 0:4be500de690c | 173 | */ |
a2824256 | 0:4be500de690c | 174 | int32_t recv_udp(int id, void *data, uint32_t amount); |
a2824256 | 0:4be500de690c | 175 | |
a2824256 | 0:4be500de690c | 176 | /** |
a2824256 | 0:4be500de690c | 177 | * Receives stream data from an open TCP socket |
a2824256 | 0:4be500de690c | 178 | * |
a2824256 | 0:4be500de690c | 179 | * @param id id to receive from |
a2824256 | 0:4be500de690c | 180 | * @param data placeholder for returned information |
a2824256 | 0:4be500de690c | 181 | * @param amount number of bytes to be received |
a2824256 | 0:4be500de690c | 182 | * @return the number of bytes received |
a2824256 | 0:4be500de690c | 183 | */ |
a2824256 | 0:4be500de690c | 184 | int32_t recv_tcp(int id, void *data, uint32_t amount); |
a2824256 | 0:4be500de690c | 185 | |
a2824256 | 0:4be500de690c | 186 | /** |
a2824256 | 0:4be500de690c | 187 | * Closes a socket |
a2824256 | 0:4be500de690c | 188 | * |
a2824256 | 0:4be500de690c | 189 | * @param id id of socket to close, valid only 0-4 |
a2824256 | 0:4be500de690c | 190 | * @return true only if socket is closed successfully |
a2824256 | 0:4be500de690c | 191 | */ |
a2824256 | 0:4be500de690c | 192 | bool close(int id); |
a2824256 | 0:4be500de690c | 193 | |
a2824256 | 0:4be500de690c | 194 | /** |
a2824256 | 0:4be500de690c | 195 | * Allows timeout to be changed between commands |
a2824256 | 0:4be500de690c | 196 | * |
a2824256 | 0:4be500de690c | 197 | * @param timeout_ms timeout of the connection |
a2824256 | 0:4be500de690c | 198 | */ |
a2824256 | 0:4be500de690c | 199 | void setTimeout(uint32_t timeout_ms); |
a2824256 | 0:4be500de690c | 200 | |
a2824256 | 0:4be500de690c | 201 | /** |
a2824256 | 0:4be500de690c | 202 | * Checks if data is available |
a2824256 | 0:4be500de690c | 203 | */ |
a2824256 | 0:4be500de690c | 204 | bool readable(); |
a2824256 | 0:4be500de690c | 205 | |
a2824256 | 0:4be500de690c | 206 | /** |
a2824256 | 0:4be500de690c | 207 | * Checks if data can be written |
a2824256 | 0:4be500de690c | 208 | */ |
a2824256 | 0:4be500de690c | 209 | bool writeable(); |
a2824256 | 0:4be500de690c | 210 | |
a2824256 | 0:4be500de690c | 211 | /** |
a2824256 | 0:4be500de690c | 212 | * Attach a function to call whenever network state has changed |
a2824256 | 0:4be500de690c | 213 | * |
a2824256 | 0:4be500de690c | 214 | * @param func A pointer to a void function, or 0 to set as none |
a2824256 | 0:4be500de690c | 215 | */ |
a2824256 | 0:4be500de690c | 216 | void attach(Callback<void()> func); |
a2824256 | 0:4be500de690c | 217 | |
a2824256 | 0:4be500de690c | 218 | /** |
a2824256 | 0:4be500de690c | 219 | * Attach a function to call whenever network state has changed |
a2824256 | 0:4be500de690c | 220 | * |
a2824256 | 0:4be500de690c | 221 | * @param obj pointer to the object to call the member function on |
a2824256 | 0:4be500de690c | 222 | * @param method pointer to the member function to call |
a2824256 | 0:4be500de690c | 223 | */ |
a2824256 | 0:4be500de690c | 224 | template <typename T, typename M> |
a2824256 | 0:4be500de690c | 225 | void attach(T *obj, M method) { |
a2824256 | 0:4be500de690c | 226 | attach(Callback<void()>(obj, method)); |
a2824256 | 0:4be500de690c | 227 | } |
a2824256 | 0:4be500de690c | 228 | |
a2824256 | 0:4be500de690c | 229 | /** |
a2824256 | 0:4be500de690c | 230 | * Read default Wifi mode from flash |
a2824256 | 0:4be500de690c | 231 | * |
a2824256 | 0:4be500de690c | 232 | * return Station, SoftAP or SoftAP+Station - 0 on failure |
a2824256 | 0:4be500de690c | 233 | */ |
a2824256 | 0:4be500de690c | 234 | int8_t get_default_wifi_mode(); |
a2824256 | 0:4be500de690c | 235 | |
a2824256 | 0:4be500de690c | 236 | /** |
a2824256 | 0:4be500de690c | 237 | * Write default Wifi mode to flash |
a2824256 | 0:4be500de690c | 238 | */ |
a2824256 | 0:4be500de690c | 239 | bool set_default_wifi_mode(const int8_t mode); |
a2824256 | 0:4be500de690c | 240 | |
a2824256 | 0:4be500de690c | 241 | static const int8_t WIFIMODE_STATION = 1; |
a2824256 | 0:4be500de690c | 242 | static const int8_t WIFIMODE_SOFTAP = 2; |
a2824256 | 0:4be500de690c | 243 | static const int8_t WIFIMODE_STATION_SOFTAP = 3; |
a2824256 | 0:4be500de690c | 244 | static const int8_t SOCKET_COUNT = 5; |
a2824256 | 0:4be500de690c | 245 | |
a2824256 | 0:4be500de690c | 246 | private: |
a2824256 | 0:4be500de690c | 247 | UARTSerial _serial; |
a2824256 | 0:4be500de690c | 248 | ATCmdParser _parser; |
a2824256 | 0:4be500de690c | 249 | Mutex _smutex; // Protect serial port access |
a2824256 | 0:4be500de690c | 250 | |
a2824256 | 0:4be500de690c | 251 | struct packet { |
a2824256 | 0:4be500de690c | 252 | struct packet *next; |
a2824256 | 0:4be500de690c | 253 | int id; |
a2824256 | 0:4be500de690c | 254 | uint32_t len; |
a2824256 | 0:4be500de690c | 255 | // data follows |
a2824256 | 0:4be500de690c | 256 | } *_packets, **_packets_end; |
a2824256 | 0:4be500de690c | 257 | void _packet_handler(); |
a2824256 | 0:4be500de690c | 258 | void _connect_error_handler(); |
a2824256 | 0:4be500de690c | 259 | bool recv_ap(nsapi_wifi_ap_t *ap); |
a2824256 | 0:4be500de690c | 260 | void _oob_socket0_closed_handler(); |
a2824256 | 0:4be500de690c | 261 | void _oob_socket1_closed_handler(); |
a2824256 | 0:4be500de690c | 262 | void _oob_socket2_closed_handler(); |
a2824256 | 0:4be500de690c | 263 | void _oob_socket3_closed_handler(); |
a2824256 | 0:4be500de690c | 264 | void _oob_socket4_closed_handler(); |
a2824256 | 0:4be500de690c | 265 | |
a2824256 | 0:4be500de690c | 266 | |
a2824256 | 0:4be500de690c | 267 | char _ip_buffer[16]; |
a2824256 | 0:4be500de690c | 268 | char _gateway_buffer[16]; |
a2824256 | 0:4be500de690c | 269 | char _netmask_buffer[16]; |
a2824256 | 0:4be500de690c | 270 | char _mac_buffer[18]; |
a2824256 | 0:4be500de690c | 271 | |
a2824256 | 0:4be500de690c | 272 | int _connect_error; |
a2824256 | 0:4be500de690c | 273 | bool _fail; |
a2824256 | 0:4be500de690c | 274 | int _socket_open[SOCKET_COUNT]; |
a2824256 | 0:4be500de690c | 275 | }; |
a2824256 | 0:4be500de690c | 276 | |
a2824256 | 0:4be500de690c | 277 | #endif |