Pelion Device Management example over 15.4 Thread for Thunderboard Sense 2 board

Dependencies:   ICM20648 BMP280 Si1133 Si7210 AMS_CCS811_gas_sensor SI7021

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

Thunderboard Sense 2

Example functionality

This example showcases the following device functionality:

  • Read onboard sensors, and report them as Pelion LWM2M resources:
    • Barometric Pressure and Temperature (BMP280)
    • Relative Humidity and Temperature (Si7021)
    • Air quality - CO2 and tVOC (CCS811)
    • Light intensity and UV level (Si1133)
    • Hall effect and Temperature (Si7210)
    • Accelerometer and Gyroscope (ICM20648)
  • It also exposes the RGB LEDs for triggering flashes in a specific color
  • On user button click, increment Pelion LWM2M button resource.

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

15.4 Thread setup

This example program requires that a Thread Border Router is available. A Border Router is a network gateway between a wireless 6LoWPAN mesh network and a backhaul network. It controls and relays traffic between the two networks. In a typical setup, a 6LoWPAN border router is connected to another router in the backhaul network (over Ethernet or a serial line) which in turn forwards traffic to/from the internet or a private company LAN, for instance.

https://raw.githubusercontent.com/ARMmbed/nanostack-border-router/f8bf21aac12c9926afba252187e2adf2525bf1eb/images/br_role.png

Instructions how to set up a Thread Border Router

Use this example with Mbed CLI

1. Import the application into your desktop:

mbed import https://os.mbed.com/teams/SiliconLabs/code/pelion-example-tbsense2

cd pelion-example-tbsense2

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 TB_SENSE_2

