Mbed OS and Pelion Device Management example over WIFI for DISCO_L475VG_IOT01 board

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

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 on the following platforms:

DISCO_L475E_IOT01A

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

Example functionality

This example showcases the following device functionality:

  • Read onboard temperature and humidity sensors, 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.
  • Uses all onboard sensors and reports them as Pelion LWM2M resources.

/media/uploads/screamer/pelion_st_humidity_reading.png?v=2

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/ST/code/pelion-example-disco-iot01

cd pelion-example-disco-iot01

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 DISCO_L475VG_IOT01A

(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: 00000000000000000000
[BOOT] OEM: 00000000000000000000
[BOOT] Layout: 0 80096F4
[BOOT] Active firmware integrity check:
[BOOT] SHA256: 0660E360D432225D5251461998FD8617B017098C5F1F90D5FB607BF8C27ED530
[BOOT] Version: 1553615309
[BOOT] Slot 0 is empty
[BOOT] Active firmware up-to-date
[BOOT] Application's start address: 0x8010400
[BOOT] Application's jump address: 0x8011041
[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.

Sensors configuration:
Invalid new address!
HTS221  humidity & temperature    = 0xBC
LPS22HB pressure & temperature    = 0xB1
LIS3MDL magnetometer              = 0x3D
LSM6DSL accelerometer & gyroscope = 0x6A

Connecting to the network using Wifi...
Connected to the network successfully. IP address: 192.168.1.3
Initializing Pelion Device Management Client...
Initialized Pelion Client. Registering...
Registered to Pelion Device Management. Endpoint Name: 0169********************001002d5

ADC temp:     23.0037 C,  vref:      0.3661 V
HTS221 temp:   28.700 C,  humidity:   31.90 %
LPS22HB temp:  29.600 C,  pressure: 1032.01 mbar
LIS3MDL mag:    0.217 x,  -0.284 y,  -0.053 z [gauss]
LSM6DSL acc:    0.005 x,  -0.014 y,   1.029 z [g]
LSM6DSL gyro:   0.910 x,  -0.910 y,   1.120 z [dps]
VL53L0X dist:    1855 mm
Committer:
screamer
Date:
Mon Dec 10 18:44:01 2018 +0000
Revision:
13:42b49a0caade
Parent:
12:1f1a50e973db
Child:
15:a0430d40a918
Add more sensors to main app, but disabled by default due to secure connect issues

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adustm 1:e86b1cffc402 1 // ----------------------------------------------------------------------------
adustm 4:cf7342047b4d 2 // Copyright 2016-2018 ARM Ltd.
adustm 1:e86b1cffc402 3 //
adustm 1:e86b1cffc402 4 // SPDX-License-Identifier: Apache-2.0
adustm 1:e86b1cffc402 5 //
adustm 1:e86b1cffc402 6 // Licensed under the Apache License, Version 2.0 (the "License");
adustm 1:e86b1cffc402 7 // you may not use this file except in compliance with the License.
adustm 1:e86b1cffc402 8 // You may obtain a copy of the License at
adustm 1:e86b1cffc402 9 //
adustm 1:e86b1cffc402 10 // http://www.apache.org/licenses/LICENSE-2.0
adustm 1:e86b1cffc402 11 //
adustm 1:e86b1cffc402 12 // Unless required by applicable law or agreed to in writing, software
adustm 1:e86b1cffc402 13 // distributed under the License is distributed on an "AS IS" BASIS,
adustm 1:e86b1cffc402 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
adustm 1:e86b1cffc402 15 // See the License for the specific language governing permissions and
adustm 1:e86b1cffc402 16 // limitations under the License.
adustm 1:e86b1cffc402 17 // ----------------------------------------------------------------------------
MarceloSalazar 9:265744785d33 18 #ifndef MBED_TEST_MODE
adustm 1:e86b1cffc402 19 #include "mbed.h"
adustm 1:e86b1cffc402 20 #include "simple-mbed-cloud-client.h"
screamer 10:b27c962b3c3f 21 #include "LittleFileSystem.h"
screamer 10:b27c962b3c3f 22 #include "HTS221Sensor.h"
screamer 10:b27c962b3c3f 23 #include "LPS22HBSensor.h"
screamer 10:b27c962b3c3f 24 #include "LSM6DSLSensor.h"
screamer 10:b27c962b3c3f 25 #include "lis3mdl_class.h"
screamer 11:8df4529f060d 26 // #include "VL53L0X.h"
screamer 10:b27c962b3c3f 27
screamer 10:b27c962b3c3f 28 static DevI2C devI2c(PB_11,PB_10);
screamer 12:1f1a50e973db 29 static HTS221Sensor sen_hum_temp(&devI2c);
screamer 12:1f1a50e973db 30 static LPS22HBSensor sen_press_temp(&devI2c);
screamer 12:1f1a50e973db 31 static LSM6DSLSensor sen_acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
screamer 12:1f1a50e973db 32 static LIS3MDL sen_mag(&devI2c);
screamer 10:b27c962b3c3f 33 static DigitalOut shutdown_pin(PC_6);
screamer 12:1f1a50e973db 34 // static VL53L0X sen_distance(&devI2c, &shutdown_pin, PC_7);
screamer 10:b27c962b3c3f 35
screamer 10:b27c962b3c3f 36 InterruptIn button(USER_BUTTON);
screamer 13:42b49a0caade 37
screamer 11:8df4529f060d 38 #define SENSORS_POLL_INTERVAL 1.0
screamer 13:42b49a0caade 39 //#define SEND_ALL_SENSORS
adustm 1:e86b1cffc402 40
adustm 4:cf7342047b4d 41 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
adustm 4:cf7342047b4d 42 // 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
adustm 4:cf7342047b4d 43 EventQueue eventQueue;
adustm 1:e86b1cffc402 44
MarceloSalazar 9:265744785d33 45 // Default network interface object
MarceloSalazar 9:265744785d33 46 NetworkInterface *net;
adustm 6:e0e1e1b93099 47
MarceloSalazar 9:265744785d33 48 // Default block device
MarceloSalazar 9:265744785d33 49 BlockDevice* bd = BlockDevice::get_default_instance();
screamer 10:b27c962b3c3f 50 SlicingBlockDevice sd(bd, 0, 2*1024*1024);
screamer 10:b27c962b3c3f 51 LittleFileSystem fs("fs", &sd);
adustm 4:cf7342047b4d 52
screamer 11:8df4529f060d 53 // Default LED to use for PUT/POST
screamer 11:8df4529f060d 54 DigitalOut led(LED1);
screamer 11:8df4529f060d 55
MarceloSalazar 9:265744785d33 56 // Declaring pointers for access to Pelion Client resources outside of main()
screamer 12:1f1a50e973db 57 MbedCloudClientResource *res_button;
screamer 12:1f1a50e973db 58 MbedCloudClientResource *res_led;
adustm 1:e86b1cffc402 59
screamer 12:1f1a50e973db 60 // Additional resources for sensor readings
screamer 12:1f1a50e973db 61 MbedCloudClientResource *res_humidity;
screamer 12:1f1a50e973db 62 MbedCloudClientResource *res_temperature;
screamer 13:42b49a0caade 63 #ifdef SEND_ALL_SENSORS
screamer 12:1f1a50e973db 64 MbedCloudClientResource *res_pressure;
screamer 12:1f1a50e973db 65 MbedCloudClientResource *res_temperature2;
screamer 12:1f1a50e973db 66 MbedCloudClientResource *res_magnometer;
screamer 12:1f1a50e973db 67 MbedCloudClientResource *res_accelerometer;
screamer 13:42b49a0caade 68 MbedCloudClientResource *res_gyroscope_x;
screamer 13:42b49a0caade 69 MbedCloudClientResource *res_gyroscope_y;
screamer 13:42b49a0caade 70 MbedCloudClientResource *res_gyroscope_z;
screamer 12:1f1a50e973db 71 MbedCloudClientResource *res_distance;
screamer 13:42b49a0caade 72 #endif /* SEND_ALL_SENSORS */
adustm 1:e86b1cffc402 73
screamer 10:b27c962b3c3f 74 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
screamer 10:b27c962b3c3f 75 static const ConnectorClientEndpointInfo* endpointInfo;
adustm 1:e86b1cffc402 76
screamer 10:b27c962b3c3f 77 /**
adustm 4:cf7342047b4d 78 * PUT handler
adustm 4:cf7342047b4d 79 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 80 * @param newValue Updated value for the resource
adustm 4:cf7342047b4d 81 */
screamer 11:8df4529f060d 82 void led_put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
adustm 4:cf7342047b4d 83 printf("PUT received, new value: %s\n", newValue.c_str());
screamer 11:8df4529f060d 84 led = atoi(newValue.c_str());
adustm 1:e86b1cffc402 85 }
adustm 1:e86b1cffc402 86
adustm 4:cf7342047b4d 87 /**
adustm 4:cf7342047b4d 88 * POST handler
adustm 4:cf7342047b4d 89 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 90 * @param buffer If a body was passed to the POST function, this contains the data.
adustm 4:cf7342047b4d 91 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
adustm 4:cf7342047b4d 92 * @param size Size of the body
adustm 4:cf7342047b4d 93 */
screamer 11:8df4529f060d 94 void led_post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
screamer 12:1f1a50e973db 95 printf("POST received. Going to blink LED pattern: %s\n", res_led->get_value().c_str());
screamer 12:1f1a50e973db 96 led = atoi(res_led->get_value().c_str());
screamer 11:8df4529f060d 97 }
adustm 1:e86b1cffc402 98
screamer 11:8df4529f060d 99 /**
screamer 13:42b49a0caade 100 * Button function triggered by the physical button press.
screamer 11:8df4529f060d 101 */
screamer 11:8df4529f060d 102 void button_press() {
screamer 12:1f1a50e973db 103 int v = res_button->get_value_int() + 1;
screamer 12:1f1a50e973db 104 res_button->set_value(v);
screamer 11:8df4529f060d 105 printf("Button clicked %d times\n", v);
adustm 1:e86b1cffc402 106 }
adustm 1:e86b1cffc402 107
adustm 4:cf7342047b4d 108 /**
adustm 4:cf7342047b4d 109 * Notification callback handler
adustm 4:cf7342047b4d 110 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 111 * @param status The delivery status of the notification
adustm 4:cf7342047b4d 112 */
adustm 4:cf7342047b4d 113 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
adustm 4:cf7342047b4d 114 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
adustm 4:cf7342047b4d 115 }
adustm 1:e86b1cffc402 116
adustm 4:cf7342047b4d 117 /**
adustm 4:cf7342047b4d 118 * Registration callback handler
adustm 4:cf7342047b4d 119 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
adustm 4:cf7342047b4d 120 */
adustm 4:cf7342047b4d 121 void registered(const ConnectorClientEndpointInfo *endpoint) {
MarceloSalazar 9:265744785d33 122 printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
screamer 10:b27c962b3c3f 123 endpointInfo = endpoint;
adustm 4:cf7342047b4d 124 }
adustm 1:e86b1cffc402 125
screamer 10:b27c962b3c3f 126 /**
screamer 10:b27c962b3c3f 127 * Initialize sensors
screamer 10:b27c962b3c3f 128 */
screamer 10:b27c962b3c3f 129 void sensors_init() {
screamer 10:b27c962b3c3f 130 uint8_t id;
screamer 10:b27c962b3c3f 131
screamer 10:b27c962b3c3f 132 // Initialize sensors
screamer 12:1f1a50e973db 133 sen_hum_temp.init(NULL);
screamer 12:1f1a50e973db 134 sen_press_temp.init(NULL);
screamer 12:1f1a50e973db 135 sen_acc_gyro.init(NULL);
screamer 12:1f1a50e973db 136 sen_mag.init(NULL);
screamer 13:42b49a0caade 137 // sen_distance.init_sensor(VL53L0X_DEFAULT_ADDRESS);
screamer 10:b27c962b3c3f 138
screamer 10:b27c962b3c3f 139 /// Call sensors enable routines
screamer 12:1f1a50e973db 140 sen_hum_temp.enable();
screamer 12:1f1a50e973db 141 sen_press_temp.enable();
screamer 12:1f1a50e973db 142 //sen_mag.enable();
screamer 12:1f1a50e973db 143 sen_acc_gyro.enable_x();
screamer 12:1f1a50e973db 144 sen_acc_gyro.enable_g();
screamer 10:b27c962b3c3f 145
screamer 10:b27c962b3c3f 146 printf("\033[2J\033[20A");
screamer 12:1f1a50e973db 147 printf ("\r\nSensors configuration:\r\n");
screamer 10:b27c962b3c3f 148
screamer 12:1f1a50e973db 149 sen_hum_temp.read_id(&id);
screamer 10:b27c962b3c3f 150 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
screamer 12:1f1a50e973db 151 sen_press_temp.read_id(&id);
screamer 10:b27c962b3c3f 152 printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
screamer 12:1f1a50e973db 153 sen_mag.read_id(&id);
screamer 10:b27c962b3c3f 154 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
screamer 12:1f1a50e973db 155 sen_acc_gyro.read_id(&id);
screamer 10:b27c962b3c3f 156 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
screamer 10:b27c962b3c3f 157
screamer 12:1f1a50e973db 158 printf("\r\n"); ;
screamer 10:b27c962b3c3f 159 }
screamer 10:b27c962b3c3f 160
screamer 10:b27c962b3c3f 161 /**
screamer 10:b27c962b3c3f 162 * Update sensors and report their values.
screamer 10:b27c962b3c3f 163 * This function is called periodically.
screamer 10:b27c962b3c3f 164 */
screamer 10:b27c962b3c3f 165 void sensors_update() {
screamer 12:1f1a50e973db 166 float fval1, fval2;
screamer 10:b27c962b3c3f 167 int32_t axes[3];
screamer 12:1f1a50e973db 168 uint32_t uval;
screamer 10:b27c962b3c3f 169
screamer 10:b27c962b3c3f 170 printf("\r\n");
screamer 10:b27c962b3c3f 171
screamer 12:1f1a50e973db 172 fval1 = fval2 = 0.0;
screamer 13:42b49a0caade 173 sen_hum_temp.get_humidity(&fval1);
screamer 13:42b49a0caade 174 sen_hum_temp.get_temperature(&fval2);
screamer 13:42b49a0caade 175 printf("HTS221 temp: %7.2f C, humidity: %7.2f %%\r\n", fval1, fval2);
screamer 10:b27c962b3c3f 176 if (endpointInfo) {
screamer 13:42b49a0caade 177 res_humidity->set_value(fval1);
screamer 13:42b49a0caade 178 res_temperature->set_value(fval2);
screamer 10:b27c962b3c3f 179 }
screamer 10:b27c962b3c3f 180
screamer 12:1f1a50e973db 181 fval1 = fval2 = 0.0;
screamer 13:42b49a0caade 182 sen_press_temp.get_pressure(&fval1);
screamer 13:42b49a0caade 183 sen_press_temp.get_temperature(&fval2);
screamer 12:1f1a50e973db 184 printf("LPS22HB temp: %7.2f C, pressure: %7.2f mbar \r\n", fval1, fval2);
screamer 13:42b49a0caade 185 #ifdef SEND_ALL_SENSORS
screamer 13:42b49a0caade 186 if (endpointInfo) {
screamer 13:42b49a0caade 187 res_pressure->set_value(fval1);
screamer 13:42b49a0caade 188 res_temperature2->set_value(fval2);
screamer 13:42b49a0caade 189 }
screamer 13:42b49a0caade 190 #endif /* SEND_ALL_SENSORS */
screamer 10:b27c962b3c3f 191
screamer 12:1f1a50e973db 192 sen_mag.get_m_axes(axes);
screamer 12:1f1a50e973db 193 printf("LIS3MDL mag: %7ld x, %7ld y, %7ld z [mgauss] \r\n", axes[0], axes[1], axes[2]);
screamer 13:42b49a0caade 194
screamer 12:1f1a50e973db 195 sen_acc_gyro.get_x_axes(axes);
screamer 12:1f1a50e973db 196 printf("LSM6DSL acc: %7ld x, %7ld y, %7ld z [mg] \r\n", axes[0], axes[1], axes[2]);
screamer 13:42b49a0caade 197
screamer 12:1f1a50e973db 198 sen_acc_gyro.get_g_axes(axes);
screamer 12:1f1a50e973db 199 printf("LSM6DSL gyro: %7ld x, %7ld y, %7ld z [mdps] \r\n", axes[0], axes[1], axes[2]);
screamer 13:42b49a0caade 200 #ifdef SEND_ALL_SENSORS
screamer 13:42b49a0caade 201 if (endpointInfo) {
screamer 13:42b49a0caade 202 res_gyroscope_x->set_value((float)axes[0]);
screamer 13:42b49a0caade 203 res_gyroscope_y->set_value((float)axes[1]);
screamer 13:42b49a0caade 204 res_gyroscope_z->set_value((float)axes[2]);
screamer 13:42b49a0caade 205 }
screamer 13:42b49a0caade 206 #endif /* SEND_ALL_SENSORS */
screamer 10:b27c962b3c3f 207
screamer 13:42b49a0caade 208 // if (sen_distance.get_distance(&uval) == VL53L0X_ERROR_NONE) {
screamer 12:1f1a50e973db 209 // printf("VL53L0X [mm]: %6ld\r\n", uval);
screamer 11:8df4529f060d 210 // if (endpointInfo) {
screamer 13:42b49a0caade 211 // res_distance->set_value((float)uval);
screamer 11:8df4529f060d 212 // }
screamer 11:8df4529f060d 213 // } else {
screamer 11:8df4529f060d 214 // printf("VL53L0X [mm]: --\r\n");
screamer 11:8df4529f060d 215 // }
screamer 10:b27c962b3c3f 216
screamer 12:1f1a50e973db 217 printf("\033[6A");
screamer 10:b27c962b3c3f 218 }
screamer 10:b27c962b3c3f 219
adustm 4:cf7342047b4d 220 int main(void) {
MarceloSalazar 9:265744785d33 221 printf("Starting Simple Pelion Device Management Client example\n");
adustm 4:cf7342047b4d 222
screamer 10:b27c962b3c3f 223 // If the User button is pressed, then format storage.
screamer 10:b27c962b3c3f 224 const int PRESSED = 0;
screamer 10:b27c962b3c3f 225 DigitalIn *user_button = new DigitalIn(USER_BUTTON);
screamer 10:b27c962b3c3f 226 if (user_button->read() == PRESSED) {
screamer 10:b27c962b3c3f 227 printf("User button is pushed on start. Formatting the storage...\n");
screamer 10:b27c962b3c3f 228 int storage_status = fs.reformat(&sd);
screamer 10:b27c962b3c3f 229 if (storage_status != 0) {
screamer 10:b27c962b3c3f 230 if (sd.erase(0, sd.size()) == 0) {
screamer 10:b27c962b3c3f 231 if (fs.format(&sd) == 0) {
screamer 10:b27c962b3c3f 232 storage_status = 0;
screamer 10:b27c962b3c3f 233 printf("The storage reformatted successfully.\n");
screamer 10:b27c962b3c3f 234 }
screamer 10:b27c962b3c3f 235 }
screamer 10:b27c962b3c3f 236 }
screamer 10:b27c962b3c3f 237 if (storage_status != 0) {
screamer 13:42b49a0caade 238 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
screamer 10:b27c962b3c3f 239 }
screamer 10:b27c962b3c3f 240 }
screamer 10:b27c962b3c3f 241
screamer 10:b27c962b3c3f 242 sensors_init();
screamer 10:b27c962b3c3f 243
adustm 4:cf7342047b4d 244 // Connect to the internet (DHCP is expected to be on)
screamer 13:42b49a0caade 245 printf("Connecting to the network using Wifi...\n");
MarceloSalazar 9:265744785d33 246 net = NetworkInterface::get_default_instance();
adustm 4:cf7342047b4d 247
screamer 10:b27c962b3c3f 248 nsapi_error_t net_status = -1;
screamer 10:b27c962b3c3f 249 for (int tries = 0; tries < 3; tries++) {
screamer 10:b27c962b3c3f 250 net_status = net->connect();
screamer 10:b27c962b3c3f 251 if (net_status == NSAPI_ERROR_OK) {
screamer 10:b27c962b3c3f 252 break;
screamer 10:b27c962b3c3f 253 } else {
screamer 13:42b49a0caade 254 printf("Unable to connect to network. Retrying...\n");
screamer 10:b27c962b3c3f 255 }
screamer 10:b27c962b3c3f 256 }
MarceloSalazar 9:265744785d33 257
screamer 10:b27c962b3c3f 258 if (net_status != NSAPI_ERROR_OK) {
screamer 13:42b49a0caade 259 printf("ERROR: Connecting to the network failed (%d)!\n", net_status);
adustm 1:e86b1cffc402 260 return -1;
adustm 1:e86b1cffc402 261 }
adustm 1:e86b1cffc402 262
MarceloSalazar 9:265744785d33 263 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
adustm 1:e86b1cffc402 264
MarceloSalazar 9:265744785d33 265 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM
MarceloSalazar 9:265744785d33 266 SimpleMbedCloudClient client(net, bd, &fs);
adustm 4:cf7342047b4d 267 int client_status = client.init();
adustm 4:cf7342047b4d 268 if (client_status != 0) {
screamer 13:42b49a0caade 269 printf("ERROR: Pelion Client initialization failed (%d)\n", client_status);
adustm 1:e86b1cffc402 270 return -1;
adustm 1:e86b1cffc402 271 }
adustm 1:e86b1cffc402 272
adustm 4:cf7342047b4d 273 // Creating resources, which can be written or read from the cloud
screamer 12:1f1a50e973db 274 res_button = client.create_resource("3200/0/5501", "button_count");
screamer 12:1f1a50e973db 275 res_button->set_value(0);
screamer 12:1f1a50e973db 276 res_button->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 277 res_button->observable(true);
screamer 12:1f1a50e973db 278 res_button->attach_notification_callback(button_callback);
adustm 1:e86b1cffc402 279
screamer 10:b27c962b3c3f 280 // Sensor resources
screamer 12:1f1a50e973db 281 res_temperature = client.create_resource("3303/0/5700", "temperature");
screamer 12:1f1a50e973db 282 res_temperature->set_value(0);
screamer 12:1f1a50e973db 283 res_temperature->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 284 res_temperature->observable(true);
screamer 10:b27c962b3c3f 285
screamer 12:1f1a50e973db 286 res_humidity = client.create_resource("3304/0/5700", "humidity");
screamer 12:1f1a50e973db 287 res_humidity->set_value(0);
screamer 12:1f1a50e973db 288 res_humidity->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 289 res_humidity->observable(true);
screamer 10:b27c962b3c3f 290
screamer 13:42b49a0caade 291 #ifdef SEND_ALL_SENSORS
screamer 13:42b49a0caade 292 res_pressure = client.create_resource("3323/1/5700", "pressure");
screamer 13:42b49a0caade 293 res_pressure->set_value(0);
screamer 13:42b49a0caade 294 res_pressure->methods(M2MMethod::GET);
screamer 13:42b49a0caade 295 res_pressure->observable(true);
screamer 13:42b49a0caade 296
screamer 13:42b49a0caade 297 res_temperature2 = client.create_resource("3303/1/5700", "temperature");
screamer 13:42b49a0caade 298 res_temperature2->set_value(0);
screamer 13:42b49a0caade 299 res_temperature2->methods(M2MMethod::GET);
screamer 13:42b49a0caade 300 res_temperature2->observable(true);
screamer 13:42b49a0caade 301
screamer 13:42b49a0caade 302 res_gyroscope_x = client.create_resource("3334/1/5702", "gyroscope_x");
screamer 13:42b49a0caade 303 res_gyroscope_x->set_value(0);
screamer 13:42b49a0caade 304 res_gyroscope_x->methods(M2MMethod::GET);
screamer 13:42b49a0caade 305 res_gyroscope_x->observable(true);
screamer 10:b27c962b3c3f 306
screamer 13:42b49a0caade 307 res_gyroscope_y = client.create_resource("3334/1/5703", "gyroscope_y");
screamer 13:42b49a0caade 308 res_gyroscope_y->set_value(0);
screamer 13:42b49a0caade 309 res_gyroscope_y->methods(M2MMethod::GET);
screamer 13:42b49a0caade 310 res_gyroscope_y->observable(true);
screamer 13:42b49a0caade 311
screamer 13:42b49a0caade 312 res_gyroscope_z = client.create_resource("3334/1/5704", "gyroscope_z");
screamer 13:42b49a0caade 313 res_gyroscope_z->set_value(0);
screamer 13:42b49a0caade 314 res_gyroscope_z->methods(M2MMethod::GET);
screamer 13:42b49a0caade 315 res_gyroscope_z->observable(true);
screamer 13:42b49a0caade 316
screamer 13:42b49a0caade 317 res_distance = client.create_resource("3330/0/5700", "distance");
screamer 13:42b49a0caade 318 res_distance->set_value(0);
screamer 13:42b49a0caade 319 res_distance->methods(M2MMethod::GET);
screamer 13:42b49a0caade 320 res_distance->observable(true);
screamer 13:42b49a0caade 321
screamer 13:42b49a0caade 322 res_led = client.create_resource("3201/0/5853", "led_state");
screamer 13:42b49a0caade 323 res_led->set_value(1);
screamer 13:42b49a0caade 324 res_led->methods(M2MMethod::GET | M2MMethod::PUT);
screamer 13:42b49a0caade 325 res_led->attach_put_callback(led_put_callback);
screamer 13:42b49a0caade 326 #endif /* SEND_ALL_SENSORS */
screamer 11:8df4529f060d 327
MarceloSalazar 9:265744785d33 328 printf("Initialized Pelion Client. Registering...\n");
adustm 1:e86b1cffc402 329
adustm 4:cf7342047b4d 330 // Callback that fires when registering is complete
adustm 4:cf7342047b4d 331 client.on_registered(&registered);
adustm 1:e86b1cffc402 332
MarceloSalazar 9:265744785d33 333 // Register with Pelion DM
adustm 4:cf7342047b4d 334 client.register_and_connect();
adustm 1:e86b1cffc402 335
screamer 12:1f1a50e973db 336 int i = 600; // wait 60 seconds
screamer 12:1f1a50e973db 337 while (i-- > 0 && !client.is_client_registered()) {
screamer 12:1f1a50e973db 338 wait_ms(100);
screamer 12:1f1a50e973db 339 }
screamer 12:1f1a50e973db 340
screamer 11:8df4529f060d 341 button.fall(eventQueue.event(&button_press));
screamer 10:b27c962b3c3f 342
adustm 4:cf7342047b4d 343 Ticker timer;
screamer 13:42b49a0caade 344 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
screamer 11:8df4529f060d 345 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);
adustm 1:e86b1cffc402 346
adustm 4:cf7342047b4d 347 // You can easily run the eventQueue in a separate thread if required
adustm 4:cf7342047b4d 348 eventQueue.dispatch_forever();
adustm 1:e86b1cffc402 349 }
MarceloSalazar 9:265744785d33 350 #endif