一区二区三区三上|欧美在线视频五区|国产午夜无码在线观看视频|亚洲国产裸体网站|无码成年人影视|亚洲AV亚洲AV|成人开心激情五月|欧美性爱内射视频|超碰人人干人人上|一区二区无码三区亚洲人区久久精品

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫(xiě)文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

如何在Raspberry Pi項(xiàng)目中添加Siri控制

賈永世 ? 來(lái)源:大洼球王 ? 作者:大洼球王 ? 2023-02-27 10:27 ? 次閱讀

這篇文章來(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è)置。

poYBAGPzJZ-AY-KWAABeQAGI8uQ997.jpg

圖1:應(yīng)用程序訪問(wèn)(關(guān)閉)

poYBAGPzJaGASqkQAABdsc2xRCE538.jpg

圖2:應(yīng)用程序訪問(wèn)(開(kāi)啟)

設(shè)置Gmail帳戶(hù)的最后一步是啟用IMAP協(xié)議。設(shè)置路徑如下: Gmail->Settings->Gear Part->Settings->Forwarding and POP/IMAP->IMAP Access.

pYYBAGPzJaKAdTbgAADT_NM49WM959.jpg

圖3:IMAP(禁用)

pYYBAGPzJaSAevnSAADj-bkeYpc407.jpg

圖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è)置完畢。

poYBAGPzJaWAbNNGAACL6B9dmNw095.jpg

圖5:帳戶(hù)和密碼

pYYBAGPzJaeADsD-AACTrO_DgGk360.jpg

圖6:Gmail帳戶(hù)下的Notes

poYBAGPzJamASxKjAABTjOPAc0c016.jpg

圖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

pYYBAGPzJaqAEnFAAAGAWjSxfsA313.jpg

圖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/

poYBAGPzJayADcTXAAD9UJf0q74715.jpg

圖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)行初始化。

pYYBAGPzJa2AEnEyAAC7fripAaU435.jpg

圖10:執(zhí)行LED點(diǎn)亮

poYBAGPzJa-Aav6kAAGeBf2KGbc255.jpg

圖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

審核編輯:湯梓紅

聲明:本文內(nèi)容及配圖由入駐作者撰寫(xiě)或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問(wèn)題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • led
    led
    +關(guān)注

    關(guān)注

    242

    文章

    23614

    瀏覽量

    669128
  • HDMI
    +關(guān)注

    關(guān)注

    32

    文章

    1804

    瀏覽量

    154125
