68 lines
3.1 KiB
Python
68 lines
3.1 KiB
Python
from smllib import SmlStreamReader
|
|
import time
|
|
import serial
|
|
from datetime import datetime
|
|
|
|
from influxdb_client import Point, WritePrecision
|
|
from influx import writeToDB
|
|
|
|
stream = SmlStreamReader()
|
|
port = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
|
|
|
|
energyCounter = 0 # Counter for values that should only be pushed to db every minute
|
|
|
|
while True:
|
|
energyCounter += 1
|
|
pushTime = datetime.utcnow() # Get current datetime and safe it in pushTime variable
|
|
pointList = [] # Create list pointList where influxdb datapoint are stored before pushing to db
|
|
sml_frame = stream.get_frame() # Create the frame object from bytestream which is None until the frame is complete
|
|
|
|
while sml_frame is None:
|
|
stream.add(port.read()) # Add bytes until full frame is received
|
|
try:
|
|
sml_frame = stream.get_frame() # Find frame from stream
|
|
except Exception:
|
|
print("CRCERROR")
|
|
pass
|
|
|
|
# A quick Shortcut to extract all values without parsing the whole frame
|
|
# In rare cases this might raise an InvalidBufferPos exception, then you have to use sml_frame.parse_frame()
|
|
obis_values = sml_frame.get_obis()
|
|
|
|
# return all values but slower
|
|
parsed_msgs = sml_frame.parse_frame()
|
|
for msg in parsed_msgs:
|
|
print(msg.format_msg()) # prints a nice overview over the received values
|
|
|
|
obis_values = parsed_msgs[1].message_body.val_list # In the parsed message the obis values are typically found like this
|
|
|
|
list_entry = obis_values[3] # The obis attribute of the SmlListEntry carries different obis representations as attributes
|
|
|
|
# Get values from obis list
|
|
heatPowerIn = int(obis_values[5].value*0.01)
|
|
heatEnergyIn = int(obis_values[2].value*0.0001)
|
|
heatEnergyInHT = int(obis_values[3].value*0.0001)
|
|
heatEnergyInNT = int(obis_values[4].value*0.0001)
|
|
|
|
# Print values to console
|
|
print("Heizung Wirkleistung aktuell: " + str(heatPowerIn) + "W")
|
|
print("Heizung Energie bezug gesamt: " + str(heatEnergyIn) + "kWh")
|
|
print("Heizung Energie bezug HT: " + str(heatEnergyInHT) + "kWh")
|
|
print("Heizung Energie bezug NT: " + str(heatEnergyInNT) + "kWh")
|
|
pointList.append( Point('power').field('heatPowerIn', heatPowerIn).time(pushTime, WritePrecision.S) )
|
|
|
|
# Only add energy to pointlist every 6th while loop (every minute)
|
|
if energyCounter % 6 == 0:
|
|
pointList.append( Point('energy').field('heatEnergyIn', heatEnergyIn).time(pushTime, WritePrecision.S) )
|
|
pointList.append( Point('energy').field('heatEnergyInHT', heatEnergyInHT).time(pushTime, WritePrecision.S) )
|
|
pointList.append( Point('energy').field('heatEnergyInNT', heatEnergyInNT).time(pushTime, WritePrecision.S) )
|
|
print("Energy in DB")
|
|
energyCounter = 0
|
|
|
|
for point in pointList: # Print all points in pointlist
|
|
print(point)
|
|
|
|
writeToDB(pointList) # Write pointlist to db
|
|
|
|
time.sleep(10) # Sleep 10 seconds before reading the next value
|