該庫僅適用於 Firmare 版本 1.7.14 或更低版本。
在較新的韌體版本中,Eon 禁用了本地 API 並使該庫過時,非常感謝 Eon! ?
閱讀第 1 期的更多內容
您好,歡迎光臨!如果您喜歡這個庫並想支持我的工作,請點擊下面的按鈕。身為軟體開發人員,你會喝很多咖啡,你知道嗎?
不用說,這完全是自願的。
以 Python 3 編寫的 E.ON Elna 內建 API 的簡單函式庫。
該庫使用 Elna 設備內部的內建 API 直接從設備本身收集有關您的功耗和/或生產的資訊。
Elna 基於 Net2Grid 的硬件,因此它可能也與 Net2Grid 系列的更多裝置相容。歡迎任何反饋。
這是一個小型命令列演示應用程序,顯示可以從設備獲取的資訊。
在此處查看演示的源代碼:smartmeter-demo.py。
這是一個更真實的演示應用程序,與眾所周知的 CLI 應用程式有很多相似之處。它將檢查您的瞬時功耗(mpc)並將其輸出到控制台。按下 CTRL+C 後,它會顯示計算出的最小、最大和平均功耗以及發送到 Elna 的資料包的往返時間等。
原始碼可在此處取得:powerping-demo.py。
設定虛擬環境:
virtualenv venv
source venv/bin/activate
使用pip
安裝最新版本:
pip install elnasmartmeter
為了使用該程式庫,您需要知道 Elna 設備的 IP 位址。您可以在路由器的 DHCP 伺服器(或執行 DHCP 伺服器的任何地方)中找到它。 Elna 的 MAC 位址印在裝置背面。
from elna import smartmeter
# Connect the library to the Elna device
meter = smartmeter . Connect ( '192.168.0.10' )
# Get general information
info = meter . get_info ()
# Get power readings
electricity = meter . get_electricity ()
# Get WLAN information
wlan = meter . get_wlan_info ()
取得您家庭的用電量/生產量就是這麼簡單。稍後我們將研究如何透過info
和electricity
物件存取資訊。
所有可從庫中呼叫的方法都會在失敗時拋出異常。可以在此處找到完整的例外清單。
from elna import smartmeter
from elna . exceptions import *
...
try :
info = meter . get_info ()
except NewConnectionError as e :
print ( e )
可以使用print()
方法輸出表示庫中各種實體的對象,以便於檢查其屬性。
例如,您可以透過將Information
物件傳遞給print()
方法來輸出它的屬性:
print ( info )
# Output: <class 'elna.classes.Information'>: {'id': '01ab:0200:00cd:03ef', 'manufacturer': 'NET2GRID', 'model': 'SBWF4602', 'firmware': '1.7.14', 'hardware': 1, 'batch': 'HMX-P0D-123456'}
庫中的所有類別也是如此: Information
、 Electricity
、 Power
和WLANInformation
。
該庫可以獲取兩部分數據:一般設備Information
和Power
統計資訊。
要取得一般設備信息,我們只需呼叫get_info()
方法。
info = meter . get_info ()
透過類別屬性存取值:
info . id # Returns the device ID : '01ab:0200:00cd:03ef' (for example).
info . manufacturer # Returns the manufacturer : 'NET2GRID'
info . model # Returns the model : 'SBWF4602'
info . firmware # Returns the firmware version : '1.7.14'
info . hardware # Returns the hardware version : 1
info . batch # Returns the batch number : 'HMX-P0D-123456'
為了取得功率讀數,我們呼叫get_electricity()
方法。這些讀數有點複雜,因為從 Elna 設備收集的資訊被分成多個子類,但其實沒有那麼複雜:
electricity = meter . get_electricity ()
取得當前耗電量:
electricity . now . key # Returns the string : 'now'
electricity . now . value # Returns the power : 453 (for example).
electricity . now . unit # Returns the unit : 'W' (as in Watt)
electricity . now . timestamp # Returns a timestamp : '2022-12-24 13:37:00'
取得該時段內的最小功耗:
electricity . minimum . key # Returns the string : 'minimum'
electricity . minimum . value # Returns the power : 202 (for example).
electricity . minimum . unit # Returns the unit : 'W' (as in Watt)
electricity . minimum . timestamp # Returns a timestamp : '2022-12-13 13:37:00'
取得該時段的最大耗電量:
electricity . maximum . key # Returns the string : 'maximum'
electricity . maximum . value # Returns the power : 14320 (for example).
electricity . maximum . unit # Returns the unit : 'W' (as in Watt)
electricity . maximum . timestamp # Returns a timestamp : '2022-12-31 13:37:00'
記錄最小值和最大值的時間範圍(週期)(對我來說)是未知的。
取得進口電源。這將是進入家庭的總電力:
electricity . imported . key # Returns the string : 'imported'
electricity . imported . value # Returns the power : 12345678 (for example).
electricity . imported . unit # Returns the unit : 'Wh' (as in Watt hours)
electricity . imported . timestamp # Returns a timestamp : '2022-12-31 13:37:00'
獲取輸出功率。這將是來自家庭的總功率:
electricity . exported . key # Returns the string : 'exported'
electricity . exported . value # Returns the power : 87654321 (for example).
electricity . exported . unit # Returns the unit : 'Wh' (as in Watt hours)
electricity . exported . timestamp # Returns a timestamp : '2022-12-31 13:37:00'
查看頂部的智慧電錶示範來嘗試。
我們也可以透過呼叫get_wlan_info()
方法來取得裝置的WLAN資訊。該設備可以充當無線用戶端(站)和接入點(AP),具體取決於它是否已連接到您的 WiFi 網路。
wlan = meter . get_wlan_info ()
透過物件的類別屬性存取 WLAN 資訊:
wlan . mode # Returns the current WLAN mode
wlan . ap_ssid # Returns the Access Point SSID
wlan . ap_key # Returns the Access Point Password
wlan . client_ssid # Returns the SSID of the AP Elna is connected to
wlan . join_status # Returns the number of clients joined to the Elna AP
wlan . mac # Returns the MAC address currently in use
wlan . ip # Returns the IP address
wlan . subnet # Returns the Subnet mask
wlan . gateway # Returns the Default gateway
wlan . dns # Returns the Primary DNS server
wlan . dnsalt # Returns the Secondary DNS server
wlan . n2g_id # Returns the Net2Grid ID number
wlan . sta_mac # Returns the MAC address of the WLAN Station
wlan . ap_mac # Returns the MAC address of the Access Point
wlan . eth_mac # Returns the Ethernet MAC address (?)
注意:上述 WLAN 屬性後面的描述是估計猜測。
此儲存庫中的產品名稱、商標和註冊商標是其各自所有者的財產,作者僅將其用於識別目的。使用這些名稱、商標和品牌並不意味著認可或隸屬關係。