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