(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 90AC
[BOOT] Active firmware integrity check:
[BOOT] SHA256: 615A11A7F03B1F048573E2CB51D8C9A5DD4E6F17A7F8E79C4B64E3241FF78974
[BOOT] Version: 1553594998
[BOOT] Slot 0 is empty
[BOOT] Active firmware up-to-date
[BOOT] Application's start address: 0x10400
[BOOT] Application's jump address: 0x10FBD
[BOOT] Application's stack address: 0x20040000
[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 802.15.4...
Connected to the network successfully. IP address: 2001:****:****:****:****:****:****:73bc
Initializing Pelion Device Management Client...
Si7021 Electronic Serial Number:                0         15b5ffff, firmware rev 20
Registered to Pelion Device Management. Endpoint Name: 0169b91a********************01a0
                                                                 
BMP280 temp:     32.750 C,   pressure: 1030.750 [mbar]            
Si7021 temp:     27.529 C,   humidity:   24.842 %                 
Si7210 temp:     34.484 C,   field:      -0.076 [mT]              
Si1133 light:  1258.574 lux, UV level:    0.031                       
CCS811 CO2:           0 ppm, VoC:             0 ppb                   
ICM20648 acc:    -0.093 x,  -0.057 y,     0.969 z [mg]            
ICM20648 gyro:   -1.503 x,   0.122 y,    -0.771 z [mdps]          
    
Revision:
3:6647d74cf212
Parent:
2:8d3f3f35f089
Child:
4:0b54e0395ac1
--- a/main.cpp	Tue Mar 26 00:14:14 2019 +0000
+++ b/main.cpp	Tue Mar 26 10:29:33 2019 +0000
@@ -34,7 +34,7 @@
 #include "ICM20648.h"
 
 // Define measurement intervals in seconds
-#define SENSORS_POLL_INTERVAL 1.0
+#define SENSORS_POLL_INTERVAL 3.0
 #define SEND_ALL_SENSORS
 
 /* Turn on power supply to ENV sensor suite */
@@ -45,7 +45,7 @@
 DigitalOut hall_en(PB10, 1);
 /* Turn on power to IMU */
 DigitalOut imu_en(PF8, 1);
-
+ 
 I2C env_i2c(PC4, PC5);
 BMP280 sens_press_temp(env_i2c);
 Si1133 sens_light(PC4, PC5);
@@ -213,21 +213,22 @@
 void sensors_init() {
     sens_press_temp.initialize();
 
-    if (!sens_light.open()) {
-        printf("ERROR: Failed to initialize sensor Si1133\n");
-    }
-
     SI7021::SI7021_status_t result = sens_hum_temp.SI7021_SoftReset();
     if (result == SI7021::SI7021_SUCCESS) {
         wait_ms(15);
         SI7021::SI7021_vector_data_t result_data;
-        result = sens_hum_temp.SI7021_Conf(SI7021::SI7021_RESOLUTION_RH_11_TEMP_11, SI7021::SI7021_HTRE_DISABLED);
+        result = sens_hum_temp.SI7021_Conf(SI7021::SI7021_RESOLUTION_RH_11_TEMP_11,
+                                        SI7021::SI7021_HTRE_DISABLED);
         result = sens_hum_temp.SI7021_GetElectronicSerialNumber(&result_data);
         result = sens_hum_temp.SI7021_GetFirmwareRevision(&result_data);
         printf("Si7021 Electronic Serial Number: %16x %16x, firmware rev %02x\n",
-            result_data.ElectronicSerialNumber_MSB,
-            result_data.ElectronicSerialNumber_LSB,
-            result_data.FirmwareRevision);
+                result_data.ElectronicSerialNumber_MSB,
+                result_data.ElectronicSerialNumber_LSB,
+                result_data.FirmwareRevision);
+    }
+
+    if (!sens_light.open()) {
+        printf("ERROR: Failed to initialize sensor Si1133\n");
     }
 
     if (!sens_aqs.init()) {
@@ -238,7 +239,7 @@
         }
 //        sens_aqs.enable_interupt(true);
     }
-
+ 
     if (!sens_imu.open()) {
         printf("ERROR: Failed to initialize sensor ICM20648\n");
     }
@@ -249,15 +250,16 @@
  * This function is called periodically.
  */
 void sensors_update() {
-    SI7021::SI7021_status_t rht_reading, humidity_reading, temp2_reading;
+    SI7021::SI7021_status_t sens_hum_temp_reading, humidity_reading, temp2_reading;
     SI7021::SI7021_vector_data_t humidity_data, temp2_data;
 
-    // BMP280 pressure and temperature (2)
-    float pressure_value = sens_press_temp.getPressure(), temp1_value = sens_press_temp.getPressure();
+    // BMP280 pressure and temperature (1)
+    float pressure_value = sens_press_temp.getPressure();
+    float temp1_value = sens_press_temp.getTemperature();
 
-    // Si7021 humidity and temperature (1)
-    rht_reading = sens_hum_temp.SI7021_TriggerHumidity(SI7021::SI7021_NO_HOLD_MASTER_MODE);
-    if (rht_reading == SI7021::SI7021_SUCCESS) {
+    // Si7021 humidity and temperature (2)
+    sens_hum_temp_reading = sens_hum_temp.SI7021_TriggerHumidity(SI7021::SI7021_NO_HOLD_MASTER_MODE);
+    if (sens_hum_temp_reading == SI7021::SI7021_SUCCESS) {
         wait_ms(30);
         humidity_reading = sens_hum_temp.SI7021_ReadHumidity(&humidity_data);
         temp2_reading = sens_hum_temp.SI7021_ReadTemperatureFromRH(&temp2_data);
@@ -276,53 +278,52 @@
     // Si7210 field and temperature (3)
     sens_hall.measureOnce();
     float field_value = sens_hall.getFieldStrength() / 1000, temp3_value = sens_hall.getTemperature() / 1000;
-
+ 
+    // ICM20648 accelerometer and gyroscope
     float acc_x, acc_y, acc_z, gyr_x, gyr_y, gyr_z, temp4_value;
     sens_imu.get_accelerometer(&acc_x, &acc_y, &acc_z);
     sens_imu.get_gyroscope(&gyr_x, &gyr_y, &gyr_z);
     sens_imu.get_temperature(&temp4_value);
 
     printf("                                                                 \n");
-    printf("BMP280 temp:   %7.2f C,   pressure: %7.2f mbar       \n", temp1_value, pressure_value);
-    printf("Si7021 temp:   %7.2f C,   humidity: %7.2f %%         \n", temp2_data.Temperature, humidity_data.RelativeHumidity);
-    printf("Si7210 temp:   %7.2f C,   field: %7.2f mT            \n", temp3_value, field_value);
-    printf("Si1133 light:  %7.2f lux, UV level: %7.2f            \n", light_value, uv_index_value);
-    printf("CCS811 CO2:    %7d ppm, VoC:     %7d ppb         \n", co2_value, tvoc_value);
-    printf("ICM20648 acc:  %7.2f x, %7.2f y, %7.2f z [mg]           \n", acc_x, acc_y, acc_z);
-    printf("ICM20648 gyro: %7.2f x, %7.2f y, %7.2f z [mdps]         \n", gyr_x, gyr_y, gyr_z);
+    printf("BMP280 temp:   %7.2f C,   pressure: %7.2f [mbar]            \n", temp1_value, pressure_value);
+    printf("Si7021 temp:   %7.2f C,   humidity: %7.2f %%                \n", temp2_data.Temperature, humidity_data.RelativeHumidity);
+    printf("Si7210 temp:   %7.2f C,   field:    %7.2f [mT]              \n", temp3_value, field_value);
+    printf("Si1133 light:  %7.2f lux, UV level: %7.2f                   \n", light_value, uv_index_value);
+    printf("CCS811 CO2:    %7d ppm, VoC:      %7d ppb                   \n", co2_value, tvoc_value);
+    printf("ICM20648 acc:  %7.2f x, %6.2f y,   %7.2f z [mg]            \n", acc_x, acc_y, acc_z);
+    printf("ICM20648 gyro: %7.2f x, %6.2f y,   %7.2f z [mdps]          \n", gyr_x, gyr_y, gyr_z);
 
     printf("\r\033[8A");
 
     if (endpointInfo) {
 #ifdef SEND_ALL_SENSORS
-        res_pressure->set_value(pressure_value);
-        res_temperature1->set_value(temp1_value);
-
+        res_pressure->set_value((float)pressure_value);
+        res_temperature1->set_value((float)temp1_value);
+ 
         if (humidity_reading == SI7021::SI7021_SUCCESS) {
-            res_humidity->set_value(humidity_data.RelativeHumidity);
+            res_humidity->set_value((float)humidity_data.RelativeHumidity);
         }
         if (temp2_reading == SI7021::SI7021_SUCCESS) {
-            res_temperature2->set_value(temp2_data.Temperature);
+            res_temperature2->set_value((float)temp2_data.Temperature);
         }
 
-        res_field->set_value(field_value);
-        res_temperature3->set_value(temp3_value);
+        res_field->set_value((float)field_value);
+        res_temperature3->set_value((float)temp3_value);
 
         if (light_reading) {
-            res_light->set_value(light_value);
+            res_light->set_value((float)light_value);
         }
 
         res_co2->set_value(co2_value);
         res_tvoc->set_value(tvoc_value);
 
-        res_accelerometer_x->set_value(acc_x);
-        res_accelerometer_y->set_value(acc_y);
-        res_accelerometer_z->set_value(acc_z);
-        res_gyroscope_x->set_value(gyr_x);
-        res_gyroscope_y->set_value(gyr_y);
-        res_gyroscope_z->set_value(gyr_z);
-
-        res_temperature4->set_value(temp4_value);
+        res_accelerometer_x->set_value((float)acc_x);
+        res_accelerometer_y->set_value((float)acc_y);
+        res_accelerometer_z->set_value((float)acc_z);
+        res_gyroscope_x->set_value((float)gyr_x);
+        res_gyroscope_y->set_value((float)gyr_y);
+        res_gyroscope_z->set_value((float)gyr_z);
 #endif /* SEND_ALL_SENSORS */
     }
 }
@@ -389,57 +390,58 @@
     }
 
     /* Create resources */
-    res_led = client.create_resource("3201/0/5853", "LED blinking (R:G:B:count)");
+    res_led = client.create_resource("3201/0/5853", "LED blinking (R:G:B:cnt)");
     res_led->observable(false);
     res_led->set_value("0:0:0:0");
     res_led->attach_post_callback(blink_callback);
     res_led->methods(M2MMethod::POST);
 
-    res_button = client.create_resource("3200/0/5501", "Button Click Count");
+    res_button = client.create_resource("3200/0/5501", "button_count");
     res_button->set_value(0);
     res_button->methods(M2MMethod::GET);
     res_button->observable(true);
     res_button->attach_notification_callback(button_callback);
 
-#ifdef SEND_ALL_SENSORS
-    res_light = client.create_resource("3301/0/5853", "LightIntensity (LUX)");
-    res_light->set_value(0);
-    res_light->observable(true);
-    res_light->methods(M2MMethod::GET);
-
+#ifdef SEND_ALL_SENSORS 
     // Sensor BMP280
     res_pressure = client.create_resource("3323/0/5853", "Barometric pressure (hPa)");
     res_pressure->set_value(0);
     res_pressure->observable(true);
     res_pressure->methods(M2MMethod::GET);
-
+ 
     res_temperature1 = client.create_resource("3303/0/5853", "Temperature BMP280 (C)");
     res_temperature1->set_value(0);
     res_temperature1->observable(true);
     res_temperature1->methods(M2MMethod::GET);
-    res_temperature2->methods(M2MMethod::GET);
-
+ 
     // Sensor Si7021
     res_humidity = client.create_resource("3304/0/5853", "Humidity (%)");
     res_humidity->set_value(0);
     res_humidity->observable(true);
     res_humidity->methods(M2MMethod::GET);
-
+ 
     res_temperature2 = client.create_resource("3303/1/5853", "Temperature Si7021 (C)");
     res_temperature2->set_value(0);
     res_temperature2->observable(true);
-
-    // Sensor Si7210
-    res_field = client.create_resource("3304/0/5853", "Magnetic Field (mT)");
+    res_temperature2->methods(M2MMethod::GET);
+ 
+     // Sensor Si7210
+    res_field = client.create_resource("33257/0/5853", "Magnetic Field (mT)");
     res_field->set_value(0);
     res_field->observable(true);
     res_field->methods(M2MMethod::GET);
-
+ 
     res_temperature3 = client.create_resource("3303/2/5853", "Temperature Si7210 (C)");
     res_temperature3->set_value(0);
     res_temperature3->observable(true);
     res_temperature3->methods(M2MMethod::GET);
 
+    // Sensor Si1133
+    res_light = client.create_resource("3301/0/5853", "LightIntensity (LUX)");
+    res_light->set_value(0);
+    res_light->observable(true);
+    res_light->methods(M2MMethod::GET);
+
     // Sensor CCS811
     res_co2 = client.create_resource("33255/0/5853", "CO2 (ppm)");
     res_co2->set_value(0);
@@ -450,38 +452,38 @@
     res_tvoc->set_value(0);
     res_tvoc->observable(true);
     res_tvoc->methods(M2MMethod::GET);
-
+ 
     // Sensor ICM20648
     res_accelerometer_x = client.create_resource("3313/0/5702", "Accelerometer X");
     res_accelerometer_x->set_value(0);
     res_accelerometer_x->methods(M2MMethod::GET);
     res_accelerometer_x->observable(true);
-
+ 
     res_accelerometer_y = client.create_resource("3313/0/5703", "Accelerometer Y");
     res_accelerometer_y->set_value(0);
     res_accelerometer_y->methods(M2MMethod::GET);
     res_accelerometer_y->observable(true);
-
+ 
     res_accelerometer_z = client.create_resource("3313/0/5704", "Accelerometer Z");
     res_accelerometer_z->set_value(0);
     res_accelerometer_z->methods(M2MMethod::GET);
     res_accelerometer_z->observable(true);
-
+ 
     res_gyroscope_x = client.create_resource("3334/0/5702", "Gyroscope X");
     res_gyroscope_x->set_value(0);
     res_gyroscope_x->methods(M2MMethod::GET);
     res_gyroscope_x->observable(true);
-
+ 
     res_gyroscope_y = client.create_resource("3334/0/5703", "Gyroscope Y");
     res_gyroscope_y->set_value(0);
     res_gyroscope_y->methods(M2MMethod::GET);
     res_gyroscope_y->observable(true);
-
+ 
     res_gyroscope_z = client.create_resource("3334/0/5704", "Gyroscope Z");
     res_gyroscope_z->set_value(0);
     res_gyroscope_z->methods(M2MMethod::GET);
     res_gyroscope_z->observable(true);
-
+ 
     res_temperature4 = client.create_resource("3303/3/5853", "Temperature ICM20648 (C)");
     res_temperature4->set_value(0);
     res_temperature4->observable(true);