Mbed OS and Pelion Device Management example over WIFI for RHOMBIO_L476DMW1K IoT kit

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:

RHOMBIO_L476DMW1K over WiFI and using onboard QSPI flash.

/media/uploads/galonso/rhombio_l476dmw1k_kit_assembled.png

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

Example functionality

This example showcases the following device functionality:

  • Read onboard MCU ADC temperature and MCU ADC voltage reference, and report them as Pelion LWM2M resources (see image below).
  • 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.
  • External sensors can be easily added to the kit and then sensor readings reported as Pelion LWM2M resources.

/media/uploads/galonso/temperature_-_sensor_value_-_pelion_device_management_portal.png

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/Rhombio/code/pelion-example-rhombio-l476dmw1k

cd pelion-example-rhombio-l476dmw1k

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 RHOMBIO_L476DMW1K -f

(supported toolchains : GCC_ARM / ARM / IAR)

5. You can connect on a virtual terminal/COM port to the platform using:

mbed sterm -b 115200

This should give you an output similar to:

[BOOT] Mbed Bootloader
[BOOT] ARM: 0000000000000000000000000000000000000000
[BOOT] OEM: 0000000000000000000000000000000000000000
[BOOT] Layout: 0 800D24C
[BOOT] Active firmware integrity check:
[BOOT] SHA256: 9036E19F1414720659B2C2FAC8855F621AF533BA211D930119720E7A2BD60BF2
[BOOT] Version: 1558516039
[BOOT] Slot 0 is empty
[BOOT] Active firmware up-to-date
[BOOT] Application's start address: 0x8010400
[BOOT] Application's jump address: 0x8010889
[BOOT] Application's stack address: 0x20018000
[BOOT] Forwarding to application...

Starting Simple Pelion Device Management Client example
You can hold the user button during boot to format the storage and change the device identity.

Connecting to the network using Wifi...
Connected to the network successfully. IP address: 192.168.1.201
Initializing Pelion Device Management Client...
Initialized Pelion Client. Registering...
Registered to Pelion Device Management. Endpoint Name: 016a***********************10006d
                                                             
ADC temp: 23.1746 C,  vref: 0.3665 V         
                                                             
