我正在使用声音传感器模块LM393来检测Raspberry Pi 3 B +型设置中打的声音频率,该频率发生在300-500Hz左右。我的小型项目的范围是检测特定范围内的声音,然后将其发送到Firebase实时数据库,以后我可以从该数据库中将其用于Web应用程序前端。当前,LM393传感器检测到使用其内置功能已校准的任何声音,并将该消息发送到firebase。
#!/usr/bin/python
import RPi.GPIO as GPIO
from time import sleep
import datetime
from firebase import firebase
import urllib2,urllib,httplib
import json
import os
from functools import partial
GPIO.setmode(GPIO.bcm)
GPIO.cleanup()
GPIO.setwarnings(False)
#GPIO SETUP
channel = 24
GPIO.setmode(GPIO.bcm)
GPIO.setup(channel,GPIO.IN)
firebase = firebase.FirebaseApplication('....',None)
def update_firebase():
print('Sound Detected')
data = {"Sound": "Sound Detected"}
firebase.post('/sensor/sound',data)
def update_firebase2():
print('Sound Detected')
data2={"Sound": "Sound Not Detected"}
firebase.post('/sensor/sound',data2)
def callback(channel):
if GPIO.input(channel):
update_firebase()
else:
update_firebase2()
GPIO.add_event_detect(channel,GPIO.BOTH,bouncetime=300) # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel,callback) # assign function to GPIO PIN,Run function on change
# infinite loop
while True:
callback(channel)
#sleepTime = int(sleepTime)
sleep(10)