Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
esp32-driver/ESP32/ESP32.h@0:8f8e8f3cbd1c, 2018-06-21 (annotated)
- Committer:
- mayur098
- Date:
- Thu Jun 21 17:50:21 2018 +0000
- Revision:
- 0:8f8e8f3cbd1c
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mayur098 | 0:8f8e8f3cbd1c | 1 | /* ESP32Interface Example |
mayur098 | 0:8f8e8f3cbd1c | 2 | * Copyright (c) 2015 ARM Limited |
mayur098 | 0:8f8e8f3cbd1c | 3 | * Copyright (c) 2017 Renesas Electronics Corporation |
mayur098 | 0:8f8e8f3cbd1c | 4 | * |
mayur098 | 0:8f8e8f3cbd1c | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mayur098 | 0:8f8e8f3cbd1c | 6 | * you may not use this file except in compliance with the License. |
mayur098 | 0:8f8e8f3cbd1c | 7 | * You may obtain a copy of the License at |
mayur098 | 0:8f8e8f3cbd1c | 8 | * |
mayur098 | 0:8f8e8f3cbd1c | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
mayur098 | 0:8f8e8f3cbd1c | 10 | * |
mayur098 | 0:8f8e8f3cbd1c | 11 | * Unless required by applicable law or agreed to in writing, software |
mayur098 | 0:8f8e8f3cbd1c | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
mayur098 | 0:8f8e8f3cbd1c | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mayur098 | 0:8f8e8f3cbd1c | 14 | * See the License for the specific language governing permissions and |
mayur098 | 0:8f8e8f3cbd1c | 15 | * limitations under the License. |
mayur098 | 0:8f8e8f3cbd1c | 16 | */ |
mayur098 | 0:8f8e8f3cbd1c | 17 | |
mayur098 | 0:8f8e8f3cbd1c | 18 | #ifndef ESP32_H |
mayur098 | 0:8f8e8f3cbd1c | 19 | #define ESP32_H |
mayur098 | 0:8f8e8f3cbd1c | 20 | |
mayur098 | 0:8f8e8f3cbd1c | 21 | #include "ATParser_os.h" |
mayur098 | 0:8f8e8f3cbd1c | 22 | |
mayur098 | 0:8f8e8f3cbd1c | 23 | #define ESP32_SOCKET_COUNT 5 |
mayur098 | 0:8f8e8f3cbd1c | 24 | |
mayur098 | 0:8f8e8f3cbd1c | 25 | /** ESP32Interface class. |
mayur098 | 0:8f8e8f3cbd1c | 26 | This is an interface to a ESP32 radio. |
mayur098 | 0:8f8e8f3cbd1c | 27 | */ |
mayur098 | 0:8f8e8f3cbd1c | 28 | class ESP32 |
mayur098 | 0:8f8e8f3cbd1c | 29 | { |
mayur098 | 0:8f8e8f3cbd1c | 30 | public: |
mayur098 | 0:8f8e8f3cbd1c | 31 | /** |
mayur098 | 0:8f8e8f3cbd1c | 32 | * Static method to create or retrieve the single ESP32 instance |
mayur098 | 0:8f8e8f3cbd1c | 33 | */ |
mayur098 | 0:8f8e8f3cbd1c | 34 | static ESP32 * getESP32Inst(PinName en, PinName io0, PinName tx, PinName rx, bool debug, |
mayur098 | 0:8f8e8f3cbd1c | 35 | PinName rts, PinName cts, int baudrate); |
mayur098 | 0:8f8e8f3cbd1c | 36 | |
mayur098 | 0:8f8e8f3cbd1c | 37 | ESP32(PinName en, PinName io0, PinName tx, PinName rx, bool debug, |
mayur098 | 0:8f8e8f3cbd1c | 38 | PinName rts, PinName cts, int baudrate); |
mayur098 | 0:8f8e8f3cbd1c | 39 | |
mayur098 | 0:8f8e8f3cbd1c | 40 | /** |
mayur098 | 0:8f8e8f3cbd1c | 41 | * Sets the Wi-Fi Mode |
mayur098 | 0:8f8e8f3cbd1c | 42 | * |
mayur098 | 0:8f8e8f3cbd1c | 43 | * @param mode mode of WIFI 1-client, 2-host, 3-both |
mayur098 | 0:8f8e8f3cbd1c | 44 | * @return true only if ESP32 was setup correctly |
mayur098 | 0:8f8e8f3cbd1c | 45 | */ |
mayur098 | 0:8f8e8f3cbd1c | 46 | bool set_mode(int mode); |
mayur098 | 0:8f8e8f3cbd1c | 47 | |
mayur098 | 0:8f8e8f3cbd1c | 48 | /** |
mayur098 | 0:8f8e8f3cbd1c | 49 | * Enable/Disable DHCP |
mayur098 | 0:8f8e8f3cbd1c | 50 | * |
mayur098 | 0:8f8e8f3cbd1c | 51 | * @param enabled DHCP enabled when true |
mayur098 | 0:8f8e8f3cbd1c | 52 | * @param mode mode of DHCP 0-softAP, 1-station, 2-both |
mayur098 | 0:8f8e8f3cbd1c | 53 | * @return true only if ESP32 enables/disables DHCP successfully |
mayur098 | 0:8f8e8f3cbd1c | 54 | */ |
mayur098 | 0:8f8e8f3cbd1c | 55 | bool dhcp(bool enabled, int mode); |
mayur098 | 0:8f8e8f3cbd1c | 56 | |
mayur098 | 0:8f8e8f3cbd1c | 57 | /** |
mayur098 | 0:8f8e8f3cbd1c | 58 | * Connect ESP32 to AP |
mayur098 | 0:8f8e8f3cbd1c | 59 | * |
mayur098 | 0:8f8e8f3cbd1c | 60 | * @param ap the name of the AP |
mayur098 | 0:8f8e8f3cbd1c | 61 | * @param passPhrase the password of AP |
mayur098 | 0:8f8e8f3cbd1c | 62 | * @return true only if ESP32 is connected successfully |
mayur098 | 0:8f8e8f3cbd1c | 63 | */ |
mayur098 | 0:8f8e8f3cbd1c | 64 | bool connect(const char *ap, const char *passPhrase); |
mayur098 | 0:8f8e8f3cbd1c | 65 | |
mayur098 | 0:8f8e8f3cbd1c | 66 | /** |
mayur098 | 0:8f8e8f3cbd1c | 67 | * Disconnect ESP32 from AP |
mayur098 | 0:8f8e8f3cbd1c | 68 | * |
mayur098 | 0:8f8e8f3cbd1c | 69 | * @return true only if ESP32 is disconnected successfully |
mayur098 | 0:8f8e8f3cbd1c | 70 | */ |
mayur098 | 0:8f8e8f3cbd1c | 71 | bool disconnect(void); |
mayur098 | 0:8f8e8f3cbd1c | 72 | |
mayur098 | 0:8f8e8f3cbd1c | 73 | /** |
mayur098 | 0:8f8e8f3cbd1c | 74 | * Get the IP address of ESP32 |
mayur098 | 0:8f8e8f3cbd1c | 75 | * |
mayur098 | 0:8f8e8f3cbd1c | 76 | * @return null-teriminated IP address or null if no IP address is assigned |
mayur098 | 0:8f8e8f3cbd1c | 77 | */ |
mayur098 | 0:8f8e8f3cbd1c | 78 | const char *getIPAddress(void); |
mayur098 | 0:8f8e8f3cbd1c | 79 | const char *getIPAddress_ap(void); |
mayur098 | 0:8f8e8f3cbd1c | 80 | |
mayur098 | 0:8f8e8f3cbd1c | 81 | /** |
mayur098 | 0:8f8e8f3cbd1c | 82 | * Get the MAC address of ESP32 |
mayur098 | 0:8f8e8f3cbd1c | 83 | * |
mayur098 | 0:8f8e8f3cbd1c | 84 | * @return null-terminated MAC address or null if no MAC address is assigned |
mayur098 | 0:8f8e8f3cbd1c | 85 | */ |
mayur098 | 0:8f8e8f3cbd1c | 86 | const char *getMACAddress(void); |
mayur098 | 0:8f8e8f3cbd1c | 87 | const char *getMACAddress_ap(void); |
mayur098 | 0:8f8e8f3cbd1c | 88 | |
mayur098 | 0:8f8e8f3cbd1c | 89 | /** Get the local gateway |
mayur098 | 0:8f8e8f3cbd1c | 90 | * |
mayur098 | 0:8f8e8f3cbd1c | 91 | * @return Null-terminated representation of the local gateway |
mayur098 | 0:8f8e8f3cbd1c | 92 | * or null if no network mask has been recieved |
mayur098 | 0:8f8e8f3cbd1c | 93 | */ |
mayur098 | 0:8f8e8f3cbd1c | 94 | const char *getGateway(); |
mayur098 | 0:8f8e8f3cbd1c | 95 | const char *getGateway_ap(); |
mayur098 | 0:8f8e8f3cbd1c | 96 | |
mayur098 | 0:8f8e8f3cbd1c | 97 | /** Get the local network mask |
mayur098 | 0:8f8e8f3cbd1c | 98 | * |
mayur098 | 0:8f8e8f3cbd1c | 99 | * @return Null-terminated representation of the local network mask |
mayur098 | 0:8f8e8f3cbd1c | 100 | * or null if no network mask has been recieved |
mayur098 | 0:8f8e8f3cbd1c | 101 | */ |
mayur098 | 0:8f8e8f3cbd1c | 102 | const char *getNetmask(); |
mayur098 | 0:8f8e8f3cbd1c | 103 | const char *getNetmask_ap(); |
mayur098 | 0:8f8e8f3cbd1c | 104 | |
mayur098 | 0:8f8e8f3cbd1c | 105 | /* Return RSSI for active connection |
mayur098 | 0:8f8e8f3cbd1c | 106 | * |
mayur098 | 0:8f8e8f3cbd1c | 107 | * @return Measured RSSI |
mayur098 | 0:8f8e8f3cbd1c | 108 | */ |
mayur098 | 0:8f8e8f3cbd1c | 109 | int8_t getRSSI(); |
mayur098 | 0:8f8e8f3cbd1c | 110 | |
mayur098 | 0:8f8e8f3cbd1c | 111 | /** |
mayur098 | 0:8f8e8f3cbd1c | 112 | * Check if ESP32 is conenected |
mayur098 | 0:8f8e8f3cbd1c | 113 | * |
mayur098 | 0:8f8e8f3cbd1c | 114 | * @return true only if the chip has an IP address |
mayur098 | 0:8f8e8f3cbd1c | 115 | */ |
mayur098 | 0:8f8e8f3cbd1c | 116 | bool isConnected(void); |
mayur098 | 0:8f8e8f3cbd1c | 117 | |
mayur098 | 0:8f8e8f3cbd1c | 118 | /** Scan for available networks |
mayur098 | 0:8f8e8f3cbd1c | 119 | * |
mayur098 | 0:8f8e8f3cbd1c | 120 | * @param ap Pointer to allocated array to store discovered AP |
mayur098 | 0:8f8e8f3cbd1c | 121 | * @param limit Size of allocated @a res array, or 0 to only count available AP |
mayur098 | 0:8f8e8f3cbd1c | 122 | * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error |
mayur098 | 0:8f8e8f3cbd1c | 123 | * see @a nsapi_error |
mayur098 | 0:8f8e8f3cbd1c | 124 | */ |
mayur098 | 0:8f8e8f3cbd1c | 125 | int scan(WiFiAccessPoint *res, unsigned limit); |
mayur098 | 0:8f8e8f3cbd1c | 126 | |
mayur098 | 0:8f8e8f3cbd1c | 127 | /** |
mayur098 | 0:8f8e8f3cbd1c | 128 | * Open a socketed connection |
mayur098 | 0:8f8e8f3cbd1c | 129 | * |
mayur098 | 0:8f8e8f3cbd1c | 130 | * @param type the type of socket to open "UDP" or "TCP" |
mayur098 | 0:8f8e8f3cbd1c | 131 | * @param id id to give the new socket, valid 0-4 |
mayur098 | 0:8f8e8f3cbd1c | 132 | * @param port port to open connection with |
mayur098 | 0:8f8e8f3cbd1c | 133 | * @param addr the IP address of the destination |
mayur098 | 0:8f8e8f3cbd1c | 134 | * @return true only if socket opened successfully |
mayur098 | 0:8f8e8f3cbd1c | 135 | */ |
mayur098 | 0:8f8e8f3cbd1c | 136 | bool open(const char *type, int id, const char* addr, int port); |
mayur098 | 0:8f8e8f3cbd1c | 137 | |
mayur098 | 0:8f8e8f3cbd1c | 138 | /** |
mayur098 | 0:8f8e8f3cbd1c | 139 | * Sends data to an open socket |
mayur098 | 0:8f8e8f3cbd1c | 140 | * |
mayur098 | 0:8f8e8f3cbd1c | 141 | * @param id id of socket to send to |
mayur098 | 0:8f8e8f3cbd1c | 142 | * @param data data to be sent |
mayur098 | 0:8f8e8f3cbd1c | 143 | * @param amount amount of data to be sent - max 1024 |
mayur098 | 0:8f8e8f3cbd1c | 144 | * @return true only if data sent successfully |
mayur098 | 0:8f8e8f3cbd1c | 145 | */ |
mayur098 | 0:8f8e8f3cbd1c | 146 | bool send(int id, const void *data, uint32_t amount); |
mayur098 | 0:8f8e8f3cbd1c | 147 | |
mayur098 | 0:8f8e8f3cbd1c | 148 | /** |
mayur098 | 0:8f8e8f3cbd1c | 149 | * Receives data from an open socket |
mayur098 | 0:8f8e8f3cbd1c | 150 | * |
mayur098 | 0:8f8e8f3cbd1c | 151 | * @param id id to receive from |
mayur098 | 0:8f8e8f3cbd1c | 152 | * @param data placeholder for returned information |
mayur098 | 0:8f8e8f3cbd1c | 153 | * @param amount number of bytes to be received |
mayur098 | 0:8f8e8f3cbd1c | 154 | * @return the number of bytes received |
mayur098 | 0:8f8e8f3cbd1c | 155 | */ |
mayur098 | 0:8f8e8f3cbd1c | 156 | int32_t recv(int id, void *data, uint32_t amount); |
mayur098 | 0:8f8e8f3cbd1c | 157 | |
mayur098 | 0:8f8e8f3cbd1c | 158 | /** |
mayur098 | 0:8f8e8f3cbd1c | 159 | * Closes a socket |
mayur098 | 0:8f8e8f3cbd1c | 160 | * |
mayur098 | 0:8f8e8f3cbd1c | 161 | * @param id id of socket to close, valid only 0-4 |
mayur098 | 0:8f8e8f3cbd1c | 162 | * @param wait_close |
mayur098 | 0:8f8e8f3cbd1c | 163 | * @return true only if socket is closed successfully |
mayur098 | 0:8f8e8f3cbd1c | 164 | */ |
mayur098 | 0:8f8e8f3cbd1c | 165 | bool close(int id, bool wait_close); |
mayur098 | 0:8f8e8f3cbd1c | 166 | |
mayur098 | 0:8f8e8f3cbd1c | 167 | /** |
mayur098 | 0:8f8e8f3cbd1c | 168 | * Allows timeout to be changed between commands |
mayur098 | 0:8f8e8f3cbd1c | 169 | * |
mayur098 | 0:8f8e8f3cbd1c | 170 | * @param timeout_ms timeout of the connection |
mayur098 | 0:8f8e8f3cbd1c | 171 | */ |
mayur098 | 0:8f8e8f3cbd1c | 172 | void setTimeout(uint32_t timeout_ms); |
mayur098 | 0:8f8e8f3cbd1c | 173 | |
mayur098 | 0:8f8e8f3cbd1c | 174 | /** |
mayur098 | 0:8f8e8f3cbd1c | 175 | * Checks if data is available |
mayur098 | 0:8f8e8f3cbd1c | 176 | */ |
mayur098 | 0:8f8e8f3cbd1c | 177 | bool readable(); |
mayur098 | 0:8f8e8f3cbd1c | 178 | |
mayur098 | 0:8f8e8f3cbd1c | 179 | /** |
mayur098 | 0:8f8e8f3cbd1c | 180 | * Checks if data can be written |
mayur098 | 0:8f8e8f3cbd1c | 181 | */ |
mayur098 | 0:8f8e8f3cbd1c | 182 | bool writeable(); |
mayur098 | 0:8f8e8f3cbd1c | 183 | |
mayur098 | 0:8f8e8f3cbd1c | 184 | void attach(int id, void (*callback)(void *), void *data); |
mayur098 | 0:8f8e8f3cbd1c | 185 | int get_free_id(); |
mayur098 | 0:8f8e8f3cbd1c | 186 | |
mayur098 | 0:8f8e8f3cbd1c | 187 | bool config_soft_ap(const char *ap, const char *passPhrase, uint8_t chl, uint8_t ecn); |
mayur098 | 0:8f8e8f3cbd1c | 188 | |
mayur098 | 0:8f8e8f3cbd1c | 189 | bool restart(); |
mayur098 | 0:8f8e8f3cbd1c | 190 | bool get_ssid(const char *ap); |
mayur098 | 0:8f8e8f3cbd1c | 191 | bool cre_server(int port); |
mayur098 | 0:8f8e8f3cbd1c | 192 | bool del_server(); |
mayur098 | 0:8f8e8f3cbd1c | 193 | bool accept(int * p_id); |
mayur098 | 0:8f8e8f3cbd1c | 194 | |
mayur098 | 0:8f8e8f3cbd1c | 195 | private: |
mayur098 | 0:8f8e8f3cbd1c | 196 | DigitalOut wifi_en; |
mayur098 | 0:8f8e8f3cbd1c | 197 | DigitalOut wifi_io0; |
mayur098 | 0:8f8e8f3cbd1c | 198 | bool init_end; |
mayur098 | 0:8f8e8f3cbd1c | 199 | BufferedSerial _serial; |
mayur098 | 0:8f8e8f3cbd1c | 200 | ATParser_os _parser; |
mayur098 | 0:8f8e8f3cbd1c | 201 | struct packet { |
mayur098 | 0:8f8e8f3cbd1c | 202 | struct packet *next; |
mayur098 | 0:8f8e8f3cbd1c | 203 | int id; |
mayur098 | 0:8f8e8f3cbd1c | 204 | uint32_t len; |
mayur098 | 0:8f8e8f3cbd1c | 205 | uint32_t index; |
mayur098 | 0:8f8e8f3cbd1c | 206 | // data follows |
mayur098 | 0:8f8e8f3cbd1c | 207 | } *_packets, **_packets_end; |
mayur098 | 0:8f8e8f3cbd1c | 208 | int _wifi_mode; |
mayur098 | 0:8f8e8f3cbd1c | 209 | int _baudrate; |
mayur098 | 0:8f8e8f3cbd1c | 210 | PinName _rts; |
mayur098 | 0:8f8e8f3cbd1c | 211 | PinName _cts; |
mayur098 | 0:8f8e8f3cbd1c | 212 | int _flow_control; |
mayur098 | 0:8f8e8f3cbd1c | 213 | |
mayur098 | 0:8f8e8f3cbd1c | 214 | std::vector<int> _accept_id; |
mayur098 | 0:8f8e8f3cbd1c | 215 | uint32_t _id_bits; |
mayur098 | 0:8f8e8f3cbd1c | 216 | uint32_t _id_bits_close; |
mayur098 | 0:8f8e8f3cbd1c | 217 | bool _server_act; |
mayur098 | 0:8f8e8f3cbd1c | 218 | Mutex _smutex; |
mayur098 | 0:8f8e8f3cbd1c | 219 | static ESP32 * instESP32; |
mayur098 | 0:8f8e8f3cbd1c | 220 | |
mayur098 | 0:8f8e8f3cbd1c | 221 | bool _ids[ESP32_SOCKET_COUNT]; |
mayur098 | 0:8f8e8f3cbd1c | 222 | struct { |
mayur098 | 0:8f8e8f3cbd1c | 223 | void (*callback)(void *); |
mayur098 | 0:8f8e8f3cbd1c | 224 | void *data; |
mayur098 | 0:8f8e8f3cbd1c | 225 | int Notified; |
mayur098 | 0:8f8e8f3cbd1c | 226 | } _cbs[ESP32_SOCKET_COUNT]; |
mayur098 | 0:8f8e8f3cbd1c | 227 | |
mayur098 | 0:8f8e8f3cbd1c | 228 | bool startup(); |
mayur098 | 0:8f8e8f3cbd1c | 229 | bool reset(void); |
mayur098 | 0:8f8e8f3cbd1c | 230 | void debugOn(bool debug); |
mayur098 | 0:8f8e8f3cbd1c | 231 | void socket_handler(bool connect, int id); |
mayur098 | 0:8f8e8f3cbd1c | 232 | void _connect_handler_0(); |
mayur098 | 0:8f8e8f3cbd1c | 233 | void _connect_handler_1(); |
mayur098 | 0:8f8e8f3cbd1c | 234 | void _connect_handler_2(); |
mayur098 | 0:8f8e8f3cbd1c | 235 | void _connect_handler_3(); |
mayur098 | 0:8f8e8f3cbd1c | 236 | void _connect_handler_4(); |
mayur098 | 0:8f8e8f3cbd1c | 237 | void _closed_handler_0(); |
mayur098 | 0:8f8e8f3cbd1c | 238 | void _closed_handler_1(); |
mayur098 | 0:8f8e8f3cbd1c | 239 | void _closed_handler_2(); |
mayur098 | 0:8f8e8f3cbd1c | 240 | void _closed_handler_3(); |
mayur098 | 0:8f8e8f3cbd1c | 241 | void _closed_handler_4(); |
mayur098 | 0:8f8e8f3cbd1c | 242 | void _packet_handler(); |
mayur098 | 0:8f8e8f3cbd1c | 243 | void event(); |
mayur098 | 0:8f8e8f3cbd1c | 244 | bool recv_ap(nsapi_wifi_ap_t *ap); |
mayur098 | 0:8f8e8f3cbd1c | 245 | |
mayur098 | 0:8f8e8f3cbd1c | 246 | char _ip_buffer[16]; |
mayur098 | 0:8f8e8f3cbd1c | 247 | char _gateway_buffer[16]; |
mayur098 | 0:8f8e8f3cbd1c | 248 | char _netmask_buffer[16]; |
mayur098 | 0:8f8e8f3cbd1c | 249 | char _mac_buffer[18]; |
mayur098 | 0:8f8e8f3cbd1c | 250 | |
mayur098 | 0:8f8e8f3cbd1c | 251 | char _ip_buffer_ap[16]; |
mayur098 | 0:8f8e8f3cbd1c | 252 | char _gateway_buffer_ap[16]; |
mayur098 | 0:8f8e8f3cbd1c | 253 | char _netmask_buffer_ap[16]; |
mayur098 | 0:8f8e8f3cbd1c | 254 | char _mac_buffer_ap[18]; |
mayur098 | 0:8f8e8f3cbd1c | 255 | }; |
mayur098 | 0:8f8e8f3cbd1c | 256 | |
mayur098 | 0:8f8e8f3cbd1c | 257 | #endif |