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.

Committer:
nherriot
Date:
Tue Feb 05 08:58:35 2013 +0000
Revision:
22:87d73be30243
Parent:
18:ad4ae0caf337
Child:
23:28b693d586ee
Adding alarm feature to the door

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nherriot 0:5136cdfaebd0 1 /* net_sms_test.cpp */
nherriot 0:5136cdfaebd0 2 /*
nherriot 0:5136cdfaebd0 3 Copyright (C) 2012 ARM Limited.
nherriot 0:5136cdfaebd0 4
nherriot 0:5136cdfaebd0 5 Permission is hereby granted, free of charge, to any person obtaining a copy of
nherriot 0:5136cdfaebd0 6 this software and associated documentation files (the "Software"), to deal in
nherriot 0:5136cdfaebd0 7 the Software without restriction, including without limitation the rights to
nherriot 0:5136cdfaebd0 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
nherriot 0:5136cdfaebd0 9 of the Software, and to permit persons to whom the Software is furnished to do
nherriot 0:5136cdfaebd0 10 so, subject to the following conditions:
nherriot 0:5136cdfaebd0 11
nherriot 0:5136cdfaebd0 12 The above copyright notice and this permission notice shall be included in all
nherriot 0:5136cdfaebd0 13 copies or substantial portions of the Software.
nherriot 0:5136cdfaebd0 14
nherriot 0:5136cdfaebd0 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
nherriot 0:5136cdfaebd0 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
nherriot 0:5136cdfaebd0 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
nherriot 0:5136cdfaebd0 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
nherriot 0:5136cdfaebd0 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
nherriot 0:5136cdfaebd0 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
nherriot 0:5136cdfaebd0 21 SOFTWARE.
nherriot 0:5136cdfaebd0 22 */
nherriot 0:5136cdfaebd0 23
nherriot 0:5136cdfaebd0 24 #define __DEBUG__ 4 //Maximum verbosity
nherriot 0:5136cdfaebd0 25 #ifndef __MODULE__
nherriot 6:5a892c3d738e 26 // #define __MODULE__ "net_voda_k3770_test.cpp"
nherriot 0:5136cdfaebd0 27 #endif
nherriot 0:5136cdfaebd0 28
nherriot 22:87d73be30243 29 #define MY_PHONE_NUMBER "+447717275049" // Nicholas' Number
nherriot 22:87d73be30243 30 #define BACKUP_NUMBER "+447825608771" // Ashleys Number
nherriot 0:5136cdfaebd0 31
nherriot 0:5136cdfaebd0 32 #include "core/fwk.h"
nherriot 0:5136cdfaebd0 33 #include "mbed.h"
nherriot 0:5136cdfaebd0 34 #include "rtos.h"
nherriot 10:c8a6f2822068 35 #include "VodafoneUSBModem.h"
nherriot 0:5136cdfaebd0 36
nherriot 2:bcc9256ecaab 37 #include <string>
nherriot 2:bcc9256ecaab 38
nherriot 0:5136cdfaebd0 39 extern "C" void HardFault_Handler() {
nherriot 0:5136cdfaebd0 40 error("Hard Fault!\n");
nherriot 0:5136cdfaebd0 41 }
nherriot 0:5136cdfaebd0 42
nherriot 8:1b4e84f4c451 43 DigitalOut led1(LED1); // LED 1 is used to show the state of the door. ON is door open, and OFF is door close.
nherriot 16:9245c3a37491 44 DigitalOut led2(LED2); // LED 2 is used to indicate that the main process is still running.
nherriot 8:1b4e84f4c451 45 DigitalOut led3(LED3); // LED 3 is use to indicate if the relay is being switched on to open the door
nherriot 22:87d73be30243 46 DigitalOut led4(LED4); // LED 4 indicate the state of the ALARM - If it's on the door is armed.
nherriot 8:1b4e84f4c451 47 DigitalOut doorContactorRelay(p5); // create a digital pin object to control the door contactor relay
nherriot 11:769f2b28b236 48 DigitalIn doorProximitySwitch(p6); // create a digital pin object to sense door position proximity switch, this works in reverse
nherriot 8:1b4e84f4c451 49 // the switch senses positive when the door is closed - need to be sure when it's closed! :-)
nherriot 8:1b4e84f4c451 50 bool openTheDoor = false; // create a state variable which is set to indicate the mbed should open the door
nherriot 8:1b4e84f4c451 51 // this variable will be used hold the latch open until it knows the door has been open.
nherriot 15:262da03e3e20 52 bool detectRegState = true; // this boolean variable will force the while loop to detect reg state - if the connection manager
nherriot 22:87d73be30243 53 // is not registered it will stay in this 'detec' phase for ever.
nherriot 22:87d73be30243 54 bool alarmActive = false; // this alarm flag will only be true if the door is open - but no signal has been detected
nherriot 22:87d73be30243 55 // to open the door. in other words there is a break in!
nherriot 22:87d73be30243 56 bool closeTheDoor = false; // this flag is used when the door is in an open legal state. once it's closed after a legal state
nherriot 22:87d73be30243 57 // it will set the alarm to active.
nherriot 22:87d73be30243 58
nherriot 22:87d73be30243 59
nherriot 10:c8a6f2822068 60 int read_digital(void) {
nherriot 10:c8a6f2822068 61 DigitalIn mydigital(p6);
nherriot 10:c8a6f2822068 62 return(mydigital.read());
nherriot 10:c8a6f2822068 63 }
nherriot 0:5136cdfaebd0 64
nherriot 0:5136cdfaebd0 65 void test(void const*) {
nherriot 10:c8a6f2822068 66 VodafoneUSBModem connectionManager; // create a connection manager object
nherriot 0:5136cdfaebd0 67
nherriot 22:87d73be30243 68 DBG("Hello world and Vodafone Connection Manager test program!");
nherriot 22:87d73be30243 69 DBG("Waiting 30 seconds for modem to settle down");
nherriot 15:262da03e3e20 70
nherriot 0:5136cdfaebd0 71
nherriot 10:c8a6f2822068 72 char num[17]; // create a buffer to hold the telephone number in
nherriot 10:c8a6f2822068 73 char msg[160]; // create a buffer to hold the text message in
nherriot 10:c8a6f2822068 74 size_t count;
nherriot 15:262da03e3e20 75 LinkMonitor::REGISTRATION_STATE regState = LinkMonitor::REGISTRATION_STATE_UNKNOWN; // an enum to hold the registration state of the connection manager
nherriot 15:262da03e3e20 76 LinkMonitor::BEARER bearer = LinkMonitor::BEARER_UNKNOWN; // an enum to hold the type of bearer established by the connection manager
nherriot 15:262da03e3e20 77 int rssi = -1000; // a signal strength indicator variable - set to a very default low value.
nherriot 15:262da03e3e20 78
nherriot 22:87d73be30243 79 // this is a fudge to let the modem settle down - we wait for 30 seconds
nherriot 22:87d73be30243 80 Thread::wait(30000);
nherriot 22:87d73be30243 81 DBG("Wait over - initialising the modem manager");
nherriot 17:9b57d29e2eab 82 int ret = connectionManager.getLinkState(&rssi, &regState, &bearer);
nherriot 16:9245c3a37491 83 // LinkMonitor::REGISTRATION_STATE regState = LinkMonitor::REGISTRATION_STATE_HOME_NETWORK;
nherriot 16:9245c3a37491 84
nherriot 15:262da03e3e20 85 // before we do anything we need to make sure we have a connection to a network.
nherriot 15:262da03e3e20 86 // so let us check that first!
nherriot 15:262da03e3e20 87
nherriot 16:9245c3a37491 88
nherriot 15:262da03e3e20 89 while(detectRegState)
nherriot 15:262da03e3e20 90 {
nherriot 17:9b57d29e2eab 91 if (detectRegState ==1)
nherriot 15:262da03e3e20 92 {
nherriot 15:262da03e3e20 93 if(rssi==-1000)
nherriot 15:262da03e3e20 94 {
nherriot 15:262da03e3e20 95 DBG("Checking signal strength - RSSI: Error.");
nherriot 15:262da03e3e20 96 }
nherriot 15:262da03e3e20 97 else
nherriot 15:262da03e3e20 98 {
nherriot 15:262da03e3e20 99 DBG("Signal strength is: RSSI: %d",rssi);
nherriot 15:262da03e3e20 100 }
nherriot 15:262da03e3e20 101
nherriot 15:262da03e3e20 102 switch(regState)
nherriot 15:262da03e3e20 103 {
nherriot 15:262da03e3e20 104 case LinkMonitor::REGISTRATION_STATE_UNKNOWN:
nherriot 15:262da03e3e20 105 DBG("regState: UNKNOWN. Failing.");
nherriot 15:262da03e3e20 106 break;
nherriot 15:262da03e3e20 107 case LinkMonitor::REGISTRATION_STATE_REGISTERING:
nherriot 15:262da03e3e20 108 DBG("regState: REGISTERING");
nherriot 15:262da03e3e20 109 break;
nherriot 15:262da03e3e20 110 case LinkMonitor::REGISTRATION_STATE_DENIED:
nherriot 15:262da03e3e20 111 DBG("regState: DENIED");
nherriot 15:262da03e3e20 112 break;
nherriot 15:262da03e3e20 113 case LinkMonitor::REGISTRATION_STATE_NO_SIGNAL:
nherriot 15:262da03e3e20 114 DBG("regState: NO SIGNAL");
nherriot 15:262da03e3e20 115 break;
nherriot 15:262da03e3e20 116 case LinkMonitor::REGISTRATION_STATE_HOME_NETWORK:
nherriot 15:262da03e3e20 117 detectRegState = false;
nherriot 15:262da03e3e20 118 DBG("regState: HOME NETWORK");
nherriot 15:262da03e3e20 119 break;
nherriot 15:262da03e3e20 120 case LinkMonitor::REGISTRATION_STATE_ROAMING:
nherriot 15:262da03e3e20 121 detectRegState = false;
nherriot 15:262da03e3e20 122 DBG("regState: ROAMING");
nherriot 15:262da03e3e20 123 break;
nherriot 15:262da03e3e20 124 default:
nherriot 15:262da03e3e20 125 DBG("regState: ERROR. Failing.");
nherriot 17:9b57d29e2eab 126
nherriot 15:262da03e3e20 127 }
nherriot 15:262da03e3e20 128 }
nherriot 16:9245c3a37491 129 Thread::wait(500);
nherriot 15:262da03e3e20 130 }
nherriot 15:262da03e3e20 131
nherriot 22:87d73be30243 132 // let the boss man know I'm alive and entering my main loop
nherriot 22:87d73be30243 133 ret = connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from Vodafone door controller 2013 - Alarm Active");
nherriot 22:87d73be30243 134 ret = connectionManager.sendSM(BACKUP_NUMBER, "Hello from Vodafone door controller 2013- Alarm Active");
nherriot 22:87d73be30243 135
nherriot 22:87d73be30243 136 // better let the boss know that I'm on a roamed network as this could cost some money! or present some routing problems
nherriot 22:87d73be30243 137 if (regState==LinkMonitor::REGISTRATION_STATE_ROAMING)
nherriot 22:87d73be30243 138 {
nherriot 22:87d73be30243 139 //ret = connectionManager.sendSM(MY_PHONE_NUMBER, "Warning sir - I'm on a roamed network!");
nherriot 22:87d73be30243 140 ret = connectionManager.sendSM(BACKUP_NUMBER, "Warning sir - I'm on a roamed network!");
nherriot 22:87d73be30243 141 }
nherriot 22:87d73be30243 142
nherriot 10:c8a6f2822068 143
nherriot 0:5136cdfaebd0 144 while(true)
nherriot 0:5136cdfaebd0 145 {
nherriot 8:1b4e84f4c451 146
nherriot 8:1b4e84f4c451 147 // if this state variable has been set I need to keep the doorContactor on until the door is open
nherriot 15:262da03e3e20 148 if ((openTheDoor) && (read_digital() == 0))
nherriot 3:0a8eebcb0acf 149 {
nherriot 15:262da03e3e20 150 DBG("The door is open now and I'm switching off the relay and reseting the state variable");
nherriot 8:1b4e84f4c451 151 doorContactorRelay = 0; // door must be open now so switch relay off.
nherriot 8:1b4e84f4c451 152 openTheDoor = false; // the door is open now so set it to false.
nherriot 8:1b4e84f4c451 153 led3 = 0; // switch the LED light off to indicate I'm switching the relay off
nherriot 22:87d73be30243 154 closeTheDoor = true; // now set the flag to indicate we are waiting for a closed door event allowing us to then activate the alarm
nherriot 22:87d73be30243 155 DBG("The closeTheDoor is now set to TRUE");
nherriot 8:1b4e84f4c451 156
nherriot 8:1b4e84f4c451 157 }
nherriot 3:0a8eebcb0acf 158
nherriot 22:87d73be30243 159 if ((read_digital() != 0) && ( closeTheDoor == true))
nherriot 22:87d73be30243 160 {
nherriot 22:87d73be30243 161 closeTheDoor = false; // the door is now closed after a legal open so reset the flag
nherriot 22:87d73be30243 162 alarmActive = true; // OK we can now activate the ALARM again
nherriot 22:87d73be30243 163 led4 = 1; // switch the alarm light on
nherriot 22:87d73be30243 164 DBG("The door is now closed after an open and the ALARM is no re-activated");
nherriot 22:87d73be30243 165
nherriot 22:87d73be30243 166 }
nherriot 22:87d73be30243 167
nherriot 22:87d73be30243 168 // check to see if the door is open and the alarm is on - if it is send warning message - but just once!!!!
nherriot 22:87d73be30243 169 if ((read_digital() == 0) && (alarmActive == true))
nherriot 22:87d73be30243 170 {
nherriot 22:87d73be30243 171 DBG("The door is now open - but the ALARM is active - send emergency text to warn our boss!");
nherriot 22:87d73be30243 172 DBG("*******************************************************************");
nherriot 22:87d73be30243 173 DBG("WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING");
nherriot 22:87d73be30243 174 DBG("*******************************************************************");
nherriot 22:87d73be30243 175 alarmActive = false; // set the alarm off as we don't want to keep sending text messages
nherriot 22:87d73be30243 176 led4 = 0; // set the alarm light to off
nherriot 22:87d73be30243 177 connectionManager.sendSM(BACKUP_NUMBER, "ALARM - ALARM - ALARM - Your door is open but I've not received an 'open' command!!!" );
nherriot 22:87d73be30243 178 connectionManager.sendSM(MY_PHONE_NUMBER, "ALARM - ALARM - ALARM - Your door is open but I've not received an 'open' command!!!" );
nherriot 22:87d73be30243 179
nherriot 22:87d73be30243 180 }
nherriot 22:87d73be30243 181
nherriot 22:87d73be30243 182 // check to see that the door is closed and the alarm can be reset. this will happen if the openTheDoor state variable
nherriot 22:87d73be30243 183 // 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
nherriot 22:87d73be30243 184 if ((openTheDoor == false) && (read_digital() !=0) && (alarmActive == false))
nherriot 22:87d73be30243 185 {
nherriot 22:87d73be30243 186 DBG("Resetting the ALARM to active again");
nherriot 22:87d73be30243 187 alarmActive = true; // set the alarm on as the door is closed again
nherriot 22:87d73be30243 188 led4 = 1;
nherriot 22:87d73be30243 189 }
nherriot 22:87d73be30243 190
nherriot 22:87d73be30243 191
nherriot 8:1b4e84f4c451 192 // check the state of the door and switch the LED light to 'ON' for door open and 'OFF' for door closed.
nherriot 10:c8a6f2822068 193 //if (doorProximitySwitch.read() == 0)
nherriot 10:c8a6f2822068 194 if (read_digital() == 0)
nherriot 8:1b4e84f4c451 195 {
nherriot 15:262da03e3e20 196 // DBG("The door proximity switch is: %d so I think it's open", doorProximitySwitch.read() );
nherriot 8:1b4e84f4c451 197 led1 = 1;
nherriot 3:0a8eebcb0acf 198 }
nherriot 8:1b4e84f4c451 199 else
nherriot 8:1b4e84f4c451 200 {
nherriot 15:262da03e3e20 201 // DBG("The door proximity switch is: %d so I think it's closed", doorProximitySwitch.read() );
nherriot 8:1b4e84f4c451 202 led1 = 0;
nherriot 8:1b4e84f4c451 203 }
nherriot 8:1b4e84f4c451 204
nherriot 8:1b4e84f4c451 205
nherriot 8:1b4e84f4c451 206
nherriot 7:316c2dac06a5 207 int ret = connectionManager.getSMCount(&count); // check to see if there is an incoming SMS message
nherriot 0:5136cdfaebd0 208 if(ret)
nherriot 0:5136cdfaebd0 209 {
nherriot 0:5136cdfaebd0 210 WARN("getSMCount returned %d", ret);
nherriot 10:c8a6f2822068 211 Thread::wait(125);
nherriot 0:5136cdfaebd0 212 continue;
nherriot 0:5136cdfaebd0 213 }
nherriot 10:c8a6f2822068 214
nherriot 10:c8a6f2822068 215 DBG("The SMS count is now at: %d for this modem", count);
nherriot 7:316c2dac06a5 216 if( count > 0) // if there are messages in the mailbox start pulling them off the queue
nherriot 0:5136cdfaebd0 217 {
nherriot 0:5136cdfaebd0 218 DBG("%d SMS to read", count);
nherriot 10:c8a6f2822068 219 ret = connectionManager.getSM(num, msg, 160);
nherriot 8:1b4e84f4c451 220
nherriot 0:5136cdfaebd0 221 if(ret)
nherriot 0:5136cdfaebd0 222 {
nherriot 0:5136cdfaebd0 223 WARN("getSM returned %d", ret);
nherriot 10:c8a6f2822068 224 Thread::wait(125);
nherriot 0:5136cdfaebd0 225 continue;
nherriot 0:5136cdfaebd0 226 }
nherriot 0:5136cdfaebd0 227
nherriot 6:5a892c3d738e 228 DBG("The message is from number: %s and the message is: \"%s\"", num, msg);
nherriot 6:5a892c3d738e 229
nherriot 15:262da03e3e20 230 // if the SMS is from your own number, ignore it!
nherriot 15:262da03e3e20 231 if(strcmp(num,"+447785666088")==0)
nherriot 15:262da03e3e20 232 {
nherriot 15:262da03e3e20 233 DBG("The message appears to be from my MSISDN number: %s, I'll do nothing.", num);
nherriot 15:262da03e3e20 234 continue;
nherriot 15:262da03e3e20 235 }
nherriot 15:262da03e3e20 236
nherriot 15:262da03e3e20 237
nherriot 15:262da03e3e20 238
nherriot 15:262da03e3e20 239 if ((strcmp (msg, "open") ==0)|| (strcmp(num,"TrackaPhone")==0))
nherriot 8:1b4e84f4c451 240 {
nherriot 2:bcc9256ecaab 241 DBG("The SMS message indicates I should open the door");
nherriot 7:316c2dac06a5 242
nherriot 7:316c2dac06a5 243 // check the door is not already open if it is do nothing and send a warning back to the sender
nherriot 7:316c2dac06a5 244 // for this door the switch 'true' then its closed - we have to have an active signal to show it's securely closed
nherriot 8:1b4e84f4c451 245
nherriot 15:262da03e3e20 246 if (read_digital() == 0 )
nherriot 7:316c2dac06a5 247 {
nherriot 7:316c2dac06a5 248 DBG("The door is already open - so I'll do nothing");
nherriot 8:1b4e84f4c451 249 connectionManager.sendSM(num, " WARNING - The door is already open sir! ");
nherriot 7:316c2dac06a5 250 }
nherriot 7:316c2dac06a5 251 else
nherriot 7:316c2dac06a5 252 {
nherriot 7:316c2dac06a5 253 doorContactorRelay = 1;
nherriot 8:1b4e84f4c451 254 openTheDoor = true; // set the state vairable to let the door contoller know it needs to open the door
nherriot 22:87d73be30243 255 alarmActive = false; // set the alarm OFF until the door is open
nherriot 22:87d73be30243 256 led4 = 0; // set the alarm light to off
nherriot 22:87d73be30243 257 DBG("The ALARM is now set to %d: ", alarmActive);
nherriot 7:316c2dac06a5 258 led3 = 1;
nherriot 7:316c2dac06a5 259 DBG("The relay has been activated to open the door");
nherriot 8:1b4e84f4c451 260 connectionManager.sendSM(num, " Door open sir... Welcome home! " );
nherriot 8:1b4e84f4c451 261 }
nherriot 8:1b4e84f4c451 262 }
nherriot 8:1b4e84f4c451 263 else
nherriot 8:1b4e84f4c451 264 {
nherriot 2:bcc9256ecaab 265 DBG("The SMS message is not recognized");
nherriot 7:316c2dac06a5 266 connectionManager.sendSM(num, " Wrong password sir... Try 'open' and I'll open! ;-) ");
nherriot 2:bcc9256ecaab 267 }
nherriot 2:bcc9256ecaab 268
nherriot 2:bcc9256ecaab 269
nherriot 2:bcc9256ecaab 270
nherriot 0:5136cdfaebd0 271 }
nherriot 6:5a892c3d738e 272 Thread::wait(500);
nherriot 0:5136cdfaebd0 273 }
nherriot 0:5136cdfaebd0 274
nherriot 0:5136cdfaebd0 275 }
nherriot 0:5136cdfaebd0 276
nherriot 0:5136cdfaebd0 277 void keepAlive(void const*) {
nherriot 0:5136cdfaebd0 278 while(1)
nherriot 0:5136cdfaebd0 279 {
nherriot 8:1b4e84f4c451 280 led2=!led2;
nherriot 0:5136cdfaebd0 281 Thread::wait(500);
nherriot 0:5136cdfaebd0 282 }
nherriot 0:5136cdfaebd0 283 }
nherriot 0:5136cdfaebd0 284
nherriot 0:5136cdfaebd0 285 void tick()
nherriot 0:5136cdfaebd0 286 {
nherriot 0:5136cdfaebd0 287 led4=!led4;
nherriot 0:5136cdfaebd0 288 }
nherriot 0:5136cdfaebd0 289
nherriot 0:5136cdfaebd0 290 int main() {
nherriot 16:9245c3a37491 291 //Ticker t;
nherriot 16:9245c3a37491 292 //t.attach(tick,1);
nherriot 0:5136cdfaebd0 293 DBG_INIT();
nherriot 12:e622e146b3cd 294 DBG_SET_SPEED(115200);
nherriot 18:ad4ae0caf337 295 Thread testTask(test, NULL, osPriorityNormal, 1024*5);
nherriot 0:5136cdfaebd0 296
donatien 14:9ee94196b75d 297 keepAlive(NULL);
nherriot 0:5136cdfaebd0 298
nherriot 0:5136cdfaebd0 299
nherriot 0:5136cdfaebd0 300 return 0;
nherriot 0:5136cdfaebd0 301 }