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

Files at this revision

API Documentation at this revision

Comitter:
screamer
Date:
Mon Dec 10 18:44:01 2018 +0000
Parent:
12:1f1a50e973db
Child:
14:2f3456616212
Commit message:
Add more sensors to main app, but disabled by default due to secure connect issues

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Dec 10 17:19:20 2018 +0000
+++ b/main.cpp	Mon Dec 10 18:44:01 2018 +0000
@@ -19,9 +19,6 @@
 #include "mbed.h"
 #include "simple-mbed-cloud-client.h"
 #include "LittleFileSystem.h"
-
-#define SENSORS_AND_BUTTONS
-#ifdef SENSORS_AND_BUTTONS
 #include "HTS221Sensor.h"
 #include "LPS22HBSensor.h"
 #include "LSM6DSLSensor.h"
@@ -37,8 +34,9 @@
 // static VL53L0X sen_distance(&devI2c, &shutdown_pin, PC_7);
 
 InterruptIn button(USER_BUTTON);
+
 #define SENSORS_POLL_INTERVAL 1.0
-#endif /* SENSORS_AND_BUTTONS */
+//#define SEND_ALL_SENSORS
 
 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
 // 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
@@ -62,14 +60,16 @@
 // Additional resources for sensor readings
 MbedCloudClientResource *res_humidity;
 MbedCloudClientResource *res_temperature;
-#ifdef SENSORS_AND_BUTTONS
+#ifdef SEND_ALL_SENSORS
 MbedCloudClientResource *res_pressure;
 MbedCloudClientResource *res_temperature2;
 MbedCloudClientResource *res_magnometer;
 MbedCloudClientResource *res_accelerometer;
-MbedCloudClientResource *res_gyroscope;
+MbedCloudClientResource *res_gyroscope_x;
+MbedCloudClientResource *res_gyroscope_y;
+MbedCloudClientResource *res_gyroscope_z;
 MbedCloudClientResource *res_distance;
-#endif /* SENSORS_AND_BUTTONS */
+#endif /* SEND_ALL_SENSORS */
 
 // When the device is registered, this variable will be used to access various useful information, like device ID etc.
 static const ConnectorClientEndpointInfo* endpointInfo;
@@ -97,7 +97,7 @@
 }
 
 /**
- * Button function triggered by the physical button press or by timer depending on SENSORS_AND_BUTTONS macro.
+ * Button function triggered by the physical button press.
  */
 void button_press() {
     int v = res_button->get_value_int() + 1;
@@ -126,7 +126,6 @@
 /**
  * Initialize sensors
  */
-#ifdef SENSORS_AND_BUTTONS
 void sensors_init() {
     uint8_t id;
 
@@ -135,7 +134,7 @@
     sen_press_temp.init(NULL);
     sen_acc_gyro.init(NULL);
     sen_mag.init(NULL);
-    // range.init_sensor(VL53L0X_DEFAULT_ADDRESS);
+    // sen_distance.init_sensor(VL53L0X_DEFAULT_ADDRESS);
 
     /// Call sensors enable routines
     sen_hum_temp.enable();
@@ -171,30 +170,45 @@
     printf("\r\n");
 
     fval1 = fval2 = 0.0;
-    sen_hum_temp.get_temperature(&fval1);
-    sen_hum_temp.get_humidity(&fval2);
+    sen_hum_temp.get_humidity(&fval1);
+    sen_hum_temp.get_temperature(&fval2);
+    printf("HTS221 temp:  %7.2f C,  humidity: %7.2f %%\r\n", fval1, fval2);
     if (endpointInfo) {
-        res_temperature->set_value(fval1);
-        res_humidity->set_value(fval2);
+        res_humidity->set_value(fval1);
+        res_temperature->set_value(fval2);
     }
-    printf("HTS221 temp:  %7.2f C,  humidity: %7.2f %%\r\n", fval1, fval2);
 
     fval1 = fval2 = 0.0;
-    sen_press_temp.get_temperature(&fval1);
-    sen_press_temp.get_pressure(&fval2);
+    sen_press_temp.get_pressure(&fval1);
+    sen_press_temp.get_temperature(&fval2);
     printf("LPS22HB temp: %7.2f C,  pressure: %7.2f mbar      \r\n", fval1, fval2);
+#ifdef SEND_ALL_SENSORS
+    if (endpointInfo) {
+        res_pressure->set_value(fval1);
+        res_temperature2->set_value(fval2);
+    }
+#endif /* SEND_ALL_SENSORS */
 
     sen_mag.get_m_axes(axes);
     printf("LIS3MDL mag:  %7ld x, %7ld y, %7ld z [mgauss]      \r\n", axes[0], axes[1], axes[2]);
+
     sen_acc_gyro.get_x_axes(axes);
     printf("LSM6DSL acc:  %7ld x, %7ld y, %7ld z [mg]       \r\n", axes[0], axes[1], axes[2]);
+
     sen_acc_gyro.get_g_axes(axes);
     printf("LSM6DSL gyro: %7ld x, %7ld y, %7ld z [mdps]      \r\n", axes[0], axes[1], axes[2]);
+#ifdef SEND_ALL_SENSORS
+    if (endpointInfo) {
+        res_gyroscope_x->set_value((float)axes[0]);
+        res_gyroscope_y->set_value((float)axes[1]);
+        res_gyroscope_z->set_value((float)axes[2]);
+    }
+#endif /* SEND_ALL_SENSORS */
 
-    // if (range.get_distance(&uval) == VL53L0X_ERROR_NONE) {
+    // if (sen_distance.get_distance(&uval) == VL53L0X_ERROR_NONE) {
     //     printf("VL53L0X [mm]:            %6ld\r\n", uval);
     //     if (endpointInfo) {
-    //         res_distance->set_value((int)uval);
+    //         res_distance->set_value((float)uval);
     //     }
     // } else {
     //     printf("VL53L0X [mm]:                --\r\n");
@@ -202,12 +216,9 @@
 
     printf("\033[6A");
 }
-#endif /* SENSORS_AND_BUTTONS */
-
 
 int main(void) {
     printf("Starting Simple Pelion Device Management Client example\n");
-    printf("Connecting to the network using Wifi...\n");
 
     // If the User button is pressed, then format storage.
     const int PRESSED = 0;
@@ -224,15 +235,14 @@
             }
         }
         if (storage_status != 0) {
-            printf("Failed to reformat the storage.\n");
+            printf("ERROR: Failed to reformat the storage (%d).\n", storage_status);
         }
     }
 
