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

Revision:
32:2871fbeb627d
Parent:
30:15743b79c6cb
Child:
33:cfd9430e7d1e
--- a/main.cpp	Mon Mar 11 11:28:07 2019 +0000
+++ b/main.cpp	Tue Mar 26 15:56:37 2019 +0000
@@ -35,7 +35,7 @@
 static DigitalOut shutdown_pin(PC_6);
 static VL53L0X sen_distance(&devI2c, &shutdown_pin, PC_7);
 
-#define SENSORS_POLL_INTERVAL 1.0
+#define SENSORS_POLL_INTERVAL 3.0
 #define SEND_ALL_SENSORS
 
 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
@@ -94,7 +94,7 @@
  * @param resource The resource that triggered the callback
  * @param newValue Updated value for the resource
  */
-void led_put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
+void put_callback(MbedCloudClientResource *resource, m2m::String newValue) {
     printf("*** PUT received, new value: %s                             \n", newValue.c_str());
     led = atoi(newValue.c_str());
 }
@@ -106,9 +106,12 @@
  *               Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
  * @param size Size of the body
  */
-void led_post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
-    printf("*** POST received. Payload: %s                              \n", res_led->get_value().c_str());
-    led = atoi(res_led->get_value().c_str());
+void post_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
+    printf("*** POST received (length %u). Payload: ", size);
+    for (size_t ix = 0; ix < size; ix++) {
+        printf("%02x ", buffer[ix]);
+    }
+    printf("\n");
 }
 
 /**
@@ -176,55 +179,59 @@
  * This function is called periodically.
  */
 void sensors_update() {
-    float t1_val, t2_val, t3_val, h_val, p_val, v_val = 0.0;
+    float temp1_value, temp2_value, temp3_value, humid_value, pressure_value, volt_value = 0.0;
     int32_t m_axes[3], a_axes[3], g_axes[3];
-    uint32_t d_val, vl_res;
+    uint32_t distance_value, distance_reading;
 
-    printf("                                                             \n");
-
-    sen_hum_temp.get_humidity(&h_val);
-    sen_hum_temp.get_temperature(&t1_val);
-    sen_press_temp.get_pressure(&p_val);
-    sen_press_temp.get_temperature(&t2_val);
+    sen_hum_temp.get_humidity(&humid_value);
+    sen_hum_temp.get_temperature(&temp1_value);
+    sen_press_temp.get_pressure(&pressure_value);
+    sen_press_temp.get_temperature(&temp2_value);
     sen_mag.get_m_axes(m_axes);
     sen_acc_gyro.get_x_axes(a_axes);
     sen_acc_gyro.get_g_axes(g_axes);
-    vl_res = sen_distance.get_distance(&d_val);
-    t3_val = adc_temp.read()*100;
-    v_val = adc_vref.read();
+    distance_reading = sen_distance.get_distance(&distance_value);
+    temp3_value = adc_temp.read()*100;
+    volt_value = adc_vref.read();
+
+    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;
+    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;
+    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;
 
-    printf("ADC temp:     %5.4f C,  vref:      %5.4f V         \n", t3_val, v_val);
-    printf("HTS221 temp:  %7.2f C,  humidity: %7.2f %%         \n", t1_val, h_val);
-    printf("LPS22HB temp: %7.2f C,  pressure: %7.2f mbar       \n", t2_val, p_val);
-    printf("LIS3MDL mag:  %7ld x, %7ld y, %7ld z [mgauss]      \n", m_axes[0], m_axes[1], m_axes[2]);
-    printf("LSM6DSL acc:  %7ld x, %7ld y, %7ld z [mg]          \n", a_axes[0], a_axes[1], a_axes[2]);
-    printf("LSM6DSL gyro: %7ld x, %7ld y, %7ld z [mdps]        \n", g_axes[0], g_axes[1], g_axes[2]);
-    if (vl_res == VL53L0X_ERROR_NONE) {
-        printf("VL53L0X dist: %7ld [mm]\n", d_val);
+    printf("                                                             \n");
+    printf("ADC temp:     %5.4f C,  vref:      %5.4f V         \n", temp3_value, volt_value);
+    printf("HTS221 temp:  %7.3f C,  humidity: %7.2f %%         \n", temp1_value, humid_value);
+    printf("LPS22HB temp: %7.3f C,  pressure: %7.2f mbar       \n", temp2_value, pressure_value);
+    printf("LIS3MDL mag:  %7.3f x, %7.3f y, %7.3f z [gauss]      \n", mag_x, mag_y, mag_z);
+    printf("LSM6DSL acc:  %7.3f x, %7.3f y, %7.3f z [g]          \n", acc_x, acc_y, acc_z);
+    printf("LSM6DSL gyro: %7.3f x, %7.3f y, %7.3f z [dps]        \n", gyro_x, gyro_y, gyro_z);
+    if (distance_reading == VL53L0X_ERROR_NONE) {
+        printf("VL53L0X dist: %7ld mm\n", distance_value);
     } else {
         printf("VL53L0X dist:        --       \n");
+        distance_value = 999;
     }
 
     printf("\r\033[8A");
 
     if (endpointInfo) {
-        res_humidity->set_value(h_val);
-        res_temperature->set_value(t1_val);
+        res_humidity->set_value(humid_value);
+        res_temperature->set_value(temp1_value);
 #ifdef SEND_ALL_SENSORS
-        res_pressure->set_value(p_val);
-        res_temperature2->set_value(t2_val);
-        res_magnometer_x->set_value((float)m_axes[0]);
-        res_magnometer_y->set_value((float)m_axes[1]);
-        res_magnometer_z->set_value((float)m_axes[2]);
-        res_accelerometer_x->set_value((float)a_axes[0]);
-        res_accelerometer_y->set_value((float)a_axes[1]);
-        res_accelerometer_z->set_value((float)a_axes[2]);
-        res_gyroscope_x->set_value((float)g_axes[0]);
-        res_gyroscope_y->set_value((float)g_axes[1]);
-        res_gyroscope_z->set_value((float)g_axes[2]);
-        res_distance->set_value((float)d_val);
-        res_adc_temp->set_value(t3_val);
-        res_adc_voltage->set_value(v_val);
+        res_pressure->set_value(pressure_value);
+        res_temperature2->set_value(temp2_value);
+        res_magnometer_x->set_value(mag_x);
+        res_magnometer_y->set_value(mag_y);
+        res_magnometer_z->set_value(mag_z);
+        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(gyro_x);
+        res_gyroscope_y->set_value(gyro_y);
+        res_gyroscope_z->set_value(gyro_z);
+        res_distance->set_value((int)distance_value);
+        res_adc_temp->set_value(temp3_value);
+        res_adc_voltage->set_value(volt_value);
 #endif /* SEND_ALL_SENSORS */
     }
 }
@@ -290,98 +297,98 @@
     }
 
     // Creating resources, which can be written or read from the cloud
