This program shows how to use the VCNL4010 (proximity and ambient light sensor) on the QW dev kit for detecting an open closet door. When the closet is open, a message will be displayed in the console window and a Sigfox message will be send.

Dependencies:   QW_Sensors mbed

Fork of QW-Closet-detection by Quicksand

QW Closet Detection

This program shows how to use the VCNL4010 (proximity and ambient light sensor) on the QW dev kit for detecting an open closet door. When the closet is open, a message will be displayed in the console window and a Sigfox message will be send.

Code explanation

The program starts with the initialisation/declaration of the leds, pushbuttons and sensor. Also the necessary function prototypes and serial communications are declared. After the initialisation phase, the sensor ID is read. After that, the program is waiting for a push on one of the buttons to calibrate the "device". Once calibrated, the program enters an infinite loop. Each loop the ambient light value is read. When a certain threshold is passed, a led is turned on to indicate an open closet, a message is shown in the console window and a Sigfox message is transmitted. When the Sigfox message is transmitted, the loop continuous.

Sigfox message payload

First there is the "05", this is the Quicksand ID of the example program. This is used by Quicksand to keep track of our example programs. The second value that is transmitted is the ambient light value the moment when an open closet was detected.

More information and other example code can be found on the component page by clicking the link below: https://developer.mbed.org/components/QW-SIGFOX-Development-Kit/

Revision:
1:95286e4370e7
Parent:
0:6c17d1a79f75
--- a/main.cpp	Thu Nov 05 09:40:10 2015 +0000
+++ b/main.cpp	Thu Dec 15 10:10:30 2016 +0000
@@ -1,48 +1,185 @@
+/* This program demonstrates how to use the VCNL4010 for detecting an open closet door.
+ * Open a serial console to the board to see extra info.
+ */
+ 
 #include "mbed.h"
 #include "VCNL4010.h"
 
+#define SER_BUFFER_SIZE 32
+
+/* The 4 onboard LEDs */
 DigitalOut LED_0 (PB_6);
 DigitalOut LED_1 (PA_7);
 DigitalOut LED_2 (PA_6);
 DigitalOut LED_3 (PA_5);
- 
-//Virtual serial port over USB
+
+/* The 2 user buttons */
+InterruptIn SW1(PA_8);
+InterruptIn SW2(PB_10);
+
+/* Proximity and ambient light sensor*/
+VCNL40x0 VCNL4010(PB_9, PB_8, VCNL40x0_ADDRESS);      // SDA, SCL pin and I2C address
+
+/* Function prototypes */
+void sw1interrupt();
+void sw2interrupt();
+void sertmout();
+bool modem_command_check_ok(char * command);
+void modem_setup();
+
+bool ser_timeout = false; 
+
+/* Serial port over USB */
 Serial pc(USBTX, USBRX);
+
+/* Serial connection to sigfox modem */
 Serial modem(PA_9, PA_10);
 
