Generic Pelion Device Management example for various Renesas-based boards.

DEPRECATED

This example application is not maintained and not recommended. It uses an old version of Mbed OS, Pelion DM, and Arm toolchain. It doesn't work with Mbed Studio.

Please use: https://os.mbed.com/teams/mbed-os-examples/code/mbed-os-example-pelion/

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

https://os.mbed.com/media/cache/platforms/GR-LYCHEE_and_cam.png.250x250_q85.png https://os.mbed.com/media/cache/platforms/GR-PEACH_C_trans.png.250x250_q85.png

Follow the Quick-Start instructions: https://cloud.mbed.com/quick-start

Example functionality

This example showcases the following device functionality:

  • On user button click, increment Pelion LWM2M button resource.
  • Allow the user to change the state of the board LED from Pelion LWM2M led_state resource and PUT request.

Instructions to use this program with Mbed CLI


1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/Renesas/code/pelion-example-common
cd pelion-example-common


2. Install the CLOUD_SDK_API_KEY

mbed config -G CLOUD_SDK_API_KEY <PELION_DM_API_KEY>

For instructions on how to generate your API key, please see the documentation.

3. Initialize firmware credentials (done once per repository). You can use the following command:

mbed dm init -d "<your company name in Pelion DM>" --model-name "<product model identifier>" -q --force

If above command do not work for your Mbed CLI, please consider upgrading Mbed CLI to version 1.8.x or above.

4. Compile and program:

mbed compile -t <toolchain> -m <TARGET_BOARD>

(supported toolchains : GCC_ARM / ARM / IAR)

Note

This platform and application is suitable for evaluation and initial development. For production purposes, we recommend to use a different variant with built-in security features - for more information please contact Renesas (https://en-support.renesas.com/mytickets)

Committer:
screamer
Date:
Wed Mar 27 18:38:10 2019 +0000
Revision:
8:5ab220a2ac08
Parent:
4:6061130e9a4f
Improve inline documentation
Update to Mbed OS 5.11.5 and PDMC 2.2.1

Who changed what in which revision?

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