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:
Tue Feb 19 23:22:35 2019 +0000
Branch:
smcc-2.1.1.3
Revision:
4:6061130e9a4f
Parent:
0:6d2053b84a92
Child:
8:5ab220a2ac08
Updated to SMCC 2.1.1.3 and Mbed OS 5.11.4

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