![](/media/cache/profiles/e1c599867ee5449a59e6c638200a3d92.50x50_q85.jpg)
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@6:5a892c3d738e, 2012-07-25 (annotated)
- Committer:
- nherriot
- Date:
- Wed Jul 25 10:33:28 2012 +0000
- Revision:
- 6:5a892c3d738e
- Parent:
- 5:ca4f06634bcb
- Child:
- 7:316c2dac06a5
removing redundant code.
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 | 6:5a892c3d738e | 34 | #include "VodafoneK3770.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 | 0:5136cdfaebd0 | 42 | DigitalOut led1(LED1); |
nherriot | 0:5136cdfaebd0 | 43 | DigitalOut led2(LED2); |
nherriot | 0:5136cdfaebd0 | 44 | DigitalOut led3(LED3); |
nherriot | 0:5136cdfaebd0 | 45 | DigitalOut led4(LED4); |
nherriot | 3:0a8eebcb0acf | 46 | DigitalOut doorOpen(p5); |
nherriot | 3:0a8eebcb0acf | 47 | |
nherriot | 0:5136cdfaebd0 | 48 | |
nherriot | 0:5136cdfaebd0 | 49 | void test(void const*) { |
nherriot | 0:5136cdfaebd0 | 50 | VodafoneK3770 threeg; |
nherriot | 0:5136cdfaebd0 | 51 | |
nherriot | 1:3c9d0c9dffa4 | 52 | DBG("Hello world and Vodafone K3770 test program!"); |
nherriot | 0:5136cdfaebd0 | 53 | |
nherriot | 0:5136cdfaebd0 | 54 | threeg.sendSM(MY_PHONE_NUMBER, "Hello from mbed:)"); |
nherriot | 0:5136cdfaebd0 | 55 | |
nherriot | 0:5136cdfaebd0 | 56 | while(true) |
nherriot | 0:5136cdfaebd0 | 57 | { |
nherriot | 0:5136cdfaebd0 | 58 | char num[17]; |
nherriot | 0:5136cdfaebd0 | 59 | char msg[64]; |
nherriot | 0:5136cdfaebd0 | 60 | size_t count; |
nherriot | 3:0a8eebcb0acf | 61 | if (doorOpen) |
nherriot | 3:0a8eebcb0acf | 62 | { |
nherriot | 3:0a8eebcb0acf | 63 | doorOpen = 0; |
nherriot | 4:99bbdfe42ae0 | 64 | led3=0; |
nherriot | 3:0a8eebcb0acf | 65 | |
nherriot | 3:0a8eebcb0acf | 66 | } |
nherriot | 0:5136cdfaebd0 | 67 | int ret = threeg.getSMCount(&count); |
nherriot | 0:5136cdfaebd0 | 68 | if(ret) |
nherriot | 0:5136cdfaebd0 | 69 | { |
nherriot | 0:5136cdfaebd0 | 70 | WARN("getSMCount returned %d", ret); |
nherriot | 0:5136cdfaebd0 | 71 | Thread::wait(3000); |
nherriot | 0:5136cdfaebd0 | 72 | continue; |
nherriot | 0:5136cdfaebd0 | 73 | } |
nherriot | 0:5136cdfaebd0 | 74 | if( count > 0) |
nherriot | 0:5136cdfaebd0 | 75 | { |
nherriot | 0:5136cdfaebd0 | 76 | DBG("%d SMS to read", count); |
nherriot | 0:5136cdfaebd0 | 77 | ret = threeg.getSM(num, msg, 64); |
nherriot | 0:5136cdfaebd0 | 78 | if(ret) |
nherriot | 0:5136cdfaebd0 | 79 | { |
nherriot | 0:5136cdfaebd0 | 80 | WARN("getSM returned %d", ret); |
nherriot | 0:5136cdfaebd0 | 81 | Thread::wait(3000); |
nherriot | 0:5136cdfaebd0 | 82 | continue; |
nherriot | 0:5136cdfaebd0 | 83 | } |
nherriot | 0:5136cdfaebd0 | 84 | |
nherriot | 6:5a892c3d738e | 85 | DBG("The message is from number: %s and the message is: \"%s\"", num, msg); |
nherriot | 6:5a892c3d738e | 86 | if(strcmp(num,"+447836503274")==0) { |
nherriot | 6:5a892c3d738e | 87 | threeg.sendSM("07836503274","SPAM ALERT! UNKNOWN NUMBER"); |
nherriot | 6:5a892c3d738e | 88 | threeg.sendSM("07836503274","INITIATING DEFENSE MECHANISM: 1 DAY OF RETALIATION SMS"); |
nherriot | 6:5a892c3d738e | 89 | for(int i=0; i<10; i++) { |
nherriot | 6:5a892c3d738e | 90 | threeg.sendSM("07836503274","ANTI-SPAM SMS PUNISHMENT. DON'T SPAM THE DOOR!!"); |
nherriot | 6:5a892c3d738e | 91 | Thread:wait(10000); |
nherriot | 6:5a892c3d738e | 92 | } |
nherriot | 6:5a892c3d738e | 93 | } |
nherriot | 6:5a892c3d738e | 94 | |
nherriot | 6:5a892c3d738e | 95 | if (strcmp (msg, "open") ==0) { |
nherriot | 2:bcc9256ecaab | 96 | DBG("The SMS message indicates I should open the door"); |
nherriot | 2:bcc9256ecaab | 97 | // do something to open the door |
nherriot | 3:0a8eebcb0acf | 98 | doorOpen = 1; |
nherriot | 5:ca4f06634bcb | 99 | led3 = 1; |
nherriot | 6:5a892c3d738e | 100 | threeg.sendSM(num, " Door open sir... Welcome home! "); |
nherriot | 6:5a892c3d738e | 101 | } else { |
nherriot | 2:bcc9256ecaab | 102 | DBG("The SMS message is not recognized"); |
nherriot | 6:5a892c3d738e | 103 | threeg.sendSM(num, " Wrong password sir... Try 'open' and I'll open! ;-) "); |
nherriot | 2:bcc9256ecaab | 104 | } |
nherriot | 2:bcc9256ecaab | 105 | |
nherriot | 2:bcc9256ecaab | 106 | |
nherriot | 2:bcc9256ecaab | 107 | |
nherriot | 0:5136cdfaebd0 | 108 | } |
nherriot | 6:5a892c3d738e | 109 | Thread::wait(500); |
nherriot | 0:5136cdfaebd0 | 110 | } |
nherriot | 0:5136cdfaebd0 | 111 | |
nherriot | 0:5136cdfaebd0 | 112 | } |
nherriot | 0:5136cdfaebd0 | 113 | |
nherriot | 0:5136cdfaebd0 | 114 | void keepAlive(void const*) { |
nherriot | 0:5136cdfaebd0 | 115 | while(1) |
nherriot | 0:5136cdfaebd0 | 116 | { |
nherriot | 0:5136cdfaebd0 | 117 | led1=!led1; |
nherriot | 0:5136cdfaebd0 | 118 | Thread::wait(500); |
nherriot | 0:5136cdfaebd0 | 119 | } |
nherriot | 0:5136cdfaebd0 | 120 | } |
nherriot | 0:5136cdfaebd0 | 121 | |
nherriot | 0:5136cdfaebd0 | 122 | void tick() |
nherriot | 0:5136cdfaebd0 | 123 | { |
nherriot | 0:5136cdfaebd0 | 124 | led4=!led4; |
nherriot | 0:5136cdfaebd0 | 125 | } |
nherriot | 0:5136cdfaebd0 | 126 | |
nherriot | 0:5136cdfaebd0 | 127 | int main() { |
nherriot | 0:5136cdfaebd0 | 128 | Ticker t; |
nherriot | 0:5136cdfaebd0 | 129 | t.attach(tick,1); |
nherriot | 0:5136cdfaebd0 | 130 | DBG_INIT(); |
nherriot | 0:5136cdfaebd0 | 131 | |
nherriot | 0:5136cdfaebd0 | 132 | Thread testTask(test, NULL, osPriorityNormal, 1024*4); |
nherriot | 6:5a892c3d738e | 133 | // keepAlive(NULL); |
nherriot | 0:5136cdfaebd0 | 134 | |
nherriot | 0:5136cdfaebd0 | 135 | |
nherriot | 0:5136cdfaebd0 | 136 | return 0; |
nherriot | 0:5136cdfaebd0 | 137 | } |