ADC temp: 23.2234 C,  vref: 0.3663 V      
Committer:
galonso@rhomb.io
Date:
Wed May 22 11:17:54 2019 +0200
Revision:
4:2aee28e71add
Parent:
0:9ff56a0e4c0d
new bootloader added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
galonso@rhomb.io 0:9ff56a0e4c0d 1 // ----------------------------------------------------------------------------
galonso@rhomb.io 0:9ff56a0e4c0d 2 // Copyright 2016-2018 ARM Ltd.
galonso@rhomb.io 0:9ff56a0e4c0d 3 //
galonso@rhomb.io 0:9ff56a0e4c0d 4 // SPDX-License-Identifier: Apache-2.0
galonso@rhomb.io 0:9ff56a0e4c0d 5 //
galonso@rhomb.io 0:9ff56a0e4c0d 6 // Licensed under the Apache License, Version 2.0 (the "License");
galonso@rhomb.io 0:9ff56a0e4c0d 7 // you may not use this file except in compliance with the License.
galonso@rhomb.io 0:9ff56a0e4c0d 8 // You may obtain a copy of the License at
galonso@rhomb.io 0:9ff56a0e4c0d 9 //
galonso@rhomb.io 0:9ff56a0e4c0d 10 // http://www.apache.org/licenses/LICENSE-2.0
galonso@rhomb.io 0:9ff56a0e4c0d 11 //
galonso@rhomb.io 0:9ff56a0e4c0d 12 // Unless required by applicable law or agreed to in writing, software
galonso@rhomb.io 0:9ff56a0e4c0d 13 // distributed under the License is distributed on an "AS IS" BASIS,
galonso@rhomb.io 0:9ff56a0e4c0d 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
galonso@rhomb.io 0:9ff56a0e4c0d 15 // See the License for the specific language governing permissions and
galonso@rhomb.io 0:9ff56a0e4c0d 16 // limitations under the License.
galonso@rhomb.io 0:9ff56a0e4c0d 17 // ----------------------------------------------------------------------------
galonso@rhomb.io 0:9ff56a0e4c0d 18 #ifndef MBED_TEST_MODE
galonso@rhomb.io 0:9ff56a0e4c0d 19
galonso@rhomb.io 0:9ff56a0e4c0d 20 #include "mbed.h"
galonso@rhomb.io 0:9ff56a0e4c0d 21 #include "simple-mbed-cloud-client.h"
galonso@rhomb.io 0:9ff56a0e4c0d 22 #include "LittleFileSystem.h"
galonso@rhomb.io 0:9ff56a0e4c0d 23
galonso@rhomb.io 0:9ff56a0e4c0d 24 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi.
galonso@rhomb.io 0:9ff56a0e4c0d 25 NetworkInterface *net;
galonso@rhomb.io 0:9ff56a0e4c0d 26
galonso@rhomb.io 0:9ff56a0e4c0d 27 // Default block device available on the target board
galonso@rhomb.io 0:9ff56a0e4c0d 28 BlockDevice* bd = BlockDevice::get_default_instance();
galonso@rhomb.io 0:9ff56a0e4c0d 29 SlicingBlockDevice sd(bd, 0, 2*1024*1024);
galonso@rhomb.io 0:9ff56a0e4c0d 30
galonso@rhomb.io 0:9ff56a0e4c0d 31 #if COMPONENT_SD || COMPONENT_NUSD
galonso@rhomb.io 0:9ff56a0e4c0d 32 // Use FATFileSystem for SD card type blockdevices
galonso@rhomb.io 0:9ff56a0e4c0d 33 FATFileSystem fs("fs");
galonso@rhomb.io 0:9ff56a0e4c0d 34 #else
galonso@rhomb.io 0:9ff56a0e4c0d 35 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions
galonso@rhomb.io 0:9ff56a0e4c0d 36 LittleFileSystem fs("fs");
galonso@rhomb.io 0:9ff56a0e4c0d 37 #endif
galonso@rhomb.io 0:9ff56a0e4c0d 38
galonso@rhomb.io 0:9ff56a0e4c0d 39 // Default User button for GET example and for resetting the storage
galonso@rhomb.io 0:9ff56a0e4c0d 40 InterruptIn button(BUTTON1);
galonso@rhomb.io 0:9ff56a0e4c0d 41 // Default LED to use for PUT/POST example
galonso@rhomb.io 0:9ff56a0e4c0d 42 DigitalOut led(LED1, 1);
galonso@rhomb.io 0:9ff56a0e4c0d 43
galonso@rhomb.io 0:9ff56a0e4c0d 44 // How often to fetch sensor data (in seconds)
galonso@rhomb.io 0:9ff56a0e4c0d 45 #define SENSORS_POLL_INTERVAL 3.0
galonso@rhomb.io 0:9ff56a0e4c0d 46
galonso@rhomb.io 0:9ff56a0e4c0d 47 // Send all sensor data or just limited (useful for when running out of memory)
galonso@rhomb.io 0:9ff56a0e4c0d 48 #define SEND_ALL_SENSORS
galonso@rhomb.io 0:9ff56a0e4c0d 49
galonso@rhomb.io 0:9ff56a0e4c0d 50 // Sensors related includes and initialization
galonso@rhomb.io 0:9ff56a0e4c0d 51
galonso@rhomb.io 0:9ff56a0e4c0d 52
galonso@rhomb.io 0:9ff56a0e4c0d 53 //static DevI2C devI2c(PB_11,PB_10);
galonso@rhomb.io 0:9ff56a0e4c0d 54 static DigitalOut shutdown_pin(PC_6);
galonso@rhomb.io 0:9ff56a0e4c0d 55 // Temperature reading from microcontroller
galonso@rhomb.io 0:9ff56a0e4c0d 56 AnalogIn adc_temp(ADC_TEMP);
galonso@rhomb.io 0:9ff56a0e4c0d 57 // Voltage reference reading from microcontroller
galonso@rhomb.io 0:9ff56a0e4c0d 58 AnalogIn adc_vref(ADC_VREF);
galonso@rhomb.io 0:9ff56a0e4c0d 59
galonso@rhomb.io 0:9ff56a0e4c0d 60 // Declaring pointers for access to Pelion Client resources outside of main()
galonso@rhomb.io 0:9ff56a0e4c0d 61 MbedCloudClientResource *res_button;
galonso@rhomb.io 0:9ff56a0e4c0d 62 MbedCloudClientResource *res_led;
galonso@rhomb.io 0:9ff56a0e4c0d 63
galonso@rhomb.io 0:9ff56a0e4c0d 64 // Additional resources for sensor readings
galonso@rhomb.io 0:9ff56a0e4c0d 65 #ifdef SEND_ALL_SENSORS
galonso@rhomb.io 0:9ff56a0e4c0d 66 MbedCloudClientResource *res_adc_temp;
galonso@rhomb.io 0:9ff56a0e4c0d 67 MbedCloudClientResource *res_adc_voltage;
galonso@rhomb.io 0:9ff56a0e4c0d 68 #endif /* SEND_ALL_SENSORS */
galonso@rhomb.io 0:9ff56a0e4c0d 69
galonso@rhomb.io 0:9ff56a0e4c0d 70 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
galonso@rhomb.io 0:9ff56a0e4c0d 71 // 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
galonso@rhomb.io 0:9ff56a0e4c0d 72 EventQueue eventQueue;
galonso@rhomb.io 0:9ff56a0e4c0d 73
galonso@rhomb.io 0:9ff56a0e4c0d 74 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
galonso@rhomb.io 0:9ff56a0e4c0d 75 static const ConnectorClientEndpointInfo* endpointInfo;
galonso@rhomb.io 0:9ff56a0e4c0d 76
galonso@rhomb.io 0:9ff56a0e4c0d 77 /**
galonso@rhomb.io 0:9ff56a0e4c0d 78 * PUT handler
galonso@rhomb.io 0:9ff56a0e4c0d 79 * @param resource The resource that triggered the callback
galonso@rhomb.io 0:9ff56a0e4c0d 80 * @param newValue Updated value for the resource
galonso@rhomb.io 0:9ff56a0e4c0d 81 */
galonso@rhomb.io 0:9ff56a0e4c0d 82 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
galonso@rhomb.io 0:9ff56a0e4c0d 83 printf("*** PUT received, new value: %s \n", newValue.c_str());
galonso@rhomb.io 0:9ff56a0e4c0d 84 led = atoi(newValue.c_str());
galonso@rhomb.io 0:9ff56a0e4c0d 85 }
galonso@rhomb.io 0:9ff56a0e4c0d 86
galonso@rhomb.io 0:9ff56a0e4c0d 87 /**
galonso@rhomb.io 0:9ff56a0e4c0d 88 * POST handler
galonso@rhomb.io 0:9ff56a0e4c0d 89 * @param resource The resource that triggered the callback
galonso@rhomb.io 0:9ff56a0e4c0d 90 * @param buffer If a body was passed to the POST function, this contains the data.
galonso@rhomb.io 0:9ff56a0e4c0d 91 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
galonso@rhomb.io 0:9ff56a0e4c0d 92 * @param size Size of the body
galonso@rhomb.io 0:9ff56a0e4c0d 93 */
galonso@rhomb.io 0:9ff56a0e4c0d 94 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
galonso@rhomb.io 0:9ff56a0e4c0d 95 printf("*** POST received (length %u). Payload: ", size);
galonso@rhomb.io 0:9ff56a0e4c0d 96 for (size_t ix = 0; ix < size; ix++) {
galonso@rhomb.io 0:9ff56a0e4c0d 97 printf("%02x ", buffer[ix]);
galonso@rhomb.io 0:9ff56a0e4c0d 98 }
galonso@rhomb.io 0:9ff56a0e4c0d 99 printf("\n");
galonso@rhomb.io 0:9ff56a0e4c0d 100 }
galonso@rhomb.io 0:9ff56a0e4c0d 101
galonso@rhomb.io 0:9ff56a0e4c0d 102 /**
galonso@rhomb.io 0:9ff56a0e4c0d 103 * Button function triggered by the physical button press.
galonso@rhomb.io 0:9ff56a0e4c0d 104 */
galonso@rhomb.io 0:9ff56a0e4c0d 105 void button_press() {
galonso@rhomb.io 0:9ff56a0e4c0d 106 int v = res_button->get_value_int() + 1;
galonso@rhomb.io 0:9ff56a0e4c0d 107 res_button->set_value(v);
galonso@rhomb.io 0:9ff56a0e4c0d 108 printf("*** Button clicked %d times \n", v);
galonso@rhomb.io 0:9ff56a0e4c0d 109 }
galonso@rhomb.io 0:9ff56a0e4c0d 110
galonso@rhomb.io 0:9ff56a0e4c0d 111 /**
galonso@rhomb.io 0:9ff56a0e4c0d 112 * Notification callback handler
galonso@rhomb.io 0:9ff56a0e4c0d 113 * @param resource The resource that triggered the callback
galonso@rhomb.io 0:9ff56a0e4c0d 114 * @param status The delivery status of the notification
galonso@rhomb.io 0:9ff56a0e4c0d 115 */
galonso@rhomb.io 0:9ff56a0e4c0d 116 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
galonso@rhomb.io 0:9ff56a0e4c0d 117 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status);
galonso@rhomb.io 0:9ff56a0e4c0d 118 }
galonso@rhomb.io 0:9ff56a0e4c0d 119
galonso@rhomb.io 0:9ff56a0e4c0d 120 /**
galonso@rhomb.io 0:9ff56a0e4c0d 121 * Registration callback handler
galonso@rhomb.io 0:9ff56a0e4c0d 122 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
galonso@rhomb.io 0:9ff56a0e4c0d 123 */
galonso@rhomb.io 0:9ff56a0e4c0d 124 void registered(const ConnectorClientEndpointInfo *endpoint) {
galonso@rhomb.io 0:9ff56a0e4c0d 125 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
galonso@rhomb.io 0:9ff56a0e4c0d 126 endpointInfo = endpoint;
galonso@rhomb.io 0:9ff56a0e4c0d 127 }
galonso@rhomb.io 0:9ff56a0e4c0d 128
galonso@rhomb.io 0:9ff56a0e4c0d 129 /**
galonso@rhomb.io 0:9ff56a0e4c0d 130 * Initialize sensors
galonso@rhomb.io 0:9ff56a0e4c0d 131 */
galonso@rhomb.io 0:9ff56a0e4c0d 132 void sensors_init() {
galonso@rhomb.io 0:9ff56a0e4c0d 133
galonso@rhomb.io 0:9ff56a0e4c0d 134 printf ("\nSensors configuration:\n");
galonso@rhomb.io 0:9ff56a0e4c0d 135 // Initialize sensors
galonso@rhomb.io 0:9ff56a0e4c0d 136
galonso@rhomb.io 0:9ff56a0e4c0d 137 // Call sensors enable routines
galonso@rhomb.io 0:9ff56a0e4c0d 138
galonso@rhomb.io 0:9ff56a0e4c0d 139 printf("\n"); ;
galonso@rhomb.io 0:9ff56a0e4c0d 140 }
galonso@rhomb.io 0:9ff56a0e4c0d 141
galonso@rhomb.io 0:9ff56a0e4c0d 142 /**
galonso@rhomb.io 0:9ff56a0e4c0d 143 * Update sensors and report their values.
galonso@rhomb.io 0:9ff56a0e4c0d 144 * This function is called periodically.
galonso@rhomb.io 0:9ff56a0e4c0d 145 */
galonso@rhomb.io 0:9ff56a0e4c0d 146 void sensors_update() {
galonso@rhomb.io 0:9ff56a0e4c0d 147 float temp3_value, volt_value = 0.0;
galonso@rhomb.io 0:9ff56a0e4c0d 148
galonso@rhomb.io 0:9ff56a0e4c0d 149 temp3_value = adc_temp.read()*100;
galonso@rhomb.io 0:9ff56a0e4c0d 150 volt_value = adc_vref.read();
galonso@rhomb.io 0:9ff56a0e4c0d 151
galonso@rhomb.io 0:9ff56a0e4c0d 152 printf(" \n");
galonso@rhomb.io 0:9ff56a0e4c0d 153 printf("ADC temp: %5.4f C, vref: %5.4f V \n", temp3_value, volt_value);
galonso@rhomb.io 0:9ff56a0e4c0d 154
galonso@rhomb.io 0:9ff56a0e4c0d 155 if (endpointInfo) {
galonso@rhomb.io 0:9ff56a0e4c0d 156 #ifdef SEND_ALL_SENSORS
galonso@rhomb.io 0:9ff56a0e4c0d 157 res_adc_temp->set_value(temp3_value);
galonso@rhomb.io 0:9ff56a0e4c0d 158 res_adc_voltage->set_value(volt_value);
galonso@rhomb.io 0:9ff56a0e4c0d 159 #endif /* SEND_ALL_SENSORS */
galonso@rhomb.io 0:9ff56a0e4c0d 160 }
galonso@rhomb.io 0:9ff56a0e4c0d 161 }
galonso@rhomb.io 0:9ff56a0e4c0d 162
galonso@rhomb.io 0:9ff56a0e4c0d 163 int main(void) {
galonso@rhomb.io 0:9ff56a0e4c0d 164 printf("\nStarting Simple Pelion Device Management Client example\n");
galonso@rhomb.io 0:9ff56a0e4c0d 165
galonso@rhomb.io 0:9ff56a0e4c0d 166 int storage_status = fs.mount(&sd);
galonso@rhomb.io 0:9ff56a0e4c0d 167 if (storage_status != 0) {
galonso@rhomb.io 0:9ff56a0e4c0d 168 printf("Storage mounting failed.\n");
galonso@rhomb.io 0:9ff56a0e4c0d 169 }
galonso@rhomb.io 0:9ff56a0e4c0d 170 // If the User button is pressed ons start, then format storage.
galonso@rhomb.io 0:9ff56a0e4c0d 171 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
galonso@rhomb.io 0:9ff56a0e4c0d 172 if (btn_pressed) {
galonso@rhomb.io 0:9ff56a0e4c0d 173 printf("User button is pushed on start...\n");
galonso@rhomb.io 0:9ff56a0e4c0d 174 }
galonso@rhomb.io 0:9ff56a0e4c0d 175
galonso@rhomb.io 0:9ff56a0e4c0d 176 if (storage_status || btn_pressed) {
galonso@rhomb.io 0:9ff56a0e4c0d 177 printf("Formatting the storage...\n");
galonso@rhomb.io 0:9ff56a0e4c0d 178 int storage_status = StorageHelper::format(&fs, &sd);
galonso@rhomb.io 0:9ff56a0e4c0d 179 if (storage_status != 0) {
galonso@rhomb.io 0:9ff56a0e4c0d 180 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
galonso@rhomb.io 0:9ff56a0e4c0d 181 }
galonso@rhomb.io 0:9ff56a0e4c0d 182 } else {
galonso@rhomb.io 0:9ff56a0e4c0d 183 printf("You can hold the user button during boot to format the storage and change the device identity.\n");
galonso@rhomb.io 0:9ff56a0e4c0d 184 }
galonso@rhomb.io 0:9ff56a0e4c0d 185
galonso@rhomb.io 0:9ff56a0e4c0d 186 sensors_init();
galonso@rhomb.io 0:9ff56a0e4c0d 187
galonso@rhomb.io 0:9ff56a0e4c0d 188 // Connect to the internet (DHCP is expected to be on)
galonso@rhomb.io 0:9ff56a0e4c0d 189 printf("Connecting to the network using Wifi...\n");
galonso@rhomb.io 0:9ff56a0e4c0d 190 net = NetworkInterface::get_default_instance();
galonso@rhomb.io 0:9ff56a0e4c0d 191
galonso@rhomb.io 0:9ff56a0e4c0d 192 nsapi_error_t net_status = -1;
galonso@rhomb.io 0:9ff56a0e4c0d 193 for (int tries = 0; tries < 3; tries++) {
galonso@rhomb.io 0:9ff56a0e4c0d 194 net_status = net->connect();
galonso@rhomb.io 0:9ff56a0e4c0d 195 if (net_status == NSAPI_ERROR_OK) {
galonso@rhomb.io 0:9ff56a0e4c0d 196 break;
galonso@rhomb.io 0:9ff56a0e4c0d 197 } else {
galonso@rhomb.io 0:9ff56a0e4c0d 198 printf("Unable to connect to network. Retrying...\n");
galonso@rhomb.io 0:9ff56a0e4c0d 199 }
galonso@rhomb.io 0:9ff56a0e4c0d 200 }
galonso@rhomb.io 0:9ff56a0e4c0d 201
galonso@rhomb.io 0:9ff56a0e4c0d 202 if (net_status != NSAPI_ERROR_OK) {
galonso@rhomb.io 0:9ff56a0e4c0d 203 printf("ERROR: Connecting to the network failed (%d)!\n", net_status);
galonso@rhomb.io 0:9ff56a0e4c0d 204 return -1;
galonso@rhomb.io 0:9ff56a0e4c0d 205 }
galonso@rhomb.io 0:9ff56a0e4c0d 206
galonso@rhomb.io 0:9ff56a0e4c0d 207 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
galonso@rhomb.io 0:9ff56a0e4c0d 208
galonso@rhomb.io 0:9ff56a0e4c0d 209 printf("Initializing Pelion Device Management Client...\n");
galonso@rhomb.io 0:9ff56a0e4c0d 210
galonso@rhomb.io 0:9ff56a0e4c0d 211 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM
galonso@rhomb.io 0:9ff56a0e4c0d 212 SimpleMbedCloudClient client(net, bd, &fs);
galonso@rhomb.io 0:9ff56a0e4c0d 213 int client_status = client.init();
galonso@rhomb.io 0:9ff56a0e4c0d 214 if (client_status != 0) {
galonso@rhomb.io 0:9ff56a0e4c0d 215 printf("ERROR: Pelion Client initialization failed (%d)\n", client_status);
galonso@rhomb.io 0:9ff56a0e4c0d 216 return -1;
galonso@rhomb.io 0:9ff56a0e4c0d 217 }
galonso@rhomb.io 0:9ff56a0e4c0d 218
galonso@rhomb.io 0:9ff56a0e4c0d 219 // Creating resources, which can be written or read from the cloud
galonso@rhomb.io 0:9ff56a0e4c0d 220 res_button = client.create_resource("3200/0/5501", "Button Count");
galonso@rhomb.io 0:9ff56a0e4c0d 221 res_button->set_value(0);
galonso@rhomb.io 0:9ff56a0e4c0d 222 res_button->methods(M2MMethod::GET);
galonso@rhomb.io 0:9ff56a0e4c0d 223 res_button->observable(true);
galonso@rhomb.io 0:9ff56a0e4c0d 224 res_button->attach_notification_callback(button_callback);
galonso@rhomb.io 0:9ff56a0e4c0d 225
galonso@rhomb.io 0:9ff56a0e4c0d 226 res_led = client.create_resource("3201/0/5853", "LED State");
galonso@rhomb.io 0:9ff56a0e4c0d 227 res_led->set_value(1);
galonso@rhomb.io 0:9ff56a0e4c0d 228 res_led->methods(M2MMethod::GET | M2MMethod::PUT);
galonso@rhomb.io 0:9ff56a0e4c0d 229 res_led->attach_put_callback(put_callback);
galonso@rhomb.io 0:9ff56a0e4c0d 230
galonso@rhomb.io 0:9ff56a0e4c0d 231 #ifdef SEND_ALL_SENSORS
galonso@rhomb.io 0:9ff56a0e4c0d 232 // Sensor resources
galonso@rhomb.io 0:9ff56a0e4c0d 233 res_adc_temp = client.create_resource("3303/2/5700", "Temperature ADC (C)");
galonso@rhomb.io 0:9ff56a0e4c0d 234 res_adc_temp->set_value(0);
galonso@rhomb.io 0:9ff56a0e4c0d 235 res_adc_temp->methods(M2MMethod::GET);
galonso@rhomb.io 0:9ff56a0e4c0d 236 res_adc_temp->observable(true);
galonso@rhomb.io 0:9ff56a0e4c0d 237
galonso@rhomb.io 0:9ff56a0e4c0d 238 res_adc_voltage = client.create_resource("3316/0/5700", "Voltage");
galonso@rhomb.io 0:9ff56a0e4c0d 239 res_adc_voltage->set_value(0);
galonso@rhomb.io 0:9ff56a0e4c0d 240 res_adc_voltage->methods(M2MMethod::GET);
galonso@rhomb.io 0:9ff56a0e4c0d 241 res_adc_voltage->observable(true);
galonso@rhomb.io 0:9ff56a0e4c0d 242
galonso@rhomb.io 0:9ff56a0e4c0d 243 #endif /* SEND_ALL_SENSORS */
galonso@rhomb.io 0:9ff56a0e4c0d 244
galonso@rhomb.io 0:9ff56a0e4c0d 245 printf("Initialized Pelion Client. Registering...\n");
galonso@rhomb.io 0:9ff56a0e4c0d 246
galonso@rhomb.io 0:9ff56a0e4c0d 247 // Callback that fires when registering is complete
galonso@rhomb.io 0:9ff56a0e4c0d 248 client.on_registered(&registered);
galonso@rhomb.io 0:9ff56a0e4c0d 249
galonso@rhomb.io 0:9ff56a0e4c0d 250 // Register with Pelion DM
galonso@rhomb.io 0:9ff56a0e4c0d 251 client.register_and_connect();
galonso@rhomb.io 0:9ff56a0e4c0d 252
galonso@rhomb.io 0:9ff56a0e4c0d 253 int i = 600; // wait up 60 seconds before attaching sensors and button events
galonso@rhomb.io 0:9ff56a0e4c0d 254 while (i-- > 0 && !client.is_client_registered()) {
galonso@rhomb.io 0:9ff56a0e4c0d 255 wait_ms(100);
galonso@rhomb.io 0:9ff56a0e4c0d 256 }
galonso@rhomb.io 0:9ff56a0e4c0d 257
galonso@rhomb.io 0:9ff56a0e4c0d 258 button.fall(eventQueue.event(&button_press));
galonso@rhomb.io 0:9ff56a0e4c0d 259
galonso@rhomb.io 0:9ff56a0e4c0d 260 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
galonso@rhomb.io 0:9ff56a0e4c0d 261 Ticker timer;
galonso@rhomb.io 0:9ff56a0e4c0d 262 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);
galonso@rhomb.io 0:9ff56a0e4c0d 263
galonso@rhomb.io 0:9ff56a0e4c0d 264 // You can easily run the eventQueue in a separate thread if required
galonso@rhomb.io 0:9ff56a0e4c0d 265 eventQueue.dispatch_forever();
galonso@rhomb.io 0:9ff56a0e4c0d 266 }
galonso@rhomb.io 0:9ff56a0e4c0d 267
galonso@rhomb.io 0:9ff56a0e4c0d 268 #endif