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