雲中樹莓派(2):將傳感器數據上傳到AWS IoT 並利用Kibana進行展示
雲中樹莓派(3):通過 AWS IoT 控制樹莓派上的Led
1. 聲音傳感器及其配置
聲音傳感器如下圖所示:

將 VCC 引腳接入樹莓派 5V 引腳,將 GND 引腳接入樹莓派 GND 引腳,將 OUT 引腳接入樹莓派 GPIO20。
要注意,模塊在環境聲音強度達不到設定閾值時,OUT輸出高電平(1),當外界環境聲音強度超過設定閾值時,模塊OUT輸出低電平(0)。
2. GPIO Event 機制
樹莓派提供了三種電信號事件反饋機制。
(1)GPIO.wait_for_edge:直接等待電信號達到某種條件(升高還是降低或者任意),並且可以設置超時時間。在超時時間內,函數會一直等待,直到期望的電信號改變出現,或者超時。
# wait for up to 5 seconds for a rising edge (timeout is in milliseconds) channel = GPIO.wait_for_edge(channel, GPIO_RISING, timeout=5000) if channel is None: print('Timeout occurred') else: print('Edge detected on channel', channel)
(2)GPIO.add_event_detect:設置事件觸發檢測,一旦檢測到,會返回True。
GPIO.add_event_detect(channel, GPIO.RISING) # add rising edge detection on a channel do_something() if GPIO.event_detected(channel): print('Button pressed')
(3)GPIO.add_event_detect:回調函數機制。注冊回調函數,一旦指定事件觸發,回調函數會被調用。
def my_callback(channel): print('This is a edge event callback function!') print('Edge detected on channel %s'%channel) print('This is run in a different thread to your main program') GPIO.add_event_detect(channel, GPIO.RISING, callback=my_callback) # add rising edge detection on a channel
3. 利用聲音檢查模塊控制Led燈
實現目標:當檢測到聲音時,改變Led 燈的狀態。
3.1 代碼
import RPi.GPIO as GPIO import time from time import sleep SOUND_PIN_NUM = 20 #聲音模塊的輸出引腳接的GPIO LED_PIN_NUM = 26 #LED 的長腳接的GPIO state = 0 #保存led 的狀態 timeLast = time.time() #保存上次觸發的時間 # in one sounding, the callback function will be invoked for a few times, so need wait for some time to validDuration = 0.1 GPIO.setmode(GPIO.BCM) GPIO.setup(SOUND_PIN_NUM, GPIO.IN) GPIO.setup(LED_PIN_NUM, GPIO.OUT) def callback_fun_soundOccurred(input_pint): global timeLast timeNow = time.time() duration = timeNow - timeLast if (duration < validDuration): print("ignored because duration " + str(duration) + " is too short") timeLast = timeNow return print("accepted for valid duration " + str(duration)) timeLast = timeNow switchLed() def switchLed(): global state if (state): turnOffLed() state = 0 else: turnOnLed() state = 1 def turnOnLed(): print("Turn on") GPIO.output(LED_PIN_NUM,GPIO.HIGH) def turnOffLed(): print("Turn off") GPIO.output(LED_PIN_NUM, GPIO.LOW) GPIO.add_event_detect(SOUND_PIN_NUM, GPIO.RISING, callback=callback_fun_soundOccurred) try: while True: sleep(0.1) except KeyboardInterrupt: GPIO.remove_event_detect(SOUND_PIN_NUM) GPIO.cleanup()
3.2 兩個小技巧
(1)盡管一個只需要一塊五毛錢,但聲音檢測模塊的靈敏度是可以調節的。使用螺絲刀轉動上面的旋鈕,邊轉變說話,看其開關指示燈的反應,亮表示檢測到聲音,亮度表示聲音大小。旋到合適的位置即可。默認時,它非常靈敏,任何細小的聲音都會觸發它。
(2)在一句話說話過程中,回調函數會被觸發好多次。因此,需要的話,如上面代碼,可以計算兩次調用之間的事件間隔,把太短的間隔過濾掉。下面是一句短話過程中函數被觸發的情況:
ignored because duration 0.000501155853271 is too short ignored because duration 0.000110864639282 is too short ignored because duration 0.00215411186218 is too short ignored because duration 0.000218868255615 is too short ignored because duration 0.000470161437988 is too short ignored because duration 0.000167846679688 is too short ignored because duration 0.000583171844482 is too short ignored because duration 0.000425815582275 is too short ignored because duration 0.0010621547699 is too short ignored because duration 0.000314950942993 is too short ignored because duration 0.000555038452148 is too short ignored because duration 0.000130891799927 is too short ignored because duration 0.000461101531982 is too short ignored because duration 0.00022292137146 is too short ignored because duration 0.00274705886841 is too short ignored because duration 0.000133037567139 is too short ignored because duration 0.00597095489502 is too short ignored because duration 0.000155925750732 is too short ignored because duration 0.00107598304749 is too short ignored because duration 0.000198125839233 is too short
參考鏈接:
- https://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-2
- https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/
歡迎大家關注我的個人公眾號:

