ctucx.git: sdm2mqtt

Publish data from SDM120M meters via MQTT

commit 2268741b67e973926dafc289109d0da565ab4be0
parent d16ff597a3de39b682541d9f5a043acda1f3c50e
Author: Leah (ctucx) <git@ctu.cx>
Date: Wed, 14 Dec 2022 15:33:20 +0100

only publish if connected
1 file changed, 8 insertions(+), 7 deletions(-)
M
src/sdm2mqtt.nim
|
15
++++++++-------
diff --git a/src/sdm2mqtt.nim b/src/sdm2mqtt.nim
@@ -37,13 +37,14 @@ proc updatePowermeter (deviceAddress: uint8, deviceName: string) {.async.} =
     "import":     `import`
   }
 
-  await mqttContext.publish("sdm2mqtt/"&deviceName,              $json,        2, true)
-  await mqttContext.publish("sdm2mqtt/"&deviceName&"/id",        $deviceName,  2, true)
-  await mqttContext.publish("sdm2mqtt/"&deviceName&"/voltage",   $voltage,     2, true)
-  await mqttContext.publish("sdm2mqtt/"&deviceName&"/frequency", $frequency,   2, true)
-  await mqttContext.publish("sdm2mqtt/"&deviceName&"/import",    $`import`,    2, true)
-  await mqttContext.publish("sdm2mqtt/"&deviceName&"/cosphi",    $cosphi,      2, true)
-  await mqttContext.publish("sdm2mqtt/"&deviceName&"/power",     $power,       2, true)
+  if mqttContext.isConnected:
+    await mqttContext.publish("sdm2mqtt/"&deviceName,              $json,        2, true)
+    await mqttContext.publish("sdm2mqtt/"&deviceName&"/id",        $deviceName,  2, true)
+    await mqttContext.publish("sdm2mqtt/"&deviceName&"/voltage",   $voltage,     2, true)
+    await mqttContext.publish("sdm2mqtt/"&deviceName&"/frequency", $frequency,   2, true)
+    await mqttContext.publish("sdm2mqtt/"&deviceName&"/import",    $`import`,    2, true)
+    await mqttContext.publish("sdm2mqtt/"&deviceName&"/cosphi",    $cosphi,      2, true)
+    await mqttContext.publish("sdm2mqtt/"&deviceName&"/power",     $power,       2, true)
 
 proc updatePowermeters (config: Config) {.async.} =
   await sleepAsync(500)