My Simple Alarm System (With Email Alerts)

cabletech

Member
Here's my simple project. For now, I'm using an old PC case to hold it, with the PC power supply (which will work quite well, supplying +12V and +5V to drive the relays, siren, and power the Webcontrol). Power consumption (according to my kill-a-watt) is 7-8 watts, including the PC power supply fan (which I plan to remove). It's a Compaq unit, ran a Pentium 133MHz (circa 1996).
 
I made a little circuit board with a female header to plug in to the Webcontrol, and on that PCB are screw terminals for the analogs, digitals, and screw terminals for the +12V in. The board also has a 7809 voltage regulator, so the output of the VR is jumpered down to the input of the Webcontrol.
 
I bought a relay board on ebay, and it works fine. I forget which one, it came from China. Relays are driven by +5DC.
 
I signed up for a gmx.us email address, and they have free SMTP. So I can send by that (but if you have a bug in your program and try to send too many, they will cut you off for a few hours. Yes, I know this... ;) ).
 
A better way to test is to use Papercut - it's an SMTP emulator, that will run on your PC and 'accept' your email, and not forward it. http://papercut.codeplex.com/
 
I also found Mady MTA v 2.3 - it's a SMTP relay that you can install on your PC, and act as an intermediary while you test your board. 2.3 is an older free version, they changed to a pay-for version with the newer ones. You can watch it relay from the Webcontrol, through your PC, and then on through a 'real' ISP so the email actually goes out.
 
The rest of the programming was pretty simple. Here it is, with comments. Let me know any way I can clean up this code. I'm not a programmer, I'm a hack. I do a little of everything, but don't try to specialize in anything. ;)
 
