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.
Note
ESP32Interface.cpp@2:cb5c0d3fa776, 2018-07-09 (annotated)
- 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?
User | Revision | Line number | New 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 | #include <string.h> |
dkato | 0:92d12d355ba9 | 18 | #include "ESP32Interface.h" |
dkato | 0:92d12d355ba9 | 19 | |
dkato | 0:92d12d355ba9 | 20 | // ESP32Interface implementation |
dkato | 1:5d78eedef723 | 21 | ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug, |
dkato | 1:5d78eedef723 | 22 | PinName rts, PinName cts, int baudrate) : |
dkato | 1:5d78eedef723 | 23 | ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate), |
dkato | 0:92d12d355ba9 | 24 | _dhcp(true), |
dkato | 0:92d12d355ba9 | 25 | _ap_ssid(), |
dkato | 0:92d12d355ba9 | 26 | _ap_pass(), |
dkato | 0:92d12d355ba9 | 27 | _ap_sec(NSAPI_SECURITY_NONE), |
dkato | 0:92d12d355ba9 | 28 | _ip_address(), |
dkato | 0:92d12d355ba9 | 29 | _netmask(), |
dkato | 0:92d12d355ba9 | 30 | _gateway(), |
dkato | 0:92d12d355ba9 | 31 | _connection_status(NSAPI_STATUS_DISCONNECTED), |
dkato | 0:92d12d355ba9 | 32 | _connection_status_cb(NULL) |
dkato | 0:92d12d355ba9 | 33 | { |
dkato | 0:92d12d355ba9 | 34 | _esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb)); |
dkato | 0:92d12d355ba9 | 35 | } |
dkato | 0:92d12d355ba9 | 36 | |
dkato | 1:5d78eedef723 | 37 | ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) : |
dkato | 1:5d78eedef723 | 38 | ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400), |
dkato | 0:92d12d355ba9 | 39 | _dhcp(true), |
dkato | 0:92d12d355ba9 | 40 | _ap_ssid(), |
dkato | 0:92d12d355ba9 | 41 | _ap_pass(), |
dkato | 0:92d12d355ba9 | 42 | _ap_sec(NSAPI_SECURITY_NONE), |
dkato | 0:92d12d355ba9 | 43 | _ip_address(), |
dkato | 0:92d12d355ba9 | 44 | _netmask(), |
dkato | 0:92d12d355ba9 | 45 | _gateway(), |
dkato | 0:92d12d355ba9 | 46 | _connection_status(NSAPI_STATUS_DISCONNECTED), |
dkato | 0:92d12d355ba9 | 47 | _connection_status_cb(NULL) |
dkato | 0:92d12d355ba9 | 48 | { |
dkato | 0:92d12d355ba9 | 49 | _esp->attach_wifi_status(callback(this, &ESP32Interface::wifi_status_cb)); |
dkato | 0:92d12d355ba9 | 50 | } |
dkato | 0:92d12d355ba9 | 51 | |
dkato | 0:92d12d355ba9 | 52 | nsapi_error_t ESP32Interface::set_network(const char *ip_address, const char *netmask, const char *gateway) |
dkato | 0:92d12d355ba9 | 53 | { |
dkato | 0:92d12d355ba9 | 54 | _dhcp = false; |
dkato | 0:92d12d355ba9 | 55 | |
dkato | 0:92d12d355ba9 | 56 | strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address)); |
dkato | 0:92d12d355ba9 | 57 | _ip_address[sizeof(_ip_address) - 1] = '\0'; |
dkato | 0:92d12d355ba9 | 58 | strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask)); |
dkato | 0:92d12d355ba9 | 59 | _netmask[sizeof(_netmask) - 1] = '\0'; |
dkato | 0:92d12d355ba9 | 60 | strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway)); |
dkato | 0:92d12d355ba9 | 61 | _gateway[sizeof(_gateway) - 1] = '\0'; |
dkato | 0:92d12d355ba9 | 62 | |
dkato | 0:92d12d355ba9 | 63 | return NSAPI_ERROR_OK; |
dkato | 0:92d12d355ba9 | 64 | } |
dkato | 0:92d12d355ba9 | 65 | |
dkato | 0:92d12d355ba9 | 66 | nsapi_error_t ESP32Interface::set_dhcp(bool dhcp) |
dkato | 0:92d12d355ba9 | 67 | { |
dkato | 0:92d12d355ba9 | 68 | _dhcp = dhcp; |
dkato | 0:92d12d355ba9 | 69 | |
dkato | 0:92d12d355ba9 | 70 | return NSAPI_ERROR_OK; |
dkato | 0:92d12d355ba9 | 71 | } |
dkato | 0:92d12d355ba9 | 72 | |
dkato | 0:92d12d355ba9 | 73 | int ESP32Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, |
dkato | 0:92d12d355ba9 | 74 | uint8_t channel) |
dkato | 0:92d12d355ba9 | 75 | { |
dkato | 0:92d12d355ba9 | 76 | if (channel != 0) { |
dkato | 0:92d12d355ba9 | 77 | return NSAPI_ERROR_UNSUPPORTED; |
dkato | 0:92d12d355ba9 | 78 | } |
dkato | 0:92d12d355ba9 | 79 | |
dkato | 0:92d12d355ba9 | 80 | set_credentials(ssid, pass, security); |
dkato | 0:92d12d355ba9 | 81 | return connect(); |
dkato | 0:92d12d355ba9 | 82 | } |
dkato | 0:92d12d355ba9 | 83 | |
dkato | 0:92d12d355ba9 | 84 | int ESP32Interface::connect() |
dkato | 0:92d12d355ba9 | 85 | { |
dkato | 0:92d12d355ba9 | 86 | if (!_esp->dhcp(_dhcp, 1)) { |
dkato | 0:92d12d355ba9 | 87 | return NSAPI_ERROR_DHCP_FAILURE; |
dkato | 0:92d12d355ba9 | 88 | } |
dkato | 0:92d12d355ba9 | 89 | |
dkato | 0:92d12d355ba9 | 90 | if (!_dhcp) { |
dkato | 0:92d12d355ba9 | 91 | if (!_esp->set_network(_ip_address, _netmask, _gateway)) { |
dkato | 0:92d12d355ba9 | 92 | return NSAPI_ERROR_DEVICE_ERROR; |
dkato | 0:92d12d355ba9 | 93 | } |
dkato | 0:92d12d355ba9 | 94 | } |
dkato | 0:92d12d355ba9 | 95 | |
dkato | 0:92d12d355ba9 | 96 | set_connection_status(NSAPI_STATUS_CONNECTING); |
dkato | 0:92d12d355ba9 | 97 | if (!_esp->connect(_ap_ssid, _ap_pass)) { |
dkato | 0:92d12d355ba9 | 98 | set_connection_status(NSAPI_STATUS_DISCONNECTED); |
dkato | 0:92d12d355ba9 | 99 | return NSAPI_ERROR_NO_CONNECTION; |
dkato | 0:92d12d355ba9 | 100 | } |
dkato | 0:92d12d355ba9 | 101 | |
dkato | 0:92d12d355ba9 | 102 | return NSAPI_ERROR_OK; |
dkato | 0:92d12d355ba9 | 103 | } |
dkato | 0:92d12d355ba9 | 104 | |
dkato | 0:92d12d355ba9 | 105 | int ESP32Interface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
dkato | 0:92d12d355ba9 | 106 | { |
dkato | 0:92d12d355ba9 | 107 | memset(_ap_ssid, 0, sizeof(_ap_ssid)); |
dkato | 0:92d12d355ba9 | 108 | strncpy(_ap_ssid, ssid, sizeof(_ap_ssid)); |
dkato | 0:92d12d355ba9 | 109 | |
dkato | 0:92d12d355ba9 | 110 | memset(_ap_pass, 0, sizeof(_ap_pass)); |
dkato | 0:92d12d355ba9 | 111 | strncpy(_ap_pass, pass, sizeof(_ap_pass)); |
dkato | 0:92d12d355ba9 | 112 | |
dkato | 0:92d12d355ba9 | 113 | _ap_sec = security; |
dkato | 0:92d12d355ba9 | 114 | |
dkato | 0:92d12d355ba9 | 115 | return 0; |
dkato | 0:92d12d355ba9 | 116 | } |
dkato | 0:92d12d355ba9 | 117 | |
dkato | 0:92d12d355ba9 | 118 | int ESP32Interface::set_channel(uint8_t channel) |
dkato | 0:92d12d355ba9 | 119 | { |
dkato | 0:92d12d355ba9 | 120 | return NSAPI_ERROR_UNSUPPORTED; |
dkato | 0:92d12d355ba9 | 121 | } |
dkato | 0:92d12d355ba9 | 122 | |
dkato | 0:92d12d355ba9 | 123 | int ESP32Interface::disconnect() |
dkato | 0:92d12d355ba9 | 124 | { |
dkato | 0:92d12d355ba9 | 125 | if (!_esp->disconnect()) { |
dkato | 0:92d12d355ba9 | 126 | return NSAPI_ERROR_DEVICE_ERROR; |
dkato | 0:92d12d355ba9 | 127 | } |
dkato | 0:92d12d355ba9 | 128 | |
dkato | 0:92d12d355ba9 | 129 | return NSAPI_ERROR_OK; |
dkato | 0:92d12d355ba9 | 130 | } |
dkato | 0:92d12d355ba9 | 131 | |
dkato | 0:92d12d355ba9 | 132 | const char *ESP32Interface::get_ip_address() |
dkato | 0:92d12d355ba9 | 133 | { |
dkato | 0:92d12d355ba9 | 134 | return _esp->getIPAddress(); |
dkato | 0:92d12d355ba9 | 135 | } |
dkato | 0:92d12d355ba9 | 136 | |
dkato | 0:92d12d355ba9 | 137 | const char *ESP32Interface::get_mac_address() |
dkato | 0:92d12d355ba9 | 138 | { |
dkato | 0:92d12d355ba9 | 139 | return _esp->getMACAddress(); |
dkato | 0:92d12d355ba9 | 140 | } |
dkato | 0:92d12d355ba9 | 141 | |
dkato | 0:92d12d355ba9 | 142 | const char *ESP32Interface::get_gateway() |
dkato | 0:92d12d355ba9 | 143 | { |
dkato | 0:92d12d355ba9 | 144 | return _esp->getGateway(); |
dkato | 0:92d12d355ba9 | 145 | } |
dkato | 0:92d12d355ba9 | 146 | |
dkato | 0:92d12d355ba9 | 147 | const char *ESP32Interface::get_netmask() |
dkato | 0:92d12d355ba9 | 148 | { |
dkato | 0:92d12d355ba9 | 149 | return _esp->getNetmask(); |
dkato | 0:92d12d355ba9 | 150 | } |
dkato | 0:92d12d355ba9 | 151 | |
dkato | 0:92d12d355ba9 | 152 | int8_t ESP32Interface::get_rssi() |
dkato | 0:92d12d355ba9 | 153 | { |
dkato | 0:92d12d355ba9 | 154 | return _esp->getRSSI(); |
dkato | 0:92d12d355ba9 | 155 | } |
dkato | 0:92d12d355ba9 | 156 | |
dkato | 0:92d12d355ba9 | 157 | int ESP32Interface::scan(WiFiAccessPoint *res, unsigned count) |
dkato | 0:92d12d355ba9 | 158 | { |
dkato | 0:92d12d355ba9 | 159 | return _esp->scan(res, count); |
dkato | 0:92d12d355ba9 | 160 | } |
dkato | 0:92d12d355ba9 | 161 | |
dkato | 0:92d12d355ba9 | 162 | void ESP32Interface::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) |
dkato | 0:92d12d355ba9 | 163 | { |
dkato | 0:92d12d355ba9 | 164 | _connection_status_cb = status_cb; |
dkato | 0:92d12d355ba9 | 165 | } |
dkato | 0:92d12d355ba9 | 166 | |
dkato | 0:92d12d355ba9 | 167 | nsapi_connection_status_t ESP32Interface::get_connection_status() const |
dkato | 0:92d12d355ba9 | 168 | { |
dkato | 0:92d12d355ba9 | 169 | return _connection_status; |
dkato | 0:92d12d355ba9 | 170 | } |
dkato | 0:92d12d355ba9 | 171 | |
dkato | 0:92d12d355ba9 | 172 | void ESP32Interface::set_connection_status(nsapi_connection_status_t connection_status) |
dkato | 0:92d12d355ba9 | 173 | { |
dkato | 0:92d12d355ba9 | 174 | if (_connection_status != connection_status) { |
dkato | 0:92d12d355ba9 | 175 | _connection_status = connection_status; |
dkato | 0:92d12d355ba9 | 176 | if (_connection_status_cb) { |
dkato | 0:92d12d355ba9 | 177 | _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status); |
dkato | 0:92d12d355ba9 | 178 | } |
dkato | 0:92d12d355ba9 | 179 | } |
dkato | 0:92d12d355ba9 | 180 | } |
dkato | 0:92d12d355ba9 | 181 | |
dkato | 0:92d12d355ba9 | 182 | void ESP32Interface::wifi_status_cb(int8_t wifi_status) |
dkato | 0:92d12d355ba9 | 183 | { |
dkato | 0:92d12d355ba9 | 184 | switch (wifi_status) { |
dkato | 0:92d12d355ba9 | 185 | case ESP32::STATUS_DISCONNECTED: |
dkato | 0:92d12d355ba9 | 186 | set_connection_status(NSAPI_STATUS_DISCONNECTED); |
dkato | 0:92d12d355ba9 | 187 | break; |
dkato | 0:92d12d355ba9 | 188 | case ESP32::STATUS_GOT_IP: |
dkato | 0:92d12d355ba9 | 189 | set_connection_status(NSAPI_STATUS_GLOBAL_UP); |
dkato | 0:92d12d355ba9 | 190 | break; |
dkato | 0:92d12d355ba9 | 191 | case ESP32::STATUS_CONNECTED: |
dkato | 0:92d12d355ba9 | 192 | default: |
dkato | 0:92d12d355ba9 | 193 | // do nothing |
dkato | 0:92d12d355ba9 | 194 | break; |
dkato | 0:92d12d355ba9 | 195 | } |
dkato | 0:92d12d355ba9 | 196 | } |
dkato | 0:92d12d355ba9 | 197 |