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