這篇文章來(lái)源于DevicePlus.com英語(yǔ)網(wǎng)站的翻譯稿。
1. 目的
本教程旨在教您如何使用SiriControl開(kāi)源Python框架在RaspberryPi項(xiàng)目中添加Siri功能和控制功能。
2. 概述
在本教程中,我將介紹如何設(shè)置和使用SiriControl Python框架。我將利用SiriControl模塊和Raspberry Pi 3來(lái)點(diǎn)亮和熄滅一個(gè)LED。學(xué)完本教程之后,您將能夠使用SiriControl將Siri語(yǔ)音命令添加到任何Raspberry Pi項(xiàng)目中。開(kāi)始學(xué)習(xí)之前,請(qǐng)確保您擁有以下設(shè)備和材料,并確保已經(jīng)安裝RaspberryPi且軟件能夠正常運(yùn)行。
3. 設(shè)備
Raspberry Pi 3 – Raspberry Pi鏈接
HDMI線–HDMI線鏈接
LED – LED的Adafruit鏈接
鼠標(biāo) + 鍵盤(pán) – 鍵盤(pán)/鼠標(biāo)的Amazon鏈接
顯示器/TV – 任何帶HDMI接口的TV或顯示器
跳線 – 跳線的Amazon鏈接
Micro USB電纜 – Micro USB 電纜的Amazon鏈接
面包板 – 面包板的Amazon鏈接
4. 目錄
為SiriControl創(chuàng)建一個(gè)Gmail帳戶(hù)
iOS設(shè)備設(shè)置
SiriControl
設(shè)置SiriControl
創(chuàng)建自己的模塊
電路接線
上傳并運(yùn)行程序
程序
1.0 為SiriControl創(chuàng)建一個(gè)Gmail帳戶(hù)
SiriControl模塊需要一個(gè)Gmail帳戶(hù)才能工作。我創(chuàng)建了一個(gè)新的Gmail帳戶(hù),僅供SiriControl使用。我建議您也這樣做。這是一個(gè)好主意,因?yàn)镻ython腳本可以包含該帳戶(hù)的用戶(hù)名和密碼。
創(chuàng)建帳戶(hù)后,我們需要允許不太安全的應(yīng)用程序訪問(wèn)Gmail。這是因?yàn)镚mail服務(wù)器將Python腳本視為不太安全的應(yīng)用程序。我們可以在Sign-in & Security(登錄和安全) 部分進(jìn)行設(shè)置。
圖1:應(yīng)用程序訪問(wèn)(關(guān)閉)
圖2:應(yīng)用程序訪問(wèn)(開(kāi)啟)
設(shè)置Gmail帳戶(hù)的最后一步是啟用IMAP協(xié)議。設(shè)置路徑如下: Gmail->Settings->Gear Part->Settings->Forwarding and POP/IMAP->IMAP Access.
圖3:IMAP(禁用)
圖4:IMAP(啟用)
2.0 iOS設(shè)備設(shè)置
將iOS設(shè)備上的“Notes”連至已經(jīng)設(shè)置好與SiriControl配合使用的Gmail帳戶(hù)。選擇 Settings->Accounts & Passwords->Add Account(設(shè)置->帳戶(hù)和密碼->添加帳戶(hù)), 添加剛才設(shè)置好的Gmail帳戶(hù)。添加該帳戶(hù)后,選擇它并啟用Notes(圖6:Gmail帳戶(hù)下的Notes)。接下來(lái),選擇 Settings->Notes,并啟用“On My iPhone” Account(我iPhone上的帳戶(hù))。 然后,將 Default Account 改為Gmail帳戶(hù)?,F(xiàn)在,我的iOS設(shè)備設(shè)置完畢。
圖5:帳戶(hù)和密碼
圖6:Gmail帳戶(hù)下的Notes
圖7:默認(rèn)帳戶(hù)-Notes
3.0 Siri控制
3.1 設(shè)置SiriControl
要使用SiriControl,我們需要將模塊的資源庫(kù)克隆到RaspberryPi。要做到這一點(diǎn),請(qǐng)打開(kāi)終端窗口并輸入以下命令:
1. sudo apt-get update
2. sudo apt-get install git-core
3. git clone https://github.com/theraspberryguy/SiriControl-System
圖8:克隆資源庫(kù)
克隆資源庫(kù)之后,打開(kāi)腳本siricontrol.py。在腳本中輸入Gmail帳戶(hù)的用戶(hù)名和密碼,然后保存腳本。
3.2 創(chuàng)建自己的模塊
Siricontrol.py 運(yùn)行時(shí),會(huì)從模塊(modules)文件夾中加載模塊腳本。執(zhí)行不同的任務(wù)時(shí),按照模板編寫(xiě)新的模塊腳本很重要。因?yàn)槲蚁肟刂埔粋€(gè)LED,所以我寫(xiě)了一個(gè)腳本來(lái)打開(kāi)LED (LED_on.py) 和一個(gè)腳本來(lái)關(guān)閉LED (LED_off.py).
要制作自己的模塊,請(qǐng)?jiān)谀0迥_本中執(zhí)行以下步驟:
1. 在“moduleName”中命名該模塊
2. 為模塊提供“commandWords”,以便讓Siri執(zhí)行命令。
3. 在execute(command) 函數(shù)下寫(xiě)入想要執(zhí)行的功能。
4. 確保將腳本保存在模塊文件夾中。
4.0 連接電路
為了讓SiriControl執(zhí)行命令,我搭建了一個(gè)簡(jiǎn)單的LED電路。我總是喜歡用Fritzing繪制接線圖。Fritzing是一款開(kāi)源原理圖設(shè)計(jì)和PCB布線軟件。Fritzing的下載地址(可選)如下:https://fritzing.org/home/
圖9:Raspberry Pi LED原理圖
LED和電阻應(yīng)串聯(lián)在引腳11(GPIO17)和引腳25(地)之間。電阻的作用是限制流過(guò)LED的電流,并應(yīng)根據(jù)您的LED進(jìn)行相應(yīng)調(diào)整,以防止電流過(guò)大將LED燒壞。請(qǐng)記住,LED較長(zhǎng)的引線是正極,應(yīng)連接到引腳11。
5.0 上傳并運(yùn)行程序
完成上述步驟后,我使用以下命令運(yùn)行SiriControl腳本:
python siricontrol.py
該腳本開(kāi)始運(yùn)行,并使用模塊文件夾中的所有模塊進(jìn)行初始化。
圖10:執(zhí)行LED點(diǎn)亮
圖11:執(zhí)行LED熄滅
現(xiàn)在我命令Siri,“Note: Turn on LED(注意:打開(kāi)LED)”,LED燈亮起,同時(shí)腳本會(huì)告訴我它執(zhí)行了我的命令并且正在傾聽(tīng)另一個(gè)命令。我現(xiàn)在說(shuō),“Note: Turn off LED(注意:關(guān)閉LED)”,LED熄滅。只要符合以下條件,該腳本就會(huì)執(zhí)行我(不管在任何地方)對(duì)Siri發(fā)出的命令:
1. Raspberry Pi正在運(yùn)行該腳本。
2. Raspberry Pi已連至互聯(lián)網(wǎng),以便可以輪詢(xún)Gmail帳戶(hù)。
現(xiàn)在,您可以為任意Raspberry Pi項(xiàng)目添加任何SiriControl控制模塊。雖然我在這個(gè)項(xiàng)目中使用了Pi,但是該教程在安裝了Python的其他Linux開(kāi)發(fā)板上同樣可以工作。
6.0 附件:腳本
6.1 Siricontrol.py
import time import imaplib import email import os import pkgutil ########################################## # Add your gmail username and password here username = "" password = "" ########################################## class ControlException(Exception): pass class Control(): def __init__(self, username, password): print("------------------------------------------------------") print("- SIRI CONTROL -") print("- Created by Sanjeet Chatterjee -") print("- Website: thereallycoolstuff.wordpress.com -") print("------------------------------------------------------") try: self.last_checked = -1 self.mail = imaplib.IMAP4_SSL("imap.gmail.com", 993) self.mail.login(username, password) self.mail.list() self.mail.select("Notes") # Gets last Note id to stop last command from executing result, uidlist = self.mail.search(None, "ALL") try: self.last_checked = uidlist[0].split()[-1] except IndexError: pass self.load() self.handle() except imaplib.IMAP4.error: print("Your username and password is incorrect") print("Or IMAP is not enabled.") def load(self): """Try to load all modules found in the modules folder""" print("n") print("Loading modules...") self.modules = [] path = os.path.join(os.path.dirname(__file__), "modules") directory = pkgutil.iter_modules(path=[path]) for finder, name, ispkg in directory: try: loader = finder.find_module(name) module = loader.load_module(name) if hasattr(module, "commandWords") and hasattr(module, "moduleName") and hasattr(module, "execute"): self.modules.append(module) print("The module '{0}' has been loaded, " "successfully.".format(name)) else: print("[ERROR] The module '{0}' is not in the " "correct format.".format(name)) except: print("[ERROR] The module '" + name + "' has some errors.") print("n") def fetch_command(self): """Retrieve the last Note created if new id found""" self.mail.list() self.mail.select("Notes") result, uidlist = self.mail.search(None, "ALL") try: latest_email_id = uidlist[0].split()[-1] except IndexError: return if latest_email_id == self.last_checked: return self.last_checked = latest_email_id result, data = self.mail.fetch(latest_email_id, "(RFC822)") voice_command = email.message_from_string(data[0][1].decode('utf-8')) return str(voice_command.get_payload()).lower().strip() def handle(self): """Handle new commands Poll continuously every second and check for new commands. """ print("Fetching commands...") print("n") while True: try: command = self.fetch_command() if not command: raise ControlException("No command found.") print("The word(s) '" + command + "' have been said") for module in self.modules: foundWords = [] for word in module.commandWords: if str(word) in command: foundWords.append(str(word)) if len(foundWords) == len(module.commandWords): try: module.execute(command) print("The module {0} has been executed " "successfully.".format(module.moduleName)) except: print("[ERROR] There has been an error " "when running the {0} module".format( module.moduleName)) else: print("n") except (TypeError, ControlException): pass except Exception as exc: print("Received an exception while running: {exc}".format( **locals())) print("Restarting...") time.sleep(1) if __name__ == '__main__': Control(username, password)
6.2 Led_on.py
#You can import any modules required here
import RPi.GPIO as GPIO #import GPIO module
import time
#This is name of the module – it can be anything you want
moduleName = “LED_on”
#These are the words you must say for this module to be executed
commandWords = [“turn”, “on”, “l(fā)ed”]
#This is the main function which will be execute when the above command words are said
def execute(command):
LED = 11 # Set LED pin to pin 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT) #configure LED as an output
print(“n”)
print(“LED is On.”)
6.3 Led_off.py
#You can import any modules required here
import RPi.GPIO as GPIO #import GPIO module
import time
#This is name of the module – it can be anything you want
moduleName = “LED_off”
#These are the words you must say for this module to be executed
commandWords = [“turn”, “off”, “l(fā)ed”]
#This is the main function which will be execute when the above command words are said
def execute(command):
LED = 11 # Set LED pin to pin 11
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED, GPIO.OUT) #configure LED as an output
print(“n”)
print(“LED is off.”)
GPIO.output(LED, GPIO.LOW) #turn LED on
審核編輯:湯梓紅
-
led
+關(guān)注
關(guān)注
242文章
23614瀏覽量
669128 -
HDMI
+關(guān)注
關(guān)注
32文章
1804瀏覽量
154125
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
如何在Raspberry Pi上安裝TensorFlow

如何在Raspbian上設(shè)置沒(méi)有顯示器和鍵盤(pán)的Raspberry Pi

如何使用Raspberry pi和Python來(lái)控制Arduino

如何在基于Raspberry Pi的項(xiàng)目中使用霍爾傳感器

基于Raspberry Pi 5的蜂窩物聯(lián)網(wǎng)項(xiàng)目

如何添加GPS時(shí)間和位置至Raspberry Pi 項(xiàng)目
安裝Raspberry Pi Clock Hat的教程分享
分享一個(gè)不錯(cuò)的Raspberry Pi串行控制臺(tái)迷你帽項(xiàng)目
分享在Raspberry Pi項(xiàng)目中添加Siri控制的方案設(shè)計(jì)

如何在Raspberry Pi Pico中使用OLED顯示器

基于諾基亞5110的Raspberry Pi帽子
在Raspberry Pi上模擬Commodore Amiga

基于Raspberry Pi的DIY面部識(shí)別功能

如何在Raspberry Pi零2W上阻止帶有Pi孔的廣告

為Raspberry Pi添加15美元的顯示器

評(píng)論