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:
27:a265d336f088
Parent:
26:b9a0fa0f2469
Child:
29:dc1458c071ba
--- a/main.cpp	Sun Feb 10 20:28:33 2013 +0000
+++ b/main.cpp	Thu Feb 21 09:29:20 2013 +0000
@@ -23,13 +23,11 @@
 
 #define __DEBUG__ 4 //Maximum verbosity
 #ifndef __MODULE__
-//#define __MODULE__ "net_voda_k3770_test.cpp"
 #endif
 
 #define MY_PHONE_NUMBER "+447717275049"     // Nicholas' Number
 #define BACKUP_NUMBER "+447825608771"       // Ashleys Number
 
-//#include "core/fwk.h"
 #include "mbed.h"
 #include "rtos.h"
 #include "VodafoneUSBModem.h"
@@ -62,7 +60,36 @@
     return(mydigital.read());
 }
 
-void test(void const*) {
+
+// http://mbed.org/forum/mbed/topic/508/
+class Watchdog 
+{
+    public:
+    // Load timeout value in watchdog timer and enable
+        void setTimeOut(float s) 
+        {
+            LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+            uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4
+            LPC_WDT->WDTC = s * (float)clk;
+            LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset
+            kick();
+        }
+    // "kick" or "feed" the dog - reset the watchdog timer
+    // by writing this required bit pattern
+        void kick() 
+        {
+            LPC_WDT->WDFEED = 0xAA;
+            LPC_WDT->WDFEED = 0x55;
+        }
+};
+ 
+
+// Setup the watchdog timer
+Watchdog watchDogTimer;
+
+
+
+void doorController(void const*) {
 
   DBG("Hello world and Vodafone Connection Manager test program!");
   DBG("Waiting 30 seconds for modem to settle down");
@@ -279,6 +306,8 @@
       
     }
     Thread::wait(500);
+    DBG("Main loop. Make sure we kick the dog to stop a restart happening!");
+    watchDogTimer.kick();
   }
 }
 
@@ -290,21 +319,35 @@
    }
 }
 
-void tick()
-{
-  led4=!led4;
-}
 
 int main() {
-  //Ticker t;
-  //t.attach(tick,1);
-  DBG_INIT();
-  DBG_SET_SPEED(115200);
-  test(NULL);
-  //Thread testTask(test, NULL, osPriorityNormal, 1024*5);
+    // On reset, indicate a watchdog reset has happened by switching all lights on for 3 seconds
+    if ((LPC_WDT->WDMOD >> 2) & 1)
+    {
+        led1 = 1;
+        led2 = 1;
+        led3 = 1;
+        led4 = 1;
+        Thread::wait(3000);
+        led1 = 0;
+        led2 = 0;
+        led3 = 0;
+        led4 = 0;
 
-  keepAlive(NULL);
+    }
+
+        
+    // setup a 30 second timeout on watchdog timer hardware
+    // needs to be longer than worst case main loop exection time
+    watchDogTimer.setTimeOut(30.0);  
+ 
+    DBG_INIT();
+    DBG_SET_SPEED(115200);
+    doorController(NULL);
+    //Thread testTask(test, NULL, osPriorityNormal, 1024*5);
+
+    keepAlive(NULL);
 
     
-    return 0;
+  return 0;
 }