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 13:18:02 2012 +0000
Revision:
20:27f878df6d14
Parent:
16:9245c3a37491
Parent:
14:9ee94196b75d
Child:
21:7068b39d3006
Merge

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