The driver for the ESP32 WiFi module
Embed:
(wiki syntax)
Show/hide line numbers
ESP32.h
00001 /* ESP32Interface Example 00002 * Copyright (c) 2015 ARM Limited 00003 * Copyright (c) 2017 Renesas Electronics Corporation 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #ifndef ESP32_H 00019 #define ESP32_H 00020 00021 #include <vector> 00022 #include "ATCmdParser.h" 00023 00024 #ifndef ESP32_CONNECT_TIMEOUT 00025 #define ESP32_CONNECT_TIMEOUT 15000 00026 #endif 00027 #ifndef ESP32_RECV_TIMEOUT 00028 #define ESP32_RECV_TIMEOUT 2000 00029 #endif 00030 #ifndef ESP32_MISC_TIMEOUT 00031 #define ESP32_MISC_TIMEOUT 2000 00032 #endif 00033 00034 /** ESP32Interface class. 00035 This is an interface to a ESP32 radio. 00036 */ 00037 class ESP32 00038 { 00039 public: 00040 /** 00041 * Static method to create or retrieve the single ESP32 instance 00042 */ 00043 static ESP32 * getESP32Inst(PinName en, PinName io0, PinName tx, PinName rx, bool debug, 00044 PinName rts, PinName cts, int baudrate); 00045 00046 ESP32(PinName en, PinName io0, PinName tx, PinName rx, bool debug, 00047 PinName rts, PinName cts, int baudrate); 00048 00049 /** 00050 * Check firmware version of ESP8266 00051 * 00052 * @return integer firmware version or -1 if firmware query command gives outdated response 00053 */ 00054 int get_firmware_version(void); 00055 00056 /** 00057 * Sets the Wi-Fi Mode 00058 * 00059 * @param mode mode of WIFI 1-client, 2-host, 3-both 00060 * @return true only if ESP32 was setup correctly 00061 */ 00062 bool set_mode(int mode); 00063 00064 /** 00065 * Enable/Disable DHCP 00066 * 00067 * @param enabled DHCP enabled when true 00068 * @param mode mode of DHCP 0-softAP, 1-station, 2-both 00069 * @return true only if ESP32 enables/disables DHCP successfully 00070 */ 00071 bool dhcp(bool enabled, int mode); 00072 00073 /** 00074 * Connect ESP32 to AP 00075 * 00076 * @param ap the name of the AP 00077 * @param passPhrase the password of AP 00078 * @return true only if ESP32 is connected successfully 00079 */ 00080 bool connect(const char *ap, const char *passPhrase); 00081 00082 /** 00083 * Disconnect ESP32 from AP 00084 * 00085 * @return true only if ESP32 is disconnected successfully 00086 */ 00087 bool disconnect(void); 00088 00089 /** 00090 * Get the IP address of ESP32 00091 * 00092 * @return null-teriminated IP address or null if no IP address is assigned 00093 */ 00094 const char *getIPAddress(void); 00095 const char *getIPAddress_ap(void); 00096 00097 /** 00098 * Get the MAC address of ESP32 00099 * 00100 * @return null-terminated MAC address or null if no MAC address is assigned 00101 */ 00102 const char *getMACAddress(void); 00103 const char *getMACAddress_ap(void); 00104 00105 /** Get the local gateway 00106 * 00107 * @return Null-terminated representation of the local gateway 00108 * or null if no network mask has been recieved 00109 */ 00110 const char *getGateway(); 00111 const char *getGateway_ap(); 00112 00113 /** Get the local network mask 00114 * 00115 * @return Null-terminated representation of the local network mask 00116 * or null if no network mask has been recieved 00117 */ 00118 const char *getNetmask(); 00119 const char *getNetmask_ap(); 00120 00121 /* Return RSSI for active connection 00122 * 00123 * @return Measured RSSI 00124 */ 00125 int8_t getRSSI(); 00126 00127 /** 00128 * Check if ESP32 is conenected 00129 * 00130 * @return true only if the chip has an IP address 00131 */ 00132 bool isConnected(void); 00133 00134 /** Scan for available networks 00135 * 00136 * @param ap Pointer to allocated array to store discovered AP 00137 * @param limit Size of allocated @a res array, or 0 to only count available AP 00138 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error 00139 * see @a nsapi_error 00140 */ 00141 int scan(WiFiAccessPoint *res, unsigned limit); 00142 00143 /** 00144 * Open a socketed connection 00145 * 00146 * @param type the type of socket to open "UDP" or "TCP" 00147 * @param id id to give the new socket, valid 0-4 00148 * @param port port to open connection with 00149 * @param addr the IP address of the destination 00150 * @param addr the IP address of the destination 00151 * @param opt type=" UDP" : UDP socket's local port, zero means any 00152 * type=" TCP" : TCP connection's keep alive time, zero means disabled 00153 * @return true only if socket opened successfully 00154 */ 00155 bool open(const char *type, int id, const char* addr, int port, int opt = 0); 00156 00157 /** 00158 * Sends data to an open socket 00159 * 00160 * @param id id of socket to send to 00161 * @param data data to be sent 00162 * @param amount amount of data to be sent - max 1024 00163 * @return true only if data sent successfully 00164 */ 00165 bool send(int id, const void *data, uint32_t amount); 00166 00167 /** 00168 * Receives data from an open socket 00169 * 00170 * @param id id to receive from 00171 * @param data placeholder for returned information 00172 * @param amount number of bytes to be received 00173 * @return the number of bytes received 00174 */ 00175 int32_t recv(int id, void *data, uint32_t amount, uint32_t timeout = ESP32_RECV_TIMEOUT); 00176 00177 /** 00178 * Closes a socket 00179 * 00180 * @param id id of socket to close, valid only 0-4 00181 * @param wait_close 00182 * @return true only if socket is closed successfully 00183 */ 00184 bool close(int id, bool wait_close = false); 00185 00186 /** 00187 * Allows timeout to be changed between commands 00188 * 00189 * @param timeout_ms timeout of the connection 00190 */ 00191 void setTimeout(uint32_t timeout_ms = ESP32_MISC_TIMEOUT); 00192 00193 /** 00194 * Checks if data is available 00195 */ 00196 bool readable(); 00197 00198 /** 00199 * Checks if data can be written 00200 */ 00201 bool writeable(); 00202 00203 void socket_attach(int id, void (*callback)(void *), void *data); 00204 int get_free_id(); 00205 00206 bool config_soft_ap(const char *ap, const char *passPhrase, uint8_t chl, uint8_t ecn); 00207 00208 bool restart(); 00209 bool get_ssid(char *ap); 00210 bool cre_server(int port); 00211 bool del_server(); 00212 bool accept(int * p_id); 00213 00214 bool set_network(const char *ip_address, const char *netmask, const char *gateway); 00215 bool set_network_ap(const char *ip_address, const char *netmask, const char *gateway); 00216 00217 /** 00218 * Attach a function to call whenever network state has changed 00219 * 00220 * @param func A pointer to a void function, or 0 to set as none 00221 */ 00222 void attach_wifi_status(mbed::Callback<void(int8_t)> status_cb); 00223 00224 /** Get the connection status 00225 * 00226 * @return The connection status according to ConnectionStatusType 00227 */ 00228 int8_t get_wifi_status() const; 00229 00230 static const int8_t WIFIMODE_STATION = 1; 00231 static const int8_t WIFIMODE_SOFTAP = 2; 00232 static const int8_t WIFIMODE_STATION_SOFTAP = 3; 00233 static const int8_t SOCKET_COUNT = 5; 00234 00235 static const int8_t STATUS_DISCONNECTED = 0; 00236 static const int8_t STATUS_CONNECTED = 1; 00237 static const int8_t STATUS_GOT_IP = 2; 00238 00239 private: 00240 DigitalOut * _p_wifi_en; 00241 DigitalOut * _p_wifi_io0; 00242 bool init_end; 00243 UARTSerial _serial; 00244 ATCmdParser _parser; 00245 struct packet { 00246 struct packet *next; 00247 int id; 00248 uint32_t len; 00249 uint32_t index; 00250 // data follows 00251 } *_packets, **_packets_end; 00252 int _wifi_mode; 00253 int _baudrate; 00254 PinName _rts; 00255 PinName _cts; 00256 int _flow_control; 00257 uint32_t last_timeout_ms; 00258 00259 std::vector<int> _accept_id; 00260 uint32_t _id_bits; 00261 uint32_t _id_bits_close; 00262 bool _server_act; 00263 rtos::Mutex _smutex; // Protect serial port access 00264 static ESP32 * instESP32; 00265 int8_t _wifi_status; 00266 Callback<void(int8_t)> _wifi_status_cb; 00267 00268 bool _ids[SOCKET_COUNT]; 00269 struct { 00270 void (*callback)(void *); 00271 void *data; 00272 int Notified; 00273 } _cbs[SOCKET_COUNT]; 00274 00275 bool startup(); 00276 bool reset(void); 00277 void debugOn(bool debug); 00278 void socket_handler(bool connect, int id); 00279 void _connect_handler_0(); 00280 void _connect_handler_1(); 00281 void _connect_handler_2(); 00282 void _connect_handler_3(); 00283 void _connect_handler_4(); 00284 void _closed_handler_0(); 00285 void _closed_handler_1(); 00286 void _closed_handler_2(); 00287 void _closed_handler_3(); 00288 void _closed_handler_4(); 00289 void _connection_status_handler(); 00290 void _packet_handler(); 00291 void _clear_socket_packets(int id); 00292 void event(); 00293 bool recv_ap(nsapi_wifi_ap_t *ap); 00294 00295 char _ip_buffer[16]; 00296 char _gateway_buffer[16]; 00297 char _netmask_buffer[16]; 00298 char _mac_buffer[18]; 00299 00300 char _ip_buffer_ap[16]; 00301 char _gateway_buffer_ap[16]; 00302 char _netmask_buffer_ap[16]; 00303 char _mac_buffer_ap[18]; 00304 }; 00305 00306 #endif
Generated on Wed Jul 13 2022 08:56:28 by 1.7.2