-    res_button = client.create_resource("3200/0/5501", "button_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);
 
     // Sensor resources
-    res_temperature = client.create_resource("3303/0/5700", "temperature");
+    res_temperature = client.create_resource("3303/0/5700", "Temperature HTS221 (C)");
     res_temperature->set_value(0);
     res_temperature->methods(M2MMethod::GET);
     res_temperature->observable(true);
 
-    res_humidity = client.create_resource("3304/0/5700", "humidity");
+    res_humidity = client.create_resource("3304/0/5700", "Humidity");
     res_humidity->set_value(0);
     res_humidity->methods(M2MMethod::GET);
     res_humidity->observable(true);
 
 #ifdef SEND_ALL_SENSORS
-    res_temperature2 = client.create_resource("3303/1/5700", "temperature");
+    res_temperature2 = client.create_resource("3303/1/5700", "Temperature LPS22HB (C)");
     res_temperature2->set_value(0);
     res_temperature2->methods(M2MMethod::GET);
     res_temperature2->observable(true);
 
-    res_adc_temp = client.create_resource("3303/2/5700", "temperature");
+    res_adc_temp = client.create_resource("3303/2/5700", "Temperature ADC (C)");
     res_adc_temp->set_value(0);
     res_adc_temp->methods(M2MMethod::GET);
     res_adc_temp->observable(true);
 
-    res_accelerometer_x = client.create_resource("3313/0/5702", "accelerometer_x");
+    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 = 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 = 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_magnometer_x = client.create_resource("3314/0/5702", "magnometer_x");
+    res_magnometer_x = client.create_resource("3314/0/5702", "Magnometer X");
     res_magnometer_x->set_value(0);
     res_magnometer_x->methods(M2MMethod::GET);
     res_magnometer_x->observable(true);
 
-    res_magnometer_y = client.create_resource("3314/0/5703", "magnometer_y");
+    res_magnometer_y = client.create_resource("3314/0/5703", "Magnometer Y");
     res_magnometer_y->set_value(0);
     res_magnometer_y->methods(M2MMethod::GET);
     res_magnometer_y->observable(true);
 
-    res_magnometer_z = client.create_resource("3314/0/5704", "magnometer_z");
+    res_magnometer_z = client.create_resource("3314/0/5704", "Magnometer Z");
     res_magnometer_z->set_value(0);
     res_magnometer_z->methods(M2MMethod::GET);
     res_magnometer_z->observable(true);
 
-    res_gyroscope_x = client.create_resource("3334/0/5702", "gyroscope_x");
+    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 = 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 = 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_adc_voltage = client.create_resource("3316/0/5700", "voltage");
+    res_adc_voltage = client.create_resource("3316/0/5700", "Voltage");
     res_adc_voltage->set_value(0);
     res_adc_voltage->methods(M2MMethod::GET);
     res_adc_voltage->observable(true);
 
-    res_pressure = client.create_resource("3323/0/5700", "pressure");
+    res_pressure = client.create_resource("3323/0/5700", "Pressure");
     res_pressure->set_value(0);
     res_pressure->methods(M2MMethod::GET);
     res_pressure->observable(true);
 
-    res_distance = client.create_resource("3330/0/5700", "distance");
+    res_distance = client.create_resource("3330/0/5700", "Distance");
     res_distance->set_value((float)999.9);
     res_distance->methods(M2MMethod::GET);
     res_distance->observable(true);
 
-    res_led = client.create_resource("3201/0/5853", "led_state");
+    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_led->attach_put_callback(put_callback);
 #endif /* SEND_ALL_SENSORS */
 
     printf("Initialized Pelion Client. Registering...\n");