繼電器
繼電器用來切換電路以及設備(我們這里就是LED)的開關。為了簡化電路我們將使用一個繼電器模塊,它包含了所有必須的元器件、管腳和接插件。注意你也可以購買多通道繼電器模塊,只要每個通道可以單獨開關即可。
面包板
面包板用來進行電路的連接而無需進行焊接,這使得裝配過程更加簡單。
發光二極管
當供電時LED會點亮,我們用它來表示項目中的物理設備(比如冰箱)。
300歐電阻
電阻用來限制通過LED的電流。沒有電阻的話,LED和樹莓派可能會因為電流過大而損壞。要使用的電阻取決于你的LED以及電路電壓。在我的演示中,使用了9V電池,因此330歐姆的電阻應當就可以了。
電池
電池為電路提供能力。我的演示中使用的是9v電池。
連接線
我們當然也需要一些電線將各個部件連接起來:
二維碼
如果你希望使用手機IOTA錢包來支付使用LED(冰箱、洗衣機....)的費用,那么一個打印好的IOTA收款地址二維碼會很方便。當使用IOTA錢包生成地址時,你會找到一個二維碼。或者在THETANGLE網站查詢已有地址的二維碼。
現在我們看下組裝好的電路:
樹莓派的管腳如下:
參考以下說明連接管腳:
在我們開始編寫Python代碼之前,需要先確認已經在樹莓派上安裝了所需要的軟件和庫。
首先,我們需要在樹莓派上安裝一個操作系統。任何樹莓派支持的LInux發行版應該都可以。在我的演示中,使用的是Raspbian發行版,因為它已經預置了Python和幾個Python編輯器。Raspbian發行版 的安裝指令可以在這里找到:
https://www.raspberrypi.org/downloads/raspbian/。
如果你要單獨安裝Python,可以參考這里:
https://www.python.org/downloads/。
最后,我們需要安裝PyIOTA API庫,利用它我們就可以使用Python來訪問IOTA tangle了。PyIOTA API庫及安裝指令參見:
https://github.com/iotaledger/iota.lib.py。
現在開始寫代碼:
# Imports some Python Date/Time functions import time import datetime # Imports GPIO library import RPi.GPIO as GPIO # Imports the PyOTA library from iota import Iota from iota import Address # Setup O/I PIN's LEDPIN=18 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(LEDPIN,GPIO.OUT) GPIO.output(LEDPIN,GPIO.LOW) # Function for checking address balance on the IOTA tangle. def checkbalance(): print("Checking balance") gb_result = api.get_balances(address) balance = gb_result['balances'] return (balance[0]) # URL to IOTA fullnode used when checking balance iotaNode = "https://nodes.thetangle.org:443" # Create an IOTA object api = Iota(iotaNode, "") # IOTA address to be checked for new light funds # IOTA addresses can be created using the IOTA Wallet address = [Address(b'GTZUHQSPRAQCTSQBZEEMLZPQUPAA9LPLGWCKFNEVKBINXEXZRACVKKKCYPWPKH9AWLGJHPLOZZOYTALAWOVSIJIYVZ')] # Get current address balance at startup and use as baseline for measuring new funds being added. currentbalance = checkbalance() lastbalance = currentbalance # Define some variables lightbalance = 0 balcheckcount = 0 lightstatus = False # Main loop that executes every 1 second while True: # Check for new funds and add to lightbalance when found. if balcheckcount == 10: currentbalance = checkbalance() if currentbalance > lastbalance: lightbalance = lightbalance + (currentbalance - lastbalance) lastbalance = currentbalance balcheckcount = 0 # Manage light balance and light ON/OFF if lightbalance > 0: if lightstatus == False: print("light ON") GPIO.output(LEDPIN,GPIO.HIGH) lightstatus=True lightbalance = lightbalance -1 else: if lightstatus == True: print("light OFF") GPIO.output(LEDPIN,GPIO.LOW) lightstatus=False # Print remaining light balance print(datetime.timedelta(seconds=lightbalance)) # Increase balance check counter balcheckcount = balcheckcount +1 # Pause for 1 sec. time.sleep(1)
運行代碼
要運行上面的代碼,我們需要先在樹莓派上保存到文件中,例如let_there_be_light.py。然后使用如下命令:
python let_there_be_light.py
現在你應當可以在終端窗口中看到代碼在執行了,顯示當前的余額,并且每個10秒鐘檢查一次LED對應的IOTA地址的余額。
要點亮LED,你只需要使用喜歡的IOTA錢包向LED的IOTA地址轉一些IOTA幣。只要轉賬交易被IOTA tangle確認,LED應該就會點亮并直到消耗完余額。在我的演示當中,我設置的收費標準是1秒鐘1個IOTA。
原文鏈接:iota物聯網硬件集成新手教程 - 匯智網