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:
MACRUM
Date:
Sat Dec 15 12:47:53 2018 +0900
Revision:
0:6d2053b84a92
Child:
4:6061130e9a4f
initial commit

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"
MACRUM 0:6d2053b84a92 23
MACRUM 0:6d2053b84a92 24 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
MACRUM 0:6d2053b84a92 25 // 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 26 EventQueue eventQueue;
MACRUM 0:6d2053b84a92 27
MACRUM 0:6d2053b84a92 28 // Default block device
MACRUM 0:6d2053b84a92 29 BlockDevice *bd = BlockDevice::get_default_instance();
MACRUM 0:6d2053b84a92 30 FATFileSystem fs("fs");
MACRUM 0:6d2053b84a92 31
MACRUM 0:6d2053b84a92 32 // Default network interface object
MACRUM 0:6d2053b84a92 33 NetworkInterface *net = NetworkInterface::get_default_instance();
MACRUM 0:6d2053b84a92 34
MACRUM 0:6d2053b84a92 35 InterruptIn btn(USER_BUTTON0);
MACRUM 0:6d2053b84a92 36 // Declaring pointers for access to Pelion Device Management Client resources outside of main()
MACRUM 0:6d2053b84a92 37 MbedCloudClientResource *button_res;
MACRUM 0:6d2053b84a92 38 MbedCloudClientResource *pattern_res;
MACRUM 0:6d2053b84a92 39
MACRUM 0:6d2053b84a92 40 void button_press() {
MACRUM 0:6d2053b84a92 41 int v = button_res->get_value_int() + 1;
MACRUM 0:6d2053b84a92 42
MACRUM 0:6d2053b84a92 43 button_res->set_value(v);
MACRUM 0:6d2053b84a92 44
MACRUM 0:6d2053b84a92 45 printf("User button clicked %d times\n", v);
MACRUM 0:6d2053b84a92 46 }
MACRUM 0:6d2053b84a92 47
MACRUM 0:6d2053b84a92 48 /**
MACRUM 0:6d2053b84a92 49 * PUT handler
MACRUM 0:6d2053b84a92 50 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 51 * @param newValue Updated value for the resource
MACRUM 0:6d2053b84a92 52 */
MACRUM 0:6d2053b84a92 53 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
MACRUM 0:6d2053b84a92 54 printf("PUT received, new value: %s\n", newValue.c_str());
MACRUM 0:6d2053b84a92 55 }
MACRUM 0:6d2053b84a92 56
MACRUM 0:6d2053b84a92 57 /**
MACRUM 0:6d2053b84a92 58 * POST handler
MACRUM 0:6d2053b84a92 59 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 60 * @param buffer If a body was passed to the POST function, this contains the data.
MACRUM 0:6d2053b84a92 61 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
MACRUM 0:6d2053b84a92 62 * @param size Size of the body
MACRUM 0:6d2053b84a92 63 */
MACRUM 0:6d2053b84a92 64 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
MACRUM 0:6d2053b84a92 65 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
MACRUM 0:6d2053b84a92 66
MACRUM 0:6d2053b84a92 67 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
MACRUM 0:6d2053b84a92 68
MACRUM 0:6d2053b84a92 69 // Parse the pattern string, and toggle the LED in that pattern
MACRUM 0:6d2053b84a92 70 string s = std::string(pattern_res->get_value().c_str());
MACRUM 0:6d2053b84a92 71 size_t i = 0;
MACRUM 0:6d2053b84a92 72 size_t pos = s.find(':');
MACRUM 0:6d2053b84a92 73 while (pos != string::npos) {
MACRUM 0:6d2053b84a92 74 wait_ms(atoi(s.substr(i, pos - i).c_str()));
MACRUM 0:6d2053b84a92 75 augmentedLed = !augmentedLed;
MACRUM 0:6d2053b84a92 76
MACRUM 0:6d2053b84a92 77 i = ++pos;
MACRUM 0:6d2053b84a92 78 pos = s.find(':', pos);
MACRUM 0:6d2053b84a92 79
MACRUM 0:6d2053b84a92 80 if (pos == string::npos) {
MACRUM 0:6d2053b84a92 81 wait_ms(atoi(s.substr(i, s.length()).c_str()));
MACRUM 0:6d2053b84a92 82 augmentedLed = !augmentedLed;
MACRUM 0:6d2053b84a92 83 }
MACRUM 0:6d2053b84a92 84 }
MACRUM 0:6d2053b84a92 85 }
MACRUM 0:6d2053b84a92 86
MACRUM 0:6d2053b84a92 87 /**
MACRUM 0:6d2053b84a92 88 * Notification callback handler
MACRUM 0:6d2053b84a92 89 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 90 * @param status The delivery status of the notification
MACRUM 0:6d2053b84a92 91 */
MACRUM 0:6d2053b84a92 92 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
MACRUM 0:6d2053b84a92 93 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
MACRUM 0:6d2053b84a92 94 }
MACRUM 0:6d2053b84a92 95
MACRUM 0:6d2053b84a92 96 /**
MACRUM 0:6d2053b84a92 97 * Registration callback handler
MACRUM 0:6d2053b84a92 98 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
MACRUM 0:6d2053b84a92 99 */
MACRUM 0:6d2053b84a92 100 void registered(const ConnectorClientEndpointInfo *endpoint) {
MACRUM 0:6d2053b84a92 101 printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
MACRUM 0:6d2053b84a92 102 }
MACRUM 0:6d2053b84a92 103
MACRUM 0:6d2053b84a92 104 int main(void) {
MACRUM 0:6d2053b84a92 105 printf("Starting Simple Pelion Device Management Client example\n");
MACRUM 0:6d2053b84a92 106 printf("Connecting to the network...\n");
MACRUM 0:6d2053b84a92 107
MACRUM 0:6d2053b84a92 108 // Connect to the internet (DHCP is expected to be on)
MACRUM 0:6d2053b84a92 109 nsapi_error_t status = net->connect();
MACRUM 0:6d2053b84a92 110
MACRUM 0:6d2053b84a92 111 if (status != NSAPI_ERROR_OK) {
MACRUM 0:6d2053b84a92 112 printf("Connecting to the network failed %d!\n", status);
MACRUM 0:6d2053b84a92 113 return -1;
MACRUM 0:6d2053b84a92 114 }
MACRUM 0:6d2053b84a92 115
MACRUM 0:6d2053b84a92 116 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
MACRUM 0:6d2053b84a92 117
MACRUM 0:6d2053b84a92 118 // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
MACRUM 0:6d2053b84a92 119 SimpleMbedCloudClient client(net, bd, &fs);
MACRUM 0:6d2053b84a92 120 int client_status = client.init();
MACRUM 0:6d2053b84a92 121 if (client_status != 0) {
MACRUM 0:6d2053b84a92 122 printf("Pelion Client initialization failed (%d)\n", client_status);
MACRUM 0:6d2053b84a92 123 return -1;
MACRUM 0:6d2053b84a92 124 }
MACRUM 0:6d2053b84a92 125
MACRUM 0:6d2053b84a92 126 // Creating resources, which can be written or read from the cloud
MACRUM 0:6d2053b84a92 127 button_res = client.create_resource("3200/0/5501", "button_count");
MACRUM 0:6d2053b84a92 128 button_res->set_value(0);
MACRUM 0:6d2053b84a92 129 button_res->methods(M2MMethod::GET);
MACRUM 0:6d2053b84a92 130 button_res->observable(true);
MACRUM 0:6d2053b84a92 131 button_res->attach_notification_callback(button_callback);
MACRUM 0:6d2053b84a92 132
MACRUM 0:6d2053b84a92 133 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
MACRUM 0:6d2053b84a92 134 pattern_res->set_value("500:500:500:500:500:500:500:500");
MACRUM 0:6d2053b84a92 135 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
MACRUM 0:6d2053b84a92 136 pattern_res->attach_put_callback(pattern_updated);
MACRUM 0:6d2053b84a92 137
MACRUM 0:6d2053b84a92 138 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
MACRUM 0:6d2053b84a92 139 blink_res->methods(M2MMethod::POST);
MACRUM 0:6d2053b84a92 140 blink_res->attach_post_callback(blink_callback);
MACRUM 0:6d2053b84a92 141
MACRUM 0:6d2053b84a92 142 printf("Initialized Pelion Client. Registering...\n");
MACRUM 0:6d2053b84a92 143
MACRUM 0:6d2053b84a92 144 // Callback that fires when registering is complete
MACRUM 0:6d2053b84a92 145 client.on_registered(&registered);
MACRUM 0:6d2053b84a92 146
MACRUM 0:6d2053b84a92 147 // Register with Pelion Device Management
MACRUM 0:6d2053b84a92 148 client.register_and_connect();
MACRUM 0:6d2053b84a92 149
MACRUM 0:6d2053b84a92 150 // Setup the button
MACRUM 0:6d2053b84a92 151 btn.mode(PullUp);
MACRUM 0:6d2053b84a92 152
MACRUM 0:6d2053b84a92 153 // The button fall handler is placed in the event queue so it will run in
MACRUM 0:6d2053b84a92 154 // thread context instead of ISR context, which allows safely updating the cloud resource
MACRUM 0:6d2053b84a92 155 btn.fall(eventQueue.event(&button_press));
MACRUM 0:6d2053b84a92 156
MACRUM 0:6d2053b84a92 157 // You can easily run the eventQueue in a separate thread if required
MACRUM 0:6d2053b84a92 158 eventQueue.dispatch_forever();
MACRUM 0:6d2053b84a92 159 }
MACRUM 0:6d2053b84a92 160 #endif