TJ Blevins
Published © GPL3+

(Updated More Advanced) Simple Arduino Home Security

This project uses Arduino, Raspberry Pi, and Telegram app, to create a home security system.

AdvancedShowcase (no instructions)4 hours3,449
(Updated More Advanced) Simple Arduino Home Security

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
5 mm LED: Red
5 mm LED: Red
×1
5 mm LED: Yellow
5 mm LED: Yellow
×1
5 mm LED: Green
5 mm LED: Green
×1
Resistor 1k ohm
Resistor 1k ohm
×3
Reed Switch
×1
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
project enclosure

Story

Read more

Schematics

ArduinoSecurity

Alarm Circuit

Code

ArduinoSecurity

Arduino
#define redled 6
#define greled 7
#define yelled 8

#define PIR 5
#define reed 4


// timer for alerts to print every minute(60s)
long minSecsBetweenAlerts = 600000L; //1 min = 60s so 1 sec equals 1000millis
long intervalN = 60000L;
long start = 0;

void setup() {
  pinMode(redled, OUTPUT);
  pinMode(greled, OUTPUT);
  pinMode(yelled, OUTPUT);
  pinMode(reed, INPUT_PULLUP);
  pinMode(PIR, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  //sensor readings could be high or low depending on the sensors used
  unsigned long now = millis();
  int trig = digitalRead(reed);
  if(trig == LOW){
    Serial.print("closed");
    digitalWrite(greled, HIGH);
  }
  if(trig == HIGH){
    if(now - start >= minSecsBetweenAlerts) {
    start = now;
    Serial.print("open");
    digitalWrite(redled, HIGH);
    delay(50);
    digitalWrite(redled, LOW);
    delay(100);
    digitalWrite(yelled, HIGH);
    delay(50);
    digitalWrite(yelled, LOW);
    delay(100);
    digitalWrite(greled, LOW);
    delay(50);
    digitalWrite(greled, HIGH);
  } else {
    Serial.println("still open");
  }
  
  long now = millis();
  int motion = (digitalRead(PIR));
  if(motion == HIGH) {
    if(now - start >= intervalN){
      Serial.println("Motion Detected");
      digitalWrite(redled, HIGH);
      start = now;
    }
    else{
      Serial.print("nothing yet");
      digitalWrite(redled, LOW);
    }
    delay(250);
  }
  }  
}

workinBot

Python
Read the comments in the code.
import requests
import serial
import subprocess
import time
import smtplib
import os
from datetime import datetime

now = datetime.now()
url = "https://api.telegram.org/bot{yourBOTIDhere}/"
#regardless of the code if you have internet dropouts errors will throw
#this has worked well for me

def get_url(url):
    response = resquests.get(url)
    content = response.content.decode("utf8")
    return content
def get_json_from_url(url):
    content = get_url(url)
    js = json.loads(content)
    return js
def get_updates_json(request):
    params = {'timeout': 100, 'offset':None}
    response = requests.get(request + 'getUpdates', data = params)
    return response.json()
def last_update(data):  
    results = data['result']
    total_updates = len(results) - 1
    return results[total_updates]
def get_chat_id(update):  
    chat_id = update['message']['chat']['id']
    return chat_id
def send_mess(chat, text):  
    params = {'chat_id': chat, 'text': text}
    response = requests.post(url + 'sendMessage', data=params)
    return response

def send_pic(chat, files):
    files = {'photo': open('/home/pi/webcam/t.jpg', 'rb')}
    data =  {'chat_id': chat}
    response = requests.post(url + 'sendPhoto' , files = files, data=data)
    #print(response.url)
    print(response.status_code)
    print(response.text)
    return response

def take_pic():
    #i will link code for webcam.sh small code for saving files
    subprocess.call("./webcam.sh", shell=True)
    f = open('/home/pi/webcam/t.jpg', 'rb')
    
   
    


chat_id = get_chat_id(last_update(get_updates_json(url)))
send_mess(chat_id, 'hello master')
take_pic()
files = open('/home/pi/webcam/t.jpg', 'rb')
chat_id = get_chat_id(last_update(get_updates_json(url)))


send_pic(chat_id, files)

while True:
                
                ser=serial.Serial('/dev/ttyUSB0',9600)
                message = ser.readline()
                print(message)
                if message[0:1] == 'M':
                        ser.close()
                        take_pic()
                        send_mess(chat_id, 'master someone is in your house')
                        send_pic(chat_id, files)
                        time.sleep(600)#use 600 
                        ser.open()
                        
        
                elif message[0:1] == 'o':
                        ser.close()
                        send_mess(chat_id, "Master someone opened your door")
                        time.sleep(600)
                        ser.open()
                        #exit()
                
                        

webcam

BatchFile
Bash script for saving photo to raspi and sending one in code.
#!/bin/bash
DATE=$(date +"%Y-%m-%d-%a_%H%M")


fswebcam -d /dev/video0 -r 1600x1200 -S15 --jpeg 100 /home/pi/webcam/$DATE.jpg
fswebcam -d /dev/video0 -r 1600x1200 -S15 --jpeg 100 /home/pi/webcam/t.jpg

Credits

TJ Blevins

TJ Blevins

3 projects • 5 followers

Comments