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 // ----------------------------------------------------------------------------
chuanga 0:43ff9e3bc244 2 // Copyright 2016-2018 ARM Ltd.
chuanga 0:43ff9e3bc244 3 //
chuanga 0:43ff9e3bc244 4 // SPDX-License-Identifier: Apache-2.0
chuanga 0:43ff9e3bc244 5 //
chuanga 0:43ff9e3bc244 6 // Licensed under the Apache License, Version 2.0 (the "License");
chuanga 0:43ff9e3bc244 7 // you may not use this file except in compliance with the License.
chuanga 0:43ff9e3bc244 8 // You may obtain a copy of the License at
chuanga 0:43ff9e3bc244 9 //
chuanga 0:43ff9e3bc244 10 // http://www.apache.org/licenses/LICENSE-2.0
chuanga 0:43ff9e3bc244 11 //
chuanga 0:43ff9e3bc244 12 // Unless required by applicable law or agreed to in writing, software
chuanga 0:43ff9e3bc244 13 // distributed under the License is distributed on an "AS IS" BASIS,
chuanga 0:43ff9e3bc244 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
chuanga 0:43ff9e3bc244 15 // See the License for the specific language governing permissions and
chuanga 0:43ff9e3bc244 16 // limitations under the License.
chuanga 0:43ff9e3bc244 17 // ----------------------------------------------------------------------------
chuanga 0:43ff9e3bc244 18 #ifndef MBED_TEST_MODE
chuanga 0:43ff9e3bc244 19
chuanga 0:43ff9e3bc244 20 #include "mbed.h"
chuanga 0:43ff9e3bc244 21 #include "simple-mbed-cloud-client.h"
chuanga 0:43ff9e3bc244 22 #include "FATFileSystem.h"
chuanga 0:43ff9e3bc244 23 #include "LittleFileSystem.h"
chuanga 0:43ff9e3bc244 24
chuanga 0:43ff9e3bc244 25 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi.
chuanga 0:43ff9e3bc244 26 NetworkInterface *net = NetworkInterface::get_default_instance();
chuanga 0:43ff9e3bc244 27
chuanga 0:43ff9e3bc244 28 // Default block device available on the target board
chuanga 0:43ff9e3bc244 29 BlockDevice *bd = BlockDevice::get_default_instance();
chuanga 0:43ff9e3bc244 30
chuanga 0:43ff9e3bc244 31 #if COMPONENT_SD || COMPONENT_NUSD
chuanga 0:43ff9e3bc244 32 // Use FATFileSystem for SD card type blockdevices
chuanga 0:43ff9e3bc244 33 //FATFileSystem fs("fs", bd);
chuanga 0:43ff9e3bc244 34 #else
chuanga 0:43ff9e3bc244 35 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions
chuanga 0:43ff9e3bc244 36 //LittleFileSystem fs("fs", bd);
chuanga 0:43ff9e3bc244 37 #endif
chuanga 0:43ff9e3bc244 38
chuanga 0:43ff9e3bc244 39 FileSystem *fs = FileSystem::get_default_instance();
chuanga 0:43ff9e3bc244 40
chuanga 0:43ff9e3bc244 41 #if USE_BUTTON == 1
chuanga 0:43ff9e3bc244 42 InterruptIn button(BUTTON1);
chuanga 0:43ff9e3bc244 43 #endif /* USE_BUTTON */
chuanga 0:43ff9e3bc244 44
chuanga 0:43ff9e3bc244 45 // Default LED to use for PUT/POST example
chuanga 0:43ff9e3bc244 46 DigitalOut led(LED1);
chuanga 0:43ff9e3bc244 47
chuanga 0:43ff9e3bc244 48 // Declaring pointers for access to Pelion Device Management Client resources outside of main()
chuanga 0:43ff9e3bc244 49 MbedCloudClientResource *button_res;
chuanga 0:43ff9e3bc244 50 MbedCloudClientResource *led_res;
chuanga 0:43ff9e3bc244 51 MbedCloudClientResource *post_res;
chuanga 0:43ff9e3bc244 52
chuanga 0:43ff9e3bc244 53 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
chuanga 0:43ff9e3bc244 54 // This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed
chuanga 0:43ff9e3bc244 55 EventQueue eventQueue;
chuanga 0:43ff9e3bc244 56
chuanga 0:43ff9e3bc244 57 /**
chuanga 0:43ff9e3bc244 58 * PUT handler - sets the value of the built-in LED
chuanga 0:43ff9e3bc244 59 * @param resource The resource that triggered the callback
chuanga 0:43ff9e3bc244 60 * @param newValue Updated value for the resource
chuanga 0:43ff9e3bc244 61 */
chuanga 0:43ff9e3bc244 62 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
chuanga 0:43ff9e3bc244 63 printf("PUT received. New value: %s\n", newValue.c_str());
chuanga 0:43ff9e3bc244 64 led = atoi(newValue.c_str());
chuanga 0:43ff9e3bc244 65 }
chuanga 0:43ff9e3bc244 66
chuanga 0:43ff9e3bc244 67 /**
chuanga 0:43ff9e3bc244 68 * POST handler - prints the content of the payload
chuanga 0:43ff9e3bc244 69 * @param resource The resource that triggered the callback
chuanga 0:43ff9e3bc244 70 * @param buffer If a body was passed to the POST function, this contains the data.
chuanga 0:43ff9e3bc244 71 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
chuanga 0:43ff9e3bc244 72 * @param size Size of the body
chuanga 0:43ff9e3bc244 73 */
chuanga 0:43ff9e3bc244 74 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
chuanga 0:43ff9e3bc244 75 printf("POST received (length %u). Payload: ", size);
chuanga 0:43ff9e3bc244 76 for (size_t ix = 0; ix < size; ix++) {
chuanga 0:43ff9e3bc244 77 printf("%02x ", buffer[ix]);
chuanga 0:43ff9e3bc244 78 }
chuanga 0:43ff9e3bc244 79 printf("\n");
chuanga 0:43ff9e3bc244 80 }
chuanga 0:43ff9e3bc244 81
chuanga 0:43ff9e3bc244 82 /**
chuanga 0:43ff9e3bc244 83 * Button handler
chuanga 0:43ff9e3bc244 84 * This function will be triggered either by a physical button press or by a ticker every 5 seconds (see below)
chuanga 0:43ff9e3bc244 85 */
chuanga 0:43ff9e3bc244 86 void button_press() {
chuanga 0:43ff9e3bc244 87 int v = button_res->get_value_int() + 1;
chuanga 0:43ff9e3bc244 88 button_res->set_value(v);
chuanga 0:43ff9e3bc244 89 printf("Button clicked %d times\n", v);
chuanga 0:43ff9e3bc244 90 }
chuanga 0:43ff9e3bc244 91
chuanga 0:43ff9e3bc244 92 /**
chuanga 0:43ff9e3bc244 93 * Notification callback handler
chuanga 0:43ff9e3bc244 94 * @param resource The resource that triggered the callback
chuanga 0:43ff9e3bc244 95 * @param status The delivery status of the notification
chuanga 0:43ff9e3bc244 96 */
chuanga 0:43ff9e3bc244 97 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
chuanga 0:43ff9e3bc244 98 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
chuanga 0:43ff9e3bc244 99 }
chuanga 0:43ff9e3bc244 100
chuanga 0:43ff9e3bc244 101 /**
chuanga 0:43ff9e3bc244 102 * Registration callback handler
chuanga 0:43ff9e3bc244 103 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
chuanga 0:43ff9e3bc244 104 */
chuanga 0:43ff9e3bc244 105 void registered(const ConnectorClientEndpointInfo *endpoint) {
chuanga 0:43ff9e3bc244 106 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
chuanga 0:43ff9e3bc244 107 }
chuanga 0:43ff9e3bc244 108
chuanga 0:43ff9e3bc244 109 int main(void) {
chuanga 0:43ff9e3bc244 110 printf("\nStarting Simple Pelion Device Management Client example\n");
chuanga 0:43ff9e3bc244 111
chuanga 0:43ff9e3bc244 112 #if USE_BUTTON == 1
chuanga 0:43ff9e3bc244 113 // If the User button is pressed ons start, then format storage.
chuanga 0:43ff9e3bc244 114 if (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE) {
chuanga 0:43ff9e3bc244 115 printf("User button is pushed on start. Formatting the storage...\n");
chuanga 0:43ff9e3bc244 116 int storage_status = StorageHelper::format(fs, bd);
chuanga 0:43ff9e3bc244 117 if (storage_status != 0) {
chuanga 0:43ff9e3bc244 118 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
chuanga 0:43ff9e3bc244 119 }
chuanga 0:43ff9e3bc244 120 } else {
chuanga 0:43ff9e3bc244 121 printf("You can hold the user button during boot to format the storage and change the device identity.\n");
chuanga 0:43ff9e3bc244 122 }
chuanga 0:43ff9e3bc244 123 #endif /* USE_BUTTON */
chuanga 0:43ff9e3bc244 124
chuanga 0:43ff9e3bc244 125 // Connect to the Internet (DHCP is expected to be on)
chuanga 0:43ff9e3bc244 126 printf("Connecting to the network using the default network interface...\n");
chuanga 0:43ff9e3bc244 127 net = NetworkInterface::get_default_instance();
chuanga 0:43ff9e3bc244 128
chuanga 0:43ff9e3bc244 129 nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION;
chuanga 0:43ff9e3bc244 130 while ((net_status = net->connect()) != NSAPI_ERROR_OK) {
chuanga 0:43ff9e3bc244 131 printf("Unable to connect to network (%d). Retrying...\n", net_status);
chuanga 0:43ff9e3bc244 132 }
chuanga 0:43ff9e3bc244 133
chuanga 0:43ff9e3bc244 134 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
chuanga 0:43ff9e3bc244 135
chuanga 0:43ff9e3bc244 136 printf("Initializing Pelion Device Management Client...\n");
chuanga 0:43ff9e3bc244 137
chuanga 0:43ff9e3bc244 138 // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
chuanga 0:43ff9e3bc244 139 SimpleMbedCloudClient client(net, bd, fs);
chuanga 0:43ff9e3bc244 140 int client_status = client.init();
chuanga 0:43ff9e3bc244 141 if (client_status != 0) {
chuanga 0:43ff9e3bc244 142 printf("Pelion Client initialization failed (%d)\n", client_status);
chuanga 0:43ff9e3bc244 143 return -1;
chuanga 0:43ff9e3bc244 144 }
chuanga 0:43ff9e3bc244 145
chuanga 0:43ff9e3bc244 146 // Creating resources, which can be written or read from the cloud
chuanga 0:43ff9e3bc244 147 button_res = client.create_resource("3200/0/5501", "button_count");
chuanga 0:43ff9e3bc244 148 button_res->set_value(0);
chuanga 0:43ff9e3bc244 149 button_res->methods(M2MMethod::GET);
chuanga 0:43ff9e3bc244 150 button_res->observable(true);
chuanga 0:43ff9e3bc244 151 button_res->attach_notification_callback(button_callback);
chuanga 0:43ff9e3bc244 152
chuanga 0:43ff9e3bc244 153 led_res = client.create_resource("3201/0/5853", "led_state");
chuanga 0:43ff9e3bc244 154 led_res->set_value(led.read());
chuanga 0:43ff9e3bc244 155 led_res->methods(M2MMethod::GET | M2MMethod::PUT);
chuanga 0:43ff9e3bc244 156 led_res->attach_put_callback(put_callback);
chuanga 0:43ff9e3bc244 157
chuanga 0:43ff9e3bc244 158 post_res = client.create_resource("3300/0/5605", "execute_function");
chuanga 0:43ff9e3bc244 159 post_res->methods(M2MMethod::POST);
chuanga 0:43ff9e3bc244 160 post_res->attach_post_callback(post_callback);
chuanga 0:43ff9e3bc244 161
chuanga 0:43ff9e3bc244 162 printf("Initialized Pelion Device Management Client. Registering...\n");
chuanga 0:43ff9e3bc244 163
chuanga 0:43ff9e3bc244 164 // Callback that fires when registering is complete
chuanga 0:43ff9e3bc244 165 client.on_registered(&registered);
chuanga 0:43ff9e3bc244 166
chuanga 0:43ff9e3bc244 167 // Register with Pelion DM
chuanga 0:43ff9e3bc244 168 client.register_and_connect();
chuanga 0:43ff9e3bc244 169
chuanga 0:43ff9e3bc244 170 #if USE_BUTTON == 1
chuanga 0:43ff9e3bc244 171 // The button fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
chuanga 0:43ff9e3bc244 172 button.fall(eventQueue.event(&button_press));
chuanga 0:43ff9e3bc244 173 printf("Press the user button to increment the LwM2M resource value...\n");
chuanga 0:43ff9e3bc244 174 #else
chuanga 0:43ff9e3bc244 175 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
chuanga 0:43ff9e3bc244 176 Ticker timer;
chuanga 0:43ff9e3bc244 177 timer.attach(eventQueue.event(&button_press), 5.0);
chuanga 0:43ff9e3bc244 178 printf("Simulating button press every 5 seconds...\n");
chuanga 0:43ff9e3bc244 179 #endif /* USE_BUTTON */
chuanga 0:43ff9e3bc244 180
chuanga 0:43ff9e3bc244 181 // You can easily run the eventQueue in a separate thread if required
chuanga 0:43ff9e3bc244 182 eventQueue.dispatch_forever();
chuanga 0:43ff9e3bc244 183 }
chuanga 0:43ff9e3bc244 184
chuanga 0:43ff9e3bc244 185 #endif /* MBED_TEST_MODE */