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:
Fri Jun 29 06:17:38 2018 +0000
Revision:
0:92d12d355ba9
Child:
1:5d78eedef723
first commit

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