Note that the comments are not Webcontrol friendly; I did them in Notepad after the fact. The program does work fine (though I'll probably add some to it in the future, like the garage door raise/lower).
 
These are my variables / inputs / outputs:
 
VAR1:
    0=Alarm Enabled
    1=Alarm Disabled
VAR2:
    0=Door Email Not Sent
    1=Door Email Sent
VAR3:
    0=Window Email Not Sent
    1=Window Email Sent
VAR4:
    0=Motion Detector Email Not Sent
    1=Motion Detector Email Sent

OP1: Rear Light
OP2: Front Light
OP8: Siren
OP7: Remote Enable/Disable

IP1: Back Door
IP2: Front Door
IP3: Garage Door
IP4: Window 1
IP5: Window 2
IP6: Window 3
IP7: Motion Detector
IP8: Enable / Disable

All inputs "normally high", "low" for alarm

//////////////////////////////////////////////////////////////////////////////////////

Program with comments (cannot put comments in code, so this is not copy/paste ready)


START    
    CALLSUB INIT_OUT   ;Initialize the output contacts, set memory values to 0
LOOP:
    CALLSUB CHECK_DIS  ;Check the status of the disable input (it controls VAR1)
    TSTEQ VAR1 0         ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_DOORS   ;If it is, check the door status inputs (1-3)
    TSTEQ VAR1 0         ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_WINDOW  ;If it is, check the window status inputs (4-6)
    TSTEQ VAR1 0       ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_MOTION  ;If it is, check the motion status input (7)
    TSTEQ VAR1 1       ;See if alarm is disabled (VAR1=1)
    CALLSUB DISAB_ALM  ;If it is, disable the output relays
    GOTO LOOP          ;Back to the start of the loop
END    

INIT_OUT:                  ;Intialize the outputs and variables / open relays, clear all
    SET OP1 0  
    SET OP2 0  
    SET OP8 0  
    SET OP7 0  
    SET VAR1 0  
    SET VAR2 0  
    SET VAR3 0  
    SET VAR4 0  
    RET    

CHECK_DIS:
    TSTEQ IP8 0        ;See if disable input = 'low'
    DELAY 50          ;If it is, wait for 1/2 second for debounce
    TSTEQ IP8 0         ;Check it again
    SET VAR1 1         ;If it is still low, set VAR1=1 (disabling alarm)
    TSTEQ IP8 0         ;OK, is it still low a few cycles later?
    CALLSUB CLEAR_EM   ;Probably is, so let's clear the flags from sending emails
    TSTEQ IP8 1        ;Check to see if input = 'high'
    SET VAR1 0         ;If it is, turn alarm back on (VAR1=0)
    RET    

CLEAR_EM:                  ;Clear 'email sent' flags
    SET VAR2 0  
    SET VAR3 0  
    SET VAR4 0  
    RET    

DISAB_ALM:                 ;Clear relay statuses / turn lights and alarm siren off
    SET OP1 0  
    SET OP2 0  
    SET OP8 0  
    DELAY 100          ;Wait for one second to allow for debounce
    RET    

CK_DOORS:                  ;Check door statuses - if 'low', call alarm and send email 1
    TSTEQ IP1 0  
    CALLSUB ALARM   
    TSTEQ IP2 0  
    CALLSUB ALARM   
    TSTEQ IP3 0  
    CALLSUB ALARM   
    TSTEQ IP1 0  
    CALLSUB EMAIL1   
    TSTEQ IP2 0  
    CALLSUB EMAIL1   
    TSTEQ IP3 0  
    CALLSUB EMAIL1   
    RET    

CK_WINDOW:                 ;Check window statuses - if 'low', call alarm and send email 2
    TSTEQ IP4 0  
    CALLSUB ALARM   
    TSTEQ IP5 0  
    CALLSUB ALARM   
    TSTEQ IP6 0  
    CALLSUB ALARM   
    TSTEQ IP4 0  
    CALLSUB EMAIL2   
    TSTEQ IP5 0  
    CALLSUB EMAIL2   
    TSTEQ IP6 0  
    CALLSUB EMAIL2   
    RET    

CK_MOTION:                 ;Check motion status - if 'low', call alarm and send email 3
    TSTEQ IP7 0  
    CALLSUB ALARM   
    TSTEQ IP7 0  
    CALLSUB EMAIL3   
    RET    

ALARM:                     ;Energize outputs 1, 2, and 8
    SET OP1 1  
    SET OP2 1  
    SET OP8 1  
    RET    

EMAIL1:
    TSTEQ VAR2 0       ;Is VAR2=0? (Email is 'unsent')
    EMAIL EM1          ;If it is, send email (EM1 = Door Alarm)
    SET VAR2 1         ;Set VAR2=1 (Email is now 'sent')
    RET    

EMAIL2:
    TSTEQ VAR3 0  
    EMAIL EM2          ;EM2=Window Alarm
    SET VAR3 1  
    RET    

EMAIL3:
    TSTEQ VAR4 0  
    EMAIL EM3          ;EM3=Motion Alarm
    SET VAR4 1  
    RET
 
 
I am working on alarm system to,can't you do TSTEQ VAR1 0  once 
 
START    
    CALLSUB INIT_OUT   ;Initialize the output contacts, set memory values to 0
LOOP:
    CALLSUB CHECK_DIS  ;Check the status of the disable input (it controls VAR1)
    TSTEQ VAR1 0         ;See if alarm is enabled (VAR1=0)     do this once
    CALLSUB CK_DOORS   ;If it is, check the door status inputs (1-3)
    TSTEQ VAR1 0         ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_WINDOW  ;If it is, check the window status inputs (4-6)
    TSTEQ VAR1 0       ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_MOTION  ;If it is, check the motion status input (7)
    TSTEQ VAR1 1       ;See if alarm is disabled (VAR1=1)
    CALLSUB DISAB_ALM  ;If it is, disable the output relays
    GOTO LOOP          ;Back to the start of the loop
END      
 
Not for my purpose. The TESTEQ command has the following characteristic:   "If test evaluates to false then the next instruction is skipped." (which implies that the instruction after the next one is executed no matter what)

  So, if the alarm is enabled ("statement is true"), then it runs the next line of code (checking door, window, motion). If the alarm is disabled ("statement is false"), then it skips the next line of code. If I remove the TESTEQ in front of CK_WINDOW and CK_MOTION, it's always going to run that code, whether or not the alarm is enabled or disabled. So, I could have a disabled alarm, but still turn on the siren and send an email if I remove the checks. Make sense?  

I'd really rather do it your way - have a full if... then with a big bracket enclosing all of my checks. But I don't see a way to do that within the constrants of the PLC language. So I do a TESTEQ individually. This is the philosophy that I took throughout all of the code.   One nice thing is that we're not under any time constraints here - so a few cycles lost here or there with these checks really doesn't matter.

I forgot to mention how the 'disable alarm' works. A +5V signal goes through relay 7 (normally closed contact) and an external normally closed switch (hidden) and lands on Input 8. If you remotely activate the relay, and/or open the switch, 5V drops out and the WebControl alarm is disabled (hence my strong interest in the Android app - I can use the app to disable / enable the alarm from outside via my phone & WiFi).
 
I understand now very nice,did you take a look at this.  My code for hottub(old hottub) sends alarm (text) to my android phone if water to hot,cold,ect..   I built web page so if alarm resets it will show up on web page that there was a alarm and i have to reset it manually. Am going to do same thing for alarm

[sharedmedia=gallery:images:535]
[sharedmedia=gallery:images:536]
 
 
cabletech said:
I'd really rather do it your way - have a full if... then with a big bracket enclosing all of my checks. But I don't see a way to do that within the constrants of the PLC language
 
START    
    CALLSUB INIT_OUT   ;Initialize the output contacts, set memory values to 0
LOOP:
    CALLSUB CHECK_DIS  ;Check the status of the disable input (it controls VAR1)
    TSTEQ VAR1 0         ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_DOORS   ;If it is, check the door status inputs (1-3)
    TSTEQ VAR1 0         ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_WINDOW  ;If it is, check the window status inputs (4-6)
    TSTEQ VAR1 0       ;See if alarm is enabled (VAR1=0)
    CALLSUB CK_MOTION  ;If it is, check the motion status input (7)
    TSTEQ VAR1 1       ;See if alarm is disabled (VAR1=1)
    CALLSUB DISAB_ALM  ;If it is, disable the output relays
    GOTO LOOP          ;Back to the start of the loop
END  
 
 
How about:

Code:
START    
    CALLSUB INIT_OUT   ;Initialize the output contacts, set memory values to 0
LOOP:
    CALLSUB CHECK_DIS  ;Check the status of the disable input (it controls VAR1)
    TSTNE VAR1 0         ;See if alarm is enabled (VAR1=0)
    GOTO NOTENABLED
    CALLSUB CK_DOORS   ;If it is, check the door status inputs (1-3)
    CALLSUB CK_WINDOW  ;If it is, check the window status inputs (4-6)
    CALLSUB CK_MOTION  ;If it is, check the motion status input (7)
NOTENABLED:
    TSTEQ VAR1 1       ;See if alarm is disabled (VAR1=1)
    CALLSUB DISAB_ALM  ;If it is, disable the output relays
    GOTO LOOP          ;Back to the start of the loop
END  
 
HI! very interrested.
I'm not a programmer, but I try to understand what plc language is. Try to do something similar but I need to understand more....
 
 
Can you tell me what is supposed to do exactly, each inputs etc...
 
 
thx
 
BTCAD said:
HI! very interrested.
I'm not a programmer, but I try to understand what plc language is. Try to do something similar but I need to understand more....
 
 
Can you tell me what is supposed to do exactly, each inputs etc...
 
 
thx
PLC programming is very simple, the simplest one is
begin
end
then you can add additional line to it.  If you don't have motor or load connected to the output, coding mistake will not cause any problem.  You can paste into the web GUI let the WebControl compile you PLC code.  You can check input state, you can turn on or off output, you can send email or http requests all by simple one line PLC command.
There are condition operators, you can use them to define your logic flow. 
Don't want to hijack this subject to something else. But if you have PLC questions, you can start new topics to discuss on this forum.
 
Hey pittom,
 
My doors are wired (well, going to be). I'm going to put a current limiting resistor on the +5V send (haven't decided the value yet, but it'll probably just be something from my grab bag, 220 ohm, 1k, whatever works), and then loop through a closed-when-closed magnetic proximity contact. So if the door opens, the contact opens, and the WebControl signal goes low - and my alarm / email is activated.
 
As for documenting more - well, I don't know what else to add. In my post, I outline the inputs (IP1 through IP8), the outputs, and then all of my code is commented there.
 
You can copy my code above in to Notepad, delete everything to the right of any semicolon (including the semicolon), copy/paste it in to the webcontrol, and it will run. If none of the digital inputs are activated, then it'll immediately go to alarm.
 
Like CAI said above, the program is basically
 
start
end
 
And then everything in between is what the program does. My program has a bunch of subroutines that handle the inputs and outputs, that get called by the main program.
 
Yeah I ttried it today and it's look easy to understand. I will look over the web for plc tutorial!
 
thx
 
cabletech said:
My doors are wired (well, going to be). I'm going to put a current limiting resistor on the +5V send (haven't decided the value yet, but it'll probably just be something from my grab bag, 220 ohm, 1k, whatever works), and then loop through a closed-when-closed magnetic proximity contact. So if the door opens, the contact opens, and the WebControl signal goes low - and my alarm / email is activated.
 
You might like to consider using an analog input.
More than one switch can be read, using weighted resistors, and decoding which combination(s) of switches are open. With only 10 bits, and jitter, don't get too ambitious, but its easy to sense 3 or 4 inputs with high reliability.
 
Yeah, I could, but I prefer the KISS principle. :) I have the digital inputs, might as well use 'em. I can also loop digitals together (break the loop, alarm goes off).
 
cabletech said:
Hey pittom,
 
My doors are wired (well, going to be). I'm going to put a current limiting resistor on the +5V send (haven't decided the value yet, but it'll probably just be something from my grab bag, 220 ohm, 1k, whatever works), and then loop through a closed-when-closed magnetic proximity contact. So if the door opens, the contact opens, and the WebControl signal goes low - and my alarm / email is activated.
I suggest you use 1K ohm resistor or larger.
 
Jaybird47
 
Back
Top