收藏 人收藏

    評(píng)論

    相關(guān)推薦

    何在Raspberry Pi上安裝TensorFlow

     在本教程中,我們將學(xué)習(xí)如何在 Raspberry Pi 上安裝 TensorFlow,并將展示一些在預(yù)訓(xùn)練神經(jīng)網(wǎng)絡(luò)上進(jìn)行簡(jiǎn)單圖像分類(lèi)的示例。
    發(fā)表于 09-01 16:35 ?2424次閱讀
    如<b class='flag-5'>何在</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>上安裝TensorFlow

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

    在本教程中,我們將了解如何在新安裝的 Raspbian 上設(shè)置沒(méi)有顯示器和鍵盤(pán)的 Raspberry Pi。
    發(fā)表于 09-22 16:31 ?1949次閱讀
    如<b class='flag-5'>何在</b>Raspbian上設(shè)置沒(méi)有顯示器和鍵盤(pán)的<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>

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

    如果你同時(shí)擁有Arduino和Raspberry pi板,本文將向您展示如何使用Raspberry pi和Python來(lái)控制Arduino。
    的頭像 發(fā)表于 11-08 16:51 ?4727次閱讀
    如何使用<b class='flag-5'>Raspberry</b> <b class='flag-5'>pi</b>和Python來(lái)<b class='flag-5'>控制</b>Arduino

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

    汽車(chē)行業(yè)產(chǎn)品中的速度、位置和距離?;魻杺鞲衅鞯亩喙δ苄允蛊涑蔀橹圃焐毯碗姎夤こ處煹谋貍淦?,這就是為什么今天,我將向我們展示如何在基于 Raspberry Pi項(xiàng)目中使用霍爾傳感器。
    的頭像 發(fā)表于 11-16 17:16 ?2775次閱讀
    如<b class='flag-5'>何在</b>基于<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>的<b class='flag-5'>項(xiàng)目中</b>使用霍爾傳感器

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

    今天小拜年給大家?guī)?lái)的是來(lái)自美國(guó)的Maker Rob Lauer的基于樹(shù)莓派5的蜂窩網(wǎng)絡(luò)項(xiàng)目,了解如何為新的 Raspberry Pi 5 單板計(jì)算機(jī)添加低帶寬蜂窩連接。
    的頭像 發(fā)表于 12-25 10:55 ?1528次閱讀
    基于<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b> 5的蜂窩物聯(lián)網(wǎng)<b class='flag-5'>項(xiàng)目</b>

    如何添加GPS時(shí)間和位置至Raspberry Pi 項(xiàng)目

    以下,歐時(shí)電子工程師將詳細(xì)描述如何在Raspberry Pi 項(xiàng)目添加GPS時(shí)間和位置。向 Raspbe
    發(fā)表于 01-25 15:57

    安裝Raspberry Pi Clock Hat的教程分享

    Raspberry Pi 系統(tǒng)的關(guān)鍵參數(shù),例如時(shí)間、日期、CPU 使用率、CPU 溫度、網(wǎng)絡(luò) IP等。除此之外,它還允許您通過(guò)板載大頭釘按鈕控制一些基本操作,例如關(guān)機(jī)和重啟。這個(gè)項(xiàng)目
    發(fā)表于 07-05 06:01

    分享一個(gè)不錯(cuò)的Raspberry Pi串行控制臺(tái)迷你帽項(xiàng)目

    描述Raspberry Pi串行控制臺(tái)迷你帽該項(xiàng)目包含帶有 USB 到 UART 轉(zhuǎn)換器的 Mini-HAT,連接到 Raspberry
    發(fā)表于 09-01 06:03

    分享在Raspberry Pi項(xiàng)目中添加Siri控制的方案設(shè)計(jì)

    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。
    的頭像 發(fā)表于 08-21 17:40 ?1513次閱讀
    分享在<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b><b class='flag-5'>項(xiàng)目中</b><b class='flag-5'>添加</b><b class='flag-5'>Siri</b><b class='flag-5'>控制</b>的方案設(shè)計(jì)

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

    電子發(fā)燒友網(wǎng)站提供《如何在Raspberry Pi Pico中使用OLED顯示器.zip》資料免費(fèi)下載
    發(fā)表于 10-18 09:15 ?4次下載
    如<b class='flag-5'>何在</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b> Pico中使用OLED顯示器

    基于諾基亞5110的Raspberry Pi帽子

    Raspberry Pi 系統(tǒng)的關(guān)鍵參數(shù),例如時(shí)間、日期、CPU 使用率、CPU 溫度、網(wǎng)絡(luò) IP等。除此之外,它還允許您控制一些基本操作,例如從機(jī)載定位按鈕關(guān)閉和重新啟動(dòng)。這個(gè)項(xiàng)目
    發(fā)表于 12-21 12:27 ?0次下載

    Raspberry Pi上模擬Commodore Amiga

    現(xiàn)在,您只要使用一個(gè)Raspberry Pi就可以輕松實(shí)現(xiàn)對(duì)Amiga的探索!在本項(xiàng)目中,您可以使用任意Pi,但是為了獲得更好的性能,Raspber
    的頭像 發(fā)表于 02-25 17:29 ?1376次閱讀
    在<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>上模擬Commodore Amiga

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

    目的:本教程的目的在于展示如何在Raspberry Pi項(xiàng)目中添加面部識(shí)別功能。注意:該面部識(shí)別門(mén)鎖設(shè)計(jì)并不應(yīng)該被應(yīng)用于保護(hù)或鎖定任何有價(jià)值
    的頭像 發(fā)表于 02-24 18:25 ?925次閱讀
    基于<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>的DIY面部識(shí)別功能

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

    電子發(fā)燒友網(wǎng)站提供《如何在Raspberry Pi零2W上阻止帶有Pi孔的廣告.zip》資料免費(fèi)下載
    發(fā)表于 06-14 10:38 ?0次下載
    如<b class='flag-5'>何在</b><b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b>零2W上阻止帶有<b class='flag-5'>Pi</b>孔的廣告

    Raspberry Pi添加15美元的顯示器

    電子發(fā)燒友網(wǎng)站提供《為Raspberry Pi添加15美元的顯示器.zip》資料免費(fèi)下載
    發(fā)表于 06-20 11:04 ?0次下載
    為<b class='flag-5'>Raspberry</b> <b class='flag-5'>Pi</b><b class='flag-5'>添加</b>15美元的顯示器