一.嘮嗑
大三下學期在跟材料學院的一個老師做項目,說是要完成自動檢測實驗室環境情況,包括溫溼度、火焰以及厭惡等,最終目標是製作出一個可以在實驗室全自動行駛的檢測機器人,整個項目組就我一個計算機的成員…
結果就是我只完成了樹莓派的傳感器檢測模塊。
二.傳感器
2.1 DHT22溫溼度傳感器
這個傳感器比DHT11傳感器更好,精確度達到了97左右,二DHT11只有92左右。價格的話都很便宜的,我記得我的導師100塊錢買了30個不同的傳感器,我大概全部連接了六七個傳感器吧。
DHT22代碼:
#!/usr/bin/python
import sys
import Adafruit_DHT
import time
import json,os
def jsonWrite(data,tdate,name):
pathfile = "web/data/"+ name + "/" + tdate +".json"
if os.path.exists(pathfile):
file =open(pathfile,"rb+")
file.seek(-1,os.SEEK_END)
file.truncate()
file.write(",")
json.dump(data,file)
file.write("]")
file.close()
else:
file =open(pathfile,"w")
file.write("[")
json.dump(data,file)
file.write("]")
file.close()
timecount = 0
while True:
sensor = Adafruit_DHT.DHT22
humidity, temperature = Adafruit_DHT.read_retry(sensor, 26)
if humidity is not None and temperature is not None:
todaytime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
msg = time.strftime('%Y-%m-%d %H:%M',time.localtime(time.time())) + '\n' + str(format(temperature,".1f")) + ' C ' + str(format(humidity,".1f")) + '%'
i = {"time":time.strftime('%H:%M',time.localtime(time.time())),"tmp":format(temperature,".1f"),"hmt":format(humidity,".1f")}
jsonWrite(i,todaytime,"min")
print(msg)
if(timecount >=60):
timecount = 0
jsonWrite(i,todaytime,"hour")
time.sleep(60)
timecount = timecount +1
2.2 煙霧傳感器
直接上代碼:
#! /usr/bin/env python3
import RPi.GPIO as GPIO # 導入庫,並進行別名的設置
import time
CHANNEL=36 # 確定引腳口。按照真實的位置確定
GPIO.setmode(GPIO.BOARD) # 選擇引腳系統,這裏我們選擇了BOARD
GPIO.setup(CHANNEL,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
#初始化引腳,將36號引腳設置爲輸入下拉電阻,因爲在初始化的時候不確定的的引電平,因此這樣設置是用來保證精準,(但是也可以不寫“pull_up_down=GPIO.PUD_DOWN”)
# 帶有異常處理的主程序
try:
while True: # 執行一個while死循環
status=GPIO.input(CHANNEL) # 檢測36號引腳口的輸入高低電平狀態
#print(status) # 實時打印此時的電平狀態
if status == True: # 如果爲高電平,說明MQ-2正常,並打印“OK”
print ( ‘ 正常 ‘ )
else: # 如果爲低電平,說明MQ-2檢測到有害氣體,並打印“dangerous”
print ( ‘ 檢測到危險氣體 ! ! ! ‘ )
time.sleep(0.1) # 睡眠0.1秒,以後再執行while循環
except KeyboardInterrupt: # 異常處理,當檢測按下鍵盤的Ctrl+C,就會退出這個>腳本
GPIO.cleanup() # 清理運行完成後的殘餘
2.3 聲音傳感器:
import RPi.GPIO as GPIO
import time
CHANNEL=7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(CHANNEL,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
try:
while True:
status=GPIO.input(CHANNEL)
#print(status)
if status == True:
print ("No Sound...")
else:
print ("Having Sound!!!")
time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup()
2.4 紅外避障傳感器
import RPi.GPIO as GPIO
import time
from twilio.rest import TwilioRestClient
CHANNEL=7
GPIO.setmode(GPIO.BOARD)
GPIO.setup(CHANNEL,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
try:
while True:
status=GPIO.input(CHANNEL)
#print(status)
if status == True:
print ("No Obstacle...")
else:
print ("Having Obstacle!!!")
time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup()
2.5 人體紅外傳感器:
# -*- coding: utf-8 -*-
import time
import RPi.GPIO as GPIO
BODY_GPIO = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(BODY_GPIO, GPIO.IN)
def body_detect():
signal = GPIO.input(BODY_GPIO)
if signal == 1:
print "DETECT BODY!"
else:
print "NO BODY!"
if __name__ == "__main__":
count = 0
while True:
body_detect()
time.sleep(6)
count += 1
if count == 20:
break
GPIO.cleanup()
2.6 用twilio免費打電話以及發短信
發短信:
#填寫你在twilio的賬號
account = "ACXXXXXXXXXXXXXXXXX"
#填入你在twilio的app token
token = "YYYYYYYYYYYYYYYYYY"
#填入你申請的號碼
twilioNumber = "00000000"
#填入你驗證的手機號
myNumber = "111111111"
client = TwilioRestClient(account, token)
message=client.messages.create(to=myNumber, from_=twilioNumber, body="Fire!!!")
print(message.sid)
打電話:
#填寫你在twilio的賬號
account = "ACXXXXXXXXXXXXXXXXX"
#填入你在twilio的app token
token = "YYYYYYYYYYYYYYYYYY"
#填入你申請的號碼
twilioNumber = "00000000"
#填入你驗證的手機號
myNumber = "111111111"
client = TwilioRestClient(account, token)
#url所對應的文件是制定的,是twilio官網的一個示例文件,可以通過構建自己的文件
client.calls.create(url='https://demo.twilio.com/welcome/voice/', to=myNumber , from_=twilioNumber)
print(call.sid)