Mbed OS example of Pelion device management client

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

https://os.mbed.com/media/cache/platforms/M46B_mbed.png.250x250_q85.jpg

To use this example, connect ESP8266 Wi-Fi module (Grove UART WiFi) to the AdBun-M46B board as below:

ESP8266 Wi-FiAdBun-M46B
TXPL1 (CN4-22)
RXPL2 (CN4-24)
VINVCC3.3 (CN11-5)
GNDGND (CN11-6)

https://dl.dropboxusercontent.com/s/5j4glkwhjg5m5ay/IMG_7457.jpg

Example functionality

  • Initialize, connect and register to Pelion DM
  • Interact with the user through the serial port (115200 bauds)
    • Press enter through putty/minicom to simulate button
    • Press i to print endpoint name
    • Press Ctrl-C to to unregister
    • Press r to reset storage and reboot (warning: it generates a new device ID!)

Instructions to use this program with Mbed CLI


1. Import the application into your desktop:

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


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. Connect Pins used with Wifi shield (ESP8266) and SD card shield:

5. Edit mbed_app.json to specify Wi-Fi access point information

            "nsapi.default-wifi-ssid"                   : "\"SSID\"",
            "nsapi.default-wifi-password"               : "\"PASSWORD\""

6. Compile and program:

mbed compile -t <toolchain> -m TMPM46B

(supported toolchains : GCC_ARM / ARM / IAR)

Committer:
MACRUM
Date:
Thu Dec 12 10:26:06 2019 +0900
Revision:
0:9f917a7bf2da
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:9f917a7bf2da 1 // ----------------------------------------------------------------------------
MACRUM 0:9f917a7bf2da 2 // Copyright 2016-2019 ARM Ltd.
MACRUM 0:9f917a7bf2da 3 //
MACRUM 0:9f917a7bf2da 4 // SPDX-License-Identifier: Apache-2.0
MACRUM 0:9f917a7bf2da 5 //
MACRUM 0:9f917a7bf2da 6 // Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:9f917a7bf2da 7 // you may not use this file except in compliance with the License.
MACRUM 0:9f917a7bf2da 8 // You may obtain a copy of the License at
MACRUM 0:9f917a7bf2da 9 //
MACRUM 0:9f917a7bf2da 10 // http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:9f917a7bf2da 11 //
MACRUM 0:9f917a7bf2da 12 // Unless required by applicable law or agreed to in writing, software
MACRUM 0:9f917a7bf2da 13 // distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:9f917a7bf2da 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:9f917a7bf2da 15 // See the License for the specific language governing permissions and
MACRUM 0:9f917a7bf2da 16 // limitations under the License.
MACRUM 0:9f917a7bf2da 17 // ----------------------------------------------------------------------------
MACRUM 0:9f917a7bf2da 18 #ifndef MBED_TEST_MODE
MACRUM 0:9f917a7bf2da 19 #include "mbed.h"
MACRUM 0:9f917a7bf2da 20 #include "kv_config.h"
MACRUM 0:9f917a7bf2da 21 #include "mbed-cloud-client/MbedCloudClient.h" // Required for new MbedCloudClient()
MACRUM 0:9f917a7bf2da 22 #include "factory_configurator_client.h" // Required for fcc_* functions and FCC_* defines
MACRUM 0:9f917a7bf2da 23 #include "m2mresource.h" // Required for M2MResource
MACRUM 0:9f917a7bf2da 24
MACRUM 0:9f917a7bf2da 25 #include "mbed-trace/mbed_trace.h" // Required for mbed_trace_*
MACRUM 0:9f917a7bf2da 26
MACRUM 0:9f917a7bf2da 27 // Pointers to the resources that will be created in main_application().
MACRUM 0:9f917a7bf2da 28 static MbedCloudClient *cloud_client;
MACRUM 0:9f917a7bf2da 29 static bool cloud_client_running = true;
MACRUM 0:9f917a7bf2da 30 static NetworkInterface *network = NULL;
MACRUM 0:9f917a7bf2da 31
MACRUM 0:9f917a7bf2da 32 // Fake entropy needed for non-TRNG boards. Suitable only for demo devices.
MACRUM 0:9f917a7bf2da 33 const uint8_t MBED_CLOUD_DEV_ENTROPY[] = { 0xf6, 0xd6, 0xc0, 0x09, 0x9e, 0x6e, 0xf2, 0x37, 0xdc, 0x29, 0x88, 0xf1, 0x57, 0x32, 0x7d, 0xde, 0xac, 0xb3, 0x99, 0x8c, 0xb9, 0x11, 0x35, 0x18, 0xeb, 0x48, 0x29, 0x03, 0x6a, 0x94, 0x6d, 0xe8, 0x40, 0xc0, 0x28, 0xcc, 0xe4, 0x04, 0xc3, 0x1f, 0x4b, 0xc2, 0xe0, 0x68, 0xa0, 0x93, 0xe6, 0x3a };
MACRUM 0:9f917a7bf2da 34
MACRUM 0:9f917a7bf2da 35 static M2MResource* m2m_get_res;
MACRUM 0:9f917a7bf2da 36 static M2MResource* m2m_put_res;
MACRUM 0:9f917a7bf2da 37 static M2MResource* m2m_post_res;
MACRUM 0:9f917a7bf2da 38 static M2MResource* m2m_deregister_res;
MACRUM 0:9f917a7bf2da 39
MACRUM 0:9f917a7bf2da 40 void print_client_ids(void)
MACRUM 0:9f917a7bf2da 41 {
MACRUM 0:9f917a7bf2da 42 printf("Account ID: %s\n", cloud_client->endpoint_info()->account_id.c_str());
MACRUM 0:9f917a7bf2da 43 printf("Endpoint name: %s\n", cloud_client->endpoint_info()->internal_endpoint_name.c_str());
MACRUM 0:9f917a7bf2da 44 printf("Device Id: %s\n\n", cloud_client->endpoint_info()->endpoint_name.c_str());
MACRUM 0:9f917a7bf2da 45 }
MACRUM 0:9f917a7bf2da 46
MACRUM 0:9f917a7bf2da 47 void button_press(void)
MACRUM 0:9f917a7bf2da 48 {
MACRUM 0:9f917a7bf2da 49 m2m_get_res->set_value(m2m_get_res->get_value_int() + 1);
MACRUM 0:9f917a7bf2da 50 printf("Counter %" PRIu64 "\n", m2m_get_res->get_value_int());
MACRUM 0:9f917a7bf2da 51 }
MACRUM 0:9f917a7bf2da 52
MACRUM 0:9f917a7bf2da 53 void put_update(const char* /*object_name*/)
MACRUM 0:9f917a7bf2da 54 {
MACRUM 0:9f917a7bf2da 55 printf("PUT update %d\n", (int)m2m_put_res->get_value_int());
MACRUM 0:9f917a7bf2da 56 }
MACRUM 0:9f917a7bf2da 57
MACRUM 0:9f917a7bf2da 58 void execute_post(void* /*arguments*/)
MACRUM 0:9f917a7bf2da 59 {
MACRUM 0:9f917a7bf2da 60 printf("POST executed\n");
MACRUM 0:9f917a7bf2da 61 }
MACRUM 0:9f917a7bf2da 62
MACRUM 0:9f917a7bf2da 63 void deregister_client(void)
MACRUM 0:9f917a7bf2da 64 {
MACRUM 0:9f917a7bf2da 65 printf("Unregistering and disconnecting from the network.\n");
MACRUM 0:9f917a7bf2da 66 cloud_client->close();
MACRUM 0:9f917a7bf2da 67 }
MACRUM 0:9f917a7bf2da 68
MACRUM 0:9f917a7bf2da 69 void deregister(void* /*arguments*/)
MACRUM 0:9f917a7bf2da 70 {
MACRUM 0:9f917a7bf2da 71 printf("POST deregister executed\n");
MACRUM 0:9f917a7bf2da 72 m2m_deregister_res->send_delayed_post_response();
MACRUM 0:9f917a7bf2da 73
MACRUM 0:9f917a7bf2da 74 deregister_client();
MACRUM 0:9f917a7bf2da 75 }
MACRUM 0:9f917a7bf2da 76
MACRUM 0:9f917a7bf2da 77 void client_registered(void)
MACRUM 0:9f917a7bf2da 78 {
MACRUM 0:9f917a7bf2da 79 printf("Client registered.\n");
MACRUM 0:9f917a7bf2da 80 print_client_ids();
MACRUM 0:9f917a7bf2da 81 }
MACRUM 0:9f917a7bf2da 82
MACRUM 0:9f917a7bf2da 83 void client_unregistered(void)
MACRUM 0:9f917a7bf2da 84 {
MACRUM 0:9f917a7bf2da 85 printf("Client unregistered.\n");
MACRUM 0:9f917a7bf2da 86 (void) network->disconnect();
MACRUM 0:9f917a7bf2da 87 cloud_client_running = false;
MACRUM 0:9f917a7bf2da 88 }
MACRUM 0:9f917a7bf2da 89
MACRUM 0:9f917a7bf2da 90 void client_error(int err)
MACRUM 0:9f917a7bf2da 91 {
MACRUM 0:9f917a7bf2da 92 printf("client_error(%d) -> %s\n", err, cloud_client->error_description());
MACRUM 0:9f917a7bf2da 93 }
MACRUM 0:9f917a7bf2da 94
MACRUM 0:9f917a7bf2da 95 void update_progress(uint32_t progress, uint32_t total)
MACRUM 0:9f917a7bf2da 96 {
MACRUM 0:9f917a7bf2da 97 uint8_t percent = (uint8_t)((uint64_t)progress * 100 / total);
MACRUM 0:9f917a7bf2da 98 printf("Update progress = %" PRIu8 "%%\n", percent);
MACRUM 0:9f917a7bf2da 99 }
MACRUM 0:9f917a7bf2da 100
MACRUM 0:9f917a7bf2da 101 int main(void)
MACRUM 0:9f917a7bf2da 102 {
MACRUM 0:9f917a7bf2da 103 int status;
MACRUM 0:9f917a7bf2da 104
MACRUM 0:9f917a7bf2da 105 status = mbed_trace_init();
MACRUM 0:9f917a7bf2da 106 if (status != 0) {
MACRUM 0:9f917a7bf2da 107 printf("mbed_trace_init() failed with %d\n", status);
MACRUM 0:9f917a7bf2da 108 return -1;
MACRUM 0:9f917a7bf2da 109 }
MACRUM 0:9f917a7bf2da 110
MACRUM 0:9f917a7bf2da 111 // Mount default kvstore
MACRUM 0:9f917a7bf2da 112 printf("Application ready\n");
MACRUM 0:9f917a7bf2da 113 status = kv_init_storage_config();
MACRUM 0:9f917a7bf2da 114 if (status != MBED_SUCCESS) {
MACRUM 0:9f917a7bf2da 115 printf("kv_init_storage_config() - failed, status %d\n", status);
MACRUM 0:9f917a7bf2da 116 return -1;
MACRUM 0:9f917a7bf2da 117 }
MACRUM 0:9f917a7bf2da 118
MACRUM 0:9f917a7bf2da 119 // Connect with NetworkInterface
MACRUM 0:9f917a7bf2da 120 printf("Connect to network\n");
MACRUM 0:9f917a7bf2da 121 network = NetworkInterface::get_default_instance();
MACRUM 0:9f917a7bf2da 122 if (network == NULL) {
MACRUM 0:9f917a7bf2da 123 printf("Failed to get default NetworkInterface\n");
MACRUM 0:9f917a7bf2da 124 return -1;
MACRUM 0:9f917a7bf2da 125 }
MACRUM 0:9f917a7bf2da 126 status = network->connect();
MACRUM 0:9f917a7bf2da 127 if (status != NSAPI_ERROR_OK) {
MACRUM 0:9f917a7bf2da 128 printf("NetworkInterface failed to connect with %d\n", status);
MACRUM 0:9f917a7bf2da 129 return -1;
MACRUM 0:9f917a7bf2da 130 }
MACRUM 0:9f917a7bf2da 131
MACRUM 0:9f917a7bf2da 132 printf("Network initialized, connected with IP %s\n\n", network->get_ip_address());
MACRUM 0:9f917a7bf2da 133
MACRUM 0:9f917a7bf2da 134 // Run developer flow
MACRUM 0:9f917a7bf2da 135 printf("Start developer flow\n");
MACRUM 0:9f917a7bf2da 136 status = fcc_init();
MACRUM 0:9f917a7bf2da 137 if (status != FCC_STATUS_SUCCESS) {
MACRUM 0:9f917a7bf2da 138 printf("fcc_init() failed with %d\n", status);
MACRUM 0:9f917a7bf2da 139 return -1;
MACRUM 0:9f917a7bf2da 140 }
MACRUM 0:9f917a7bf2da 141
MACRUM 0:9f917a7bf2da 142 // Inject hardcoded entropy for the device. Suitable only for demo devices.
MACRUM 0:9f917a7bf2da 143 (void) fcc_entropy_set(MBED_CLOUD_DEV_ENTROPY, sizeof(MBED_CLOUD_DEV_ENTROPY));
MACRUM 0:9f917a7bf2da 144 status = fcc_developer_flow();
MACRUM 0:9f917a7bf2da 145 if (status != FCC_STATUS_SUCCESS && status != FCC_STATUS_KCM_FILE_EXIST_ERROR && status != FCC_STATUS_CA_ERROR) {
MACRUM 0:9f917a7bf2da 146 printf("fcc_developer_flow() failed with %d\n", status);
MACRUM 0:9f917a7bf2da 147 return -1;
MACRUM 0:9f917a7bf2da 148 }
MACRUM 0:9f917a7bf2da 149
MACRUM 0:9f917a7bf2da 150 printf("Create resources\n");
MACRUM 0:9f917a7bf2da 151 M2MObjectList m2m_obj_list;
MACRUM 0:9f917a7bf2da 152
MACRUM 0:9f917a7bf2da 153 // GET resource 3200/0/5501
MACRUM 0:9f917a7bf2da 154 m2m_get_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3200, 0, 5501, M2MResourceInstance::INTEGER, M2MBase::GET_ALLOWED);
MACRUM 0:9f917a7bf2da 155 if (m2m_get_res->set_value(0) != true) {
MACRUM 0:9f917a7bf2da 156 printf("m2m_get_res->set_value() failed\n");
MACRUM 0:9f917a7bf2da 157 return -1;
MACRUM 0:9f917a7bf2da 158 }
MACRUM 0:9f917a7bf2da 159
MACRUM 0:9f917a7bf2da 160 // PUT resource 3201/0/5853
MACRUM 0:9f917a7bf2da 161 m2m_put_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3201, 0, 5853, M2MResourceInstance::INTEGER, M2MBase::GET_PUT_ALLOWED);
MACRUM 0:9f917a7bf2da 162 if (m2m_put_res->set_value(0) != true) {
MACRUM 0:9f917a7bf2da 163 printf("m2m_led_res->set_value() failed\n");
MACRUM 0:9f917a7bf2da 164 return -1;
MACRUM 0:9f917a7bf2da 165 }
MACRUM 0:9f917a7bf2da 166 if (m2m_put_res->set_value_updated_function(put_update) != true) {
MACRUM 0:9f917a7bf2da 167 printf("m2m_put_res->set_value_updated_function() failed\n");
MACRUM 0:9f917a7bf2da 168 return -1;
MACRUM 0:9f917a7bf2da 169 }
MACRUM 0:9f917a7bf2da 170
MACRUM 0:9f917a7bf2da 171 // POST resource 3201/0/5850
MACRUM 0:9f917a7bf2da 172 m2m_post_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 3201, 0, 5850, M2MResourceInstance::INTEGER, M2MBase::POST_ALLOWED);
MACRUM 0:9f917a7bf2da 173 if (m2m_post_res->set_execute_function(execute_post) != true) {
MACRUM 0:9f917a7bf2da 174 printf("m2m_post_res->set_execute_function() failed\n");
MACRUM 0:9f917a7bf2da 175 return -1;
MACRUM 0:9f917a7bf2da 176 }
MACRUM 0:9f917a7bf2da 177
MACRUM 0:9f917a7bf2da 178 // POST resource 5000/0/1 to trigger deregister.
MACRUM 0:9f917a7bf2da 179 m2m_deregister_res = M2MInterfaceFactory::create_resource(m2m_obj_list, 5000, 0, 1, M2MResourceInstance::INTEGER, M2MBase::POST_ALLOWED);
MACRUM 0:9f917a7bf2da 180
MACRUM 0:9f917a7bf2da 181 // Use delayed response
MACRUM 0:9f917a7bf2da 182 m2m_deregister_res->set_delayed_response(true);
MACRUM 0:9f917a7bf2da 183
MACRUM 0:9f917a7bf2da 184 if (m2m_deregister_res->set_execute_function(deregister) != true) {
MACRUM 0:9f917a7bf2da 185 printf("m2m_post_res->set_execute_function() failed\n");
MACRUM 0:9f917a7bf2da 186 return -1;
MACRUM 0:9f917a7bf2da 187 }
MACRUM 0:9f917a7bf2da 188
MACRUM 0:9f917a7bf2da 189 printf("Register Pelion Device Management Client\n\n");
MACRUM 0:9f917a7bf2da 190 cloud_client = new MbedCloudClient(client_registered, client_unregistered, client_error, NULL, update_progress);
MACRUM 0:9f917a7bf2da 191 cloud_client->add_objects(m2m_obj_list);
MACRUM 0:9f917a7bf2da 192 cloud_client->setup(network); // cloud_client->setup(NULL); -- https://jira.arm.com/browse/IOTCLT-3114
MACRUM 0:9f917a7bf2da 193
MACRUM 0:9f917a7bf2da 194 while(cloud_client_running) {
MACRUM 0:9f917a7bf2da 195 int in_char = getchar();
MACRUM 0:9f917a7bf2da 196 if (in_char == 'i') {
MACRUM 0:9f917a7bf2da 197 print_client_ids(); // When 'i' is pressed, print endpoint info
MACRUM 0:9f917a7bf2da 198 continue;
MACRUM 0:9f917a7bf2da 199 } else if (in_char == 'r') {
MACRUM 0:9f917a7bf2da 200 (void) fcc_storage_delete(); // When 'r' is pressed, erase storage and reboot the board.
MACRUM 0:9f917a7bf2da 201 printf("Storage erased, rebooting the device.\n\n");
MACRUM 0:9f917a7bf2da 202 wait(1);
MACRUM 0:9f917a7bf2da 203 NVIC_SystemReset();
MACRUM 0:9f917a7bf2da 204 } else if (in_char > 0 && in_char != 0x03) { // Ctrl+C is 0x03 in Mbed OS and Linux returns negative number
MACRUM 0:9f917a7bf2da 205 button_press(); // Simulate button press
MACRUM 0:9f917a7bf2da 206 continue;
MACRUM 0:9f917a7bf2da 207 }
MACRUM 0:9f917a7bf2da 208 deregister_client();
MACRUM 0:9f917a7bf2da 209 break;
MACRUM 0:9f917a7bf2da 210 }
MACRUM 0:9f917a7bf2da 211 return 0;
MACRUM 0:9f917a7bf2da 212 }
MACRUM 0:9f917a7bf2da 213
MACRUM 0:9f917a7bf2da 214 #endif /* MBED_TEST_MODE */