In this mini blog series we will build an IoT based intruder detection and alert system.
Blogs:
Part1 : Connecting Pir Sensor
Part2 : Capturing intruder image
Part3 : Email alert <--- this blog
In previous blog we integrated a camera to Riotboard for capturing images on IR detection.
In this mini blog we will send email (with captured image) of the intruder.
now add below line to previous code (below ln30)
system("python /opt/send_snapshot.py");
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main() { int ret =0; int fd=0; char buf[1]; system("devmem 0x20E00A0 w 0x5 "); system("echo 113 > /sys/class/gpio/unexport"); system("echo 113 > /sys/class/gpio/export"); usleep(100000); system("echo in > /sys/class/gpio/gpio113/direction"); usleep(100000); fd = open("/sys/class/gpio/gpio113/value",O_RDONLY); while(1) { ret = read(fd,buf,sizeof(1)); if(lseek(fd,0,SEEK_SET) < 0) return 1; if(ret>0 && (atoi(buf)==1)) { printf("\n\033[031;43m HUMAN DETECTED \033[0m "); fflush(stdout); system("fswebcam -d /dev/video2 capture.jpg"); usleep(500000); system("python /opt/intrusion_detection/send_snapshot.py"); } else printf("\nNo Detection"); fflush(stdin); fflush(stdout); usleep(500000); } close(fd); }
Now create a file "send_snapshot.py" in /opt/ directory
cd /opt/
touch send_snapshot.py
copy the below contents to "send_snapshot.py"
#!/usr/bin/python import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders import os send_user = "YOUR_GMAIL_ID@gmail.com" #sender_email send_pwd = "YOUR_GMAIL_PASSWORD" #sender_password recv_user = "YOUR_GMAIL_ID@gmail.com" #receiver_email subject = "intrusion detected !!!!" def mail(to, subject, text, attach): msg = MIMEMultipart() msg['From'] = send_user msg['To'] = recv_user msg['Subject'] = subject msg.attach(MIMEText(text)) part = MIMEBase('application', 'octet-stream') part.set_payload(open(attach, 'rb').read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(attach)) msg.attach(part) mailServer = smtplib.SMTP("smtp.gmail.com", 587) mailServer.ehlo() mailServer.starttls() mailServer.ehlo() mailServer.login(send_user, send_pwd) mailServer.sendmail(send_user, to, msg.as_string()) mailServer.close() mail(recv_user, subject,"below is the snapshot of intruder !!!","capture.jpg")
this script sends snapshot to gmail, in line 10,11 & 12 replace the defaults with your sender gmail id, sender gmail password and receiver email.
Now perform some hand movement infront of IR sensor, it will show human detected, captures image as "capture.jpg" and sends an email.
Received email:
So we conclude this mini blog series on Intrusion detection and notification.