fixed, working version of sdm630_mqtt
This commit is contained in:
parent
c02c770776
commit
0f304ea949
1 changed files with 64 additions and 57 deletions
|
@ -31,6 +31,8 @@ class SDM630_MQTT:
|
||||||
self.mqtt.on_connect = self._on_mqtt_connect
|
self.mqtt.on_connect = self._on_mqtt_connect
|
||||||
self.mqtt.on_disconnect = self._on_mqtt_disconnect
|
self.mqtt.on_disconnect = self._on_mqtt_disconnect
|
||||||
|
|
||||||
|
self.idx = 0
|
||||||
|
|
||||||
def _publish(self, topic, msg):
|
def _publish(self, topic, msg):
|
||||||
LOG.debug(f"_publish({topic!r}, {msg!r})")
|
LOG.debug(f"_publish({topic!r}, {msg!r})")
|
||||||
self.mqtt.publish(f"{self.mqtt_prefix.rstrip('/')}/{topic}", msg)
|
self.mqtt.publish(f"{self.mqtt_prefix.rstrip('/')}/{topic}", msg)
|
||||||
|
@ -45,15 +47,16 @@ class SDM630_MQTT:
|
||||||
LOG.debug("_start()")
|
LOG.debug("_start()")
|
||||||
self.mqtt.connect(self.mqtt_host, self.mqtt_port, 10)
|
self.mqtt.connect(self.mqtt_host, self.mqtt_port, 10)
|
||||||
self.mqtt.loop_start()
|
self.mqtt.loop_start()
|
||||||
|
self.modbus.open()
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
|
LOG.debug("_stop()")
|
||||||
|
self.modbus.close()
|
||||||
self.mqtt.loop_stop()
|
self.mqtt.loop_stop()
|
||||||
self.mqtt.disconnect()
|
self.mqtt.disconnect()
|
||||||
|
|
||||||
def request_and_publish(self):
|
def request_and_publish(self):
|
||||||
self.modbus.open()
|
for readable, address in {
|
||||||
for idx, (readable, address) in enumerate(
|
|
||||||
{
|
|
||||||
"active-power/L1": 0x000C,
|
"active-power/L1": 0x000C,
|
||||||
"active-power/L2": 0x000E,
|
"active-power/L2": 0x000E,
|
||||||
"active-power/L3": 0x0010,
|
"active-power/L3": 0x0010,
|
||||||
|
@ -102,10 +105,13 @@ class SDM630_MQTT:
|
||||||
"voltage/L3_L1": 0x00CC,
|
"voltage/L3_L1": 0x00CC,
|
||||||
"voltage/L3_N": 0x0004,
|
"voltage/L3_N": 0x0004,
|
||||||
"voltage/average": 0x002A,
|
"voltage/average": 0x002A,
|
||||||
}.items()
|
}.items():
|
||||||
):
|
|
||||||
try:
|
try:
|
||||||
read = self.modbus.read_input_registers(address, 2)
|
read = self.modbus.read_input_registers(address, 2)
|
||||||
|
self.idx += 1
|
||||||
|
if read is None:
|
||||||
|
LOG.error(f'reading {readable} was None: {self.modbus.last_error_as_txt}')
|
||||||
|
continue
|
||||||
longs = word_list_to_long(read)
|
longs = word_list_to_long(read)
|
||||||
if len(longs) == 1:
|
if len(longs) == 1:
|
||||||
self._publish(readable, decode_ieee(longs[0]))
|
self._publish(readable, decode_ieee(longs[0]))
|
||||||
|
@ -113,10 +119,10 @@ class SDM630_MQTT:
|
||||||
self._publish(readable, sum([decode_ieee(x) for x in longs]))
|
self._publish(readable, sum([decode_ieee(x) for x in longs]))
|
||||||
except Exception:
|
except Exception:
|
||||||
LOG.exception(f'error while reading {readable} from meter')
|
LOG.exception(f'error while reading {readable} from meter')
|
||||||
if idx % 20:
|
if self.idx >= 20:
|
||||||
self.modbus.close()
|
self.modbus.close()
|
||||||
self.modbus.open()
|
self.modbus.open()
|
||||||
self.modbus.close()
|
self.idx = 0
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -127,11 +133,12 @@ if __name__ == "__main__":
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Usage: {argv[0]} config.toml")
|
print(f"Usage: {argv[0]} config.toml")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
try:
|
||||||
client = SDM630_MQTT(config)
|
client = SDM630_MQTT(config)
|
||||||
client.start()
|
client.start()
|
||||||
try:
|
|
||||||
while True:
|
while True:
|
||||||
client.request_and_publish()
|
client.request_and_publish()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
LOG.exception("oops")
|
LOG.exception("oops")
|
||||||
|
finally:
|
||||||
client.stop()
|
client.stop()
|
||||||
|
|
Loading…
Reference in a new issue