161 lines
4.5 KiB
Python
161 lines
4.5 KiB
Python
|
|
#!/usr/bin/python
|
||
|
|
|
||
|
|
import sys, string, time, serial
|
||
|
|
|
||
|
|
from gurux_dlms.GXByteBuffer import GXByteBuffer
|
||
|
|
from gurux_dlms.GXDLMSTranslator import GXDLMSTranslator
|
||
|
|
from gurux_dlms.GXDLMSTranslatorMessage import GXDLMSTranslatorMessage
|
||
|
|
|
||
|
|
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
||
|
|
|
||
|
|
from binascii import unhexlify
|
||
|
|
|
||
|
|
import paho.mqtt.client as mqtt
|
||
|
|
|
||
|
|
from bs4 import BeautifulSoup
|
||
|
|
|
||
|
|
# EVN Schlüssel
|
||
|
|
evn_schluessel = "B294F5695DC03D82E1A34D368432BDFC"
|
||
|
|
|
||
|
|
# MQTT verwenden (True | False)
|
||
|
|
useMQTT = True
|
||
|
|
|
||
|
|
# MQTT Broker
|
||
|
|
#mqttBroker = 'localhost'
|
||
|
|
#mqttPort = 1883
|
||
|
|
mqttBroker = '192.168.0.11'
|
||
|
|
mqttPort = 1883
|
||
|
|
mqttuser = 'smartmeter-neues-haus'
|
||
|
|
mqttpasswort = '07ZzHFfwE0lac3pUUc1z'
|
||
|
|
|
||
|
|
# Aktuelle Werte auf Console ausgeben (True | False)
|
||
|
|
printValue = False
|
||
|
|
|
||
|
|
# COM Port
|
||
|
|
comport = '/dev/ttyUSB0'
|
||
|
|
|
||
|
|
# MQTT init
|
||
|
|
def on_connect(client, userdata, flags, rc):
|
||
|
|
if rc != 0:
|
||
|
|
print("Connection to MQTT broker failed with rc = ", rc)
|
||
|
|
|
||
|
|
if useMQTT:
|
||
|
|
try:
|
||
|
|
client = mqtt.Client("smartmeter-neues-haus")
|
||
|
|
client.username_pw_set(mqttuser, mqttpasswort)
|
||
|
|
client.on_connect = on_connect
|
||
|
|
client.connect(mqttBroker, port=mqttPort)
|
||
|
|
client.loop_start()
|
||
|
|
except:
|
||
|
|
print("Connection to MQTT broker failed")
|
||
|
|
sys.exit()
|
||
|
|
|
||
|
|
tr = GXDLMSTranslator()
|
||
|
|
tr.blockCipherKey = GXByteBuffer(evn_schluessel)
|
||
|
|
tr.comments = True
|
||
|
|
ser = serial.Serial(
|
||
|
|
port=comport,
|
||
|
|
baudrate=2400,
|
||
|
|
bytesize=serial.EIGHTBITS,
|
||
|
|
parity=serial.PARITY_NONE,
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
while 1:
|
||
|
|
daten = ser.read(size=282).hex()
|
||
|
|
# print('\nRohdaten: \n\n' + daten)
|
||
|
|
|
||
|
|
msg = GXDLMSTranslatorMessage()
|
||
|
|
msg.message = GXByteBuffer(daten)
|
||
|
|
|
||
|
|
xml = ""
|
||
|
|
pdu = GXByteBuffer()
|
||
|
|
tr.completePdu = True
|
||
|
|
while tr.findNextFrame(msg, pdu):
|
||
|
|
pdu.clear()
|
||
|
|
xml += tr.messageToXml(msg)
|
||
|
|
|
||
|
|
# print('\nXML entschlüsselt: \n\n' + xml)
|
||
|
|
|
||
|
|
soup = BeautifulSoup(xml, 'lxml')
|
||
|
|
|
||
|
|
results_32 = soup.find_all('uint32')
|
||
|
|
results_16 = soup.find_all('uint16')
|
||
|
|
|
||
|
|
# Wirkenergie A+ in KiloWattstunden
|
||
|
|
WirkenergieP = int(str(results_32)[16:16+8],16)/1000
|
||
|
|
|
||
|
|
# Wirkenergie A- in KiloWattstunden
|
||
|
|
WirkenergieN = int(str(results_32)[52:52+8],16)/1000
|
||
|
|
|
||
|
|
|
||
|
|
# Momentanleistung P+ in Watt
|
||
|
|
MomentanleistungP = int(str(results_32)[88:88+8],16)
|
||
|
|
|
||
|
|
# Momentanleistung P- in Watt
|
||
|
|
MomentanleistungN = int(str(results_32)[124:124+8],16)
|
||
|
|
|
||
|
|
# Spannung L1 in Volt
|
||
|
|
SpannungL1 = int(str(results_16)[16:20],16)/10
|
||
|
|
|
||
|
|
# Spannung L2 in Volt
|
||
|
|
SpannungL2 = int(str(results_16)[48:52],16)/10
|
||
|
|
|
||
|
|
# Spannung L3 in Volt
|
||
|
|
SpannungL3 = int(str(results_16)[80:84],16)/10
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
# Strom L1 in Ampere
|
||
|
|
StromL1 = int(str(results_16)[112:116],16)/100
|
||
|
|
|
||
|
|
# Strom L2 in Ampere
|
||
|
|
StromL2 = int(str(results_16)[144:148],16)/100
|
||
|
|
|
||
|
|
# Strom L3 in Ampere
|
||
|
|
StromL3 = int(str(results_16)[176:180],16)/100
|
||
|
|
|
||
|
|
|
||
|
|
# Leistungsfaktor
|
||
|
|
Leistungsfaktor = int(str(results_16)[208:212],16)/1000
|
||
|
|
|
||
|
|
|
||
|
|
# Shell
|
||
|
|
if printValue:
|
||
|
|
print('Wirkenergie+: ' + str(WirkenergieP))
|
||
|
|
print('Wirkenergie-: ' + str(WirkenergieN))
|
||
|
|
print('MomentanleistungP+: ' + str(MomentanleistungP))
|
||
|
|
print('MomentanleistungP-: ' + str(MomentanleistungN))
|
||
|
|
print('Spannung L1: ' + str(SpannungL1))
|
||
|
|
print('Spannung L2: ' + str(SpannungL2))
|
||
|
|
print('Spannung L3: ' + str(SpannungL3))
|
||
|
|
print('Strom L1: ' + str(StromL1))
|
||
|
|
print('Strom L2: ' + str(StromL2))
|
||
|
|
print('Strom L3: ' + str(StromL3))
|
||
|
|
print('Leistungsfaktor: ' + str(Leistungsfaktor))
|
||
|
|
print('Momentanleistung: ' + str(MomentanleistungP-MomentanleistungN))
|
||
|
|
print()
|
||
|
|
print()
|
||
|
|
|
||
|
|
|
||
|
|
# MQTT
|
||
|
|
if useMQTT:
|
||
|
|
client.publish("neues-haus/smartmeter/WirkenergieP", WirkenergieP)
|
||
|
|
client.publish("neues-haus/smartmeter/WirkenergieN", WirkenergieN)
|
||
|
|
client.publish("neues-haus/smartmeter/MomentanleistungP", MomentanleistungP)
|
||
|
|
client.publish("neues-haus/smartmeter/MomentanleistungN", MomentanleistungN)
|
||
|
|
client.publish("neues-haus/smartmeter/Momentanleistung", MomentanleistungP - MomentanleistungN)
|
||
|
|
client.publish("neues-haus/smartmeter/SpannungL1", SpannungL1)
|
||
|
|
client.publish("neues-haus/smartmeter/SpannungL2", SpannungL2)
|
||
|
|
client.publish("neues-haus/smartmeter/SpannungL3", SpannungL3)
|
||
|
|
client.publish("neues-haus/smartmeter/StromL1", StromL1)
|
||
|
|
client.publish("neues-haus/smartmeter/StromL2", StromL2)
|
||
|
|
client.publish("neues-haus/smartmeter/StromL3", StromL3)
|
||
|
|
client.publish("neues-haus/smartmeter/Leistungsfaktor", Leistungsfaktor)
|
||
|
|
#except BaseException as err:
|
||
|
|
# print("Fehler: ", format(err))
|
||
|
|
# sys.exit()
|
||
|
|
|
||
|
|
|
||
|
|
|