-#ifdef SENSORS_AND_BUTTONS
     sensors_init();
-#endif /* SENSORS_AND_BUTTONS */
 
     // Connect to the internet (DHCP is expected to be on)
+    printf("Connecting to the network using Wifi...\n");
     net = NetworkInterface::get_default_instance();
 
     nsapi_error_t net_status = -1;
@@ -241,12 +251,12 @@
         if (net_status == NSAPI_ERROR_OK) {
             break;
         } else {
-            printf("[WARN] Unable to connect to network. Retrying...\n");
+            printf("Unable to connect to network. Retrying...\n");
         }
     }
 
     if (net_status != NSAPI_ERROR_OK) {
-        printf("Connecting to the network failed %d!\n", net_status);
+        printf("ERROR: Connecting to the network failed (%d)!\n", net_status);
         return -1;
     }
 
@@ -256,7 +266,7 @@
     SimpleMbedCloudClient client(net, bd, &fs);
     int client_status = client.init();
     if (client_status != 0) {
-        printf("Pelion Client initialization failed (%d)\n", client_status);
+        printf("ERROR: Pelion Client initialization failed (%d)\n", client_status);
         return -1;
     }
 
@@ -267,7 +277,6 @@
     res_button->observable(true);
     res_button->attach_notification_callback(button_callback);
 
-#ifdef SENSORS_AND_BUTTONS
     // Sensor resources
     res_temperature = client.create_resource("3303/0/5700", "temperature");
     res_temperature->set_value(0);
@@ -279,16 +288,42 @@
     res_humidity->methods(M2MMethod::GET);
     res_humidity->observable(true);
 
-    // res_distance = client.create_resource("3330/0/5700", "distance");
-    // res_distance->set_value(0);
-    // res_distance->methods(M2MMethod::GET);
-    // res_distance->observable(true);
-#endif /* SENSORS_AND_BUTTONS */
+#ifdef SEND_ALL_SENSORS
+    res_pressure = client.create_resource("3323/1/5700", "pressure");
+    res_pressure->set_value(0);
+    res_pressure->methods(M2MMethod::GET);
+    res_pressure->observable(true);
+
+    res_temperature2 = client.create_resource("3303/1/5700", "temperature");
+    res_temperature2->set_value(0);
+    res_temperature2->methods(M2MMethod::GET);
+    res_temperature2->observable(true);
+
+    res_gyroscope_x = client.create_resource("3334/1/5702", "gyroscope_x");
+    res_gyroscope_x->set_value(0);
+    res_gyroscope_x->methods(M2MMethod::GET);
+    res_gyroscope_x->observable(true);
 
-    // res_led = client.create_resource("3201/0/5853", "led_state");
-    // res_led->set_value(1);
-    // res_led->methods(M2MMethod::GET | M2MMethod::PUT);
-    // res_led->attach_put_callback(led_put_callback);
+    res_gyroscope_y = client.create_resource("3334/1/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/1/5704", "gyroscope_z");
+    res_gyroscope_z->set_value(0);
+    res_gyroscope_z->methods(M2MMethod::GET);
+    res_gyroscope_z->observable(true);
+
+    res_distance = client.create_resource("3330/0/5700", "distance");
+    res_distance->set_value(0);
+    res_distance->methods(M2MMethod::GET);
+    res_distance->observable(true);
+
+    res_led = client.create_resource("3201/0/5853", "led_state");
+    res_led->set_value(1);
+    res_led->methods(M2MMethod::GET | M2MMethod::PUT);
+    res_led->attach_put_callback(led_put_callback);
+#endif /* SEND_ALL_SENSORS */
 
     printf("Initialized Pelion Client. Registering...\n");
 
@@ -303,17 +338,11 @@
         wait_ms(100);
     }
 
-    // Placeholder for callback to update local resource when GET comes.
-    // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
-#ifdef SENSORS_AND_BUTTONS
     button.fall(eventQueue.event(&button_press));
 
     Ticker timer;
+    // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
     timer.attach(eventQueue.event(&sensors_update), SENSORS_POLL_INTERVAL);
-#else /* SENSORS_AND_BUTTONS */
-    Ticker timer;
-    timer.attach(eventQueue.event(&button_press), 5.0);
-#endif /* SENSORS_AND_BUTTONS */
 
     // You can easily run the eventQueue in a separate thread if required
     eventQueue.dispatch_forever();