my customized lib
esp8266-driver/ESP8266/ESP8266.h@0:8094b249013c, 2017-11-26 (annotated)
- Committer:
- DuyLionTran
- Date:
- Sun Nov 26 15:08:14 2017 +0000
- Revision:
- 0:8094b249013c
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DuyLionTran | 0:8094b249013c | 1 | /* ESP8266Interface Example |
DuyLionTran | 0:8094b249013c | 2 | * Copyright (c) 2015 ARM Limited |
DuyLionTran | 0:8094b249013c | 3 | * |
DuyLionTran | 0:8094b249013c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
DuyLionTran | 0:8094b249013c | 5 | * you may not use this file except in compliance with the License. |
DuyLionTran | 0:8094b249013c | 6 | * You may obtain a copy of the License at |
DuyLionTran | 0:8094b249013c | 7 | * |
DuyLionTran | 0:8094b249013c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
DuyLionTran | 0:8094b249013c | 9 | * |
DuyLionTran | 0:8094b249013c | 10 | * Unless required by applicable law or agreed to in writing, software |
DuyLionTran | 0:8094b249013c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
DuyLionTran | 0:8094b249013c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
DuyLionTran | 0:8094b249013c | 13 | * See the License for the specific language governing permissions and |
DuyLionTran | 0:8094b249013c | 14 | * limitations under the License. |
DuyLionTran | 0:8094b249013c | 15 | */ |
DuyLionTran | 0:8094b249013c | 16 | |
DuyLionTran | 0:8094b249013c | 17 | #ifndef ESP8266_H |
DuyLionTran | 0:8094b249013c | 18 | #define ESP8266_H |
DuyLionTran | 0:8094b249013c | 19 | |
DuyLionTran | 0:8094b249013c | 20 | #include "ATParser.h" |
DuyLionTran | 0:8094b249013c | 21 | |
DuyLionTran | 0:8094b249013c | 22 | /** ESP8266Interface class. |
DuyLionTran | 0:8094b249013c | 23 | This is an interface to a ESP8266 radio. |
DuyLionTran | 0:8094b249013c | 24 | */ |
DuyLionTran | 0:8094b249013c | 25 | class ESP8266 |
DuyLionTran | 0:8094b249013c | 26 | { |
DuyLionTran | 0:8094b249013c | 27 | public: |
DuyLionTran | 0:8094b249013c | 28 | ESP8266(PinName tx, PinName rx, bool debug=false); |
DuyLionTran | 0:8094b249013c | 29 | |
DuyLionTran | 0:8094b249013c | 30 | /** |
DuyLionTran | 0:8094b249013c | 31 | * Startup the ESP8266 |
DuyLionTran | 0:8094b249013c | 32 | * |
DuyLionTran | 0:8094b249013c | 33 | * @param mode mode of WIFI 1-client, 2-host, 3-both |
DuyLionTran | 0:8094b249013c | 34 | * @return true only if ESP8266 was setup correctly |
DuyLionTran | 0:8094b249013c | 35 | */ |
DuyLionTran | 0:8094b249013c | 36 | bool startup(int mode); |
DuyLionTran | 0:8094b249013c | 37 | |
DuyLionTran | 0:8094b249013c | 38 | /** |
DuyLionTran | 0:8094b249013c | 39 | * Reset ESP8266 |
DuyLionTran | 0:8094b249013c | 40 | * |
DuyLionTran | 0:8094b249013c | 41 | * @return true only if ESP8266 resets successfully |
DuyLionTran | 0:8094b249013c | 42 | */ |
DuyLionTran | 0:8094b249013c | 43 | bool reset(void); |
DuyLionTran | 0:8094b249013c | 44 | |
DuyLionTran | 0:8094b249013c | 45 | /** |
DuyLionTran | 0:8094b249013c | 46 | * Enable/Disable DHCP |
DuyLionTran | 0:8094b249013c | 47 | * |
DuyLionTran | 0:8094b249013c | 48 | * @param enabled DHCP enabled when true |
DuyLionTran | 0:8094b249013c | 49 | * @param mode mode of DHCP 0-softAP, 1-station, 2-both |
DuyLionTran | 0:8094b249013c | 50 | * @return true only if ESP8266 enables/disables DHCP successfully |
DuyLionTran | 0:8094b249013c | 51 | */ |
DuyLionTran | 0:8094b249013c | 52 | bool dhcp(bool enabled, int mode); |
DuyLionTran | 0:8094b249013c | 53 | |
DuyLionTran | 0:8094b249013c | 54 | /** |
DuyLionTran | 0:8094b249013c | 55 | * Connect ESP8266 to AP |
DuyLionTran | 0:8094b249013c | 56 | * |
DuyLionTran | 0:8094b249013c | 57 | * @param ap the name of the AP |
DuyLionTran | 0:8094b249013c | 58 | * @param passPhrase the password of AP |
DuyLionTran | 0:8094b249013c | 59 | * @return true only if ESP8266 is connected successfully |
DuyLionTran | 0:8094b249013c | 60 | */ |
DuyLionTran | 0:8094b249013c | 61 | bool connect(const char *ap, const char *passPhrase); |
DuyLionTran | 0:8094b249013c | 62 | |
DuyLionTran | 0:8094b249013c | 63 | /** |
DuyLionTran | 0:8094b249013c | 64 | * Disconnect ESP8266 from AP |
DuyLionTran | 0:8094b249013c | 65 | * |
DuyLionTran | 0:8094b249013c | 66 | * @return true only if ESP8266 is disconnected successfully |
DuyLionTran | 0:8094b249013c | 67 | */ |
DuyLionTran | 0:8094b249013c | 68 | bool disconnect(void); |
DuyLionTran | 0:8094b249013c | 69 | |
DuyLionTran | 0:8094b249013c | 70 | /** |
DuyLionTran | 0:8094b249013c | 71 | * Get the IP address of ESP8266 |
DuyLionTran | 0:8094b249013c | 72 | * |
DuyLionTran | 0:8094b249013c | 73 | * @return null-teriminated IP address or null if no IP address is assigned |
DuyLionTran | 0:8094b249013c | 74 | */ |
DuyLionTran | 0:8094b249013c | 75 | const char *getIPAddress(void); |
DuyLionTran | 0:8094b249013c | 76 | |
DuyLionTran | 0:8094b249013c | 77 | /** |
DuyLionTran | 0:8094b249013c | 78 | * Get the MAC address of ESP8266 |
DuyLionTran | 0:8094b249013c | 79 | * |
DuyLionTran | 0:8094b249013c | 80 | * @return null-terminated MAC address or null if no MAC address is assigned |
DuyLionTran | 0:8094b249013c | 81 | */ |
DuyLionTran | 0:8094b249013c | 82 | const char *getMACAddress(void); |
DuyLionTran | 0:8094b249013c | 83 | |
DuyLionTran | 0:8094b249013c | 84 | /** Get the local gateway |
DuyLionTran | 0:8094b249013c | 85 | * |
DuyLionTran | 0:8094b249013c | 86 | * @return Null-terminated representation of the local gateway |
DuyLionTran | 0:8094b249013c | 87 | * or null if no network mask has been recieved |
DuyLionTran | 0:8094b249013c | 88 | */ |
DuyLionTran | 0:8094b249013c | 89 | const char *getGateway(); |
DuyLionTran | 0:8094b249013c | 90 | |
DuyLionTran | 0:8094b249013c | 91 | /** Get the local network mask |
DuyLionTran | 0:8094b249013c | 92 | * |
DuyLionTran | 0:8094b249013c | 93 | * @return Null-terminated representation of the local network mask |
DuyLionTran | 0:8094b249013c | 94 | * or null if no network mask has been recieved |
DuyLionTran | 0:8094b249013c | 95 | */ |
DuyLionTran | 0:8094b249013c | 96 | const char *getNetmask(); |
DuyLionTran | 0:8094b249013c | 97 | |
DuyLionTran | 0:8094b249013c | 98 | /* Return RSSI for active connection |
DuyLionTran | 0:8094b249013c | 99 | * |
DuyLionTran | 0:8094b249013c | 100 | * @return Measured RSSI |
DuyLionTran | 0:8094b249013c | 101 | */ |
DuyLionTran | 0:8094b249013c | 102 | int8_t getRSSI(); |
DuyLionTran | 0:8094b249013c | 103 | |
DuyLionTran | 0:8094b249013c | 104 | /** |
DuyLionTran | 0:8094b249013c | 105 | * Check if ESP8266 is conenected |
DuyLionTran | 0:8094b249013c | 106 | * |
DuyLionTran | 0:8094b249013c | 107 | * @return true only if the chip has an IP address |
DuyLionTran | 0:8094b249013c | 108 | */ |
DuyLionTran | 0:8094b249013c | 109 | bool isConnected(void); |
DuyLionTran | 0:8094b249013c | 110 | |
DuyLionTran | 0:8094b249013c | 111 | /** Scan for available networks |
DuyLionTran | 0:8094b249013c | 112 | * |
DuyLionTran | 0:8094b249013c | 113 | * @param ap Pointer to allocated array to store discovered AP |
DuyLionTran | 0:8094b249013c | 114 | * @param limit Size of allocated @a res array, or 0 to only count available AP |
DuyLionTran | 0:8094b249013c | 115 | * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error |
DuyLionTran | 0:8094b249013c | 116 | * see @a nsapi_error |
DuyLionTran | 0:8094b249013c | 117 | */ |
DuyLionTran | 0:8094b249013c | 118 | int scan(WiFiAccessPoint *res, unsigned limit); |
DuyLionTran | 0:8094b249013c | 119 | |
DuyLionTran | 0:8094b249013c | 120 | /** |
DuyLionTran | 0:8094b249013c | 121 | * Open a socketed connection |
DuyLionTran | 0:8094b249013c | 122 | * |
DuyLionTran | 0:8094b249013c | 123 | * @param type the type of socket to open "UDP" or "TCP" |
DuyLionTran | 0:8094b249013c | 124 | * @param id id to give the new socket, valid 0-4 |
DuyLionTran | 0:8094b249013c | 125 | * @param port port to open connection with |
DuyLionTran | 0:8094b249013c | 126 | * @param addr the IP address of the destination |
DuyLionTran | 0:8094b249013c | 127 | * @return true only if socket opened successfully |
DuyLionTran | 0:8094b249013c | 128 | */ |
DuyLionTran | 0:8094b249013c | 129 | bool open(const char *type, int id, const char* addr, int port); |
DuyLionTran | 0:8094b249013c | 130 | |
DuyLionTran | 0:8094b249013c | 131 | /** |
DuyLionTran | 0:8094b249013c | 132 | * Sends data to an open socket |
DuyLionTran | 0:8094b249013c | 133 | * |
DuyLionTran | 0:8094b249013c | 134 | * @param id id of socket to send to |
DuyLionTran | 0:8094b249013c | 135 | * @param data data to be sent |
DuyLionTran | 0:8094b249013c | 136 | * @param amount amount of data to be sent - max 1024 |
DuyLionTran | 0:8094b249013c | 137 | * @return true only if data sent successfully |
DuyLionTran | 0:8094b249013c | 138 | */ |
DuyLionTran | 0:8094b249013c | 139 | bool send(int id, const void *data, uint32_t amount); |
DuyLionTran | 0:8094b249013c | 140 | |
DuyLionTran | 0:8094b249013c | 141 | /** |
DuyLionTran | 0:8094b249013c | 142 | * Receives data from an open socket |
DuyLionTran | 0:8094b249013c | 143 | * |
DuyLionTran | 0:8094b249013c | 144 | * @param id id to receive from |
DuyLionTran | 0:8094b249013c | 145 | * @param data placeholder for returned information |
DuyLionTran | 0:8094b249013c | 146 | * @param amount number of bytes to be received |
DuyLionTran | 0:8094b249013c | 147 | * @return the number of bytes received |
DuyLionTran | 0:8094b249013c | 148 | */ |
DuyLionTran | 0:8094b249013c | 149 | int32_t recv(int id, void *data, uint32_t amount); |
DuyLionTran | 0:8094b249013c | 150 | |
DuyLionTran | 0:8094b249013c | 151 | /** |
DuyLionTran | 0:8094b249013c | 152 | * Closes a socket |
DuyLionTran | 0:8094b249013c | 153 | * |
DuyLionTran | 0:8094b249013c | 154 | * @param id id of socket to close, valid only 0-4 |
DuyLionTran | 0:8094b249013c | 155 | * @return true only if socket is closed successfully |
DuyLionTran | 0:8094b249013c | 156 | */ |
DuyLionTran | 0:8094b249013c | 157 | bool close(int id); |
DuyLionTran | 0:8094b249013c | 158 | |
DuyLionTran | 0:8094b249013c | 159 | /** |
DuyLionTran | 0:8094b249013c | 160 | * Allows timeout to be changed between commands |
DuyLionTran | 0:8094b249013c | 161 | * |
DuyLionTran | 0:8094b249013c | 162 | * @param timeout_ms timeout of the connection |
DuyLionTran | 0:8094b249013c | 163 | */ |
DuyLionTran | 0:8094b249013c | 164 | void setTimeout(uint32_t timeout_ms); |
DuyLionTran | 0:8094b249013c | 165 | |
DuyLionTran | 0:8094b249013c | 166 | /** |
DuyLionTran | 0:8094b249013c | 167 | * Checks if data is available |
DuyLionTran | 0:8094b249013c | 168 | */ |
DuyLionTran | 0:8094b249013c | 169 | bool readable(); |
DuyLionTran | 0:8094b249013c | 170 | |
DuyLionTran | 0:8094b249013c | 171 | /** |
DuyLionTran | 0:8094b249013c | 172 | * Checks if data can be written |
DuyLionTran | 0:8094b249013c | 173 | */ |
DuyLionTran | 0:8094b249013c | 174 | bool writeable(); |
DuyLionTran | 0:8094b249013c | 175 | |
DuyLionTran | 0:8094b249013c | 176 | /** |
DuyLionTran | 0:8094b249013c | 177 | * Attach a function to call whenever network state has changed |
DuyLionTran | 0:8094b249013c | 178 | * |
DuyLionTran | 0:8094b249013c | 179 | * @param func A pointer to a void function, or 0 to set as none |
DuyLionTran | 0:8094b249013c | 180 | */ |
DuyLionTran | 0:8094b249013c | 181 | void attach(Callback<void()> func); |
DuyLionTran | 0:8094b249013c | 182 | |
DuyLionTran | 0:8094b249013c | 183 | /** |
DuyLionTran | 0:8094b249013c | 184 | * Attach a function to call whenever network state has changed |
DuyLionTran | 0:8094b249013c | 185 | * |
DuyLionTran | 0:8094b249013c | 186 | * @param obj pointer to the object to call the member function on |
DuyLionTran | 0:8094b249013c | 187 | * @param method pointer to the member function to call |
DuyLionTran | 0:8094b249013c | 188 | */ |
DuyLionTran | 0:8094b249013c | 189 | template <typename T, typename M> |
DuyLionTran | 0:8094b249013c | 190 | void attach(T *obj, M method) { |
DuyLionTran | 0:8094b249013c | 191 | attach(Callback<void()>(obj, method)); |
DuyLionTran | 0:8094b249013c | 192 | } |
DuyLionTran | 0:8094b249013c | 193 | |
DuyLionTran | 0:8094b249013c | 194 | private: |
DuyLionTran | 0:8094b249013c | 195 | BufferedSerial _serial; |
DuyLionTran | 0:8094b249013c | 196 | ATParser _parser; |
DuyLionTran | 0:8094b249013c | 197 | |
DuyLionTran | 0:8094b249013c | 198 | struct packet { |
DuyLionTran | 0:8094b249013c | 199 | struct packet *next; |
DuyLionTran | 0:8094b249013c | 200 | int id; |
DuyLionTran | 0:8094b249013c | 201 | uint32_t len; |
DuyLionTran | 0:8094b249013c | 202 | // data follows |
DuyLionTran | 0:8094b249013c | 203 | } *_packets, **_packets_end; |
DuyLionTran | 0:8094b249013c | 204 | void _packet_handler(); |
DuyLionTran | 0:8094b249013c | 205 | bool recv_ap(nsapi_wifi_ap_t *ap); |
DuyLionTran | 0:8094b249013c | 206 | |
DuyLionTran | 0:8094b249013c | 207 | char _ip_buffer[16]; |
DuyLionTran | 0:8094b249013c | 208 | char _gateway_buffer[16]; |
DuyLionTran | 0:8094b249013c | 209 | char _netmask_buffer[16]; |
DuyLionTran | 0:8094b249013c | 210 | char _mac_buffer[18]; |
DuyLionTran | 0:8094b249013c | 211 | }; |
DuyLionTran | 0:8094b249013c | 212 | |
DuyLionTran | 0:8094b249013c | 213 | #endif |