Door controller and proximity switch detection.

Dependencies:   mbed-rtos mbed VodafoneUSBModem_bleedingedge

This program takes in a digital input - which is a reed switch for a door - and an output to a relay. The door is open with a simple 'text' to the modem on the USB bus (i used k3770 vodafone dongle).

The door will send an alarm message to your nominated numbers at the start of the program if the door is open without a command being sent.

Very simple - and just meant as a demonstration of how to incorporate GSM/3G functionality to a evice.

Revision:
26:b9a0fa0f2469
Parent:
21:7068b39d3006
Parent:
23:28b693d586ee
Child:
27:a265d336f088
--- a/main.cpp	Sun Feb 10 16:39:50 2013 +0000
+++ b/main.cpp	Sun Feb 10 20:28:33 2013 +0000
@@ -23,10 +23,11 @@
 
 #define __DEBUG__ 4 //Maximum verbosity
 #ifndef __MODULE__
-#define __MODULE__ "net_voda_k3770_test.cpp"
+//#define __MODULE__ "net_voda_k3770_test.cpp"
 #endif
 
-#define MY_PHONE_NUMBER "+447717275049"
+#define MY_PHONE_NUMBER "+447717275049"     // Nicholas' Number
+#define BACKUP_NUMBER "+447825608771"       // Ashleys Number
 
 //#include "core/fwk.h"
 #include "mbed.h"
@@ -42,27 +43,32 @@
 DigitalOut  led1(LED1);                     // LED 1 is used to show the state of the door. ON is door open, and OFF is door close.
 DigitalOut  led2(LED2);                     // LED 2 is used to indicate that the main process is still running.
 DigitalOut  led3(LED3);                     // LED 3 is use to indicate if the relay is being switched on to open the door
-DigitalOut  led4(LED4);
+DigitalOut  led4(LED4);                     // LED 4 indicate the state of the ALARM - If it's on the door is armed.
 DigitalOut  doorContactorRelay(p5);         // create a digital pin object to control the door contactor relay
 DigitalIn   doorProximitySwitch(p6);        // create a digital pin object to sense door position proximity switch, this works in reverse
                                             // the switch senses positive when the door is closed - need to be sure when it's closed! :-)
 bool        openTheDoor = false;            // create a state variable which is set to indicate the mbed should open the door
                                             // this variable will be used hold the latch open until it knows the door has been open.
 bool        detectRegState = true;          // this boolean variable will force the while loop to detect reg state - if the connection manager
-                                            // is not registered it will stay in this 'detec' phase for ever.                                            
-                                            
+                                            // is not registered it will stay in this 'detec' phase for ever.  
+bool        alarmActive = false;            // this alarm flag will only be true if the door is open - but no signal has been detected 
+                                            // to open the door. in other words there is a break in!                                          
+bool        closeTheDoor = false;           // this flag is used when the door is in an open legal state. once it's closed after a legal state
+                                            // it will set the alarm to active. 
+
+
 int read_digital(void) {
     DigitalIn mydigital(p6);
     return(mydigital.read());
 }
 
 void test(void const*) {
+
+  DBG("Hello world and Vodafone Connection Manager test program!");
+  DBG("Waiting 30 seconds for modem to settle down");
+  Thread::wait(10000);
   VodafoneUSBModem connectionManager;                     // create a connection manager object
 
-  DBG("Hello world and Vodafone K3770 test program!");
-    
-
-
   char num[17];                                           // create a buffer to hold the telephone number in
   char msg[160];                                          // create a buffer to hold the text message in
   size_t count;
@@ -70,6 +76,12 @@
   LinkMonitor::BEARER bearer = LinkMonitor::BEARER_UNKNOWN;                                 // an enum to hold the type of bearer established by the connection manager
   int rssi = -1000;                                                                         // a signal strength indicator variable - set to a very default low value.
 
+  // this is a fudge to let the modem settle down - we wait for 30 seconds
+  
+  DBG("Wait over - initialising the modem manager");
+  
+  
+  
   int ret = connectionManager.getLinkState(&rssi, &regState, &bearer);
   
   // LinkMonitor::REGISTRATION_STATE regState = LinkMonitor::REGISTRATION_STATE_HOME_NETWORK;
@@ -80,7 +92,6 @@
   
   while(detectRegState)
   {
-      //if(connectionManager.getLinkState(&rssi, &regState, &bearer)==0) 
       if (detectRegState ==1)
       {
         if(rssi==-1000) 
@@ -106,6 +117,9 @@
             case LinkMonitor::REGISTRATION_STATE_NO_SIGNAL:
                 DBG("regState: NO SIGNAL");
                 break;
+            case HARDWARE_NO_RESPONSE:
+                DBG("Looks like there is no modem inserted, or power to the modem module has failed?");
+                break;
             case LinkMonitor::REGISTRATION_STATE_HOME_NETWORK:
                 detectRegState = false;
                 DBG("regState: HOME NETWORK");
@@ -120,10 +134,21 @@
         }
       }
   Thread::wait(500);
+  int ret = connectionManager.getLinkState(&rssi, &regState, &bearer);
   }
 
