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.
main.cpp@14:9ee94196b75d, 2012-09-25 (annotated)
- Committer:
- donatien
- Date:
- Tue Sep 25 14:57:07 2012 +0000
- Revision:
- 14:9ee94196b75d
- Parent:
- 12:e622e146b3cd
- Child:
- 18:ad4ae0caf337
- Child:
- 20:27f878df6d14
Uncommented "keepAlive()" call in main to prevent from returning
Who changed what in which revision?
User | Revision | Line number | New 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 | 10:c8a6f2822068 | 51 | int read_digital(void) { |
nherriot | 10:c8a6f2822068 | 52 | DigitalIn mydigital(p6); |
nherriot | 10:c8a6f2822068 | 53 | return(mydigital.read()); |
nherriot | 10:c8a6f2822068 | 54 | } |
nherriot | 0:5136cdfaebd0 | 55 | |
nherriot | 0:5136cdfaebd0 | 56 | void test(void const*) { |
nherriot | 10:c8a6f2822068 | 57 | VodafoneUSBModem connectionManager; // create a connection manager object |
nherriot | 0:5136cdfaebd0 | 58 | |
nherriot | 1:3c9d0c9dffa4 | 59 | DBG("Hello world and Vodafone K3770 test program!"); |
nherriot | 0:5136cdfaebd0 | 60 | |
nherriot | 7:316c2dac06a5 | 61 | connectionManager.sendSM(MY_PHONE_NUMBER, "Hello from mbed door controller:)"); |
nherriot | 0:5136cdfaebd0 | 62 | |
nherriot | 10:c8a6f2822068 | 63 | char num[17]; // create a buffer to hold the telephone number in |
nherriot | 10:c8a6f2822068 | 64 | char msg[160]; // create a buffer to hold the text message in |
nherriot | 10:c8a6f2822068 | 65 | size_t count; |
nherriot | 10:c8a6f2822068 | 66 | |
nherriot | 0:5136cdfaebd0 | 67 | while(true) |
nherriot | 0:5136cdfaebd0 | 68 | { |
nherriot | 8:1b4e84f4c451 | 69 | |
nherriot | 8:1b4e84f4c451 | 70 | // if this state variable has been set I need to keep the doorContactor on until the door is open |
nherriot | 8:1b4e84f4c451 | 71 | if ((openTheDoor) || (doorProximitySwitch.read() == 0)) |
nherriot | 3:0a8eebcb0acf | 72 | { |
nherriot | 8:1b4e84f4c451 | 73 | |
nherriot | 8:1b4e84f4c451 | 74 | doorContactorRelay = 0; // door must be open now so switch relay off. |
nherriot | 8:1b4e84f4c451 | 75 | openTheDoor = false; // the door is open now so set it to false. |
nherriot | 8:1b4e84f4c451 | 76 | led3 = 0; // switch the LED light off to indicate I'm switching the relay off |
nherriot | 8:1b4e84f4c451 | 77 | |
nherriot | 8:1b4e84f4c451 | 78 | } |
nherriot | 3:0a8eebcb0acf | 79 | |
nherriot | 8:1b4e84f4c451 | 80 | // check the state of the door and switch the LED light to 'ON' for door open and 'OFF' for door closed. |
nherriot | 10:c8a6f2822068 | 81 | //if (doorProximitySwitch.read() == 0) |
nherriot | 10:c8a6f2822068 | 82 | if (read_digital() == 0) |
nherriot | 8:1b4e84f4c451 | 83 | { |
nherriot | 8:1b4e84f4c451 | 84 | DBG("The door proximity switch is: %d so I think it's open", doorProximitySwitch.read() ); |
nherriot | 8:1b4e84f4c451 | 85 | led1 = 1; |
nherriot | 3:0a8eebcb0acf | 86 | } |
nherriot | 8:1b4e84f4c451 | 87 | else |
nherriot | 8:1b4e84f4c451 | 88 | { |
nherriot | 8:1b4e84f4c451 | 89 | DBG("The door proximity switch is: %d so I think it's closed", doorProximitySwitch.read() ); |
nherriot | 8:1b4e84f4c451 | 90 | led1 = 0; |
nherriot | 8:1b4e84f4c451 | 91 | } |
nherriot | 8:1b4e84f4c451 | 92 | |
nherriot | 8:1b4e84f4c451 | 93 | |
nherriot | 8:1b4e84f4c451 | 94 | |
nherriot | 7:316c2dac06a5 | 95 | int ret = connectionManager.getSMCount(&count); // check to see if there is an incoming SMS message |
nherriot | 0:5136cdfaebd0 | 96 | if(ret) |
nherriot | 0:5136cdfaebd0 | 97 | { |
nherriot | 0:5136cdfaebd0 | 98 | WARN("getSMCount returned %d", ret); |
nherriot | 10:c8a6f2822068 | 99 | Thread::wait(125); |
nherriot | 0:5136cdfaebd0 | 100 | continue; |
nherriot | 0:5136cdfaebd0 | 101 | } |
nherriot | 10:c8a6f2822068 | 102 | |
nherriot | 10:c8a6f2822068 | 103 | DBG("The SMS count is now at: %d for this modem", count); |
nherriot | 7:316c2dac06a5 | 104 | if( count > 0) // if there are messages in the mailbox start pulling them off the queue |
nherriot | 0:5136cdfaebd0 | 105 | { |
nherriot | 0:5136cdfaebd0 | 106 | DBG("%d SMS to read", count); |
nherriot | 10:c8a6f2822068 | 107 | ret = connectionManager.getSM(num, msg, 160); |
nherriot | 8:1b4e84f4c451 | 108 | |
nherriot | 0:5136cdfaebd0 | 109 | if(ret) |
nherriot | 0:5136cdfaebd0 | 110 | { |
nherriot | 0:5136cdfaebd0 | 111 | WARN("getSM returned %d", ret); |
nherriot | 10:c8a6f2822068 | 112 | Thread::wait(125); |
nherriot | 0:5136cdfaebd0 | 113 | continue; |
nherriot | 0:5136cdfaebd0 | 114 | } |
nherriot | 0:5136cdfaebd0 | 115 | |
nherriot | 6:5a892c3d738e | 116 | DBG("The message is from number: %s and the message is: \"%s\"", num, msg); |
nherriot | 6:5a892c3d738e | 117 | |
nherriot | 8:1b4e84f4c451 | 118 | if (strcmp (msg, "open") ==0) |
nherriot | 8:1b4e84f4c451 | 119 | { |
nherriot | 2:bcc9256ecaab | 120 | DBG("The SMS message indicates I should open the door"); |
nherriot | 7:316c2dac06a5 | 121 | |
nherriot | 7:316c2dac06a5 | 122 | // check the door is not already open if it is do nothing and send a warning back to the sender |
nherriot | 7:316c2dac06a5 | 123 | // 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 | 124 | |
nherriot | 7:316c2dac06a5 | 125 | if ( doorProximitySwitch.read() == 0) |
nherriot | 7:316c2dac06a5 | 126 | { |
nherriot | 7:316c2dac06a5 | 127 | DBG("The door is already open - so I'll do nothing"); |
nherriot | 8:1b4e84f4c451 | 128 | connectionManager.sendSM(num, " WARNING - The door is already open sir! "); |
nherriot | 7:316c2dac06a5 | 129 | } |
nherriot | 7:316c2dac06a5 | 130 | else |
nherriot | 7:316c2dac06a5 | 131 | { |
nherriot | 7:316c2dac06a5 | 132 | doorContactorRelay = 1; |
nherriot | 8:1b4e84f4c451 | 133 | openTheDoor = true; // set the state vairable to let the door contoller know it needs to open the door |
nherriot | 7:316c2dac06a5 | 134 | led3 = 1; |
nherriot | 7:316c2dac06a5 | 135 | DBG("The relay has been activated to open the door"); |
nherriot | 8:1b4e84f4c451 | 136 | connectionManager.sendSM(num, " Door open sir... Welcome home! " ); |
nherriot | 8:1b4e84f4c451 | 137 | } |
nherriot | 8:1b4e84f4c451 | 138 | } |
nherriot | 8:1b4e84f4c451 | 139 | else |
nherriot | 8:1b4e84f4c451 | 140 | { |
nherriot | 2:bcc9256ecaab | 141 | DBG("The SMS message is not recognized"); |
nherriot | 7:316c2dac06a5 | 142 | connectionManager.sendSM(num, " Wrong password sir... Try 'open' and I'll open! ;-) "); |
nherriot | 2:bcc9256ecaab | 143 | } |
nherriot | 2:bcc9256ecaab | 144 | |
nherriot | 2:bcc9256ecaab | 145 | |
nherriot | 2:bcc9256ecaab | 146 | |
nherriot | 0:5136cdfaebd0 | 147 | } |
nherriot | 6:5a892c3d738e | 148 | Thread::wait(500); |
nherriot | 0:5136cdfaebd0 | 149 | } |
nherriot | 0:5136cdfaebd0 | 150 | |
nherriot | 0:5136cdfaebd0 | 151 | } |
nherriot | 0:5136cdfaebd0 | 152 | |
nherriot | 0:5136cdfaebd0 | 153 | void keepAlive(void const*) { |
nherriot | 0:5136cdfaebd0 | 154 | while(1) |
nherriot | 0:5136cdfaebd0 | 155 | { |
nherriot | 8:1b4e84f4c451 | 156 | led2=!led2; |
nherriot | 0:5136cdfaebd0 | 157 | Thread::wait(500); |
nherriot | 0:5136cdfaebd0 | 158 | } |
nherriot | 0:5136cdfaebd0 | 159 | } |
nherriot | 0:5136cdfaebd0 | 160 | |
nherriot | 0:5136cdfaebd0 | 161 | void tick() |
nherriot | 0:5136cdfaebd0 | 162 | { |
nherriot | 0:5136cdfaebd0 | 163 | led4=!led4; |
nherriot | 0:5136cdfaebd0 | 164 | } |
nherriot | 0:5136cdfaebd0 | 165 | |
nherriot | 0:5136cdfaebd0 | 166 | int main() { |
nherriot | 0:5136cdfaebd0 | 167 | Ticker t; |
nherriot | 0:5136cdfaebd0 | 168 | t.attach(tick,1); |
nherriot | 0:5136cdfaebd0 | 169 | DBG_INIT(); |
nherriot | 12:e622e146b3cd | 170 | DBG_SET_SPEED(115200); |
nherriot | 0:5136cdfaebd0 | 171 | |
nherriot | 12:e622e146b3cd | 172 | Thread testTask(test, NULL, osPriorityNormal, 1024*5); |
donatien | 14:9ee94196b75d | 173 | keepAlive(NULL); |
nherriot | 0:5136cdfaebd0 | 174 | |
nherriot | 0:5136cdfaebd0 | 175 | |
nherriot | 0:5136cdfaebd0 | 176 | return 0; |
nherriot | 0:5136cdfaebd0 | 177 | } |