The driver for the ESP32 WiFi module

The ESP32 WiFi driver for Mbed OS

The Mbed OS driver for the ESP32 WiFi module.

Firmware version

How to write mbed-os compatible firmware : https://github.com/d-kato/GR-Boards_ESP32_Serial_Bridge

Restrictions

  • Setting up an UDP server is not possible
  • The serial port does not have hardware flow control enabled. The AT command set does not either have a way to limit the download rate. Therefore, downloading anything larger than the serial port input buffer is unreliable. An application should be able to read fast enough to stay ahead of the network. This affects mostly the TCP protocol where data would be lost with no notification. On UDP, this would lead to only packet losses which the higher layer protocol should recover from.

Committer:
dkato
Date:
Mon Jul 09 10:26:03 2018 +0000
Revision:
2:cb5c0d3fa776
Parent:
1:5d78eedef723
Synchronized with git revision 78a139f3bce17e23f2b4bd9342dd7adca023d248

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:92d12d355ba9 1 /* ESP32 implementation of NetworkInterfaceAPI
dkato 0:92d12d355ba9 2 * Copyright (c) 2017 Renesas Electronics Corporation
dkato 0:92d12d355ba9 3 *
dkato 0:92d12d355ba9 4 * Licensed under the Apache License, Version 2.0 (the "License");
dkato 0:92d12d355ba9 5 * you may not use this file except in compliance with the License.
dkato 0:92d12d355ba9 6 * You may obtain a copy of the License at
dkato 0:92d12d355ba9 7 *
dkato 0:92d12d355ba9 8 * http://www.apache.org/licenses/LICENSE-2.0
dkato 0:92d12d355ba9 9 *
dkato 0:92d12d355ba9 10 * Unless required by applicable law or agreed to in writing, software
dkato 0:92d12d355ba9 11 * distributed under the License is distributed on an "AS IS" BASIS,
dkato 0:92d12d355ba9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dkato 0:92d12d355ba9 13 * See the License for the specific language governing permissions and
dkato 0:92d12d355ba9 14 * limitations under the License.
dkato 0:92d12d355ba9 15 */
dkato 0:92d12d355ba9 16
dkato 0:92d12d355ba9 17 #ifndef ESP32_INTERFACE_AP_H
dkato 0:92d12d355ba9 18 #define ESP32_INTERFACE_AP_H
dkato 0:92d12d355ba9 19
dkato 0:92d12d355ba9 20 #include "mbed.h"
dkato 0:92d12d355ba9 21 #include "ESP32Stack.h"
dkato 0:92d12d355ba9 22
dkato 0:92d12d355ba9 23
dkato 0:92d12d355ba9 24 /** ESP32Interface class
dkato 0:92d12d355ba9 25 * Implementation of the NetworkStack for the ESP32
dkato 0:92d12d355ba9 26 */
dkato 0:92d12d355ba9 27 class ESP32InterfaceAP : public ESP32Stack, public WiFiInterface
dkato 0:92d12d355ba9 28 {
dkato 0:92d12d355ba9 29 public:
dkato 0:92d12d355ba9 30 /** ESP32InterfaceAP lifetime
dkato 0:92d12d355ba9 31 * @param en EN pin (If not used this pin, please set "NC")
dkato 0:92d12d355ba9 32 * @param io0 IO0 pin (If not used this pin, please set "NC")
dkato 0:92d12d355ba9 33 * @param tx TX pin
dkato 0:92d12d355ba9 34 * @param rx RX pin
dkato 0:92d12d355ba9 35 * @param debug Enable debugging
dkato 1:5d78eedef723 36 * @param rts RTS pin
dkato 1:5d78eedef723 37 * @param cts CTS pin
dkato 0:92d12d355ba9 38 * @param baudrate The baudrate of the serial port (default = 230400).
dkato 0:92d12d355ba9 39 */
dkato 1:5d78eedef723 40 ESP32InterfaceAP(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false,
dkato 1:5d78eedef723 41 PinName rts = NC, PinName cts = NC, int baudrate = 230400);
dkato 0:92d12d355ba9 42
dkato 0:92d12d355ba9 43 /** ESP32InterfaceAP lifetime
dkato 0:92d12d355ba9 44 * @param tx TX pin
dkato 0:92d12d355ba9 45 * @param rx RX pin
dkato 0:92d12d355ba9 46 * @param debug Enable debugging
dkato 0:92d12d355ba9 47 */
dkato 1:5d78eedef723 48 ESP32InterfaceAP(PinName tx, PinName rx, bool debug = false);
dkato 0:92d12d355ba9 49
dkato 0:92d12d355ba9 50 /** Set a static IP address
dkato 0:92d12d355ba9 51 *
dkato 0:92d12d355ba9 52 * Configures this network interface to use a static IP address.
dkato 0:92d12d355ba9 53 * Implicitly disables DHCP, which can be enabled in set_dhcp.
dkato 0:92d12d355ba9 54 * Requires that the network is disconnected.
dkato 0:92d12d355ba9 55 *
dkato 0:92d12d355ba9 56 * @param ip_address Null-terminated representation of the local IP address
dkato 0:92d12d355ba9 57 * @param netmask Null-terminated representation of the local network mask
dkato 0:92d12d355ba9 58 * @param gateway Null-terminated representation of the local gateway
dkato 0:92d12d355ba9 59 * @return 0 on success, negative error code on failure
dkato 0:92d12d355ba9 60 */
dkato 0:92d12d355ba9 61 virtual nsapi_error_t set_network(
dkato 0:92d12d355ba9 62 const char *ip_address, const char *netmask, const char *gateway);
dkato 0:92d12d355ba9 63
dkato 0:92d12d355ba9 64 /** Enable or disable DHCP on the network
dkato 0:92d12d355ba9 65 *
dkato 0:92d12d355ba9 66 * Enables DHCP on connecting the network. Defaults to enabled unless
dkato 0:92d12d355ba9 67 * a static IP address has been assigned. Requires that the network is
dkato 0:92d12d355ba9 68 * disconnected.
dkato 0:92d12d355ba9 69 *
dkato 0:92d12d355ba9 70 * @param dhcp True to enable DHCP
dkato 0:92d12d355ba9 71 * @return 0 on success, negative error code on failure
dkato 0:92d12d355ba9 72 */
dkato 0:92d12d355ba9 73 virtual nsapi_error_t set_dhcp(bool dhcp);
dkato 0:92d12d355ba9 74
dkato 0:92d12d355ba9 75 /** Start the interface
dkato 0:92d12d355ba9 76 *
dkato 0:92d12d355ba9 77 * Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
dkato 0:92d12d355ba9 78 * If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
dkato 0:92d12d355ba9 79 *
dkato 0:92d12d355ba9 80 * @return 0 on success, negative error code on failure
dkato 0:92d12d355ba9 81 */
dkato 0:92d12d355ba9 82 virtual int connect();
dkato 0:92d12d355ba9 83
dkato 0:92d12d355ba9 84 /** Start the interface
dkato 0:92d12d355ba9 85 *
dkato 0:92d12d355ba9 86 * Attempts to connect to a WiFi network.
dkato 0:92d12d355ba9 87 *
dkato 0:92d12d355ba9 88 * @param ssid Name of the network to connect to
dkato 0:92d12d355ba9 89 * @param pass Security passphrase to connect to the network
dkato 0:92d12d355ba9 90 * @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
dkato 0:92d12d355ba9 91 * @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
dkato 0:92d12d355ba9 92 * @return 0 on success, or error code on failure
dkato 0:92d12d355ba9 93 */
dkato 0:92d12d355ba9 94 virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
dkato 0:92d12d355ba9 95 uint8_t channel = 0);
dkato 0:92d12d355ba9 96
dkato 0:92d12d355ba9 97 /** Set the WiFi network credentials
dkato 0:92d12d355ba9 98 *
dkato 0:92d12d355ba9 99 * @param ssid Name of the network to connect to
dkato 0:92d12d355ba9 100 * @param pass Security passphrase to connect to the network
dkato 0:92d12d355ba9 101 * @param security Type of encryption for connection
dkato 0:92d12d355ba9 102 * (defaults to NSAPI_SECURITY_NONE)
dkato 0:92d12d355ba9 103 * @return 0 on success, or error code on failure
dkato 0:92d12d355ba9 104 */
dkato 0:92d12d355ba9 105 virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
dkato 0:92d12d355ba9 106
dkato 0:92d12d355ba9 107 /** Set the WiFi network channel - NOT SUPPORTED
dkato 0:92d12d355ba9 108 *
dkato 0:92d12d355ba9 109 * This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
dkato 0:92d12d355ba9 110 *
dkato 0:92d12d355ba9 111 * @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
dkato 0:92d12d355ba9 112 * @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
dkato 0:92d12d355ba9 113 */
dkato 0:92d12d355ba9 114 virtual int set_channel(uint8_t channel);
dkato 0:92d12d355ba9 115
dkato 0:92d12d355ba9 116 /** Stop the interface
dkato 0:92d12d355ba9 117 * @return 0 on success, negative on failure
dkato 0:92d12d355ba9 118 */
dkato 0:92d12d355ba9 119 virtual int disconnect();
dkato 0:92d12d355ba9 120
dkato 0:92d12d355ba9 121 /** Get the internally stored IP address
dkato 0:92d12d355ba9 122 * @return IP address of the interface or null if not yet connected
dkato 0:92d12d355ba9 123 */
dkato 0:92d12d355ba9 124 virtual const char *get_ip_address();
dkato 0:92d12d355ba9 125
dkato 0:92d12d355ba9 126 /** Get the internally stored MAC address
dkato 0:92d12d355ba9 127 * @return MAC address of the interface
dkato 0:92d12d355ba9 128 */
dkato 0:92d12d355ba9 129 virtual const char *get_mac_address();
dkato 0:92d12d355ba9 130
dkato 0:92d12d355ba9 131 /** Get the local gateway
dkato 0:92d12d355ba9 132 *
dkato 0:92d12d355ba9 133 * @return Null-terminated representation of the local gateway
dkato 0:92d12d355ba9 134 * or null if no network mask has been recieved
dkato 0:92d12d355ba9 135 */
dkato 0:92d12d355ba9 136 virtual const char *get_gateway();
dkato 0:92d12d355ba9 137
dkato 0:92d12d355ba9 138 /** Get the local network mask
dkato 0:92d12d355ba9 139 *
dkato 0:92d12d355ba9 140 * @return Null-terminated representation of the local network mask
dkato 0:92d12d355ba9 141 * or null if no network mask has been recieved
dkato 0:92d12d355ba9 142 */
dkato 0:92d12d355ba9 143 virtual const char *get_netmask();
dkato 0:92d12d355ba9 144
dkato 0:92d12d355ba9 145 /** Gets the current radio signal strength for active connection
dkato 0:92d12d355ba9 146 *
dkato 0:92d12d355ba9 147 * @return Connection strength in dBm (negative value)
dkato 0:92d12d355ba9 148 */
dkato 0:92d12d355ba9 149 virtual int8_t get_rssi();
dkato 0:92d12d355ba9 150
dkato 0:92d12d355ba9 151 /** Scan for available networks
dkato 0:92d12d355ba9 152 *
dkato 0:92d12d355ba9 153 * This function will block.
dkato 0:92d12d355ba9 154 *
dkato 0:92d12d355ba9 155 * @param ap Pointer to allocated array to store discovered AP
dkato 0:92d12d355ba9 156 * @param count Size of allocated @a res array, or 0 to only count available AP
dkato 0:92d12d355ba9 157 * @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
dkato 0:92d12d355ba9 158 * @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
dkato 0:92d12d355ba9 159 * see @a nsapi_error
dkato 0:92d12d355ba9 160 */
dkato 0:92d12d355ba9 161 virtual int scan(WiFiAccessPoint *res, unsigned count);
dkato 0:92d12d355ba9 162
dkato 0:92d12d355ba9 163 /** Translates a hostname to an IP address with specific version
dkato 0:92d12d355ba9 164 *
dkato 0:92d12d355ba9 165 * The hostname may be either a domain name or an IP address. If the
dkato 0:92d12d355ba9 166 * hostname is an IP address, no network transactions will be performed.
dkato 0:92d12d355ba9 167 *
dkato 0:92d12d355ba9 168 * If no stack-specific DNS resolution is provided, the hostname
dkato 0:92d12d355ba9 169 * will be resolve using a UDP socket on the stack.
dkato 0:92d12d355ba9 170 *
dkato 0:92d12d355ba9 171 * @param address Destination for the host SocketAddress
dkato 0:92d12d355ba9 172 * @param host Hostname to resolve
dkato 0:92d12d355ba9 173 * @param version IP version of address to resolve, NSAPI_UNSPEC indicates
dkato 0:92d12d355ba9 174 * version is chosen by the stack (defaults to NSAPI_UNSPEC)
dkato 0:92d12d355ba9 175 * @return 0 on success, negative error code on failure
dkato 0:92d12d355ba9 176 */
dkato 0:92d12d355ba9 177 using NetworkInterface::gethostbyname;
dkato 0:92d12d355ba9 178
dkato 0:92d12d355ba9 179 /** Add a domain name server to list of servers to query
dkato 0:92d12d355ba9 180 *
dkato 0:92d12d355ba9 181 * @param addr Destination for the host address
dkato 0:92d12d355ba9 182 * @return 0 on success, negative error code on failure
dkato 0:92d12d355ba9 183 */
dkato 0:92d12d355ba9 184 using NetworkInterface::add_dns_server;
dkato 0:92d12d355ba9 185
dkato 0:92d12d355ba9 186 /** Register callback for status reporting
dkato 0:92d12d355ba9 187 *
dkato 0:92d12d355ba9 188 * The specified status callback function will be called on status changes
dkato 0:92d12d355ba9 189 * on the network. The parameters on the callback are the event type and
dkato 0:92d12d355ba9 190 * event-type dependent reason parameter.
dkato 0:92d12d355ba9 191 *
dkato 0:92d12d355ba9 192 * In ESP32 the callback will be called when processing OOB-messages via
dkato 0:92d12d355ba9 193 * AT-parser. Do NOT call any ESP8266Interface -functions or do extensive
dkato 0:92d12d355ba9 194 * processing in the callback.
dkato 0:92d12d355ba9 195 *
dkato 0:92d12d355ba9 196 * @param status_cb The callback for status changes
dkato 0:92d12d355ba9 197 */
dkato 0:92d12d355ba9 198 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
dkato 0:92d12d355ba9 199
dkato 0:92d12d355ba9 200 /** Get the connection status
dkato 0:92d12d355ba9 201 *
dkato 0:92d12d355ba9 202 * @return The connection status according to ConnectionStatusType
dkato 0:92d12d355ba9 203 */
dkato 0:92d12d355ba9 204 virtual nsapi_connection_status_t get_connection_status() const;
dkato 0:92d12d355ba9 205
dkato 0:92d12d355ba9 206
dkato 0:92d12d355ba9 207 /** Provide access to the NetworkStack object
dkato 0:92d12d355ba9 208 *
dkato 0:92d12d355ba9 209 * @return The underlying NetworkStack object
dkato 0:92d12d355ba9 210 */
dkato 0:92d12d355ba9 211 virtual NetworkStack *get_stack()
dkato 0:92d12d355ba9 212 {
dkato 0:92d12d355ba9 213 return this;
dkato 0:92d12d355ba9 214 }
dkato 0:92d12d355ba9 215
dkato 0:92d12d355ba9 216 private:
dkato 0:92d12d355ba9 217 bool _dhcp;
dkato 0:92d12d355ba9 218 uint8_t _own_ch;
dkato 0:92d12d355ba9 219 char _own_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
dkato 0:92d12d355ba9 220 char _own_pass[64]; /* The longest allowed passphrase */
dkato 0:92d12d355ba9 221 nsapi_security_t _own_sec;
dkato 0:92d12d355ba9 222 char _ip_address[NSAPI_IPv6_SIZE];
dkato 0:92d12d355ba9 223 char _netmask[NSAPI_IPv4_SIZE];
dkato 0:92d12d355ba9 224 char _gateway[NSAPI_IPv4_SIZE];
dkato 0:92d12d355ba9 225 nsapi_connection_status_t _connection_status;
dkato 0:92d12d355ba9 226 Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
dkato 0:92d12d355ba9 227 };
dkato 0:92d12d355ba9 228
dkato 0:92d12d355ba9 229 #endif