Compare commits
5 Commits
231d90ece5
...
main
Author | SHA1 | Date | |
---|---|---|---|
a39cfe2bb5
|
|||
c67b5eb24a
|
|||
1d574a2cae | |||
8509f99483 | |||
f23cb435d2 |
1
.env
1
.env
@@ -11,5 +11,6 @@ MQUSER=<USER OF THE MQTT BROKER>
|
||||
MQPWD=<PASSWORD OF THE MQTT BROKER>
|
||||
MQTOPIC_CUR=<TOPIC WHERE THE CURRENT STATUS SHALL BE WRITTEN TO>
|
||||
MQTOPIC_SUM=<TOPIC WHERE THE TOTAL STATUS SHALL BE WRITTEN TO>
|
||||
BLOCK_ZERO_READS=<BLOCK WRITING DATA TO MQTT WHEN THE FEEDIN or CONSUMPTION COUNTERS ARE READ AS 0 (HAPPENS WHEN THE READ HEAD IS TAKEN FROM THE SMARTMETER)>
|
||||
PYTHONUNBUFFERED=1
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#!/usr/local/bin/python3
|
||||
import sys
|
||||
import serial
|
||||
from time import sleep
|
||||
|
||||
class bcolors:
|
||||
HEADER = '\033[95m'
|
||||
@@ -42,6 +43,7 @@ class LGE320:
|
||||
# formula: (((b1*256)+b2)*256+b3)*256+b4
|
||||
#result[energy_value] = ((( watthours[0]*256 ) + watthours[1] )*256 + watthours[2])*256 + watthours[3]
|
||||
result[energy_value] = int.from_bytes(watthours, "big", signed=True)
|
||||
|
||||
return result
|
||||
|
||||
def read(self):
|
||||
@@ -68,7 +70,15 @@ class LGE320:
|
||||
"T2": read_buffer[read_buffer.find(b'\x07\x01\x00\x02\x08\x00\xFF'):read_buffer.find(b'\x01\x77\x07\x01\x00\x10\x07\x00')], # 2.8.0
|
||||
"P": read_buffer[read_buffer.find(b'\x07\x01\x00\x10\x07\x00\xFF'):read_buffer.find(b'\x01\x01\x01\x63')], # current power
|
||||
}
|
||||
return self._get_energy_value(energy_values)
|
||||
|
||||
result = self._get_energy_value(energy_values) # extract and convert binary data
|
||||
|
||||
if block_zero_reads and (float(result["T1"]) == 0.0 or float(result["T2"]) == 0.0): # zero reads may happen when the read head is taken from the meter
|
||||
print(f"{bcolors.WARNING}Received zero readings: {bcolors.ENDC} @{bcolors.OKBLUE}{datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}{bcolors.ENDC} | {bcolors.OKCYAN}T1.8.0:{bcolors.ENDC} {bcolors.BOLD}{(result['T1'])/10}{bcolors.ENDC} | {bcolors.OKCYAN}T2.8.0:{bcolors.ENDC} {bcolors.BOLD}{(result['T2'])/10}{bcolors.ENDC} | {bcolors.FAIL}Ignoring these (because of BLOCK_ZERO_READS) and waiting for 1s before trying again.{bcolors.ENDC}")
|
||||
sleep(1)
|
||||
return self.read()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
# report data to influxdb (every 10s) and mqtt (as often as possible; when new data is received)
|
||||
@@ -106,6 +116,7 @@ if __name__ == "__main__": # if run from cli (not imported as a module)
|
||||
print(bcolors.WARNING + f"No serial port specified, trying {port}" + bcolors.ENDC)
|
||||
lge320 = LGE320(port)
|
||||
|
||||
block_zero_reads = os.getenv('BLOCK_ZERO_READS', True)
|
||||
|
||||
## define the clients
|
||||
def on_connect(client, userdata, flags, reason_code, properties):
|
||||
@@ -123,8 +134,6 @@ if __name__ == "__main__": # if run from cli (not imported as a module)
|
||||
mqtt_client.connect(mq_broker, mq_port, 60)
|
||||
mqtt_client.loop_start()
|
||||
|
||||
influx_client = InfluxDBClient(db_host, db_port, db_user, db_pwd, db_name)
|
||||
|
||||
## define the report functions (to be run in a thread)
|
||||
def threaded_mqtt_report():
|
||||
last_data = data
|
||||
@@ -172,6 +181,7 @@ if __name__ == "__main__": # if run from cli (not imported as a module)
|
||||
}
|
||||
}
|
||||
]
|
||||
influx_client = InfluxDBClient(db_host, db_port, db_user, db_pwd, db_name)
|
||||
influx_client.write_points(json_body)
|
||||
print(f"{bcolors.OKBLUE}Written data to the {bcolors.BOLD}influx database.{bcolors.ENDC}")
|
||||
|
||||
|
Reference in New Issue
Block a user