Generic Pelion Device Management example for various short range U-blox-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:

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

UBLOX_EVK_ODIN_W2

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.
  • (currently disabled) Read ADC temperature and ADC vref, and report them as Pelion LWM2M resources.

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/ublox/code/pelion-example-common-odinw2

cd pelion-example-common-odinw2

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)

Committer:
fahimalavi
Date:
Fri Oct 04 18:56:50 2019 +0500
Revision:
3:fd659f991dc9
Parent:
0:7e9105051efa
Reverting invalid commit during merge

Who changed what in which revision?

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