ไลบรารีนี้ใช้งานได้กับ Firmare เวอร์ชัน 1.7.14 หรือต่ำกว่าเท่านั้น
ในเฟิร์มแวร์เวอร์ชันใหม่ Eon ได้ปิดการใช้งาน Local API และทำให้ไลบรารี่นี้ล้าสมัย ต้องขอบคุณ Eon มาก ! -
อ่านเพิ่มเติมในฉบับ #1
สวัสดีและยินดีต้อนรับ! คลิกปุ่มด้านล่างหากคุณชอบห้องสมุดนี้และต้องการสนับสนุนงานของฉัน นักพัฒนาซอฟต์แวร์บริโภคกาแฟจำนวนมากรู้ไหม ?
ไม่จำเป็นต้องพูดว่า นี่เป็นความสมัครใจโดยสมบูรณ์
ไลบรารีอย่างง่ายสำหรับ E.ON Elna API ในตัวที่เขียนด้วย Python 3 Elna เป็นอุปกรณ์วัดพลังงานอัจฉริยะที่เสียบเข้ากับพอร์ต HAN (Home Area Network) ของมิเตอร์ไฟฟ้าโดยใช้ตัวเชื่อมต่อ RJ12
ไลบรารีนี้ใช้ API ในตัวภายในอุปกรณ์ Elna เพื่อรวบรวมข้อมูลเกี่ยวกับ การใช้ พลังงานและ/หรือ การผลิต ของคุณโดยตรงจากอุปกรณ์นั้นเอง
Elna ใช้ฮาร์ดแวร์จาก Net2Grid ดังนั้นจึงอาจเข้ากันได้กับอุปกรณ์เพิ่มเติมจากตระกูล Net2Grid ข้อเสนอแนะใด ๆ ยินดีต้อนรับ
นี่คือแอปพลิเคชันสาธิตบรรทัดคำสั่งขนาดเล็กที่แสดงข้อมูลที่สามารถรับได้จากอุปกรณ์
ตรวจสอบซอร์สโค้ดของการสาธิตที่นี่: smartmeter-demo.py
นี่เป็นแอปพลิเคชันสาธิตในชีวิตจริงที่มีความคล้ายคลึงกับแอปพลิเคชัน CLI ที่รู้จักกันดีมาก มันจะตรวจสอบการใช้พลังงานชั่วขณะ (mpc) ของคุณและส่งออกไปยังคอนโซล เมื่อกด CTRL+C จะแสดงปริมาณการใช้พลังงานขั้นต่ำ สูงสุด และเฉลี่ยที่คำนวณได้ ตลอดจนเวลาไปกลับของแพ็กเก็ตที่ส่งไปยัง Elna และอื่นๆ
มีซอร์สโค้ดอยู่ที่นี่: powerping-demo.py
ตั้งค่าสภาพแวดล้อมเสมือน:
virtualenv venv
source venv/bin/activate
ติดตั้งเวอร์ชันล่าสุดด้วย pip
:
pip install elnasmartmeter
ในการใช้ไลบรารีคุณจำเป็นต้องทราบที่อยู่ IP ของอุปกรณ์ Elna คุณสามารถค้นหาได้ในเซิร์ฟเวอร์ DHCP ของเราเตอร์ของคุณ (หรือที่ใดก็ตามที่คุณใช้เซิร์ฟเวอร์ DHCP) ที่อยู่ MAC ของ Elna จะพิมพ์อยู่ที่ด้านหลังของอุปกรณ์
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'
ลองชมการสาธิตสมาร์ทมิเตอร์ที่ด้านบนเพื่อทดลองใช้
นอกจากนี้เรายังสามารถรับข้อมูล WLAN ของอุปกรณ์ได้โดยการเรียกเมธอด get_wlan_info()
อุปกรณ์สามารถทำหน้าที่เป็นทั้งไคลเอนต์ไร้สาย (สถานี) และจุดเข้าใช้งาน (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 ข้างต้นเป็นการคาดเดาโดยประมาณ
ชื่อผลิตภัณฑ์ เครื่องหมายการค้า และเครื่องหมายการค้าจดทะเบียนในพื้นที่เก็บข้อมูลนี้เป็นทรัพย์สินของเจ้าของที่เกี่ยวข้อง และผู้เขียนใช้เพื่อวัตถุประสงค์ในการระบุตัวตนเท่านั้น การใช้ชื่อ เครื่องหมายการค้า และแบรนด์เหล่านี้ ไม่ได้หมายความถึงการรับรองหรือความเกี่ยวข้อง