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 Sep 25 10:01:54 2012 +0000
Revision:
15:262da03e3e20
Parent:
12:e622e146b3cd
Child:
16:9245c3a37491
Adding a reg state section of code. This makes the door controller continually monitor state before trying to send a message to the modem.

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 0:5136cdfaebd0 29 #define MY_PHONE_NUMBER "+447717275049"
nherriot 0:5136cdfaebd0 30
nherriot 0:5136cdfaebd0 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 7:316c2dac06a5 43 DigitalOut led2(LED2);
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 15:262da03e3e20 73 // before we do anything we need to make sure we have a connection to a network.
nherriot 15:262da03e3e20 74 // so let us check that first!
nherriot 15:262da03e3e20 75
nherriot 15:262da03e3e20 76 while(detectRegState)
nherriot 15:262da03e3e20 77 {
nherriot 15:262da03e3e20 78 if(connectionManager.getLinkState(&rssi, &regState, &bearer)==0)
nherriot 15:262da03e3e20 79 {
nherriot 15:262da03e3e20 80 if(rssi==-1000)
nherriot 15:262da03e3e20 81 {
nherriot 15:262da03e3e20 82 DBG("Checking signal strength - RSSI: Error.");
nherriot 15:262da03e3e20 83 }
nherriot 15:262da03e3e20 84 else
nherriot 15:262da03e3e20 85 {
nherriot 15:262da03e3e20 86 DBG("Signal strength is: RSSI: %d",rssi);
nherriot 15:262da03e3e20 87 }
nherriot 15:262da03e3e20 88
nherriot 15:262da03e3e20 89 switch(regState)
nherriot 15:262da03e3e20 90 {
nherriot 15:262da03e3e20 91 case LinkMonitor::REGISTRATION_STATE_UNKNOWN:
nherriot 15:262da03e3e20 92 DBG("regState: UNKNOWN. Failing.");
nherriot 15:262da03e3e20 93 break;
nherriot 15:262da03e3e20 94 case LinkMonitor::REGISTRATION_STATE_REGISTERING:
nherriot 15:262da03e3e20 95 DBG("regState: REGISTERING");
nherriot 15:262da03e3e20 96 break;
nherriot 15:262da03e3e20 97 case LinkMonitor::REGISTRATION_STATE_DENIED:
nherriot 15:262da03e3e20 98 DBG("regState: DENIED");
nherriot 15:262da03e3e20 99 break;
nherriot 15:262da03e3e20 100 case LinkMonitor::REGISTRATION_STATE_NO_SIGNAL:
nherriot 15:262da03e3e20 101 DBG("regState: NO SIGNAL");
nherriot 15:262da03e3e20 102 break;
nherriot 15:262da03e3e20 103 case LinkMonitor::REGISTRATION_STATE_HOME_NETWORK:
nherriot 15:262da03e3e20 104 detectRegState = false;
nherriot 15:262da03e3e20 105 DBG("regState: HOME NETWORK");
nherriot 15:262da03e3e20 106 break;
nherriot 15:262da03e3e20 107 case LinkMonitor::REGISTRATION_STATE_ROAMING:
nherriot 15:262da03e3e20 108 detectRegState = false;
nherriot 15:262da03e3e20 109 DBG("regState: ROAMING");
nherriot 15:262da03e3e20 110 break;
nherriot 15:262da03e3e20 111 default:
nherriot 15:262da03e3e20 112 DBG("regState: ERROR. Failing.");
nherriot 15:262da03e3e20 113 break;
nherriot 15:262da03e3e20 114 }
nherriot 15:262da03e3e20 115 }
nherriot 15:262da03e3e20 116 }
nherriot 15:262da03e3e20 117
nherriot 15:262da03e3e20 118
nherriot 15:262da03e3e20 119
nherriot 15:262da03e3e20 120
nherriot 15:262da03e3e20 121
nherriot 15:262da03e3e20 122 connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from mbed door controller:)");
nherriot 15:262da03e3e20 123
nherriot 15:262da03e3e20 124
nherriot 10:c8a6f2822068 125
nherriot 0:5136cdfaebd0 126 while(true)
nherriot 0:5136cdfaebd0 127 {
nherriot 8:1b4e84f4c451 128
nherriot 8:1b4e84f4c451 129 // if this state variable has been set I need to keep the doorContactor on until the door is open
nherriot 15:262da03e3e20 130 if ((openTheDoor) && (read_digital() == 0))
nherriot 3:0a8eebcb0acf 131 {
nherriot 15:262da03e3e20 132 DBG("The door is open now and I'm switching off the relay and reseting the state variable");
nherriot 8:1b4e84f4c451 133 doorContactorRelay = 0; // door must be open now so switch relay off.
nherriot 8:1b4e84f4c451 134 openTheDoor = false; // the door is open now so set it to false.
nherriot 8:1b4e84f4c451 135 led3 = 0; // switch the LED light off to indicate I'm switching the relay off
nherriot 8:1b4e84f4c451 136
nherriot 8:1b4e84f4c451 137 }
nherriot 3:0a8eebcb0acf 138
nherriot 8:1b4e84f4c451 139 // check the state of the door and switch the LED light to 'ON' for door open and 'OFF' for door closed.
nherriot 10:c8a6f2822068 140 //if (doorProximitySwitch.read() == 0)
nherriot 10:c8a6f2822068 141 if (read_digital() == 0)
nherriot 8:1b4e84f4c451 142 {
nherriot 15:262da03e3e20 143 // DBG("The door proximity switch is: %d so I think it's open", doorProximitySwitch.read() );
nherriot 8:1b4e84f4c451 144 led1 = 1;
nherriot 3:0a8eebcb0acf 145 }
nherriot 8:1b4e84f4c451 146 else
nherriot 8:1b4e84f4c451 147 {
nherriot 15:262da03e3e20 148 // DBG("The door proximity switch is: %d so I think it's closed", doorProximitySwitch.read() );
nherriot 8:1b4e84f4c451 149 led1 = 0;
nherriot 8:1b4e84f4c451 150 }
nherriot 8:1b4e84f4c451 151
nherriot 8:1b4e84f4c451 152
nherriot 8:1b4e84f4c451 153
nherriot 7:316c2dac06a5 154 int ret = connectionManager.getSMCount(&count); // check to see if there is an incoming SMS message
nherriot 0:5136cdfaebd0 155 if(ret)
nherriot 0:5136cdfaebd0 156 {
nherriot 0:5136cdfaebd0 157 WARN("getSMCount returned %d", ret);
nherriot 10:c8a6f2822068 158 Thread::wait(125);
nherriot 0:5136cdfaebd0 159 continue;
nherriot 0:5136cdfaebd0 160 }
nherriot 10:c8a6f2822068 161
nherriot 15:262da03e3e20 162 // DBG("The SMS count is now at: %d for this modem", count);
nherriot 7:316c2dac06a5 163 if( count > 0) // if there are messages in the mailbox start pulling them off the queue
nherriot 0:5136cdfaebd0 164 {
nherriot 0:5136cdfaebd0 165 DBG("%d SMS to read", count);
nherriot 10:c8a6f2822068 166 ret = connectionManager.getSM(num, msg, 160);
nherriot 8:1b4e84f4c451 167
nherriot 0:5136cdfaebd0 168 if(ret)
nherriot 0:5136cdfaebd0 169 {
nherriot 0:5136cdfaebd0 170 WARN("getSM returned %d", ret);
nherriot 10:c8a6f2822068 171 Thread::wait(125);
nherriot 0:5136cdfaebd0 172 continue;
nherriot 0:5136cdfaebd0 173 }
nherriot 0:5136cdfaebd0 174
nherriot 6:5a892c3d738e 175 DBG("The message is from number: %s and the message is: \"%s\"", num, msg);
nherriot 6:5a892c3d738e 176
nherriot 15:262da03e3e20 177 // if the SMS is from your own number, ignore it!
nherriot 15:262da03e3e20 178 if(strcmp(num,"+447785666088")==0)
nherriot 15:262da03e3e20 179 {
nherriot 15:262da03e3e20 180 DBG("The message appears to be from my MSISDN number: %s, I'll do nothing.", num);
nherriot 15:262da03e3e20 181 continue;
nherriot 15:262da03e3e20 182 }
nherriot 15:262da03e3e20 183
nherriot 15:262da03e3e20 184
nherriot 15:262da03e3e20 185
nherriot 15:262da03e3e20 186 if ((strcmp (msg, "open") ==0)|| (strcmp(num,"TrackaPhone")==0))
nherriot 8:1b4e84f4c451 187 {
nherriot 2:bcc9256ecaab 188 DBG("The SMS message indicates I should open the door");
nherriot 7:316c2dac06a5 189
nherriot 7:316c2dac06a5 190 // check the door is not already open if it is do nothing and send a warning back to the sender
nherriot 7:316c2dac06a5 191 // 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 192
nherriot 15:262da03e3e20 193 if (read_digital() == 0 )
nherriot 7:316c2dac06a5 194 {
nherriot 7:316c2dac06a5 195 DBG("The door is already open - so I'll do nothing");
nherriot 8:1b4e84f4c451 196 connectionManager.sendSM(num, " WARNING - The door is already open sir! ");
nherriot 7:316c2dac06a5 197 }
nherriot 7:316c2dac06a5 198 else
nherriot 7:316c2dac06a5 199 {
nherriot 7:316c2dac06a5 200 doorContactorRelay = 1;
nherriot 8:1b4e84f4c451 201 openTheDoor = true; // set the state vairable to let the door contoller know it needs to open the door
nherriot 7:316c2dac06a5 202 led3 = 1;
nherriot 7:316c2dac06a5 203 DBG("The relay has been activated to open the door");
nherriot 8:1b4e84f4c451 204 connectionManager.sendSM(num, " Door open sir... Welcome home! " );
nherriot 8:1b4e84f4c451 205 }
nherriot 8:1b4e84f4c451 206 }
nherriot 8:1b4e84f4c451 207 else
nherriot 8:1b4e84f4c451 208 {
nherriot 2:bcc9256ecaab 209 DBG("The SMS message is not recognized");
nherriot 7:316c2dac06a5 210 connectionManager.sendSM(num, " Wrong password sir... Try 'open' and I'll open! ;-) ");
nherriot 2:bcc9256ecaab 211 }
nherriot 2:bcc9256ecaab 212
nherriot 2:bcc9256ecaab 213
nherriot 2:bcc9256ecaab 214
nherriot 0:5136cdfaebd0 215 }
nherriot 6:5a892c3d738e 216 Thread::wait(500);
nherriot 0:5136cdfaebd0 217 }
nherriot 0:5136cdfaebd0 218
nherriot 0:5136cdfaebd0 219 }
nherriot 0:5136cdfaebd0 220
nherriot 0:5136cdfaebd0 221 void keepAlive(void const*) {
nherriot 0:5136cdfaebd0 222 while(1)
nherriot 0:5136cdfaebd0 223 {
nherriot 8:1b4e84f4c451 224 led2=!led2;
nherriot 0:5136cdfaebd0 225 Thread::wait(500);
nherriot 0:5136cdfaebd0 226 }
nherriot 0:5136cdfaebd0 227 }
nherriot 0:5136cdfaebd0 228
nherriot 0:5136cdfaebd0 229 void tick()
nherriot 0:5136cdfaebd0 230 {
nherriot 0:5136cdfaebd0 231 led4=!led4;
nherriot 0:5136cdfaebd0 232 }
nherriot 0:5136cdfaebd0 233
nherriot 0:5136cdfaebd0 234 int main() {
nherriot 0:5136cdfaebd0 235 Ticker t;
nherriot 0:5136cdfaebd0 236 t.attach(tick,1);
nherriot 0:5136cdfaebd0 237 DBG_INIT();
nherriot 12:e622e146b3cd 238 DBG_SET_SPEED(115200);
nherriot 0:5136cdfaebd0 239
nherriot 12:e622e146b3cd 240 Thread testTask(test, NULL, osPriorityNormal, 1024*5);
nherriot 6:5a892c3d738e 241 // keepAlive(NULL);
nherriot 0:5136cdfaebd0 242
nherriot 0:5136cdfaebd0 243
nherriot 0:5136cdfaebd0 244 return 0;
nherriot 0:5136cdfaebd0 245 }