Generic Pelion Device Management example for various U-blox-based boards.

Dependencies:   ublox-at-cellular-interface ublox-cellular-base

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:

For Odin-W2 please go to Repository link

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

UBLOX_C030_U201

UBLOX_C030_R412M

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

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)

Committer:
fahimalavi
Date:
Wed Jul 03 10:58:54 2019 +0500
Revision:
11:7cdc7397d270
Parent:
10:4c2ed226f5a8
Child:
12:dd17519b5e30
Build errors fixed for ODIN_W2 platform

Who changed what in which revision?

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