# 下載範例程式 ~$ wget http://max543.com/debugger/class/python02/人臉辨識與物體辨識/code/ch6.zip ~$ unzip ch6.zip ### Lab6-1 ### ~$ pinout # 在終端機中查詢樹莓派的腳位編號 ### Lab6-2 ### # 查詢 GPIO 的模組版本 >>> python3 >>> import RPi.GPIO >>> RPi.GPIO.VERSION ### Lab6-3 (6-1.py) ### import RPi.GPIO as GPIO # 匯入 GPIO 模組 import time # 匯入時間模組 LED_PIN = 18 # 設定 LED_PIN 為 GPIO 18 來控制 GPIO.setmode(GPIO.BCM) # GPIO.BCM 指定 GPIO 後面的號碼 GPIO.setup(LED_PIN, GPIO.OUT) # 設定 LED_PIN 為輸出 GPIO.output(LED_PIN, GPIO.HIGH) # 設定 LED_PIN 為高電位,點亮 LED time.sleep(1) # 暫停 1 秒 GPIO.output(LED_PIN, GPIO.LOW) # 設定 LED_PIN 為低電位,熄滅 LED GPIO.cleanup() # 程式結束時可以把 GPIO 的設定清掉 ### Lab6-4 (6-2.py) ### import RPi.GPIO as GPIO import time LED_PIN = 18 GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) # 設定間隔 1 秒,不斷閃爍 LED while True: GPIO.output(LED_PIN, GPIO.HIGH) time.sleep(1) GPIO.output(LED_PIN, GPIO.LOW) time.sleep(1) GPIO.cleanup() ### Lab6-5 (6-3.py) ### import RPi.GPIO as GPIO from flask import Flask, render_template # render_template: 用來使用 html app = Flask(__name__) # Flask 類別初始化時傳入的 __name__ 參數,代表當前模組的名稱。是固定用法,以便讓 Flask 知道在哪裡尋找資源 (template__folder 或 static_folder 資料夾位置) GPIO.setmode(GPIO.BCM) pins = { # 創建一個名為 pins 的字典來存儲 pin 號、名稱和 pin 狀態 18: {'name': 'GPIO 18', 'state': GPIO.LOW}, 23: {'name': 'GPIO 23', 'state': GPIO.LOW} } for pin in pins: # 走訪字典 pins 的鍵 GPIO.setup(pin, GPIO.OUT) # 將每個引腳設置為輸出 GPIO.output(pin, GPIO.LOW) # 並將其設為低電位 @app.route('/') def main(): for pin in pins: # 對於每個引腳,讀取引腳狀態,並將其儲存在引腳字典中 pins[pin]['state'] = GPIO.input(pin) templateData = { # 將 pins 字典放入 templateData 暫時的數據字典中 'pins' : pins } return render_template('main.html', **templateData) # 將 templateData 數據字典傳入 main.html,返回給用戶 @app.route('//') # 當有人請求一個包含密碼和操作的 URL 時,將執行以下函數 def action(changePin, action): changePin = int(changePin) # 將 URL 中的 pin 轉換為整數 deviceName = pins[changePin]['name'] # 獲取正在更改的引腳名稱 if action == 'on': # 如果 URL 的動作部分是 'on' GPIO.output(changePin, GPIO.HIGH) # 則設定高電位 message = 'Turned ' + deviceName + ' on.' # 保存要傳遞到模板中的狀態消息 if action == 'off': # 如果 URL 的動作部分是 'on',則設定高電位 GPIO.output(changePin, GPIO.LOW) # 則設定低電位 message = 'Turned ' + deviceName + ' off.' # 保存要傳遞到模板中的狀態消息 for pin in pins: # 對於每個引腳,讀取引腳狀態,並將其儲存在引腳字典中 pins[pin]['state'] = GPIO.input(pin) templateData = { # 連同 pins 字典,將消息放入 templateData 數據字典中 'pins' : pins } return render_template('main.html', **templateData) if __name__ == '__main__': app.run(host = '0.0.0.0', port = 80, debug = True) ### Lab6-5 (main.html) ### RPi Web Server

RPi Web Server

{% for pin in pins %}

{{ pins[pin].name }} {% if pins[pin].state == true %} is currently on

{% else %} is currently off {% endif %} {% endfor %} ### Lab6-5 (6-4.py) ### import RPi.GPIO as GPIO import dht11 # 匯入 dht11 模組 import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup() dht = dht11.DHT11(pin = 18) # 宣告 GPIO 18 為輸入腳位 result = dht.read() # 讀取感測器的資料 while True: if result.is_valid(): # 如果有讀到資料 print('Temperature: %d C' % result.temperature) # 顯示溫度 print('Humidity: %d %%' % result.humidity) # 顯示濕度 time.sleep(1) else: print('Error: %d' % result.error_code) # 否則顯示錯誤訊息 ### Lab6-6 ### https://thingspeak.com ### Lab6-7 (6-5.py) ### import RPi.GPIO as GPIO import thingspeak # 匯入 thingspeak import time import dht11 GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.cleanup() channel_id = 你的 Channel ID # 你的 Channel ID (整數型別) write_key = '你的 API Key' # 你的 API Key (字串型別) dht = dht11.DHT11(pin = 18) result = dht.read() def measure(channel): if result.is_valid(): print('Temperature: %d C' % result.temperature) print('Humidity: %d %%' % result.humidity) else: print('Did not receive any reading from sensor. Please check!') response = channel.update({'field1': result.temperature, 'field2': result.humidity}) if __name__ == '__main__': channel = thingspeak.Channel(id = channel_id, api_key = write_key) while True: measure(channel) time.sleep(5) # 每 5 秒上傳一次 ### Lab6-8 ### # 安裝 Node-RED ~$ bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) # 啟動 Node-RED 服務 ~$ node-red-start # 停止 Node-RED 服務 ~$ node-red-stop # 停止並重新啟動 Node-RED 服務 ~$ node-red-restart # 開機自動啟動 ~$ sudo systemctl enable nodered.service # 禁用自動啟動 ~$ sudo systemctl disable nodered.service