Garage Door Monitor and Opener

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Introduction

This system implements a simple garage door opener and environmental monitor. The hardware connects to the internet using Wi-Fi then on to the Pelion Device Management Platform which provides device monitoring and secure firmware updates over the air (FOTA). Pelion Device Management provides a flexible set of REST APIs which we will use to communicate to a web application running on an EC-2 instance in AWS. The web application will serve a web page where we can monitor and control our garage..

This project is intended to work on the DISCO-L475VG-IOT01A from ST Microelectronics It implements a simple actuator to drive a relay to simulate pushing the "open" button on older style garage doors which do not use a rolling code interface.

The system is designed to be mounted over the door so that the on board time of flight sensor can be used to detect if the door is open or closed.

The system also monitors temperature, humidity and barometric pressure.

https://os.mbed.com/media/uploads/JimCarver/garageopener.jpg

Hardware Requirements:

DISCO-L475G-IOT01A https://os.mbed.com/platforms/ST-Discovery-L475E-IOT01A/

Seeed Studio Grove Relay module https://www.seeedstudio.com/Grove-Relay.html

Seeed Studio Grove cable, I used this one: https://www.seeedstudio.com/Grove-4-pin-Male-Jumper-to-Grove-4-pin-Conversion-Cable-5-PCs-per-Pack.html

Connect to the PMOD connector like this:

https://os.mbed.com/media/uploads/JimCarver/opener.jpg

This shows how I installed so that the time of flight sensor can detect when the door is open

https://os.mbed.com/media/uploads/JimCarver/opener1.jpg https://os.mbed.com/media/uploads/JimCarver/opener2.jpg

To use the project:

You will also need a Pelion developers account.

I suggest you first use the Pelion quick state to become familiar with Pelion Device Management. https://os.mbed.com/guides/connect-device-to-pelion/1/?board=ST-Discovery-L475E-IOT01A

Web Interface

For my web interface I am running node-red under Ubuntu in an EC2 instance on AWS. This can run for 12 month within the constraints of their free tier. Here is a tutorial: https://nodered.org/docs/getting-started/aws

You will also need to install several node-red add ons:

sudo npm install -g node-red-dashboard

sudo npm install -g node-red-contrib-mbed-cloud

sudo npm istall -g node-red-contrib-moment

After starting node-red import the contents of GarageFlow.txt from the project, pin the flow into the page.

To enable your web app to access your Pelion account you need an API key.

First you will neet to use your Pelion account to create an API key.

https://os.mbed.com/media/uploads/JimCarver/api_portal.jpg

Now we need to apply that API key to your Node-Red flow.

https://os.mbed.com/media/uploads/JimCarver/api_node-red.jpg

