Generic Pelion Device Management example for various Advantech modules.

This example is known to work great on the following platforms:

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.

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?

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