-  ret = connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from mbed door controller");
-  
+  // let the boss man know I'm alive and entering my main loop  
+  ret = connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from Vodafone door controller 2013 - Alarm Active");
+  ret = connectionManager.sendSM(BACKUP_NUMBER, "Hello from Vodafone door controller 2013- Alarm Active");
+
+  // better let the boss know that I'm on a roamed network as this could cost some money! or present some routing problems
+  if (regState==LinkMonitor::REGISTRATION_STATE_ROAMING)
+  {
+      //ret = connectionManager.sendSM(MY_PHONE_NUMBER, "Warning sir - I'm on a roamed network!");
+      ret = connectionManager.sendSM(BACKUP_NUMBER, "Warning sir - I'm on a roamed network!");
+  }
+    
+
 
   while(true)
   {
@@ -135,9 +160,44 @@
         doorContactorRelay = 0;                             // door must be open now so switch relay off.
         openTheDoor = false;                                // the door is open now so set it to false.
         led3 = 0;                                           // switch the LED light off to indicate I'm switching the relay off
+        closeTheDoor = true;                                // now set the flag to indicate we are waiting for a closed door event allowing us to then activate the alarm
+        DBG("The closeTheDoor is now set to TRUE");
 
     }
     
+    if ((read_digital() != 0) && ( closeTheDoor == true))
+    {
+        closeTheDoor = false;                               // the door is now closed after a legal open so reset the flag
+        alarmActive = true;                                 // OK we can now activate the ALARM again
+        led4 = 1;                                           // switch the alarm light on
+        DBG("The door is now closed after an open and the ALARM is no re-activated");
+    
+    }
+    
+    // check to see if the door is open and the alarm is on - if it is send warning message - but just once!!!!
+    if ((read_digital() == 0) && (alarmActive == true))
+    {
+        DBG("The door is now open - but the ALARM is active - send emergency text to warn our boss!");
+        DBG("*******************************************************************");
+        DBG("WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING");
+        DBG("*******************************************************************");
+        alarmActive = false;    // set the alarm off as we don't want to keep sending text messages
+        led4 = 0;               // set the alarm light to off
+        connectionManager.sendSM(BACKUP_NUMBER, "ALARM - ALARM - ALARM - Your door is open but I've not received an 'open' command!!!" );
+        connectionManager.sendSM(MY_PHONE_NUMBER, "ALARM - ALARM - ALARM - Your door is open but I've not received an 'open' command!!!" );
+        
+    }
+    
+    // check to see that the door is closed and the alarm can be reset. this will happen if the openTheDoor state variable
+    // is FALSE (indicating that the system is not trying to hold the relay open to allow accesss, the door is closed and the ALARM is false
+    if ((openTheDoor == false) && (read_digital() !=0) && (alarmActive == false))
+    {
+        DBG("Resetting the ALARM to active again");
+        alarmActive = true;     // set the alarm on as the door is closed again
+        led4 = 1;
+    }
+        
+        
     // check the state of the door and switch the LED light to 'ON' for door open and 'OFF' for door closed.
     //if (doorProximitySwitch.read() == 0)
     if (read_digital() == 0)
@@ -161,7 +221,7 @@
       continue;
     }
         
-    DBG("The SMS count is now at: %d for this modem", count);
+    // DBG("The SMS count is now at: %d for this modem", count);
     if( count > 0)                                          // if there are messages in the mailbox start pulling them off the queue
     {
       DBG("%d SMS to read", count);
@@ -201,6 +261,9 @@
         {
             doorContactorRelay = 1;
             openTheDoor = true;     // set the state vairable to let the door contoller know it needs to open the door
+            alarmActive = false;    // set the alarm OFF until the door is open
+            led4 = 0;               // set the alarm light to off
+            DBG("The ALARM is now set to %d: ", alarmActive);
             led3 = 1;
             DBG("The relay has been activated to open the door");
             connectionManager.sendSM(num, " Door open sir... Welcome home! " );
@@ -237,7 +300,8 @@
   //t.attach(tick,1);
   DBG_INIT();
   DBG_SET_SPEED(115200);
-  Thread testTask(test, NULL, osPriorityNormal, 1024*6);
+  test(NULL);
+  //Thread testTask(test, NULL, osPriorityNormal, 1024*5);
 
   keepAlive(NULL);