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:
chris
Date:
Fri Mar 13 13:18:07 2020 +0000
Revision:
36:aaf44d2a6c33
Parent:
35:061bc344e4ec
First revision

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
screamer 28:0e774865873d 19
adustm 1:e86b1cffc402 20 #include "mbed.h"
adustm 1:e86b1cffc402 21 #include "simple-mbed-cloud-client.h"
screamer 10:b27c962b3c3f 22 #include "LittleFileSystem.h"
screamer 33:cfd9430e7d1e 23
screamer 33:cfd9430e7d1e 24 // Default network interface object. Don't forget to change the WiFi SSID/password in mbed_app.json if you're using WiFi.
chris 35:061bc344e4ec 25 // NetworkInterface *net;
chris 35:061bc344e4ec 26 static WiFiInterface *net = NULL;
chris 35:061bc344e4ec 27
chris 35:061bc344e4ec 28
chris 36:aaf44d2a6c33 29 #define WIFI_SSID "<your ssid>"
chris 36:aaf44d2a6c33 30 #define WIFI_PASSWORD "<your pwd>"
screamer 33:cfd9430e7d1e 31
screamer 33:cfd9430e7d1e 32 // Default block device available on the target board
screamer 33:cfd9430e7d1e 33 BlockDevice* bd = BlockDevice::get_default_instance();
screamer 33:cfd9430e7d1e 34 SlicingBlockDevice sd(bd, 0, 2*1024*1024);
screamer 33:cfd9430e7d1e 35
screamer 33:cfd9430e7d1e 36 #if COMPONENT_SD || COMPONENT_NUSD
screamer 33:cfd9430e7d1e 37 // Use FATFileSystem for SD card type blockdevices
screamer 33:cfd9430e7d1e 38 FATFileSystem fs("fs");
screamer 33:cfd9430e7d1e 39 #else
screamer 33:cfd9430e7d1e 40 // Use LittleFileSystem for non-SD block devices to enable wear leveling and other functions
screamer 33:cfd9430e7d1e 41 LittleFileSystem fs("fs");
screamer 33:cfd9430e7d1e 42 #endif
screamer 33:cfd9430e7d1e 43
screamer 33:cfd9430e7d1e 44 // Default User button for GET example and for resetting the storage
screamer 33:cfd9430e7d1e 45 InterruptIn button(BUTTON1);
screamer 33:cfd9430e7d1e 46 // Default LED to use for PUT/POST example
screamer 33:cfd9430e7d1e 47 DigitalOut led(LED1, 1);
screamer 33:cfd9430e7d1e 48
screamer 33:cfd9430e7d1e 49 // How often to fetch sensor data (in seconds)
screamer 33:cfd9430e7d1e 50 #define SENSORS_POLL_INTERVAL 3.0
screamer 33:cfd9430e7d1e 51
screamer 33:cfd9430e7d1e 52 // Send all sensor data or just limited (useful for when running out of memory)
screamer 33:cfd9430e7d1e 53 #define SEND_ALL_SENSORS
screamer 33:cfd9430e7d1e 54
screamer 33:cfd9430e7d1e 55 // Sensors related includes and initialization
screamer 10:b27c962b3c3f 56 #include "HTS221Sensor.h"
screamer 10:b27c962b3c3f 57 #include "LPS22HBSensor.h"
screamer 10:b27c962b3c3f 58 #include "LSM6DSLSensor.h"
screamer 10:b27c962b3c3f 59 #include "lis3mdl_class.h"
screamer 28:0e774865873d 60 #include "VL53L0X.h"
screamer 10:b27c962b3c3f 61
screamer 10:b27c962b3c3f 62 static DevI2C devI2c(PB_11,PB_10);
screamer 12:1f1a50e973db 63 static HTS221Sensor sen_hum_temp(&devI2c);
screamer 12:1f1a50e973db 64 static LPS22HBSensor sen_press_temp(&devI2c);
screamer 12:1f1a50e973db 65 static LSM6DSLSensor sen_acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
screamer 12:1f1a50e973db 66 static LIS3MDL sen_mag(&devI2c);
screamer 28:0e774865873d 67 static DigitalOut shutdown_pin(PC_6);
screamer 28:0e774865873d 68 static VL53L0X sen_distance(&devI2c, &shutdown_pin, PC_7);
screamer 33:cfd9430e7d1e 69 // Temperature reading from microcontroller
screamer 33:cfd9430e7d1e 70 AnalogIn adc_temp(ADC_TEMP);
screamer 33:cfd9430e7d1e 71 // Voltage reference reading from microcontroller
screamer 33:cfd9430e7d1e 72 AnalogIn adc_vref(ADC_VREF);
screamer 11:8df4529f060d 73
MarceloSalazar 9:265744785d33 74 // Declaring pointers for access to Pelion Client resources outside of main()
screamer 12:1f1a50e973db 75 MbedCloudClientResource *res_button;
screamer 12:1f1a50e973db 76 MbedCloudClientResource *res_led;
adustm 1:e86b1cffc402 77
screamer 33:cfd9430e7d1e 78 // Additional resources for sensor readings
screamer 28:0e774865873d 79 #ifdef SEND_ALL_SENSORS
screamer 12:1f1a50e973db 80 MbedCloudClientResource *res_humidity;
screamer 12:1f1a50e973db 81 MbedCloudClientResource *res_temperature;
screamer 12:1f1a50e973db 82 MbedCloudClientResource *res_pressure;
screamer 12:1f1a50e973db 83 MbedCloudClientResource *res_temperature2;
screamer 28:0e774865873d 84 MbedCloudClientResource *res_magnometer_x;
screamer 28:0e774865873d 85 MbedCloudClientResource *res_magnometer_y;
screamer 28:0e774865873d 86 MbedCloudClientResource *res_magnometer_z;
screamer 28:0e774865873d 87 MbedCloudClientResource *res_accelerometer_x;
screamer 28:0e774865873d 88 MbedCloudClientResource *res_accelerometer_y;
screamer 28:0e774865873d 89 MbedCloudClientResource *res_accelerometer_z;
screamer 13:42b49a0caade 90 MbedCloudClientResource *res_gyroscope_x;
screamer 13:42b49a0caade 91 MbedCloudClientResource *res_gyroscope_y;
screamer 13:42b49a0caade 92 MbedCloudClientResource *res_gyroscope_z;
screamer 12:1f1a50e973db 93 MbedCloudClientResource *res_distance;
screamer 28:0e774865873d 94 MbedCloudClientResource *res_adc_temp;
screamer 28:0e774865873d 95 MbedCloudClientResource *res_adc_voltage;
screamer 13:42b49a0caade 96 #endif /* SEND_ALL_SENSORS */
adustm 1:e86b1cffc402 97
screamer 33:cfd9430e7d1e 98 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
screamer 33:cfd9430e7d1e 99 // 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
screamer 33:cfd9430e7d1e 100 EventQueue eventQueue;
screamer 33:cfd9430e7d1e 101
screamer 10:b27c962b3c3f 102 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
screamer 10:b27c962b3c3f 103 static const ConnectorClientEndpointInfo* endpointInfo;
adustm 1:e86b1cffc402 104
screamer 10:b27c962b3c3f 105 /**
adustm 4:cf7342047b4d 106 * PUT handler
adustm 4:cf7342047b4d 107 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 108 * @param newValue Updated value for the resource
adustm 4:cf7342047b4d 109 */
screamer 32:2871fbeb627d 110 void put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
screamer 29:6ff737b67e7d 111 printf("*** PUT received, new value: %s \n", newValue.c_str());
screamer 11:8df4529f060d 112 led = atoi(newValue.c_str());
adustm 1:e86b1cffc402 113 }
adustm 1:e86b1cffc402 114
adustm 4:cf7342047b4d 115 /**
adustm 4:cf7342047b4d 116 * POST handler
adustm 4:cf7342047b4d 117 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 118 * @param buffer If a body was passed to the POST function, this contains the data.
adustm 4:cf7342047b4d 119 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
adustm 4:cf7342047b4d 120 * @param size Size of the body
adustm 4:cf7342047b4d 121 */
screamer 32:2871fbeb627d 122 void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
screamer 32:2871fbeb627d 123 printf("*** POST received (length %u). Payload: ", size);
screamer 32:2871fbeb627d 124 for (size_t ix = 0; ix < size; ix++) {
screamer 32:2871fbeb627d 125 printf("%02x ", buffer[ix]);
screamer 32:2871fbeb627d 126 }
screamer 32:2871fbeb627d 127 printf("\n");
screamer 11:8df4529f060d 128 }
adustm 1:e86b1cffc402 129
screamer 11:8df4529f060d 130 /**
screamer 13:42b49a0caade 131 * Button function triggered by the physical button press.
screamer 11:8df4529f060d 132 */
screamer 11:8df4529f060d 133 void button_press() {
screamer 12:1f1a50e973db 134 int v = res_button->get_value_int() + 1;
screamer 12:1f1a50e973db 135 res_button->set_value(v);
screamer 29:6ff737b67e7d 136 printf("*** Button clicked %d times \n", v);
adustm 1:e86b1cffc402 137 }
adustm 1:e86b1cffc402 138
adustm 4:cf7342047b4d 139 /**
adustm 4:cf7342047b4d 140 * Notification callback handler
adustm 4:cf7342047b4d 141 * @param resource The resource that triggered the callback
adustm 4:cf7342047b4d 142 * @param status The delivery status of the notification
adustm 4:cf7342047b4d 143 */
adustm 4:cf7342047b4d 144 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
screamer 29:6ff737b67e7d 145 printf("*** Button notification, status %s (%d) \n", MbedCloudClientResource::delivery_status_to_string(status), status);
adustm 4:cf7342047b4d 146 }
adustm 1:e86b1cffc402 147
adustm 4:cf7342047b4d 148 /**
adustm 4:cf7342047b4d 149 * Registration callback handler
adustm 4:cf7342047b4d 150 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
adustm 4:cf7342047b4d 151 */
adustm 4:cf7342047b4d 152 void registered(const ConnectorClientEndpointInfo *endpoint) {
screamer 17:fc98adcf835a 153 printf("Registered to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
screamer 10:b27c962b3c3f 154 endpointInfo = endpoint;
adustm 4:cf7342047b4d 155 }
adustm 1:e86b1cffc402 156
screamer 10:b27c962b3c3f 157 /**
screamer 10:b27c962b3c3f 158 * Initialize sensors
screamer 10:b27c962b3c3f 159 */
screamer 10:b27c962b3c3f 160 void sensors_init() {
screamer 29:6ff737b67e7d 161 uint8_t id1, id2, id3, id4;
screamer 10:b27c962b3c3f 162
screamer 29:6ff737b67e7d 163 printf ("\nSensors configuration:\n");
screamer 10:b27c962b3c3f 164 // Initialize sensors
screamer 12:1f1a50e973db 165 sen_hum_temp.init(NULL);
screamer 12:1f1a50e973db 166 sen_press_temp.init(NULL);
screamer 12:1f1a50e973db 167 sen_acc_gyro.init(NULL);
screamer 12:1f1a50e973db 168 sen_mag.init(NULL);
screamer 28:0e774865873d 169 sen_distance.init_sensor(VL53L0X_DEFAULT_ADDRESS);
screamer 10:b27c962b3c3f 170
screamer 10:b27c962b3c3f 171 /// Call sensors enable routines
screamer 12:1f1a50e973db 172 sen_hum_temp.enable();
screamer 12:1f1a50e973db 173 sen_press_temp.enable();
screamer 12:1f1a50e973db 174 sen_acc_gyro.enable_x();
screamer 12:1f1a50e973db 175 sen_acc_gyro.enable_g();
screamer 10:b27c962b3c3f 176
screamer 29:6ff737b67e7d 177 sen_hum_temp.read_id(&id1);
screamer 29:6ff737b67e7d 178 sen_press_temp.read_id(&id2);
screamer 29:6ff737b67e7d 179 sen_mag.read_id(&id3);
screamer 29:6ff737b67e7d 180 sen_acc_gyro.read_id(&id4);
screamer 10:b27c962b3c3f 181
screamer 29:6ff737b67e7d 182 printf("HTS221 humidity & temperature = 0x%X\n", id1);
screamer 29:6ff737b67e7d 183 printf("LPS22HB pressure & temperature = 0x%X\n", id2);
screamer 29:6ff737b67e7d 184 printf("LIS3MDL magnetometer = 0x%X\n", id3);
screamer 29:6ff737b67e7d 185 printf("LSM6DSL accelerometer & gyroscope = 0x%X\n", id4);
screamer 10:b27c962b3c3f 186
screamer 17:fc98adcf835a 187 printf("\n"); ;
screamer 10:b27c962b3c3f 188 }
screamer 10:b27c962b3c3f 189
screamer 10:b27c962b3c3f 190 /**
screamer 10:b27c962b3c3f 191 * Update sensors and report their values.
screamer 10:b27c962b3c3f 192 * This function is called periodically.
screamer 10:b27c962b3c3f 193 */
screamer 10:b27c962b3c3f 194 void sensors_update() {
screamer 32:2871fbeb627d 195 float temp1_value, temp2_value, temp3_value, humid_value, pressure_value, volt_value = 0.0;
screamer 28:0e774865873d 196 int32_t m_axes[3], a_axes[3], g_axes[3];
screamer 32:2871fbeb627d 197 uint32_t distance_value, distance_reading;
screamer 10:b27c962b3c3f 198
screamer 32:2871fbeb627d 199 sen_hum_temp.get_humidity(&humid_value);
screamer 32:2871fbeb627d 200 sen_hum_temp.get_temperature(&temp1_value);
screamer 32:2871fbeb627d 201 sen_press_temp.get_pressure(&pressure_value);
screamer 32:2871fbeb627d 202 sen_press_temp.get_temperature(&temp2_value);
screamer 28:0e774865873d 203 sen_mag.get_m_axes(m_axes);
screamer 28:0e774865873d 204 sen_acc_gyro.get_x_axes(a_axes);
screamer 28:0e774865873d 205 sen_acc_gyro.get_g_axes(g_axes);
screamer 32:2871fbeb627d 206 distance_reading = sen_distance.get_distance(&distance_value);
screamer 32:2871fbeb627d 207 temp3_value = adc_temp.read()*100;
screamer 32:2871fbeb627d 208 volt_value = adc_vref.read();
screamer 32:2871fbeb627d 209
screamer 32:2871fbeb627d 210 float mag_x = (double)m_axes[0] / 1000.0, mag_y = (double)m_axes[1] / 1000.0, mag_z = (double)m_axes[2] / 1000.0;
screamer 32:2871fbeb627d 211 float acc_x = (double)a_axes[0] / 1000.0, acc_y = (double)a_axes[1] / 1000.0, acc_z = (double)a_axes[2] / 1000.0;
screamer 32:2871fbeb627d 212 float gyro_x = (double)g_axes[0] / 1000.0, gyro_y = (double)g_axes[1] / 1000.0, gyro_z = (double)g_axes[2] / 1000.0;
screamer 28:0e774865873d 213
screamer 32:2871fbeb627d 214 printf(" \n");
screamer 32:2871fbeb627d 215 printf("ADC temp: %5.4f C, vref: %5.4f V \n", temp3_value, volt_value);
screamer 32:2871fbeb627d 216 printf("HTS221 temp: %7.3f C, humidity: %7.2f %% \n", temp1_value, humid_value);
screamer 32:2871fbeb627d 217 printf("LPS22HB temp: %7.3f C, pressure: %7.2f mbar \n", temp2_value, pressure_value);
screamer 32:2871fbeb627d 218 printf("LIS3MDL mag: %7.3f x, %7.3f y, %7.3f z [gauss] \n", mag_x, mag_y, mag_z);
screamer 32:2871fbeb627d 219 printf("LSM6DSL acc: %7.3f x, %7.3f y, %7.3f z [g] \n", acc_x, acc_y, acc_z);
screamer 32:2871fbeb627d 220 printf("LSM6DSL gyro: %7.3f x, %7.3f y, %7.3f z [dps] \n", gyro_x, gyro_y, gyro_z);
screamer 32:2871fbeb627d 221 if (distance_reading == VL53L0X_ERROR_NONE) {
screamer 32:2871fbeb627d 222 printf("VL53L0X dist: %7ld mm\n", distance_value);
screamer 28:0e774865873d 223 } else {
screamer 28:0e774865873d 224 printf("VL53L0X dist: -- \n");
screamer 32:2871fbeb627d 225 distance_value = 999;
screamer 10:b27c962b3c3f 226 }
screamer 10:b27c962b3c3f 227
screamer 28:0e774865873d 228 printf("\r\033[8A");
screamer 28:0e774865873d 229
screamer 13:42b49a0caade 230 if (endpointInfo) {
screamer 33:cfd9430e7d1e 231 #ifdef SEND_ALL_SENSORS
screamer 32:2871fbeb627d 232 res_humidity->set_value(humid_value);
screamer 32:2871fbeb627d 233 res_temperature->set_value(temp1_value);
screamer 32:2871fbeb627d 234 res_pressure->set_value(pressure_value);
screamer 32:2871fbeb627d 235 res_temperature2->set_value(temp2_value);
screamer 32:2871fbeb627d 236 res_magnometer_x->set_value(mag_x);
screamer 32:2871fbeb627d 237 res_magnometer_y->set_value(mag_y);
screamer 32:2871fbeb627d 238 res_magnometer_z->set_value(mag_z);
screamer 32:2871fbeb627d 239 res_accelerometer_x->set_value(acc_x);
screamer 32:2871fbeb627d 240 res_accelerometer_y->set_value(acc_y);
screamer 32:2871fbeb627d 241 res_accelerometer_z->set_value(acc_z);
screamer 32:2871fbeb627d 242 res_gyroscope_x->set_value(gyro_x);
screamer 32:2871fbeb627d 243 res_gyroscope_y->set_value(gyro_y);
screamer 32:2871fbeb627d 244 res_gyroscope_z->set_value(gyro_z);
screamer 32:2871fbeb627d 245 res_distance->set_value((int)distance_value);
screamer 32:2871fbeb627d 246 res_adc_temp->set_value(temp3_value);
screamer 32:2871fbeb627d 247 res_adc_voltage->set_value(volt_value);
screamer 13:42b49a0caade 248 #endif /* SEND_ALL_SENSORS */
screamer 28:0e774865873d 249 }
screamer 10:b27c962b3c3f 250 }
screamer 10:b27c962b3c3f 251
chris 35:061bc344e4ec 252
adustm 4:cf7342047b4d 253 int main(void) {
screamer 17:fc98adcf835a 254 printf("\nStarting Simple Pelion Device Management Client example\n");
chris 35:061bc344e4ec 255 printf("\nSSID : %s, PWD : %s\n",WIFI_SSID, WIFI_PASSWORD);
adustm 4:cf7342047b4d 256
screamer 29:6ff737b67e7d 257 int storage_status = fs.mount(&sd);
screamer 29:6ff737b67e7d 258 if (storage_status != 0) {
screamer 29:6ff737b67e7d 259 printf("Storage mounting failed.\n");
screamer 29:6ff737b67e7d 260 }
screamer 30:15743b79c6cb 261 // If the User button is pressed ons start, then format storage.
screamer 29:6ff737b67e7d 262 bool btn_pressed = (button.read() == MBED_CONF_APP_BUTTON_PRESSED_STATE);
screamer 29:6ff737b67e7d 263 if (btn_pressed) {
screamer 29:6ff737b67e7d 264 printf("User button is pushed on start...\n");
screamer 29:6ff737b67e7d 265 }
screamer 30:15743b79c6cb 266
screamer 29:6ff737b67e7d 267 if (storage_status || btn_pressed) {
screamer 29:6ff737b67e7d 268 printf("Formatting the storage...\n");
screamer 30:15743b79c6cb 269 int storage_status = StorageHelper::format(&fs, &sd);
screamer 10:b27c962b3c3f 270 if (storage_status != 0) {
screamer 13:42b49a0caade 271 printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
screamer 10:b27c962b3c3f 272 }
screamer 28:0e774865873d 273 } else {
screamer 28:0e774865873d 274 printf("You can hold the user button during boot to format the storage and change the device identity.\n");
screamer 10:b27c962b3c3f 275 }
screamer 10:b27c962b3c3f 276
screamer 10:b27c962b3c3f 277 sensors_init();
screamer 10:b27c962b3c3f 278
adustm 4:cf7342047b4d 279 // Connect to the internet (DHCP is expected to be on)
screamer 13:42b49a0caade 280 printf("Connecting to the network using Wifi...\n");
chris 35:061bc344e4ec 281 // net = NetworkInterface::get_default_instance();
chris 35:061bc344e4ec 282 net = WiFiInterface::get_default_instance();
adustm 4:cf7342047b4d 283
screamer 10:b27c962b3c3f 284 nsapi_error_t net_status = -1;
chris 35:061bc344e4ec 285
screamer 10:b27c962b3c3f 286 for (int tries = 0; tries < 3; tries++) {
chris 35:061bc344e4ec 287
chris 35:061bc344e4ec 288 //net_status = net->connect();
chris 35:061bc344e4ec 289 net_status = net->connect(WIFI_SSID, WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
chris 35:061bc344e4ec 290
screamer 10:b27c962b3c3f 291 if (net_status == NSAPI_ERROR_OK) {
screamer 10:b27c962b3c3f 292 break;
screamer 10:b27c962b3c3f 293 } else {
screamer 13:42b49a0caade 294 printf("Unable to connect to network. Retrying...\n");
screamer 10:b27c962b3c3f 295 }
screamer 10:b27c962b3c3f 296 }
MarceloSalazar 9:265744785d33 297
screamer 10:b27c962b3c3f 298 if (net_status != NSAPI_ERROR_OK) {
screamer 13:42b49a0caade 299 printf("ERROR: Connecting to the network failed (%d)!\n", net_status);
adustm 1:e86b1cffc402 300 return -1;
adustm 1:e86b1cffc402 301 }
adustm 1:e86b1cffc402 302
MarceloSalazar 9:265744785d33 303 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
adustm 1:e86b1cffc402 304
screamer 17:fc98adcf835a 305 printf("Initializing Pelion Device Management Client...\n");
screamer 17:fc98adcf835a 306
MarceloSalazar 9:265744785d33 307 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM
MarceloSalazar 9:265744785d33 308 SimpleMbedCloudClient client(net, bd, &fs);
adustm 4:cf7342047b4d 309 int client_status = client.init();
adustm 4:cf7342047b4d 310 if (client_status != 0) {
screamer 13:42b49a0caade 311 printf("ERROR: Pelion Client initialization failed (%d)\n", client_status);
adustm 1:e86b1cffc402 312 return -1;
adustm 1:e86b1cffc402 313 }
adustm 1:e86b1cffc402 314
adustm 4:cf7342047b4d 315 // Creating resources, which can be written or read from the cloud
screamer 32:2871fbeb627d 316 res_button = client.create_resource("3200/0/5501", "Button Count");
screamer 12:1f1a50e973db 317 res_button->set_value(0);
screamer 12:1f1a50e973db 318 res_button->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 319 res_button->observable(true);
screamer 12:1f1a50e973db 320 res_button->attach_notification_callback(button_callback);
adustm 1:e86b1cffc402 321
screamer 33:cfd9430e7d1e 322 res_led = client.create_resource("3201/0/5853", "LED State");
screamer 33:cfd9430e7d1e 323 res_led->set_value(1);
screamer 33:cfd9430e7d1e 324 res_led->methods(M2MMethod::GET | M2MMethod::PUT);
screamer 33:cfd9430e7d1e 325 res_led->attach_put_callback(put_callback);
screamer 33:cfd9430e7d1e 326
screamer 33:cfd9430e7d1e 327 #ifdef SEND_ALL_SENSORS
screamer 10:b27c962b3c3f 328 // Sensor resources
screamer 32:2871fbeb627d 329 res_temperature = client.create_resource("3303/0/5700", "Temperature HTS221 (C)");
screamer 12:1f1a50e973db 330 res_temperature->set_value(0);
screamer 12:1f1a50e973db 331 res_temperature->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 332 res_temperature->observable(true);
screamer 10:b27c962b3c3f 333
screamer 32:2871fbeb627d 334 res_humidity = client.create_resource("3304/0/5700", "Humidity");
screamer 12:1f1a50e973db 335 res_humidity->set_value(0);
screamer 12:1f1a50e973db 336 res_humidity->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 337 res_humidity->observable(true);
screamer 10:b27c962b3c3f 338
screamer 32:2871fbeb627d 339 res_temperature2 = client.create_resource("3303/1/5700", "Temperature LPS22HB (C)");
screamer 13:42b49a0caade 340 res_temperature2->set_value(0);
screamer 13:42b49a0caade 341 res_temperature2->methods(M2MMethod::GET);
screamer 13:42b49a0caade 342 res_temperature2->observable(true);
screamer 13:42b49a0caade 343
screamer 32:2871fbeb627d 344 res_adc_temp = client.create_resource("3303/2/5700", "Temperature ADC (C)");
screamer 28:0e774865873d 345 res_adc_temp->set_value(0);
screamer 28:0e774865873d 346 res_adc_temp->methods(M2MMethod::GET);
screamer 28:0e774865873d 347 res_adc_temp->observable(true);
screamer 28:0e774865873d 348
screamer 32:2871fbeb627d 349 res_accelerometer_x = client.create_resource("3313/0/5702", "Accelerometer X");
screamer 28:0e774865873d 350 res_accelerometer_x->set_value(0);
screamer 28:0e774865873d 351 res_accelerometer_x->methods(M2MMethod::GET);
screamer 28:0e774865873d 352 res_accelerometer_x->observable(true);
screamer 28:0e774865873d 353
screamer 32:2871fbeb627d 354 res_accelerometer_y = client.create_resource("3313/0/5703", "Accelerometer Y");
screamer 28:0e774865873d 355 res_accelerometer_y->set_value(0);
screamer 28:0e774865873d 356 res_accelerometer_y->methods(M2MMethod::GET);
screamer 28:0e774865873d 357 res_accelerometer_y->observable(true);
screamer 28:0e774865873d 358
screamer 32:2871fbeb627d 359 res_accelerometer_z = client.create_resource("3313/0/5704", "Accelerometer Z");
screamer 28:0e774865873d 360 res_accelerometer_z->set_value(0);
screamer 28:0e774865873d 361 res_accelerometer_z->methods(M2MMethod::GET);
screamer 28:0e774865873d 362 res_accelerometer_z->observable(true);
screamer 28:0e774865873d 363
screamer 32:2871fbeb627d 364 res_magnometer_x = client.create_resource("3314/0/5702", "Magnometer X");
screamer 28:0e774865873d 365 res_magnometer_x->set_value(0);
screamer 28:0e774865873d 366 res_magnometer_x->methods(M2MMethod::GET);
screamer 28:0e774865873d 367 res_magnometer_x->observable(true);
screamer 28:0e774865873d 368
screamer 32:2871fbeb627d 369 res_magnometer_y = client.create_resource("3314/0/5703", "Magnometer Y");
screamer 28:0e774865873d 370 res_magnometer_y->set_value(0);
screamer 28:0e774865873d 371 res_magnometer_y->methods(M2MMethod::GET);
screamer 28:0e774865873d 372 res_magnometer_y->observable(true);
screamer 28:0e774865873d 373
screamer 32:2871fbeb627d 374 res_magnometer_z = client.create_resource("3314/0/5704", "Magnometer Z");
screamer 28:0e774865873d 375 res_magnometer_z->set_value(0);
screamer 28:0e774865873d 376 res_magnometer_z->methods(M2MMethod::GET);
screamer 28:0e774865873d 377 res_magnometer_z->observable(true);
screamer 28:0e774865873d 378
screamer 32:2871fbeb627d 379 res_gyroscope_x = client.create_resource("3334/0/5702", "Gyroscope X");
screamer 13:42b49a0caade 380 res_gyroscope_x->set_value(0);
screamer 13:42b49a0caade 381 res_gyroscope_x->methods(M2MMethod::GET);
screamer 13:42b49a0caade 382 res_gyroscope_x->observable(true);
screamer 10:b27c962b3c3f 383
screamer 32:2871fbeb627d 384 res_gyroscope_y = client.create_resource("3334/0/5703", "Gyroscope Y");
screamer 13:42b49a0caade 385 res_gyroscope_y->set_value(0);
screamer 13:42b49a0caade 386 res_gyroscope_y->methods(M2MMethod::GET);
screamer 13:42b49a0caade 387 res_gyroscope_y->observable(true);
screamer 13:42b49a0caade 388
screamer 32:2871fbeb627d 389 res_gyroscope_z = client.create_resource("3334/0/5704", "Gyroscope Z");
screamer 13:42b49a0caade 390 res_gyroscope_z->set_value(0);
screamer 13:42b49a0caade 391 res_gyroscope_z->methods(M2MMethod::GET);
screamer 13:42b49a0caade 392 res_gyroscope_z->observable(true);
screamer 13:42b49a0caade 393
screamer 32:2871fbeb627d 394 res_adc_voltage = client.create_resource("3316/0/5700", "Voltage");
screamer 28:0e774865873d 395 res_adc_voltage->set_value(0);
screamer 28:0e774865873d 396 res_adc_voltage->methods(M2MMethod::GET);
screamer 28:0e774865873d 397 res_adc_voltage->observable(true);
screamer 28:0e774865873d 398
screamer 32:2871fbeb627d 399 res_pressure = client.create_resource("3323/0/5700", "Pressure");
screamer 28:0e774865873d 400 res_pressure->set_value(0);
screamer 28:0e774865873d 401 res_pressure->methods(M2MMethod::GET);
screamer 28:0e774865873d 402 res_pressure->observable(true);
screamer 28:0e774865873d 403
screamer 32:2871fbeb627d 404 res_distance = client.create_resource("3330/0/5700", "Distance");
screamer 28:0e774865873d 405 res_distance->set_value((float)999.9);
screamer 13:42b49a0caade 406 res_distance->methods(M2MMethod::GET);
screamer 13:42b49a0caade 407 res_distance->observable(true);
screamer 13:42b49a0caade 408 #endif /* SEND_ALL_SENSORS */
screamer 11:8df4529f060d 409
MarceloSalazar 9:265744785d33 410 printf("Initialized Pelion Client. Registering...\n");
adustm 1:e86b1cffc402 411
adustm 4:cf7342047b4d 412 // Callback that fires when registering is complete
adustm 4:cf7342047b4d 413 client.on_registered(&registered);
adustm 1:e86b1cffc402 414
MarceloSalazar 9:265744785d33 415 // Register with Pelion DM
adustm 4:cf7342047b4d 416 client.register_and_connect();
adustm 1:e86b1cffc402 417
screamer 17:fc98adcf835a 418 int i = 600; // wait up 60 seconds before attaching sensors and button events
screamer 12:1f1a50e973db 419 while (i-- > 0 && !client.is_client_registered()) {
screamer 12:1f1a50e973db 420 wait_ms(100);
screamer 12:1f1a50e973db 421 }
screamer 12:1f1a50e973db 422
screamer 11:8df4529f060d 423 button.fall(eventQueue.event(&button_press));
screamer 10:b27c962b3c3f 424
screamer 15:a0430d40a918 425 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
adustm 4:cf7342047b4d 426 Ticker timer;
screamer 11:8df4529f060d 427 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);
adustm 1:e86b1cffc402 428
adustm 4:cf7342047b4d 429 // You can easily run the eventQueue in a separate thread if required
adustm 4:cf7342047b4d 430 eventQueue.dispatch_forever();
adustm 1:e86b1cffc402 431 }
screamer 28:0e774865873d 432
MarceloSalazar 9:265744785d33 433 #endif