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:
donatien
Date:
Thu Sep 27 14:49:15 2012 +0000
Revision:
21:7068b39d3006
Parent:
20:27f878df6d14
Parent:
18:ad4ae0caf337
Child:
26:b9a0fa0f2469
Fixed ATCommandInterface.cpp

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__
donatien 21:7068b39d3006 26 #define __MODULE__ "net_voda_k3770_test.cpp"
nherriot 0:5136cdfaebd0 27 #endif
nherriot 0:5136cdfaebd0 28
nherriot 0:5136cdfaebd0 29 #define MY_PHONE_NUMBER "+447717275049"
nherriot 0:5136cdfaebd0 30
donatien 21:7068b39d3006 31 //#include "core/fwk.h"
nherriot 0:5136cdfaebd0 32 #include "mbed.h"
nherriot 0:5136cdfaebd0 33 #include "rtos.h"
nherriot 10:c8a6f2822068 34 #include "VodafoneUSBModem.h"
nherriot 0:5136cdfaebd0 35
nherriot 2:bcc9256ecaab 36 #include <string>
nherriot 2:bcc9256ecaab 37
nherriot 0:5136cdfaebd0 38 extern "C" void HardFault_Handler() {
nherriot 0:5136cdfaebd0 39 error("Hard Fault!\n");
nherriot 0:5136cdfaebd0 40 }
nherriot 0:5136cdfaebd0 41
nherriot 8:1b4e84f4c451 42 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 43 DigitalOut led2(LED2); // LED 2 is used to indicate that the main process is still running.
nherriot 8:1b4e84f4c451 44 DigitalOut led3(LED3); // LED 3 is use to indicate if the relay is being switched on to open the door
nherriot 7:316c2dac06a5 45 DigitalOut led4(LED4);
nherriot 8:1b4e84f4c451 46 DigitalOut doorContactorRelay(p5); // create a digital pin object to control the door contactor relay
nherriot 11:769f2b28b236 47 DigitalIn doorProximitySwitch(p6); // create a digital pin object to sense door position proximity switch, this works in reverse
nherriot 8:1b4e84f4c451 48 // the switch senses positive when the door is closed - need to be sure when it's closed! :-)
nherriot 8:1b4e84f4c451 49 bool openTheDoor = false; // create a state variable which is set to indicate the mbed should open the door
nherriot 8:1b4e84f4c451 50 // this variable will be used hold the latch open until it knows the door has been open.
nherriot 15:262da03e3e20 51 bool detectRegState = true; // this boolean variable will force the while loop to detect reg state - if the connection manager
nherriot 15:262da03e3e20 52 // is not registered it will stay in this 'detec' phase for ever.
nherriot 15:262da03e3e20 53
nherriot 10:c8a6f2822068 54 int read_digital(void) {
nherriot 10:c8a6f2822068 55 DigitalIn mydigital(p6);
nherriot 10:c8a6f2822068 56 return(mydigital.read());
nherriot 10:c8a6f2822068 57 }
nherriot 0:5136cdfaebd0 58
nherriot 0:5136cdfaebd0 59 void test(void const*) {
nherriot 10:c8a6f2822068 60 VodafoneUSBModem connectionManager; // create a connection manager object
nherriot 0:5136cdfaebd0 61
nherriot 1:3c9d0c9dffa4 62 DBG("Hello world and Vodafone K3770 test program!");
nherriot 0:5136cdfaebd0 63
nherriot 15:262da03e3e20 64
nherriot 0:5136cdfaebd0 65
nherriot 10:c8a6f2822068 66 char num[17]; // create a buffer to hold the telephone number in
nherriot 10:c8a6f2822068 67 char msg[160]; // create a buffer to hold the text message in
nherriot 10:c8a6f2822068 68 size_t count;
nherriot 15:262da03e3e20 69 LinkMonitor::REGISTRATION_STATE regState = LinkMonitor::REGISTRATION_STATE_UNKNOWN; // an enum to hold the registration state of the connection manager
nherriot 15:262da03e3e20 70 LinkMonitor::BEARER bearer = LinkMonitor::BEARER_UNKNOWN; // an enum to hold the type of bearer established by the connection manager
nherriot 15:262da03e3e20 71 int rssi = -1000; // a signal strength indicator variable - set to a very default low value.
nherriot 15:262da03e3e20 72
nherriot 17:9b57d29e2eab 73 int ret = connectionManager.getLinkState(&rssi, &regState, &bearer);
donatien 21:7068b39d3006 74
nherriot 16:9245c3a37491 75 // LinkMonitor::REGISTRATION_STATE regState = LinkMonitor::REGISTRATION_STATE_HOME_NETWORK;
nherriot 16:9245c3a37491 76
nherriot 15:262da03e3e20 77 // before we do anything we need to make sure we have a connection to a network.
nherriot 15:262da03e3e20 78 // so let us check that first!
nherriot 15:262da03e3e20 79
nherriot 16:9245c3a37491 80
nherriot 15:262da03e3e20 81 while(detectRegState)
nherriot 15:262da03e3e20 82 {
nherriot 17:9b57d29e2eab 83 //if(connectionManager.getLinkState(&rssi, &regState, &bearer)==0)
nherriot 17:9b57d29e2eab 84 if (detectRegState ==1)
nherriot 15:262da03e3e20 85 {
nherriot 15:262da03e3e20 86 if(rssi==-1000)
nherriot 15:262da03e3e20 87 {
nherriot 15:262da03e3e20 88 DBG("Checking signal strength - RSSI: Error.");
nherriot 15:262da03e3e20 89 }
nherriot 15:262da03e3e20 90 else
nherriot 15:262da03e3e20 91 {
nherriot 15:262da03e3e20 92 DBG("Signal strength is: RSSI: %d",rssi);
nherriot 15:262da03e3e20 93 }
nherriot 15:262da03e3e20 94
nherriot 15:262da03e3e20 95 switch(regState)
nherriot 15:262da03e3e20 96 {
nherriot 15:262da03e3e20 97 case LinkMonitor::REGISTRATION_STATE_UNKNOWN:
nherriot 15:262da03e3e20 98 DBG("regState: UNKNOWN. Failing.");
nherriot 15:262da03e3e20 99 break;
nherriot 15:262da03e3e20 100 case LinkMonitor::REGISTRATION_STATE_REGISTERING:
nherriot 15:262da03e3e20 101 DBG("regState: REGISTERING");
nherriot 15:262da03e3e20 102 break;
nherriot 15:262da03e3e20 103 case LinkMonitor::REGISTRATION_STATE_DENIED:
nherriot 15:262da03e3e20 104 DBG("regState: DENIED");
nherriot 15:262da03e3e20 105 break;
nherriot 15:262da03e3e20 106 case LinkMonitor::REGISTRATION_STATE_NO_SIGNAL:
nherriot 15:262da03e3e20 107 DBG("regState: NO SIGNAL");
nherriot 15:262da03e3e20 108 break;
nherriot 15:262da03e3e20 109 case LinkMonitor::REGISTRATION_STATE_HOME_NETWORK:
nherriot 15:262da03e3e20 110 detectRegState = false;
nherriot 15:262da03e3e20 111 DBG("regState: HOME NETWORK");
nherriot 15:262da03e3e20 112 break;
nherriot 15:262da03e3e20 113 case LinkMonitor::REGISTRATION_STATE_ROAMING:
nherriot 15:262da03e3e20 114 detectRegState = false;
nherriot 15:262da03e3e20 115 DBG("regState: ROAMING");
nherriot 15:262da03e3e20 116 break;
nherriot 15:262da03e3e20 117 default:
nherriot 15:262da03e3e20 118 DBG("regState: ERROR. Failing.");
nherriot 17:9b57d29e2eab 119
nherriot 15:262da03e3e20 120 }
nherriot 15:262da03e3e20 121 }
nherriot 16:9245c3a37491 122 Thread::wait(500);
nherriot 15:262da03e3e20 123 }
nherriot 15:262da03e3e20 124
nherriot 17:9b57d29e2eab 125 ret = connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from mbed door controller");
donatien 21:7068b39d3006 126
nherriot 10:c8a6f2822068 127
nherriot 0:5136cdfaebd0 128 while(true)
nherriot 0:5136cdfaebd0 129 {
nherriot 8:1b4e84f4c451 130
nherriot 8:1b4e84f4c451 131 // if this state variable has been set I need to keep the doorContactor on until the door is open
nherriot 15:262da03e3e20 132 if ((openTheDoor) && (read_digital() == 0))
nherriot 3:0a8eebcb0acf 133 {
nherriot 15:262da03e3e20 134 DBG("The door is open now and I'm switching off the relay and reseting the state variable");
nherriot 8:1b4e84f4c451 135 doorContactorRelay = 0; // door must be open now so switch relay off.
nherriot 8:1b4e84f4c451 136 openTheDoor = false; // the door is open now so set it to false.
nherriot 8:1b4e84f4c451 137 led3 = 0; // switch the LED light off to indicate I'm switching the relay off
nherriot 8:1b4e84f4c451 138
nherriot 8:1b4e84f4c451 139 }
nherriot 3:0a8eebcb0acf 140
nherriot 8:1b4e84f4c451 141 // check the state of the door and switch the LED light to 'ON' for door open and 'OFF' for door closed.
nherriot 10:c8a6f2822068 142 //if (doorProximitySwitch.read() == 0)
nherriot 10:c8a6f2822068 143 if (read_digital() == 0)
nherriot 8:1b4e84f4c451 144 {
nherriot 15:262da03e3e20 145 // DBG("The door proximity switch is: %d so I think it's open", doorProximitySwitch.read() );
nherriot 8:1b4e84f4c451 146 led1 = 1;
nherriot 3:0a8eebcb0acf 147 }
nherriot 8:1b4e84f4c451 148 else
nherriot 8:1b4e84f4c451 149 {
nherriot 15:262da03e3e20 150 // DBG("The door proximity switch is: %d so I think it's closed", doorProximitySwitch.read() );
nherriot 8:1b4e84f4c451 151 led1 = 0;
nherriot 8:1b4e84f4c451 152 }
nherriot 8:1b4e84f4c451 153
nherriot 8:1b4e84f4c451 154
nherriot 8:1b4e84f4c451 155
nherriot 7:316c2dac06a5 156 int ret = connectionManager.getSMCount(&count); // check to see if there is an incoming SMS message
nherriot 0:5136cdfaebd0 157 if(ret)
nherriot 0:5136cdfaebd0 158 {
nherriot 0:5136cdfaebd0 159 WARN("getSMCount returned %d", ret);
nherriot 10:c8a6f2822068 160 Thread::wait(125);
nherriot 0:5136cdfaebd0 161 continue;
nherriot 0:5136cdfaebd0 162 }
nherriot 10:c8a6f2822068 163
nherriot 10:c8a6f2822068 164 DBG("The SMS count is now at: %d for this modem", count);
nherriot 7:316c2dac06a5 165 if( count > 0) // if there are messages in the mailbox start pulling them off the queue
nherriot 0:5136cdfaebd0 166 {
nherriot 0:5136cdfaebd0 167 DBG("%d SMS to read", count);
nherriot 10:c8a6f2822068 168 ret = connectionManager.getSM(num, msg, 160);
nherriot 8:1b4e84f4c451 169
nherriot 0:5136cdfaebd0 170 if(ret)
nherriot 0:5136cdfaebd0 171 {
nherriot 0:5136cdfaebd0 172 WARN("getSM returned %d", ret);
nherriot 10:c8a6f2822068 173 Thread::wait(125);
nherriot 0:5136cdfaebd0 174 continue;
nherriot 0:5136cdfaebd0 175 }
nherriot 0:5136cdfaebd0 176
nherriot 6:5a892c3d738e 177 DBG("The message is from number: %s and the message is: \"%s\"", num, msg);
nherriot 6:5a892c3d738e 178
nherriot 15:262da03e3e20 179 // if the SMS is from your own number, ignore it!
nherriot 15:262da03e3e20 180 if(strcmp(num,"+447785666088")==0)
nherriot 15:262da03e3e20 181 {
nherriot 15:262da03e3e20 182 DBG("The message appears to be from my MSISDN number: %s, I'll do nothing.", num);
nherriot 15:262da03e3e20 183 continue;
nherriot 15:262da03e3e20 184 }
nherriot 15:262da03e3e20 185
nherriot 15:262da03e3e20 186
nherriot 15:262da03e3e20 187
nherriot 15:262da03e3e20 188 if ((strcmp (msg, "open") ==0)|| (strcmp(num,"TrackaPhone")==0))
nherriot 8:1b4e84f4c451 189 {
nherriot 2:bcc9256ecaab 190 DBG("The SMS message indicates I should open the door");
nherriot 7:316c2dac06a5 191
nherriot 7:316c2dac06a5 192 // check the door is not already open if it is do nothing and send a warning back to the sender
nherriot 7:316c2dac06a5 193 // 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 194
nherriot 15:262da03e3e20 195 if (read_digital() == 0 )
nherriot 7:316c2dac06a5 196 {
nherriot 7:316c2dac06a5 197 DBG("The door is already open - so I'll do nothing");
nherriot 8:1b4e84f4c451 198 connectionManager.sendSM(num, " WARNING - The door is already open sir! ");
nherriot 7:316c2dac06a5 199 }
nherriot 7:316c2dac06a5 200 else
nherriot 7:316c2dac06a5 201 {
nherriot 7:316c2dac06a5 202 doorContactorRelay = 1;
nherriot 8:1b4e84f4c451 203 openTheDoor = true; // set the state vairable to let the door contoller know it needs to open the door
nherriot 7:316c2dac06a5 204 led3 = 1;
nherriot 7:316c2dac06a5 205 DBG("The relay has been activated to open the door");
nherriot 8:1b4e84f4c451 206 connectionManager.sendSM(num, " Door open sir... Welcome home! " );
nherriot 8:1b4e84f4c451 207 }
nherriot 8:1b4e84f4c451 208 }
nherriot 8:1b4e84f4c451 209 else
nherriot 8:1b4e84f4c451 210 {
nherriot 2:bcc9256ecaab 211 DBG("The SMS message is not recognized");
nherriot 7:316c2dac06a5 212 connectionManager.sendSM(num, " Wrong password sir... Try 'open' and I'll open! ;-) ");
nherriot 2:bcc9256ecaab 213 }
nherriot 2:bcc9256ecaab 214
nherriot 2:bcc9256ecaab 215
nherriot 2:bcc9256ecaab 216
nherriot 0:5136cdfaebd0 217 }
nherriot 6:5a892c3d738e 218 Thread::wait(500);
nherriot 0:5136cdfaebd0 219 }
nherriot 0:5136cdfaebd0 220 }
nherriot 0:5136cdfaebd0 221
nherriot 0:5136cdfaebd0 222 void keepAlive(void const*) {
nherriot 0:5136cdfaebd0 223 while(1)
nherriot 0:5136cdfaebd0 224 {
nherriot 8:1b4e84f4c451 225 led2=!led2;
nherriot 0:5136cdfaebd0 226 Thread::wait(500);
nherriot 0:5136cdfaebd0 227 }
nherriot 0:5136cdfaebd0 228 }
nherriot 0:5136cdfaebd0 229
nherriot 0:5136cdfaebd0 230 void tick()
nherriot 0:5136cdfaebd0 231 {
nherriot 0:5136cdfaebd0 232 led4=!led4;
nherriot 0:5136cdfaebd0 233 }
nherriot 0:5136cdfaebd0 234
nherriot 0:5136cdfaebd0 235 int main() {
nherriot 16:9245c3a37491 236 //Ticker t;
nherriot 16:9245c3a37491 237 //t.attach(tick,1);
nherriot 0:5136cdfaebd0 238 DBG_INIT();
nherriot 12:e622e146b3cd 239 DBG_SET_SPEED(115200);
donatien 21:7068b39d3006 240 Thread testTask(test, NULL, osPriorityNormal, 1024*6);
nherriot 0:5136cdfaebd0 241
donatien 14:9ee94196b75d 242 keepAlive(NULL);
nherriot 0:5136cdfaebd0 243
nherriot 0:5136cdfaebd0 244
nherriot 0:5136cdfaebd0 245 return 0;
nherriot 0:5136cdfaebd0 246 }