![](/media/cache/group/Advantech_logo.jpg.50x50_q85.jpg)
Generic Pelion Device Management example for various Advantech modules.
This example is known to work great on the following platforms:
- WISE-1530 WiFi Module using DB-1505 carrier board and external SD card reader.
Example Functionality
This example showcases the following device functionality:
- On timer button increment, simulate Pelion LWM2M button resource change
Use this example with Mbed CLI
1. Import the application into your desktop:
mbed import https://os.mbed.com/teams/Advantech/code/pelion-example-common cd pelion-example-common
2. Download your developer certificate from pelion portal
3. Compile the program
mbed compile -t <toolchain> -m <TARGET_BOARD>
(supported toolchains : GCC_ARM / ARM / IAR)
4. Copy the binary file pelion-example-common.bin to your mbed device.
drivers/network/COMPONENT_WIFI_ESP32/ESP32InterfaceAP.cpp@0:43ff9e3bc244, 2019-03-12 (annotated)
- Committer:
- chuanga
- Date:
- Tue Mar 12 13:48:39 2019 +0800
- Revision:
- 0:43ff9e3bc244
copying sources from github repository
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chuanga | 0:43ff9e3bc244 | 1 | /* ESP32 implementation of NetworkInterfaceAPI |
chuanga | 0:43ff9e3bc244 | 2 | * Copyright (c) 2017 Renesas Electronics Corporation |
chuanga | 0:43ff9e3bc244 | 3 | * |
chuanga | 0:43ff9e3bc244 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
chuanga | 0:43ff9e3bc244 | 5 | * you may not use this file except in compliance with the License. |
chuanga | 0:43ff9e3bc244 | 6 | * You may obtain a copy of the License at |
chuanga | 0:43ff9e3bc244 | 7 | * |
chuanga | 0:43ff9e3bc244 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
chuanga | 0:43ff9e3bc244 | 9 | * |
chuanga | 0:43ff9e3bc244 | 10 | * Unless required by applicable law or agreed to in writing, software |
chuanga | 0:43ff9e3bc244 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
chuanga | 0:43ff9e3bc244 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
chuanga | 0:43ff9e3bc244 | 13 | * See the License for the specific language governing permissions and |
chuanga | 0:43ff9e3bc244 | 14 | * limitations under the License. |
chuanga | 0:43ff9e3bc244 | 15 | */ |
chuanga | 0:43ff9e3bc244 | 16 | |
chuanga | 0:43ff9e3bc244 | 17 | #include <string.h> |
chuanga | 0:43ff9e3bc244 | 18 | #include "ESP32InterfaceAP.h" |
chuanga | 0:43ff9e3bc244 | 19 | |
chuanga | 0:43ff9e3bc244 | 20 | // ESP32InterfaceAP implementation |
chuanga | 0:43ff9e3bc244 | 21 | ESP32InterfaceAP::ESP32InterfaceAP(PinName en, PinName io0, PinName tx, PinName rx, bool debug, |
chuanga | 0:43ff9e3bc244 | 22 | PinName rts, PinName cts, int baudrate) : |
chuanga | 0:43ff9e3bc244 | 23 | ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate), |
chuanga | 0:43ff9e3bc244 | 24 | _dhcp(true), |
chuanga | 0:43ff9e3bc244 | 25 | _own_ch(1), |
chuanga | 0:43ff9e3bc244 | 26 | _own_ssid(), |
chuanga | 0:43ff9e3bc244 | 27 | _own_pass(), |
chuanga | 0:43ff9e3bc244 | 28 | _own_sec(NSAPI_SECURITY_NONE), |
chuanga | 0:43ff9e3bc244 | 29 | _ip_address(), |
chuanga | 0:43ff9e3bc244 | 30 | _netmask(), |
chuanga | 0:43ff9e3bc244 | 31 | _gateway(), |
chuanga | 0:43ff9e3bc244 | 32 | _connection_status(NSAPI_STATUS_DISCONNECTED), |
chuanga | 0:43ff9e3bc244 | 33 | _connection_status_cb(NULL) |
chuanga | 0:43ff9e3bc244 | 34 | { |
chuanga | 0:43ff9e3bc244 | 35 | } |
chuanga | 0:43ff9e3bc244 | 36 | |
chuanga | 0:43ff9e3bc244 | 37 | ESP32InterfaceAP::ESP32InterfaceAP(PinName tx, PinName rx, bool debug) : |
chuanga | 0:43ff9e3bc244 | 38 | ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400), |
chuanga | 0:43ff9e3bc244 | 39 | _dhcp(true), |
chuanga | 0:43ff9e3bc244 | 40 | _own_ch(1), |
chuanga | 0:43ff9e3bc244 | 41 | _own_ssid(), |
chuanga | 0:43ff9e3bc244 | 42 | _own_pass(), |
chuanga | 0:43ff9e3bc244 | 43 | _own_sec(NSAPI_SECURITY_NONE), |
chuanga | 0:43ff9e3bc244 | 44 | _ip_address(), |
chuanga | 0:43ff9e3bc244 | 45 | _netmask(), |
chuanga | 0:43ff9e3bc244 | 46 | _gateway(), |
chuanga | 0:43ff9e3bc244 | 47 | _connection_status(NSAPI_STATUS_DISCONNECTED), |
chuanga | 0:43ff9e3bc244 | 48 | _connection_status_cb(NULL) |
chuanga | 0:43ff9e3bc244 | 49 | { |
chuanga | 0:43ff9e3bc244 | 50 | } |
chuanga | 0:43ff9e3bc244 | 51 | |
chuanga | 0:43ff9e3bc244 | 52 | nsapi_error_t ESP32InterfaceAP::set_network(const char *ip_address, const char *netmask, const char *gateway) |
chuanga | 0:43ff9e3bc244 | 53 | { |
chuanga | 0:43ff9e3bc244 | 54 | _dhcp = false; |
chuanga | 0:43ff9e3bc244 | 55 | |
chuanga | 0:43ff9e3bc244 | 56 | strncpy(_ip_address, ip_address ? ip_address : "", sizeof(_ip_address)); |
chuanga | 0:43ff9e3bc244 | 57 | _ip_address[sizeof(_ip_address) - 1] = '\0'; |
chuanga | 0:43ff9e3bc244 | 58 | strncpy(_netmask, netmask ? netmask : "", sizeof(_netmask)); |
chuanga | 0:43ff9e3bc244 | 59 | _netmask[sizeof(_netmask) - 1] = '\0'; |
chuanga | 0:43ff9e3bc244 | 60 | strncpy(_gateway, gateway ? gateway : "", sizeof(_gateway)); |
chuanga | 0:43ff9e3bc244 | 61 | _gateway[sizeof(_gateway) - 1] = '\0'; |
chuanga | 0:43ff9e3bc244 | 62 | |
chuanga | 0:43ff9e3bc244 | 63 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 64 | } |
chuanga | 0:43ff9e3bc244 | 65 | |
chuanga | 0:43ff9e3bc244 | 66 | nsapi_error_t ESP32InterfaceAP::set_dhcp(bool dhcp) |
chuanga | 0:43ff9e3bc244 | 67 | { |
chuanga | 0:43ff9e3bc244 | 68 | _dhcp = dhcp; |
chuanga | 0:43ff9e3bc244 | 69 | |
chuanga | 0:43ff9e3bc244 | 70 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 71 | } |
chuanga | 0:43ff9e3bc244 | 72 | |
chuanga | 0:43ff9e3bc244 | 73 | int ESP32InterfaceAP::connect(const char *ssid, const char *pass, nsapi_security_t security, |
chuanga | 0:43ff9e3bc244 | 74 | uint8_t channel) |
chuanga | 0:43ff9e3bc244 | 75 | { |
chuanga | 0:43ff9e3bc244 | 76 | int ret; |
chuanga | 0:43ff9e3bc244 | 77 | |
chuanga | 0:43ff9e3bc244 | 78 | ret = set_credentials(ssid, pass, security); |
chuanga | 0:43ff9e3bc244 | 79 | if (ret != 0) { |
chuanga | 0:43ff9e3bc244 | 80 | return ret; |
chuanga | 0:43ff9e3bc244 | 81 | } |
chuanga | 0:43ff9e3bc244 | 82 | |
chuanga | 0:43ff9e3bc244 | 83 | ret = set_channel(channel); |
chuanga | 0:43ff9e3bc244 | 84 | if (ret != 0) { |
chuanga | 0:43ff9e3bc244 | 85 | return ret; |
chuanga | 0:43ff9e3bc244 | 86 | } |
chuanga | 0:43ff9e3bc244 | 87 | |
chuanga | 0:43ff9e3bc244 | 88 | return connect(); |
chuanga | 0:43ff9e3bc244 | 89 | } |
chuanga | 0:43ff9e3bc244 | 90 | |
chuanga | 0:43ff9e3bc244 | 91 | int ESP32InterfaceAP::connect() |
chuanga | 0:43ff9e3bc244 | 92 | { |
chuanga | 0:43ff9e3bc244 | 93 | if (!_esp->set_mode(ESP32::WIFIMODE_STATION_SOFTAP)) { |
chuanga | 0:43ff9e3bc244 | 94 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 95 | } |
chuanga | 0:43ff9e3bc244 | 96 | |
chuanga | 0:43ff9e3bc244 | 97 | if (!_esp->dhcp(_dhcp, 1)) { |
chuanga | 0:43ff9e3bc244 | 98 | return NSAPI_ERROR_DHCP_FAILURE; |
chuanga | 0:43ff9e3bc244 | 99 | } |
chuanga | 0:43ff9e3bc244 | 100 | |
chuanga | 0:43ff9e3bc244 | 101 | if (!_dhcp) { |
chuanga | 0:43ff9e3bc244 | 102 | if (!_esp->set_network_ap(_ip_address, _netmask, _gateway)) { |
chuanga | 0:43ff9e3bc244 | 103 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 104 | } |
chuanga | 0:43ff9e3bc244 | 105 | } |
chuanga | 0:43ff9e3bc244 | 106 | |
chuanga | 0:43ff9e3bc244 | 107 | if (!_esp->config_soft_ap(_own_ssid, _own_pass, _own_ch, (uint8_t)_own_sec)) { |
chuanga | 0:43ff9e3bc244 | 108 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 109 | } |
chuanga | 0:43ff9e3bc244 | 110 | |
chuanga | 0:43ff9e3bc244 | 111 | _connection_status = NSAPI_STATUS_GLOBAL_UP; |
chuanga | 0:43ff9e3bc244 | 112 | if (_connection_status_cb) { |
chuanga | 0:43ff9e3bc244 | 113 | _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status); |
chuanga | 0:43ff9e3bc244 | 114 | } |
chuanga | 0:43ff9e3bc244 | 115 | |
chuanga | 0:43ff9e3bc244 | 116 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 117 | } |
chuanga | 0:43ff9e3bc244 | 118 | |
chuanga | 0:43ff9e3bc244 | 119 | int ESP32InterfaceAP::set_credentials(const char *ssid, const char *pass, nsapi_security_t security) |
chuanga | 0:43ff9e3bc244 | 120 | { |
chuanga | 0:43ff9e3bc244 | 121 | switch (security) { |
chuanga | 0:43ff9e3bc244 | 122 | case NSAPI_SECURITY_NONE: |
chuanga | 0:43ff9e3bc244 | 123 | case NSAPI_SECURITY_WPA: |
chuanga | 0:43ff9e3bc244 | 124 | case NSAPI_SECURITY_WPA2: |
chuanga | 0:43ff9e3bc244 | 125 | case NSAPI_SECURITY_WPA_WPA2: |
chuanga | 0:43ff9e3bc244 | 126 | _own_sec = security; |
chuanga | 0:43ff9e3bc244 | 127 | break; |
chuanga | 0:43ff9e3bc244 | 128 | case NSAPI_SECURITY_UNKNOWN: |
chuanga | 0:43ff9e3bc244 | 129 | case NSAPI_SECURITY_WEP: |
chuanga | 0:43ff9e3bc244 | 130 | default: |
chuanga | 0:43ff9e3bc244 | 131 | return NSAPI_ERROR_UNSUPPORTED; |
chuanga | 0:43ff9e3bc244 | 132 | } |
chuanga | 0:43ff9e3bc244 | 133 | |
chuanga | 0:43ff9e3bc244 | 134 | memset(_own_ssid, 0, sizeof(_own_ssid)); |
chuanga | 0:43ff9e3bc244 | 135 | strncpy(_own_ssid, ssid, sizeof(_own_ssid)); |
chuanga | 0:43ff9e3bc244 | 136 | |
chuanga | 0:43ff9e3bc244 | 137 | memset(_own_pass, 0, sizeof(_own_pass)); |
chuanga | 0:43ff9e3bc244 | 138 | strncpy(_own_pass, pass, sizeof(_own_pass)); |
chuanga | 0:43ff9e3bc244 | 139 | |
chuanga | 0:43ff9e3bc244 | 140 | return 0; |
chuanga | 0:43ff9e3bc244 | 141 | } |
chuanga | 0:43ff9e3bc244 | 142 | |
chuanga | 0:43ff9e3bc244 | 143 | int ESP32InterfaceAP::set_channel(uint8_t channel) |
chuanga | 0:43ff9e3bc244 | 144 | { |
chuanga | 0:43ff9e3bc244 | 145 | if (channel != 0) { |
chuanga | 0:43ff9e3bc244 | 146 | _own_ch = channel; |
chuanga | 0:43ff9e3bc244 | 147 | } |
chuanga | 0:43ff9e3bc244 | 148 | |
chuanga | 0:43ff9e3bc244 | 149 | return 0; |
chuanga | 0:43ff9e3bc244 | 150 | } |
chuanga | 0:43ff9e3bc244 | 151 | |
chuanga | 0:43ff9e3bc244 | 152 | int ESP32InterfaceAP::disconnect() |
chuanga | 0:43ff9e3bc244 | 153 | { |
chuanga | 0:43ff9e3bc244 | 154 | if (!_esp->set_mode(ESP32::WIFIMODE_STATION)) { |
chuanga | 0:43ff9e3bc244 | 155 | return NSAPI_ERROR_DEVICE_ERROR; |
chuanga | 0:43ff9e3bc244 | 156 | } |
chuanga | 0:43ff9e3bc244 | 157 | |
chuanga | 0:43ff9e3bc244 | 158 | _connection_status = NSAPI_STATUS_DISCONNECTED; |
chuanga | 0:43ff9e3bc244 | 159 | if (_connection_status_cb) { |
chuanga | 0:43ff9e3bc244 | 160 | _connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status); |
chuanga | 0:43ff9e3bc244 | 161 | } |
chuanga | 0:43ff9e3bc244 | 162 | |
chuanga | 0:43ff9e3bc244 | 163 | return NSAPI_ERROR_OK; |
chuanga | 0:43ff9e3bc244 | 164 | } |
chuanga | 0:43ff9e3bc244 | 165 | |
chuanga | 0:43ff9e3bc244 | 166 | const char *ESP32InterfaceAP::get_ip_address() |
chuanga | 0:43ff9e3bc244 | 167 | { |
chuanga | 0:43ff9e3bc244 | 168 | return _esp->getIPAddress_ap(); |
chuanga | 0:43ff9e3bc244 | 169 | } |
chuanga | 0:43ff9e3bc244 | 170 | |
chuanga | 0:43ff9e3bc244 | 171 | const char *ESP32InterfaceAP::get_mac_address() |
chuanga | 0:43ff9e3bc244 | 172 | { |
chuanga | 0:43ff9e3bc244 | 173 | return _esp->getMACAddress_ap(); |
chuanga | 0:43ff9e3bc244 | 174 | } |
chuanga | 0:43ff9e3bc244 | 175 | |
chuanga | 0:43ff9e3bc244 | 176 | const char *ESP32InterfaceAP::get_gateway() |
chuanga | 0:43ff9e3bc244 | 177 | { |
chuanga | 0:43ff9e3bc244 | 178 | return _esp->getGateway_ap(); |
chuanga | 0:43ff9e3bc244 | 179 | } |
chuanga | 0:43ff9e3bc244 | 180 | |
chuanga | 0:43ff9e3bc244 | 181 | const char *ESP32InterfaceAP::get_netmask() |
chuanga | 0:43ff9e3bc244 | 182 | { |
chuanga | 0:43ff9e3bc244 | 183 | return _esp->getNetmask_ap(); |
chuanga | 0:43ff9e3bc244 | 184 | } |
chuanga | 0:43ff9e3bc244 | 185 | |
chuanga | 0:43ff9e3bc244 | 186 | int8_t ESP32InterfaceAP::get_rssi() |
chuanga | 0:43ff9e3bc244 | 187 | { |
chuanga | 0:43ff9e3bc244 | 188 | return 0; |
chuanga | 0:43ff9e3bc244 | 189 | } |
chuanga | 0:43ff9e3bc244 | 190 | |
chuanga | 0:43ff9e3bc244 | 191 | int ESP32InterfaceAP::scan(WiFiAccessPoint *res, unsigned count) |
chuanga | 0:43ff9e3bc244 | 192 | { |
chuanga | 0:43ff9e3bc244 | 193 | return _esp->scan(res, count); |
chuanga | 0:43ff9e3bc244 | 194 | } |
chuanga | 0:43ff9e3bc244 | 195 | |
chuanga | 0:43ff9e3bc244 | 196 | void ESP32InterfaceAP::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) |
chuanga | 0:43ff9e3bc244 | 197 | { |
chuanga | 0:43ff9e3bc244 | 198 | _connection_status_cb = status_cb; |
chuanga | 0:43ff9e3bc244 | 199 | } |
chuanga | 0:43ff9e3bc244 | 200 | |
chuanga | 0:43ff9e3bc244 | 201 | nsapi_connection_status_t ESP32InterfaceAP::get_connection_status() const |
chuanga | 0:43ff9e3bc244 | 202 | { |
chuanga | 0:43ff9e3bc244 | 203 | return _connection_status; |
chuanga | 0:43ff9e3bc244 | 204 | } |
chuanga | 0:43ff9e3bc244 | 205 |