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 /* ESP32Interface Example
chuanga 0:43ff9e3bc244 2 * Copyright (c) 2015 ARM Limited
chuanga 0:43ff9e3bc244 3 * Copyright (c) 2017 Renesas Electronics Corporation
chuanga 0:43ff9e3bc244 4 *
chuanga 0:43ff9e3bc244 5 * Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 6 * you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 7 * You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 8 *
chuanga 0:43ff9e3bc244 9 * http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 10 *
chuanga 0:43ff9e3bc244 11 * Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 12 * distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 14 * See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 15 * limitations under the License.
chuanga 0:43ff9e3bc244 16 */
chuanga 0:43ff9e3bc244 17
chuanga 0:43ff9e3bc244 18 #ifndef ESP32_H
chuanga 0:43ff9e3bc244 19 #define ESP32_H
chuanga 0:43ff9e3bc244 20
chuanga 0:43ff9e3bc244 21 #if DEVICE_SERIAL && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_PRESENT)
chuanga 0:43ff9e3bc244 22 #include <vector>
chuanga 0:43ff9e3bc244 23 #include <stdint.h>
chuanga 0:43ff9e3bc244 24 #include <stdlib.h>
chuanga 0:43ff9e3bc244 25 #include "drivers/DigitalOut.h"
chuanga 0:43ff9e3bc244 26 #include "drivers/SerialBase.h"
chuanga 0:43ff9e3bc244 27 #include "drivers/UARTSerial.h"
chuanga 0:43ff9e3bc244 28 #include "features/netsocket/nsapi_types.h"
chuanga 0:43ff9e3bc244 29 #include "features/netsocket/WiFiAccessPoint.h"
chuanga 0:43ff9e3bc244 30 #include "PinNames.h"
chuanga 0:43ff9e3bc244 31 #include "platform/ATCmdParser.h"
chuanga 0:43ff9e3bc244 32 #include "platform/Callback.h"
chuanga 0:43ff9e3bc244 33 #include "platform/mbed_error.h"
chuanga 0:43ff9e3bc244 34 #include "rtos/Mutex.h"
chuanga 0:43ff9e3bc244 35 #include "rtos/ThisThread.h"
chuanga 0:43ff9e3bc244 36
chuanga 0:43ff9e3bc244 37 #ifndef ESP32_CONNECT_TIMEOUT
chuanga 0:43ff9e3bc244 38 #define ESP32_CONNECT_TIMEOUT 15000
chuanga 0:43ff9e3bc244 39 #endif
chuanga 0:43ff9e3bc244 40 #ifndef ESP32_SEND_TIMEOUT
chuanga 0:43ff9e3bc244 41 #define ESP32_SEND_TIMEOUT 2000
chuanga 0:43ff9e3bc244 42 #endif
chuanga 0:43ff9e3bc244 43 #ifndef ESP32_RECV_TIMEOUT
chuanga 0:43ff9e3bc244 44 #define ESP32_RECV_TIMEOUT 2000
chuanga 0:43ff9e3bc244 45 #endif
chuanga 0:43ff9e3bc244 46 #ifndef ESP32_MISC_TIMEOUT
chuanga 0:43ff9e3bc244 47 #define ESP32_MISC_TIMEOUT 2000
chuanga 0:43ff9e3bc244 48 #endif
chuanga 0:43ff9e3bc244 49
chuanga 0:43ff9e3bc244 50 /** ESP32Interface class.
chuanga 0:43ff9e3bc244 51 This is an interface to a ESP32 radio.
chuanga 0:43ff9e3bc244 52 */
chuanga 0:43ff9e3bc244 53 class ESP32
chuanga 0:43ff9e3bc244 54 {
chuanga 0:43ff9e3bc244 55 public:
chuanga 0:43ff9e3bc244 56 /**
chuanga 0:43ff9e3bc244 57 * Static method to create or retrieve the single ESP32 instance
chuanga 0:43ff9e3bc244 58 */
chuanga 0:43ff9e3bc244 59 static ESP32 * getESP32Inst(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
chuanga 0:43ff9e3bc244 60 PinName rts, PinName cts, int baudrate);
chuanga 0:43ff9e3bc244 61
chuanga 0:43ff9e3bc244 62 ESP32(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
chuanga 0:43ff9e3bc244 63 PinName rts, PinName cts, int baudrate);
chuanga 0:43ff9e3bc244 64
chuanga 0:43ff9e3bc244 65 /**
chuanga 0:43ff9e3bc244 66 * Check firmware version of ESP8266
chuanga 0:43ff9e3bc244 67 *
chuanga 0:43ff9e3bc244 68 * @return integer firmware version or -1 if firmware query command gives outdated response
chuanga 0:43ff9e3bc244 69 */
chuanga 0:43ff9e3bc244 70 int get_firmware_version(void);
chuanga 0:43ff9e3bc244 71
chuanga 0:43ff9e3bc244 72 /**
chuanga 0:43ff9e3bc244 73 * Sets the Wi-Fi Mode
chuanga 0:43ff9e3bc244 74 *
chuanga 0:43ff9e3bc244 75 * @param mode mode of WIFI 1-client, 2-host, 3-both
chuanga 0:43ff9e3bc244 76 * @return true only if ESP32 was setup correctly
chuanga 0:43ff9e3bc244 77 */
chuanga 0:43ff9e3bc244 78 bool set_mode(int mode);
chuanga 0:43ff9e3bc244 79
chuanga 0:43ff9e3bc244 80 /**
chuanga 0:43ff9e3bc244 81 * Enable/Disable DHCP
chuanga 0:43ff9e3bc244 82 *
chuanga 0:43ff9e3bc244 83 * @param enabled DHCP enabled when true
chuanga 0:43ff9e3bc244 84 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
chuanga 0:43ff9e3bc244 85 * @return true only if ESP32 enables/disables DHCP successfully
chuanga 0:43ff9e3bc244 86 */
chuanga 0:43ff9e3bc244 87 bool dhcp(bool enabled, int mode);
chuanga 0:43ff9e3bc244 88
chuanga 0:43ff9e3bc244 89 /**
chuanga 0:43ff9e3bc244 90 * Connect ESP32 to AP
chuanga 0:43ff9e3bc244 91 *
chuanga 0:43ff9e3bc244 92 * @param ap the name of the AP
chuanga 0:43ff9e3bc244 93 * @param passPhrase the password of AP
chuanga 0:43ff9e3bc244 94 * @return true only if ESP32 is connected successfully
chuanga 0:43ff9e3bc244 95 */
chuanga 0:43ff9e3bc244 96 bool connect(const char *ap, const char *passPhrase);
chuanga 0:43ff9e3bc244 97
chuanga 0:43ff9e3bc244 98 /**
chuanga 0:43ff9e3bc244 99 * Disconnect ESP32 from AP
chuanga 0:43ff9e3bc244 100 *
chuanga 0:43ff9e3bc244 101 * @return true only if ESP32 is disconnected successfully
chuanga 0:43ff9e3bc244 102 */
chuanga 0:43ff9e3bc244 103 bool disconnect(void);
chuanga 0:43ff9e3bc244 104
chuanga 0:43ff9e3bc244 105 /**
chuanga 0:43ff9e3bc244 106 * Get the IP address of ESP32
chuanga 0:43ff9e3bc244 107 *
chuanga 0:43ff9e3bc244 108 * @return null-teriminated IP address or null if no IP address is assigned
chuanga 0:43ff9e3bc244 109 */
chuanga 0:43ff9e3bc244 110 const char *getIPAddress(void);
chuanga 0:43ff9e3bc244 111 const char *getIPAddress_ap(void);
chuanga 0:43ff9e3bc244 112
chuanga 0:43ff9e3bc244 113 /**
chuanga 0:43ff9e3bc244 114 * Get the MAC address of ESP32
chuanga 0:43ff9e3bc244 115 *
chuanga 0:43ff9e3bc244 116 * @return null-terminated MAC address or null if no MAC address is assigned
chuanga 0:43ff9e3bc244 117 */
chuanga 0:43ff9e3bc244 118 const char *getMACAddress(void);
chuanga 0:43ff9e3bc244 119 const char *getMACAddress_ap(void);
chuanga 0:43ff9e3bc244 120
chuanga 0:43ff9e3bc244 121 /** Get the local gateway
chuanga 0:43ff9e3bc244 122 *
chuanga 0:43ff9e3bc244 123 * @return Null-terminated representation of the local gateway
chuanga 0:43ff9e3bc244 124 * or null if no network mask has been recieved
chuanga 0:43ff9e3bc244 125 */
chuanga 0:43ff9e3bc244 126 const char *getGateway();
chuanga 0:43ff9e3bc244 127 const char *getGateway_ap();
chuanga 0:43ff9e3bc244 128
chuanga 0:43ff9e3bc244 129 /** Get the local network mask
chuanga 0:43ff9e3bc244 130 *
chuanga 0:43ff9e3bc244 131 * @return Null-terminated representation of the local network mask
chuanga 0:43ff9e3bc244 132 * or null if no network mask has been recieved
chuanga 0:43ff9e3bc244 133 */
chuanga 0:43ff9e3bc244 134 const char *getNetmask();
chuanga 0:43ff9e3bc244 135 const char *getNetmask_ap();
chuanga 0:43ff9e3bc244 136
chuanga 0:43ff9e3bc244 137 /* Return RSSI for active connection
chuanga 0:43ff9e3bc244 138 *
chuanga 0:43ff9e3bc244 139 * @return Measured RSSI
chuanga 0:43ff9e3bc244 140 */
chuanga 0:43ff9e3bc244 141 int8_t getRSSI();
chuanga 0:43ff9e3bc244 142
chuanga 0:43ff9e3bc244 143 /**
chuanga 0:43ff9e3bc244 144 * Check if ESP32 is conenected
chuanga 0:43ff9e3bc244 145 *
chuanga 0:43ff9e3bc244 146 * @return true only if the chip has an IP address
chuanga 0:43ff9e3bc244 147 */
chuanga 0:43ff9e3bc244 148 bool isConnected(void);
chuanga 0:43ff9e3bc244 149
chuanga 0:43ff9e3bc244 150 /** Scan for available networks
chuanga 0:43ff9e3bc244 151 *
chuanga 0:43ff9e3bc244 152 * @param ap Pointer to allocated array to store discovered AP
chuanga 0:43ff9e3bc244 153 * @param limit Size of allocated @a res array, or 0 to only count available AP
chuanga 0:43ff9e3bc244 154 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
chuanga 0:43ff9e3bc244 155 * see @a nsapi_error
chuanga 0:43ff9e3bc244 156 */
chuanga 0:43ff9e3bc244 157 int scan(WiFiAccessPoint *res, unsigned limit);
chuanga 0:43ff9e3bc244 158
chuanga 0:43ff9e3bc244 159 /**
chuanga 0:43ff9e3bc244 160 * Open a socketed connection
chuanga 0:43ff9e3bc244 161 *
chuanga 0:43ff9e3bc244 162 * @param type the type of socket to open "UDP" or "TCP"
chuanga 0:43ff9e3bc244 163 * @param id id to give the new socket, valid 0-4
chuanga 0:43ff9e3bc244 164 * @param port port to open connection with
chuanga 0:43ff9e3bc244 165 * @param addr the IP address of the destination
chuanga 0:43ff9e3bc244 166 * @param addr the IP address of the destination
chuanga 0:43ff9e3bc244 167 * @param opt type=" UDP" : UDP socket's local port, zero means any
chuanga 0:43ff9e3bc244 168 * type=" TCP" : TCP connection's keep alive time, zero means disabled
chuanga 0:43ff9e3bc244 169 * @return true only if socket opened successfully
chuanga 0:43ff9e3bc244 170 */
chuanga 0:43ff9e3bc244 171 bool open(const char *type, int id, const char* addr, int port, int opt = 0);
chuanga 0:43ff9e3bc244 172
chuanga 0:43ff9e3bc244 173 /**
chuanga 0:43ff9e3bc244 174 * Sends data to an open socket
chuanga 0:43ff9e3bc244 175 *
chuanga 0:43ff9e3bc244 176 * @param id id of socket to send to
chuanga 0:43ff9e3bc244 177 * @param data data to be sent
chuanga 0:43ff9e3bc244 178 * @param amount amount of data to be sent - max 1024
chuanga 0:43ff9e3bc244 179 * @return true only if data sent successfully
chuanga 0:43ff9e3bc244 180 */
chuanga 0:43ff9e3bc244 181 bool send(int id, const void *data, uint32_t amount);
chuanga 0:43ff9e3bc244 182
chuanga 0:43ff9e3bc244 183 /**
chuanga 0:43ff9e3bc244 184 * Receives data from an open socket
chuanga 0:43ff9e3bc244 185 *
chuanga 0:43ff9e3bc244 186 * @param id id to receive from
chuanga 0:43ff9e3bc244 187 * @param data placeholder for returned information
chuanga 0:43ff9e3bc244 188 * @param amount number of bytes to be received
chuanga 0:43ff9e3bc244 189 * @return the number of bytes received
chuanga 0:43ff9e3bc244 190 */
chuanga 0:43ff9e3bc244 191 int32_t recv(int id, void *data, uint32_t amount, uint32_t timeout = ESP32_RECV_TIMEOUT);
chuanga 0:43ff9e3bc244 192
chuanga 0:43ff9e3bc244 193 /**
chuanga 0:43ff9e3bc244 194 * Closes a socket
chuanga 0:43ff9e3bc244 195 *
chuanga 0:43ff9e3bc244 196 * @param id id of socket to close, valid only 0-4
chuanga 0:43ff9e3bc244 197 * @param wait_close
chuanga 0:43ff9e3bc244 198 * @return true only if socket is closed successfully
chuanga 0:43ff9e3bc244 199 */
chuanga 0:43ff9e3bc244 200 bool close(int id, bool wait_close = false);
chuanga 0:43ff9e3bc244 201
chuanga 0:43ff9e3bc244 202 /**
chuanga 0:43ff9e3bc244 203 * Allows timeout to be changed between commands
chuanga 0:43ff9e3bc244 204 *
chuanga 0:43ff9e3bc244 205 * @param timeout_ms timeout of the connection
chuanga 0:43ff9e3bc244 206 */
chuanga 0:43ff9e3bc244 207 void setTimeout(uint32_t timeout_ms = ESP32_MISC_TIMEOUT);
chuanga 0:43ff9e3bc244 208
chuanga 0:43ff9e3bc244 209 /**
chuanga 0:43ff9e3bc244 210 * Checks if data is available
chuanga 0:43ff9e3bc244 211 */
chuanga 0:43ff9e3bc244 212 bool readable();
chuanga 0:43ff9e3bc244 213
chuanga 0:43ff9e3bc244 214 /**
chuanga 0:43ff9e3bc244 215 * Checks if data can be written
chuanga 0:43ff9e3bc244 216 */
chuanga 0:43ff9e3bc244 217 bool writeable();
chuanga 0:43ff9e3bc244 218
chuanga 0:43ff9e3bc244 219 void socket_attach(int id, void (*callback)(void *), void *data);
chuanga 0:43ff9e3bc244 220 int get_free_id();
chuanga 0:43ff9e3bc244 221
chuanga 0:43ff9e3bc244 222 bool config_soft_ap(const char *ap, const char *passPhrase, uint8_t chl, uint8_t ecn);
chuanga 0:43ff9e3bc244 223
chuanga 0:43ff9e3bc244 224 bool restart();
chuanga 0:43ff9e3bc244 225 bool get_ssid(char *ap);
chuanga 0:43ff9e3bc244 226 bool cre_server(int port);
chuanga 0:43ff9e3bc244 227 bool del_server();
chuanga 0:43ff9e3bc244 228 bool accept(int * p_id);
chuanga 0:43ff9e3bc244 229
chuanga 0:43ff9e3bc244 230 bool set_network(const char *ip_address, const char *netmask, const char *gateway);
chuanga 0:43ff9e3bc244 231 bool set_network_ap(const char *ip_address, const char *netmask, const char *gateway);
chuanga 0:43ff9e3bc244 232
chuanga 0:43ff9e3bc244 233 /**
chuanga 0:43ff9e3bc244 234 * Attach a function to call whenever network state has changed
chuanga 0:43ff9e3bc244 235 *
chuanga 0:43ff9e3bc244 236 * @param func A pointer to a void function, or 0 to set as none
chuanga 0:43ff9e3bc244 237 */
chuanga 0:43ff9e3bc244 238 void attach_wifi_status(mbed::Callback<void(int8_t)> status_cb);
chuanga 0:43ff9e3bc244 239
chuanga 0:43ff9e3bc244 240 /** Get the connection status
chuanga 0:43ff9e3bc244 241 *
chuanga 0:43ff9e3bc244 242 * @return The connection status according to ConnectionStatusType
chuanga 0:43ff9e3bc244 243 */
chuanga 0:43ff9e3bc244 244 int8_t get_wifi_status() const;
chuanga 0:43ff9e3bc244 245
chuanga 0:43ff9e3bc244 246 static const int8_t WIFIMODE_STATION = 1;
chuanga 0:43ff9e3bc244 247 static const int8_t WIFIMODE_SOFTAP = 2;
chuanga 0:43ff9e3bc244 248 static const int8_t WIFIMODE_STATION_SOFTAP = 3;
chuanga 0:43ff9e3bc244 249 static const int8_t SOCKET_COUNT = 5;
chuanga 0:43ff9e3bc244 250
chuanga 0:43ff9e3bc244 251 static const int8_t STATUS_DISCONNECTED = 0;
chuanga 0:43ff9e3bc244 252 static const int8_t STATUS_CONNECTED = 1;
chuanga 0:43ff9e3bc244 253 static const int8_t STATUS_GOT_IP = 2;
chuanga 0:43ff9e3bc244 254
chuanga 0:43ff9e3bc244 255 private:
chuanga 0:43ff9e3bc244 256 mbed::DigitalOut * _p_wifi_en;
chuanga 0:43ff9e3bc244 257 mbed::DigitalOut * _p_wifi_io0;
chuanga 0:43ff9e3bc244 258 bool init_end;
chuanga 0:43ff9e3bc244 259 mbed::UARTSerial _serial;
chuanga 0:43ff9e3bc244 260 mbed::ATCmdParser _parser;
chuanga 0:43ff9e3bc244 261 struct packet {
chuanga 0:43ff9e3bc244 262 struct packet *next;
chuanga 0:43ff9e3bc244 263 int id;
chuanga 0:43ff9e3bc244 264 uint32_t len;
chuanga 0:43ff9e3bc244 265 uint32_t index;
chuanga 0:43ff9e3bc244 266 // data follows
chuanga 0:43ff9e3bc244 267 } *_packets, **_packets_end;
chuanga 0:43ff9e3bc244 268 int _wifi_mode;
chuanga 0:43ff9e3bc244 269 int _baudrate;
chuanga 0:43ff9e3bc244 270 PinName _rts;
chuanga 0:43ff9e3bc244 271 PinName _cts;
chuanga 0:43ff9e3bc244 272 int _flow_control;
chuanga 0:43ff9e3bc244 273 uint32_t last_timeout_ms;
chuanga 0:43ff9e3bc244 274
chuanga 0:43ff9e3bc244 275 std::vector<int> _accept_id;
chuanga 0:43ff9e3bc244 276 uint32_t _id_bits;
chuanga 0:43ff9e3bc244 277 uint32_t _id_bits_close;
chuanga 0:43ff9e3bc244 278 bool _server_act;
chuanga 0:43ff9e3bc244 279 rtos::Mutex _smutex; // Protect serial port access
chuanga 0:43ff9e3bc244 280 static ESP32 * instESP32;
chuanga 0:43ff9e3bc244 281 int8_t _wifi_status;
chuanga 0:43ff9e3bc244 282 mbed::Callback<void(int8_t)> _wifi_status_cb;
chuanga 0:43ff9e3bc244 283
chuanga 0:43ff9e3bc244 284 bool _ids[SOCKET_COUNT];
chuanga 0:43ff9e3bc244 285 struct {
chuanga 0:43ff9e3bc244 286 void (*callback)(void *);
chuanga 0:43ff9e3bc244 287 void *data;
chuanga 0:43ff9e3bc244 288 int Notified;
chuanga 0:43ff9e3bc244 289 } _cbs[SOCKET_COUNT];
chuanga 0:43ff9e3bc244 290
chuanga 0:43ff9e3bc244 291 bool startup();
chuanga 0:43ff9e3bc244 292 bool reset(void);
chuanga 0:43ff9e3bc244 293 void debugOn(bool debug);
chuanga 0:43ff9e3bc244 294 void socket_handler(bool connect, int id);
chuanga 0:43ff9e3bc244 295 void _connect_handler_0();
chuanga 0:43ff9e3bc244 296 void _connect_handler_1();
chuanga 0:43ff9e3bc244 297 void _connect_handler_2();
chuanga 0:43ff9e3bc244 298 void _connect_handler_3();
chuanga 0:43ff9e3bc244 299 void _connect_handler_4();
chuanga 0:43ff9e3bc244 300 void _closed_handler_0();
chuanga 0:43ff9e3bc244 301 void _closed_handler_1();
chuanga 0:43ff9e3bc244 302 void _closed_handler_2();
chuanga 0:43ff9e3bc244 303 void _closed_handler_3();
chuanga 0:43ff9e3bc244 304 void _closed_handler_4();
chuanga 0:43ff9e3bc244 305 void _connection_status_handler();
chuanga 0:43ff9e3bc244 306 void _packet_handler();
chuanga 0:43ff9e3bc244 307 void _clear_socket_packets(int id);
chuanga 0:43ff9e3bc244 308 void event();
chuanga 0:43ff9e3bc244 309 bool recv_ap(nsapi_wifi_ap_t *ap);
chuanga 0:43ff9e3bc244 310
chuanga 0:43ff9e3bc244 311 char _ip_buffer[16];
chuanga 0:43ff9e3bc244 312 char _gateway_buffer[16];
chuanga 0:43ff9e3bc244 313 char _netmask_buffer[16];
chuanga 0:43ff9e3bc244 314 char _mac_buffer[18];
chuanga 0:43ff9e3bc244 315
chuanga 0:43ff9e3bc244 316 char _ip_buffer_ap[16];
chuanga 0:43ff9e3bc244 317 char _gateway_buffer_ap[16];
chuanga 0:43ff9e3bc244 318 char _netmask_buffer_ap[16];
chuanga 0:43ff9e3bc244 319 char _mac_buffer_ap[18];
chuanga 0:43ff9e3bc244 320 };
chuanga 0:43ff9e3bc244 321 #endif
chuanga 0:43ff9e3bc244 322 #endif