Committer:
screamer
Date:
Mon Dec 10 17:19:20 2018 +0000
Revision:
12:1f1a50e973db
Parent:
11:8df4529f060d
Child:
13:42b49a0caade
Update to main application

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
screamer 11:8df4529f060d 23 #define SENSORS_AND_BUTTONS
screamer 10:b27c962b3c3f 24 #ifdef SENSORS_AND_BUTTONS
screamer 10:b27c962b3c3f 25 #include "HTS221Sensor.h"
screamer 10:b27c962b3c3f 26 #include "LPS22HBSensor.h"
screamer 10:b27c962b3c3f 27 #include "LSM6DSLSensor.h"
screamer 10:b27c962b3c3f 28 #include "lis3mdl_class.h"
screamer 11:8df4529f060d 29 // #include "VL53L0X.h"
screamer 10:b27c962b3c3f 30
screamer 10:b27c962b3c3f 31 static DevI2C devI2c(PB_11,PB_10);
screamer 12:1f1a50e973db 32 static HTS221Sensor sen_hum_temp(&devI2c);
screamer 12:1f1a50e973db 33 static LPS22HBSensor sen_press_temp(&devI2c);
screamer 12:1f1a50e973db 34 static LSM6DSLSensor sen_acc_gyro(&devI2c,LSM6DSL_ACC_GYRO_I2C_ADDRESS_LOW,PD_11); // low address
screamer 12:1f1a50e973db 35 static LIS3MDL sen_mag(&devI2c);
screamer 10:b27c962b3c3f 36 static DigitalOut shutdown_pin(PC_6);
screamer 12:1f1a50e973db 37 // static VL53L0X sen_distance(&devI2c, &shutdown_pin, PC_7);
screamer 10:b27c962b3c3f 38
screamer 10:b27c962b3c3f 39 InterruptIn button(USER_BUTTON);
screamer 11:8df4529f060d 40 #define SENSORS_POLL_INTERVAL 1.0
screamer 10:b27c962b3c3f 41 #endif /* SENSORS_AND_BUTTONS */
adustm 1:e86b1cffc402 42
adustm 4:cf7342047b4d 43 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
adustm 4:cf7342047b4d 44 // 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 45 EventQueue eventQueue;
adustm 1:e86b1cffc402 46
MarceloSalazar 9:265744785d33 47 // Default network interface object
MarceloSalazar 9:265744785d33 48 NetworkInterface *net;
adustm 6:e0e1e1b93099 49
MarceloSalazar 9:265744785d33 50 // Default block device
MarceloSalazar 9:265744785d33 51 BlockDevice* bd = BlockDevice::get_default_instance();
screamer 10:b27c962b3c3f 52 SlicingBlockDevice sd(bd, 0, 2*1024*1024);
screamer 10:b27c962b3c3f 53 LittleFileSystem fs("fs", &sd);
adustm 4:cf7342047b4d 54
screamer 11:8df4529f060d 55 // Default LED to use for PUT/POST
screamer 11:8df4529f060d 56 DigitalOut led(LED1);
screamer 11:8df4529f060d 57
MarceloSalazar 9:265744785d33 58 // Declaring pointers for access to Pelion Client resources outside of main()
screamer 12:1f1a50e973db 59 MbedCloudClientResource *res_button;
screamer 12:1f1a50e973db 60 MbedCloudClientResource *res_led;
adustm 1:e86b1cffc402 61
screamer 12:1f1a50e973db 62 // Additional resources for sensor readings
screamer 12:1f1a50e973db 63 MbedCloudClientResource *res_humidity;
screamer 12:1f1a50e973db 64 MbedCloudClientResource *res_temperature;
screamer 10:b27c962b3c3f 65 #ifdef SENSORS_AND_BUTTONS
screamer 12:1f1a50e973db 66 MbedCloudClientResource *res_pressure;
screamer 12:1f1a50e973db 67 MbedCloudClientResource *res_temperature2;
screamer 12:1f1a50e973db 68 MbedCloudClientResource *res_magnometer;
screamer 12:1f1a50e973db 69 MbedCloudClientResource *res_accelerometer;
screamer 12:1f1a50e973db 70 MbedCloudClientResource *res_gyroscope;
screamer 12:1f1a50e973db 71 MbedCloudClientResource *res_distance;
screamer 10:b27c962b3c3f 72 #endif /* SENSORS_AND_BUTTONS */
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 11:8df4529f060d 100 * Button function triggered by the physical button press or by timer depending on SENSORS_AND_BUTTONS macro.
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 11:8df4529f060d 129 #ifdef SENSORS_AND_BUTTONS
screamer 10:b27c962b3c3f 130 void sensors_init() {
screamer 10:b27c962b3c3f 131 uint8_t id;
screamer 10:b27c962b3c3f 132
screamer 10:b27c962b3c3f 133 // Initialize sensors
screamer 12:1f1a50e973db 134 sen_hum_temp.init(NULL);
screamer 12:1f1a50e973db 135 sen_press_temp.init(NULL);
screamer 12:1f1a50e973db 136 sen_acc_gyro.init(NULL);
screamer 12:1f1a50e973db 137 sen_mag.init(NULL);
screamer 11:8df4529f060d 138 // range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
screamer 10:b27c962b3c3f 139
screamer 10:b27c962b3c3f 140 /// Call sensors enable routines
screamer 12:1f1a50e973db 141 sen_hum_temp.enable();
screamer 12:1f1a50e973db 142 sen_press_temp.enable();
screamer 12:1f1a50e973db 143 //sen_mag.enable();
screamer 12:1f1a50e973db 144 sen_acc_gyro.enable_x();
screamer 12:1f1a50e973db 145 sen_acc_gyro.enable_g();
screamer 10:b27c962b3c3f 146
screamer 10:b27c962b3c3f 147 printf("\033[2J\033[20A");
screamer 12:1f1a50e973db 148 printf ("\r\nSensors configuration:\r\n");
screamer 10:b27c962b3c3f 149
screamer 12:1f1a50e973db 150 sen_hum_temp.read_id(&id);
screamer 10:b27c962b3c3f 151 printf("HTS221 humidity & temperature = 0x%X\r\n", id);
screamer 12:1f1a50e973db 152 sen_press_temp.read_id(&id);
screamer 10:b27c962b3c3f 153 printf("LPS22HB pressure & temperature = 0x%X\r\n", id);
screamer 12:1f1a50e973db 154 sen_mag.read_id(&id);
screamer 10:b27c962b3c3f 155 printf("LIS3MDL magnetometer = 0x%X\r\n", id);
screamer 12:1f1a50e973db 156 sen_acc_gyro.read_id(&id);
screamer 10:b27c962b3c3f 157 printf("LSM6DSL accelerometer & gyroscope = 0x%X\r\n", id);
screamer 10:b27c962b3c3f 158
screamer 12:1f1a50e973db 159 printf("\r\n"); ;
screamer 10:b27c962b3c3f 160 }
screamer 10:b27c962b3c3f 161
screamer 10:b27c962b3c3f 162 /**
screamer 10:b27c962b3c3f 163 * Update sensors and report their values.
screamer 10:b27c962b3c3f 164 * This function is called periodically.
screamer 10:b27c962b3c3f 165 */
screamer 10:b27c962b3c3f 166 void sensors_update() {
screamer 12:1f1a50e973db 167 float fval1, fval2;
screamer 10:b27c962b3c3f 168 int32_t axes[3];
screamer 12:1f1a50e973db 169 uint32_t uval;
screamer 10:b27c962b3c3f 170
screamer 10:b27c962b3c3f 171 printf("\r\n");
screamer 10:b27c962b3c3f 172
screamer 12:1f1a50e973db 173 fval1 = fval2 = 0.0;
screamer 12:1f1a50e973db 174 sen_hum_temp.get_temperature(&fval1);
screamer 12:1f1a50e973db 175 sen_hum_temp.get_humidity(&fval2);
screamer 10:b27c962b3c3f 176 if (endpointInfo) {
screamer 12:1f1a50e973db 177 res_temperature->set_value(fval1);
screamer 12:1f1a50e973db 178 res_humidity->set_value(fval2);
screamer 10:b27c962b3c3f 179 }
screamer 12:1f1a50e973db 180 printf("HTS221 temp: %7.2f C, humidity: %7.2f %%\r\n", fval1, fval2);
screamer 10:b27c962b3c3f 181
screamer 12:1f1a50e973db 182 fval1 = fval2 = 0.0;
screamer 12:1f1a50e973db 183 sen_press_temp.get_temperature(&fval1);
screamer 12:1f1a50e973db 184 sen_press_temp.get_pressure(&fval2);
screamer 12:1f1a50e973db 185 printf("LPS22HB temp: %7.2f C, pressure: %7.2f mbar \r\n", fval1, fval2);
screamer 10:b27c962b3c3f 186
screamer 12:1f1a50e973db 187 sen_mag.get_m_axes(axes);
screamer 12:1f1a50e973db 188 printf("LIS3MDL mag: %7ld x, %7ld y, %7ld z [mgauss] \r\n", axes[0], axes[1], axes[2]);
screamer 12:1f1a50e973db 189 sen_acc_gyro.get_x_axes(axes);
screamer 12:1f1a50e973db 190 printf("LSM6DSL acc: %7ld x, %7ld y, %7ld z [mg] \r\n", axes[0], axes[1], axes[2]);
screamer 12:1f1a50e973db 191 sen_acc_gyro.get_g_axes(axes);
screamer 12:1f1a50e973db 192 printf("LSM6DSL gyro: %7ld x, %7ld y, %7ld z [mdps] \r\n", axes[0], axes[1], axes[2]);
screamer 10:b27c962b3c3f 193
screamer 12:1f1a50e973db 194 // if (range.get_distance(&uval) == VL53L0X_ERROR_NONE) {
screamer 12:1f1a50e973db 195 // printf("VL53L0X [mm]: %6ld\r\n", uval);
screamer 11:8df4529f060d 196 // if (endpointInfo) {
screamer 12:1f1a50e973db 197 // res_distance->set_value((int)uval);
screamer 11:8df4529f060d 198 // }
screamer 11:8df4529f060d 199 // } else {
screamer 11:8df4529f060d 200 // printf("VL53L0X [mm]: --\r\n");
screamer 11:8df4529f060d 201 // }
screamer 10:b27c962b3c3f 202
screamer 12:1f1a50e973db 203 printf("\033[6A");
screamer 10:b27c962b3c3f 204 }
screamer 10:b27c962b3c3f 205 #endif /* SENSORS_AND_BUTTONS */
screamer 10:b27c962b3c3f 206
screamer 10:b27c962b3c3f 207
adustm 4:cf7342047b4d 208 int main(void) {
MarceloSalazar 9:265744785d33 209 printf("Starting Simple Pelion Device Management Client example\n");
adustm 4:cf7342047b4d 210 printf("Connecting to the network using Wifi...\n");
adustm 4:cf7342047b4d 211
screamer 10:b27c962b3c3f 212 // If the User button is pressed, then format storage.
screamer 10:b27c962b3c3f 213 const int PRESSED = 0;
screamer 10:b27c962b3c3f 214 DigitalIn *user_button = new DigitalIn(USER_BUTTON);
screamer 10:b27c962b3c3f 215 if (user_button->read() == PRESSED) {
screamer 10:b27c962b3c3f 216 printf("User button is pushed on start. Formatting the storage...\n");
screamer 10:b27c962b3c3f 217 int storage_status = fs.reformat(&sd);
screamer 10:b27c962b3c3f 218 if (storage_status != 0) {
screamer 10:b27c962b3c3f 219 if (sd.erase(0, sd.size()) == 0) {
screamer 10:b27c962b3c3f 220 if (fs.format(&sd) == 0) {
screamer 10:b27c962b3c3f 221 storage_status = 0;
screamer 10:b27c962b3c3f 222 printf("The storage reformatted successfully.\n");
screamer 10:b27c962b3c3f 223 }
screamer 10:b27c962b3c3f 224 }
screamer 10:b27c962b3c3f 225 }
screamer 10:b27c962b3c3f 226 if (storage_status != 0) {
screamer 10:b27c962b3c3f 227 printf("Failed to reformat the storage.\n");
screamer 10:b27c962b3c3f 228 }
screamer 10:b27c962b3c3f 229 }
screamer 10:b27c962b3c3f 230
screamer 10:b27c962b3c3f 231 #ifdef SENSORS_AND_BUTTONS
screamer 10:b27c962b3c3f 232 sensors_init();
screamer 10:b27c962b3c3f 233 #endif /* SENSORS_AND_BUTTONS */
screamer 10:b27c962b3c3f 234
adustm 4:cf7342047b4d 235 // Connect to the internet (DHCP is expected to be on)
MarceloSalazar 9:265744785d33 236 net = NetworkInterface::get_default_instance();
adustm 4:cf7342047b4d 237
screamer 10:b27c962b3c3f 238 nsapi_error_t net_status = -1;
screamer 10:b27c962b3c3f 239 for (int tries = 0; tries < 3; tries++) {
screamer 10:b27c962b3c3f 240 net_status = net->connect();
screamer 10:b27c962b3c3f 241 if (net_status == NSAPI_ERROR_OK) {
screamer 10:b27c962b3c3f 242 break;
screamer 10:b27c962b3c3f 243 } else {
screamer 10:b27c962b3c3f 244 printf("[WARN] Unable to connect to network. Retrying...\n");
screamer 10:b27c962b3c3f 245 }
screamer 10:b27c962b3c3f 246 }
MarceloSalazar 9:265744785d33 247
screamer 10:b27c962b3c3f 248 if (net_status != NSAPI_ERROR_OK) {
screamer 10:b27c962b3c3f 249 printf("Connecting to the network failed %d!\n", net_status);
adustm 1:e86b1cffc402 250 return -1;
adustm 1:e86b1cffc402 251 }
adustm 1:e86b1cffc402 252
MarceloSalazar 9:265744785d33 253 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
adustm 1:e86b1cffc402 254
MarceloSalazar 9:265744785d33 255 // SimpleMbedCloudClient handles registering over LwM2M to Pelion DM
MarceloSalazar 9:265744785d33 256 SimpleMbedCloudClient client(net, bd, &fs);
adustm 4:cf7342047b4d 257 int client_status = client.init();
adustm 4:cf7342047b4d 258 if (client_status != 0) {
MarceloSalazar 9:265744785d33 259 printf("Pelion Client initialization failed (%d)\n", client_status);
adustm 1:e86b1cffc402 260 return -1;
adustm 1:e86b1cffc402 261 }
adustm 1:e86b1cffc402 262
adustm 4:cf7342047b4d 263 // Creating resources, which can be written or read from the cloud
screamer 12:1f1a50e973db 264 res_button = client.create_resource("3200/0/5501", "button_count");
screamer 12:1f1a50e973db 265 res_button->set_value(0);
screamer 12:1f1a50e973db 266 res_button->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 267 res_button->observable(true);
screamer 12:1f1a50e973db 268 res_button->attach_notification_callback(button_callback);
adustm 1:e86b1cffc402 269
screamer 10:b27c962b3c3f 270 #ifdef SENSORS_AND_BUTTONS
screamer 10:b27c962b3c3f 271 // Sensor resources
screamer 12:1f1a50e973db 272 res_temperature = client.create_resource("3303/0/5700", "temperature");
screamer 12:1f1a50e973db 273 res_temperature->set_value(0);
screamer 12:1f1a50e973db 274 res_temperature->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 275 res_temperature->observable(true);
screamer 10:b27c962b3c3f 276
screamer 12:1f1a50e973db 277 res_humidity = client.create_resource("3304/0/5700", "humidity");
screamer 12:1f1a50e973db 278 res_humidity->set_value(0);
screamer 12:1f1a50e973db 279 res_humidity->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 280 res_humidity->observable(true);
screamer 10:b27c962b3c3f 281
screamer 12:1f1a50e973db 282 // res_distance = client.create_resource("3330/0/5700", "distance");
screamer 12:1f1a50e973db 283 // res_distance->set_value(0);
screamer 12:1f1a50e973db 284 // res_distance->methods(M2MMethod::GET);
screamer 12:1f1a50e973db 285 // res_distance->observable(true);
screamer 10:b27c962b3c3f 286 #endif /* SENSORS_AND_BUTTONS */
screamer 10:b27c962b3c3f 287
screamer 12:1f1a50e973db 288 // res_led = client.create_resource("3201/0/5853", "led_state");
screamer 12:1f1a50e973db 289 // res_led->set_value(1);
screamer 12:1f1a50e973db 290 // res_led->methods(M2MMethod::GET | M2MMethod::PUT);
screamer 12:1f1a50e973db 291 // res_led->attach_put_callback(led_put_callback);
screamer 11:8df4529f060d 292
MarceloSalazar 9:265744785d33 293 printf("Initialized Pelion Client. Registering...\n");
adustm 1:e86b1cffc402 294
adustm 4:cf7342047b4d 295 // Callback that fires when registering is complete
adustm 4:cf7342047b4d 296 client.on_registered(&registered);
adustm 1:e86b1cffc402 297
MarceloSalazar 9:265744785d33 298 // Register with Pelion DM
adustm 4:cf7342047b4d 299 client.register_and_connect();
adustm 1:e86b1cffc402 300
screamer 12:1f1a50e973db 301 int i = 600; // wait 60 seconds
screamer 12:1f1a50e973db 302 while (i-- > 0 && !client.is_client_registered()) {
screamer 12:1f1a50e973db 303 wait_ms(100);
screamer 12:1f1a50e973db 304 }
screamer 12:1f1a50e973db 305
adustm 1:e86b1cffc402 306 // Placeholder for callback to update local resource when GET comes.
adustm 4:cf7342047b4d 307 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
screamer 10:b27c962b3c3f 308 #ifdef SENSORS_AND_BUTTONS
screamer 11:8df4529f060d 309 button.fall(eventQueue.event(&button_press));
screamer 10:b27c962b3c3f 310
adustm 4:cf7342047b4d 311 Ticker timer;
screamer 11:8df4529f060d 312 timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);
screamer 10:b27c962b3c3f 313 #else /* SENSORS_AND_BUTTONS */
screamer 10:b27c962b3c3f 314 Ticker timer;
screamer 10:b27c962b3c3f 315 timer.attach(eventQueue.event(&button_press), 5.0);
screamer 10:b27c962b3c3f 316 #endif /* SENSORS_AND_BUTTONS */
adustm 1:e86b1cffc402 317
adustm 4:cf7342047b4d 318 // You can easily run the eventQueue in a separate thread if required
adustm 4:cf7342047b4d 319 eventQueue.dispatch_forever();
adustm 1:e86b1cffc402 320 }
MarceloSalazar 9:265744785d33 321 #endif