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:
15:262da03e3e20
Parent:
12:e622e146b3cd
Child:
16:9245c3a37491
--- a/main.cpp	Thu Sep 20 08:05:18 2012 +0000
+++ b/main.cpp	Tue Sep 25 10:01:54 2012 +0000
@@ -48,6 +48,9 @@
                                             // 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.                                            
+                                            
 int read_digital(void) {
     DigitalIn mydigital(p6);
     return(mydigital.read());
@@ -58,19 +61,75 @@
 
   DBG("Hello world and Vodafone K3770 test program!");
     
-  connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from mbed door controller:)");
+
 
   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;
+  LinkMonitor::REGISTRATION_STATE regState = LinkMonitor::REGISTRATION_STATE_UNKNOWN;       // an enum to hold the registration state of the connection manager
+  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.
+
+  // before we do anything we need to make sure we have a connection to a network.
+  // so let us check that first!
+  
+  while(detectRegState)
+  {
+      if(connectionManager.getLinkState(&rssi, &regState, &bearer)==0) 
+      {
+        if(rssi==-1000) 
+        { 
+            DBG("Checking signal strength - RSSI: Error.");
+        } 
+        else 
+        { 
+            DBG("Signal strength is: RSSI: %d",rssi);
+        }
+            
+        switch(regState) 
+        {
+            case LinkMonitor::REGISTRATION_STATE_UNKNOWN:
+                DBG("regState: UNKNOWN. Failing.");
+                break;
+            case LinkMonitor::REGISTRATION_STATE_REGISTERING:
+                DBG("regState: REGISTERING");
+                break;
+            case LinkMonitor::REGISTRATION_STATE_DENIED:
+                DBG("regState: DENIED");
+                break;
+            case LinkMonitor::REGISTRATION_STATE_NO_SIGNAL:
+                DBG("regState: NO SIGNAL");
+                break;
+            case LinkMonitor::REGISTRATION_STATE_HOME_NETWORK:
+                detectRegState = false;
+                DBG("regState: HOME NETWORK");
+                break;
+            case LinkMonitor::REGISTRATION_STATE_ROAMING:
+                detectRegState = false;
+                DBG("regState: ROAMING");
+                break;
+            default:
+                DBG("regState: ERROR. Failing.");
+                break;
+        }
+      }
+  }
+
+
+
+
+
+  connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from mbed door controller:)");
+
+
 
   while(true)
   {
     
     // if this state variable has been set I need to keep the doorContactor on until the door is open
-    if ((openTheDoor) || (doorProximitySwitch.read() == 0))
+    if ((openTheDoor) && (read_digital() == 0))
     {
-        
+        DBG("The door is open now and I'm switching off the relay and reseting the state variable");
         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
@@ -81,12 +140,12 @@
     //if (doorProximitySwitch.read() == 0)
     if (read_digital() == 0)
     {
-        DBG("The door proximity switch is: %d so I think it's open", doorProximitySwitch.read() );
+        // DBG("The door proximity switch is: %d so I think it's open", doorProximitySwitch.read() );
         led1 = 1;
     }
     else
     {
-        DBG("The door proximity switch is: %d so I think it's closed", doorProximitySwitch.read() );
+        // DBG("The door proximity switch is: %d so I think it's closed", doorProximitySwitch.read() );
         led1 = 0;
     }
     
@@ -100,7 +159,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);
@@ -115,14 +174,23 @@
 
       DBG("The message is from number: %s and the message is: \"%s\"", num, msg);
       
-      if (strcmp (msg, "open") ==0) 
+      // if the SMS is from your own number, ignore it!
+      if(strcmp(num,"+447785666088")==0) 
+      {
+        DBG("The message appears to be from my MSISDN number: %s, I'll do nothing.", num);
+        continue;
+      }
+      
+  
+      
+      if ((strcmp (msg, "open") ==0)|| (strcmp(num,"TrackaPhone")==0)) 
       {
         DBG("The SMS message indicates I should open the door");
         
         // check the door is not already open if it is do nothing and send a warning back to the sender
         // for this door the switch 'true' then its closed - we have to have an active signal to show it's securely closed
 
-        if ( doorProximitySwitch.read() == 0)
+        if (read_digital() == 0 )
         {
             DBG("The door is already open - so I'll do nothing");
             connectionManager.sendSM(num, " WARNING - The door is already open sir! ");