-// configure VCNL4010
-VCNL40x0 VCNL40x0_Device (PB_9, PB_8, VCNL40x0_ADDRESS);      // Define SDA, SCL pin and I2C address
+
+bool indoor = false;
+int indoorLight; 
 
 
 int main() {
     
+    unsigned char ID=0;
+    unsigned int  AmbiValue=0;
+    
+    /* Setup TD120x */
+    wait(3);
+    modem_setup();
+    
+    /* Turn off all LED */
     LED_0 = 1;
     LED_1 = 1;
     LED_2 = 1;
     LED_3 = 1;
+    
+    /* Setup button interrupts */    
+    SW1.fall(&sw1interrupt);
+    SW2.fall(&sw2interrupt);    
 
-    unsigned char ID=0;
-    unsigned int  AmbiValue=0;
+    /* Read VCNL40x0 product ID revision register */
+    VCNL4010.ReadID (&ID);
+    pc.printf("\n\nProduct ID Revision Register: %d", ID);
+
+    wait_ms(3000);                                      //Wait 3s (only for display)
     
-       // print information on screen
-    pc.printf("\n\n VCNL4010 Proximity/Ambient Light Sensor");
-    pc.printf("\n Read Ambillight on demand in endless loop");
-
-    VCNL40x0_Device.ReadID (&ID);                           // Read VCNL40x0 product ID revision register
-    pc.printf("\n\n Product ID Revision Register: %d", ID);
-
-    wait_ms(3000);                                          // wait 3s (only for display)
+    /* Calibrate the system by measuring the indoor light */
+    while(indoor != true){
+        LED_3 = 0;
+        wait_ms(500);  
+        LED_3 = 1;
+        wait_ms(500);
+    }
+    VCNL4010.ReadAmbiOnDemand (&AmbiValue);             //Read ambi value on demand
+    indoorLight = AmbiValue;
+    pc.printf("\rIndoor light value is: %5.0i\n", indoorLight);
     
     while(1) { 
-        VCNL40x0_Device.ReadAmbiOnDemand (&AmbiValue);      // read ambi value on demand
-        pc.printf("Ambient light: %5.0i cts \tIlluminance: %7.2f lx\r", AmbiValue, AmbiValue/4.0);
-        if(AmbiValue < 5000) LED_3 = 0;
-        else LED_3 = 1;
-        if(AmbiValue < 4000) LED_2 = 0;
-        else LED_2 = 1;
-        if(AmbiValue < 3000) LED_1 = 0;
-        else LED_1 = 1;
-        if(AmbiValue < 2000) LED_0 = 0;
-        else LED_0 = 1;
+        VCNL4010.ReadAmbiOnDemand (&AmbiValue);         //Read ambi value on demand
+        
+        if(AmbiValue > (indoorLight+50)){
+            LED_0 = 0;                                  //Turn on LED_0 to indicate open closet and Sigfox transmission
+            pc.printf("\rCloset door has been opened!\n");
+            char command[SER_BUFFER_SIZE];
+            sprintf(command, "AT$SF=05%04x,2,0\n", (int) AmbiValue );
+            pc.printf("Sending ambient value %5.0i over Sigfox using modem command: %s\n", AmbiValue , command);
+            modem_command_check_ok(command);
+            LED_0 = 1;                                  //Turn off LED_0
+            wait_ms(5000);                              //Wait 5 seconds before checking again
+        } 
+    }
+}
+
+void modem_setup()
+{
+    /* Reset to factory defaults */
+    if(modem_command_check_ok("AT&F")) 
+    {
+        pc.printf("Factory reset succesfull\r\n");
+    }
+    else 
+    {
+        pc.printf("Factory reset TD120x failed\r\n");
+    }
+    /* Disable local echo */
+    modem.printf("ATE0\n");
+    if(modem_command_check_ok("ATE0")) 
+    {
+        pc.printf("Local echo disabled\r\n");
     }
-}
\ No newline at end of file
+    /* Write to mem */
+    if(modem_command_check_ok("AT&W")) 
+    {
+        pc.printf("Settings saved!\r\n");
+    }    
+}
+
+bool modem_command_check_ok(char * command)
+{
+    /* First clear serial data buffers */
+    while(modem.readable()) modem.getc();
+    /* Timeout for response of the modem */
+    Timeout tmout;
+    ser_timeout = false;
+    /* Buffer for incoming data */
+    char responsebuffer[6];
+    /* Flag to set when we get 'OK' response */
+    bool ok = false;
+    bool error = false;
+    /* Print command to TD120x */
+    modem.printf(command);
+    /* Newline to activate command */
+    modem.printf("\n");
+    /* Wait untill serial feedback, min 7 seconds before timeout */
+    tmout.attach(&sertmout, 7.0);
+    while(!modem.readable()&& ser_timeout == false);
+    while(!ok && !ser_timeout && !error) 
+    {
+        if(modem.readable()) 
+        {
+            for(int i = 0; i < 5; i++)
+            {
+                responsebuffer[i] = responsebuffer[i+1];
+            }
+            responsebuffer[5] = modem.getc();
+            if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'O' && responsebuffer[3] == 'K' && responsebuffer[4] == '\r' && responsebuffer[5] == '\n' ) 
+            {
+                ok = true;
+            }
+            else if(responsebuffer[0] == '\r' && responsebuffer[1] == '\n' && responsebuffer[2] == 'E' && responsebuffer[3] == 'R' && responsebuffer[4] == 'R' && responsebuffer[5] == 'O' ) 
+            {
+                error = true;
+            }
+        }
+    }
+    tmout.detach();
+    return ok;
+}
+
+/* Button 1 ISR */
+void sw1interrupt()
+{
+    pc.printf("\n\rButton 1 pressed\n\r");
+    indoor =  true;
+    pc.printf("\n\rIndoor light measured\n\r");   
+}
+
+/* Button 2 ISR */
+void sw2interrupt()
+{
+    pc.printf("\n\rButton 2 pressed\n\r");
+    indoor =  true;
+    pc.printf("\n\rIndoor light measured\n\r");
+    
+}
+
+/* ISR for serial timeout */
+void sertmout()
+{
+    ser_timeout = true;
+}
+
+
+