-->
Page 1 of 1

Burglar alarm upgrade with esp8266

PostPosted: Tue Apr 11, 2017 12:10 pm
by blast
Hi all,
this is my first post in this forum and this is my first project with my "brand new" esp8266-12 dev board.
I would like to upgrade my wired 17 years old home burglar alarm system. I would like to send some email/text (SMS) on alarm status change(triggered/stopped).

So I start searching infos for this new system, and now I'm working on this beta version system.

Here following schematic diagram and LUA code. "Al" screw connector is +12V and it goes up when alarm relais is triggered (now I'm using perboard for testing),

init.lua
Code: Select alllocal SSID = "yourwifisid"
local SSID_PASSWORD = "yoursecretpassword"

wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
    if wifi.sta.getip() == nil then
        print("IP unavaiable, Waiting...")
    else
        tmr.stop(1)
        print("ESP8266 mode is: " .. wifi.getmode())
        print("The module MAC address is: " .. wifi.ap.getmac())
        print("Config done, IP is "..wifi.sta.getip())
        dofile ("alarmtrigger.lua")
    end
end)


alarmtrigger.lua
Code: Select allprint('alarmtrigger.lua started')

local led = 7            --  GPIO13
local input = 5
local status
local on_status = "GET /trigger_alarm.php?status=on"          -- change this to your script
          .." HTTP/1.1\r\n"
          .."Host: www.yoursite.com\r\n"                     -- change this to your host
          .."Connection: close\r\n"
          .."Accept: */*\r\n"
          .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
          .."\r\n"
         
local off_status = "GET /trigger_alarm.php?status=off"          -- change this to your script
          .." HTTP/1.1\r\n"
          .."Host: www.yoursite.com\r\n"                     -- change this to your host
          .."Connection: close\r\n"
          .."Accept: */*\r\n"
          .."User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n"
          .."\r\n"


gpio.mode(7 ,gpio.OUTPUT)
gpio.write(7 ,gpio.LOW)


function turnOffLed()
    gpio.write(7 ,gpio.LOW)
end

function turnOnLed()
    gpio.write(7 ,gpio.HIGH)
end

function connection(msg)
    conn=net.createConnection(net.TCP, 0)
    conn:on("receive", function(conn, payload)
        --print(payload)
        if string.match(payload, 'sent', 0) then   -- change this to the expected reply
            print('email sent!')           
            turnOnLed()     
            tmr.alarm( 1, 2000, 1, turnOffLed )
        end
     
    end)
      -- when connected, request page (send parameters to a script)
    conn:on("connection", function(conn, payload)
         print('Connected')
   
         if msg == "on" then conn:send(on_status) end
         if msg == "off" then conn:send(off_status) end
    end)
    -- when disconnected, let it be known
    conn:on("disconnection", function(conn, payload)
          print('Disconnected\n')
          turnOffLed()
    end)
    conn:connect(80,'www.yoursite.com')                       -- change this to your host
    conn=nil
end

function alarm_triggered()
    print("Alarm Triggered!")
    stat = "ON"
    turnOnLed()     
    tmr.alarm(  2, 200, 1, turnOffLed )
    tmr.delay(10)
    connection("on")
    gpio.trig(button,"up",alarm_stopped)
    return stat
end -- btnINT

function alarm_stopped()
     print("Alarm Stopped!")
     stat = "OFF"
     tmr.delay(10)
     connection("off")
     gpio.trig(button,"down",alarm_triggered)  -- trigger on rising edge
    return stat
end

-- setup gpio pins

gpio.mode(5 , gpio.INT, gpio.PULLUP)
gpio.trig(5, "down",alarm_triggered)


trigger_alarm.php
Code: Select all<?php
$status = NULL;
error_reporting(E_ALL);
ini_set('display_errors', '1');

parse_str($_SERVER['QUERY_STRING']);

$subj ="";
$msg = "\nAlarm status: ". $status . "\n";
$subj = $msg;

if (!($status === NULL)) echo mail("sender@email.com",$subj,$msg,"From: destination@email.com\r\n") ? 'sent' : 'failed';
?>


I can't get var with pin name working inside functions, and something could be optimized.
Code is working, but it is ugly. Any suggestion will be appreciated.
Regards
b.

Re: Burglar alarm upgrade with esp8266

PostPosted: Wed Apr 12, 2017 1:43 am
by pras
Hi,

Maybe you could check this LINK.