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:
2:8d3f3f35f089
Parent:
1:cf0bd0446785
Child:
3:6647d74cf212
--- a/main.cpp	Mon Mar 25 22:47:24 2019 +0000
+++ b/main.cpp	Tue Mar 26 00:14:14 2019 +0000
@@ -29,7 +29,9 @@
 #include "BMP280.h"
 #include "Si1133.h"
 #include "SI7021.h"
+#include "Si7210.h"
 #include "AMS_CCS811.h"
+#include "ICM20648.h"
 
 // Define measurement intervals in seconds
 #define SENSORS_POLL_INTERVAL 1.0
@@ -39,14 +41,21 @@
 DigitalOut env_en(PF9, 1);
 /* Turn on power to CCS811 sensor */
 DigitalOut ccs_en(PF14, 1);
+/* Turn on power to hall effect sensor */
+DigitalOut hall_en(PB10, 1);
+/* Turn on power to IMU */
+DigitalOut imu_en(PF8, 1);
 
-BMP280 barometer(PC4, PC5);
-Si1133 light_sensor(PC4, PC5);
-SI7021 rht_sensor(PC4, PC5, SI7021::SI7021_ADDRESS, 400000);
-
+I2C env_i2c(PC4, PC5);
+BMP280 sens_press_temp(env_i2c);
+Si1133 sens_light(PC4, PC5);
+SI7021 sens_hum_temp(PC4, PC5, SI7021::SI7021_ADDRESS, 400000);
+I2C hall_i2c(PB8, PB9);
+silabs::Si7210 sens_hall(&hall_i2c);
 InterruptIn ccs_int(PF13);
 I2C ccs_i2c(PB6, PB7);
-AMS_CCS811 aqs(&ccs_i2c, PF15);
+AMS_CCS811 sens_aqs(&ccs_i2c, PF15);
+ICM20648 sens_imu(PC0, PC1, PC2, PC3, PF12);
 
 // Declaring pointers for access to Pelion Client resources outside of main()
 MbedCloudClientResource *res_button;
@@ -54,11 +63,21 @@
 
 #ifdef SEND_ALL_SENSORS
 MbedCloudClientResource *res_light;
-MbedCloudClientResource *res_barometer;
-MbedCloudClientResource *res_temperature;
+MbedCloudClientResource *res_pressure;
+MbedCloudClientResource *res_temperature1;
 MbedCloudClientResource *res_humidity;
+MbedCloudClientResource *res_temperature2;
 MbedCloudClientResource *res_co2;
 MbedCloudClientResource *res_tvoc;
+MbedCloudClientResource *res_field;
+MbedCloudClientResource *res_temperature3;
+MbedCloudClientResource *res_accelerometer_x;
+MbedCloudClientResource *res_accelerometer_y;
+MbedCloudClientResource *res_accelerometer_z;
+MbedCloudClientResource *res_gyroscope_x;
+MbedCloudClientResource *res_gyroscope_y;
+MbedCloudClientResource *res_gyroscope_z;
+MbedCloudClientResource *res_temperature4;
 #endif /* SEND_ALL_SENSORS */
 
 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
@@ -192,31 +211,36 @@
  * Initialize sensors
  */
 void sensors_init() {
-    barometer.initialize();
+    sens_press_temp.initialize();
 
-    light_sensor.open();
+    if (!sens_light.open()) {
+        printf("ERROR: Failed to initialize sensor Si1133\n");
+    }
 
-    SI7021::SI7021_status_t result = rht_sensor.SI7021_SoftReset();
+    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 = rht_sensor.SI7021_Conf(SI7021::SI7021_RESOLUTION_RH_11_TEMP_11,
-                                        SI7021::SI7021_HTRE_DISABLED);
-        result = rht_sensor.SI7021_GetElectronicSerialNumber(&result_data);
-        result = rht_sensor.SI7021_GetFirmwareRevision(&result_data);
+        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(!aqs.init()) {
-        printf("CCS811: Failed init()\n");
+    if (!sens_aqs.init()) {
+        printf("ERROR: Failed to initialize sensor CCS811\n");
     } else {
-        if(!aqs.mode(AMS_CCS811::SIXTY_SECOND)) {
-            printf("CCS811: Failed to set mode\n");
+        if (!sens_aqs.mode(AMS_CCS811::SIXTY_SECOND)) {
+            printf("ERROR: Failed to set mode for sensor CCS811\n");
         }
-//        aqs.enable_interupt(true);
+//        sens_aqs.enable_interupt(true);
+    }
+
+    if (!sens_imu.open()) {
+        printf("ERROR: Failed to initialize sensor ICM20648\n");
     }
 }
 
@@ -225,49 +249,80 @@
  * This function is called periodically.
  */
 void sensors_update() {
-    static size_t CCS811_samples = 0;
-    SI7021::SI7021_status_t rht_reading, humidity_reading, temp_reading;
-    SI7021::SI7021_vector_data_t humidity_data, temp_data;
+    SI7021::SI7021_status_t rht_reading, humidity_reading, temp2_reading;
+    SI7021::SI7021_vector_data_t humidity_data, temp2_data;
 
-    float val_barometer = barometer.getPressure();
+    // BMP280 pressure and temperature (2)
+    float pressure_value = sens_press_temp.getPressure(), temp1_value = sens_press_temp.getPressure();
 
-    float light_level, uv_index;
-    bool light_reading = light_sensor.get_light_and_uv(&light_level, &uv_index);
-
-    rht_reading = rht_sensor.SI7021_TriggerHumidity(SI7021::SI7021_NO_HOLD_MASTER_MODE);
+    // Si7021 humidity and temperature (1)
+    rht_reading = sens_hum_temp.SI7021_TriggerHumidity(SI7021::SI7021_NO_HOLD_MASTER_MODE);
     if (rht_reading == SI7021::SI7021_SUCCESS) {
         wait_ms(30);
-        humidity_reading = rht_sensor.SI7021_ReadHumidity(&humidity_data);
-        temp_reading = rht_sensor.SI7021_ReadTemperatureFromRH(&temp_data);
-        aqs.env_data(humidity_data.RelativeHumidity, temp_data.Temperature);
+        humidity_reading = sens_hum_temp.SI7021_ReadHumidity(&humidity_data);
+        temp2_reading = sens_hum_temp.SI7021_ReadTemperatureFromRH(&temp2_data);
+        sens_aqs.env_data(humidity_data.RelativeHumidity, temp2_data.Temperature);
     }
 
-    aqs.has_new_data();
-    int co2 = aqs.co2_read();
-    int tvoc = aqs.tvoc_read();
+    // Si1133 light and UV index
+    float light_value, uv_index_value;
+    bool light_reading = sens_light.get_light_and_uv(&light_value, &uv_index_value);
+
+    // CCS811 air quality CO2 and TVoC
+    sens_aqs.has_new_data();
+    int co2_value = sens_aqs.co2_read();
+    int tvoc_value = sens_aqs.tvoc_read();
+
+    // Si7210 field and temperature (3)
+    sens_hall.measureOnce();
+    float field_value = sens_hall.getFieldStrength() / 1000, temp3_value = sens_hall.getTemperature() / 1000;
 
-    printf("                                                             \n");
-    printf("Si1133 light: %7.2f lux, UV level: %7.2f            \n", light_level, uv_index);
-    printf("SI7021 temp:  %7.2f C,   humidity: %7.2f %%         \n", temp_data.Temperature, humidity_data.RelativeHumidity);
-    printf("CCS811 CO2:   %7d ppm, TVOC:     %7d ppm        \n", co2, tvoc);
-    printf("BMP280 pressure: %7.2f hPa       \n", val_barometer);
+    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("\r\033[5A");
+    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("\r\033[8A");
 
     if (endpointInfo) {
 #ifdef SEND_ALL_SENSORS
-        res_barometer->set_value(val_barometer);
-        if (light_reading) {
-            res_light->set_value(light_level);
-        }
+        res_pressure->set_value(pressure_value);
+        res_temperature1->set_value(temp1_value);
+
         if (humidity_reading == SI7021::SI7021_SUCCESS) {
             res_humidity->set_value(humidity_data.RelativeHumidity);
         }
-        if (temp_reading == SI7021::SI7021_SUCCESS) {
-            res_temperature->set_value(temp_data.Temperature);
+        if (temp2_reading == SI7021::SI7021_SUCCESS) {
+            res_temperature2->set_value(temp2_data.Temperature);
+        }
+
+        res_field->set_value(field_value);
+        res_temperature3->set_value(temp3_value);
+
+        if (light_reading) {
+            res_light->set_value(light_value);
         }
-        res_co2->set_value(co2);
-        res_tvoc->set_value(tvoc);
+
+        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);
 #endif /* SEND_ALL_SENSORS */
     }
 }
@@ -334,39 +389,58 @@
     }
 
     /* Create resources */
-    res_led = client.create_resource("3201/0/5853", "LED blinking (R:G:B:cnt)");
+    res_led = client.create_resource("3201/0/5853", "LED blinking (R:G:B:count)");
     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_count");
+    res_button = client.create_resource("3200/0/5501", "Button Click 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 = client.create_resource("3301/0/5853", "LightIntensity (LUX)");
     res_light->set_value(0);
     res_light->observable(true);
     res_light->methods(M2MMethod::GET);
 
-    res_temperature = client.create_resource("3303/0/5853", "Temperature (C)");
-    res_temperature->set_value(0);
-    res_temperature->observable(true);
-    res_temperature->methods(M2MMethod::GET);
+    // 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_barometer = client.create_resource("3323/0/5853", "Barometric pressure (hPa)");
-    res_barometer->set_value(0);
-    res_barometer->observable(true);
-    res_barometer->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_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 CCS811
     res_co2 = client.create_resource("33255/0/5853", "CO2 (ppm)");
     res_co2->set_value(0);
     res_co2->observable(true);
@@ -376,6 +450,42 @@
     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);
+    res_temperature4->methods(M2MMethod::GET);
 #endif /* SEND_ALL_SENSORS */
 
     sensors_init();