本文共 4734 字,大约阅读时间需要 15 分钟。
1.所需材料:人体感应模块、蜂鸣器以及树莓派摄像头
人体感应模块接线:中间数据引脚接的是pin12,剩下两个引脚分别接电源和接地。
蜂鸣器接线:一根接电源线,一根接pin11
2.树莓派结合人体感应模块进行蜂鸣器报警
当人体感应模块感应到有人体活动时,蜂鸣器进行报警,同时将报警信息写入到MySQL数据库中。
(1)数据库设计
这里的步骤同上一章,在jiankong数据库进行建立buzzer表操作
mysql> use jiankong;Database changedmysql> create table buzzer(id int auto_increment primary key not null, time char(255),descr char(255));Query OK, 0 rows affected (0.03 sec)
(2)buzzer.py文件
import RPi.GPIO as GPIOimport MySQLdbimport timedef init(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.IN) GPIO.setup(11,GPIO.OUT) passdef beep(): for i in range(1,6): GPIO.output(11, GPIO.LOW) time.sleep(0.5) GPIO.output(11, GPIO.HIGH) time.sleep(0.5) print "the Buzzer will make sound"def detct(): for i in range(1, 31): if GPIO.input(12) == True: print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+ " Someone is closing!" conn=MySQLdb.connect( host='localhost', port=3306, user='root', passwd='root', db='jiankong', ) sql="insert into buzzer(time,descr) values('%s','%s')"%(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'someone is closing') cur=conn.cursor() cur.execute(sql) cur.close() conn.commit() conn.close() beep() else: GPIO.output(11, GPIO.HIGH) print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+" Noanybody!" time.sleep(10)time.sleep(2)init()detct()
(3)运行结果
3.短信预警
这里采用Twilio免费给手机发短信。
(1)在树莓派上通过命令行下载安装twilio
sudo pip3 install twilio
(2)在twilio注册账号
在官网
选择Get a free APIkey,然后注册账号即可。
这三个问题我分别选的是SMS、Phone Verification和Python。
这里可能注册失败,因为注册的时候需要验证码,而在国内验证码是无法显示的,所以这里给大家提供两个办法,这两个办法都是基于谷歌游览器的,第一个办法是给谷歌游览器安装谷歌上网助手插件,另外一个办法在最新版谷歌游览器上访问https://www.rabb.it/,然后通过这个网站提供的谷歌游览器来注册账号。
最后账号还需要手机号的验证。
注册成功登陆之后,选择Get Started,会免费得到一个twilio电话号码。
此时,新建一个项目,这里我新建了一个家庭安防系统的项目,这里记住你的ACCOUNT SID和AUTH token.
(3)python结合twilio向用户发送短信
from twilio.rest import Client# Your Account SID from twilio.com/consoleaccount_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"# Your Auth Token from twilio.com/consoleauth_token = "your_auth_token"client = Client(account_sid, auth_token)message = client.messages.create( to="+15558675309", from_="+15017250604", body="Hello from Python!")print(message.sid)
注意这里to中的电话号码是你注册twilio的号码,from是你注册账号时免费得到的twilio电话号码。
通过上述python代码便可以向用户发送短信,下面是接收的短信情况。
具体的代码情况可参考:
4.智能监控结合
在第二章我们做了定时监控模块,这里我们将定时监控模块加入进来,重新进行整合。
当人体感应模块感应到人体活动时,蜂鸣器警报报警,与此同时树莓派摄像头模块抓拍照片传到七牛云服务器,并且还会通过上述的twilio向用户发送一条示警短信。
这里提供jiankong.py的代码
# -*- coding: utf-8 -*-import RPi.GPIO as GPIOimport MySQLdbimport timeimport picamerafrom qiniu import Auth, put_file, etag, urlsafe_base64_encodeimport qiniu.configimport os#需要填写你的 Access Key 和 Secret Keyaccess_key = '' #这里的密钥填上刚才我让你记住的密钥对secret_key = '' #这里的密钥填上刚才我让你记住的密钥对#构建鉴权对象q = Auth(access_key, secret_key)#要上传的空间bucket_name = 'jiankong'#上传到七牛后保存的文件名key = '%s_%s_%s_%s_%s_%s.jpg'%(time.localtime()[0],time.localtime()[1],time.localtime()[2],time.localtime()[3],time.localtime()[4],time.localtime()[5])#生成上传 Token,可以指定过期时间等token = q.upload_token(bucket_name, key, 3600)#camera=picamera.PiCamera()def uploadfile(): camera.capture("current_photo.jpg")#要上传文件的本地路径localfile = 'current_photo.jpg'ret, info = put_file(token, key, localfile)filename = 'current_photo.jpg'if os.path.exists(filename):os.remove(filename)def init(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.IN) GPIO.setup(11,GPIO.OUT) passdef beep(): for i in range(1,6): GPIO.output(11, GPIO.LOW) time.sleep(0.5) GPIO.output(11, GPIO.HIGH) time.sleep(0.5) print "the Buzzer will make sound"def detct(): for i in range(1, 31): if GPIO.input(12) == True: print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+ " Someone is closing!" conn=MySQLdb.connect( host='localhost', port=3306, user='root', passwd='root', db='jiankong', ) sql="insert into buzzer(time,descr) values('%s','%s')"%(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),'someone is closing') cur=conn.cursor() cur.execute(sql) cur.close() conn.commit() conn.close() beep() uploadfile() else: GPIO.output(11, GPIO.HIGH) print time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+" Noanybody!" time.sleep(10)time.sleep(2)init()detct()