]> mj.ucw.cz Git - home-hw.git/blobdiff - power/daemon/burrow-powerd.c
Updated to new MQTT server and fixed a couple of bugs
[home-hw.git] / power / daemon / burrow-powerd.c
index 93e37bb6503821462ed1480ae946fed15cf06fce..ad34efbea551c7405d224370ec048b265354bc2e 100644 (file)
@@ -16,6 +16,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 
 #include <modbus.h>
 #include <unistd.h>
 
 #include <modbus.h>
@@ -56,7 +57,7 @@ static void mqtt_publish(const char *topic, const char *fmt, ...)
        va_end(args);
 }
 
        va_end(args);
 }
 
-static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
+static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level UNUSED, const char *message UNUSED)
 {
        // msg(L_INFO, "MQTT(%d): %s", level, message);
 }
 {
        // msg(L_INFO, "MQTT(%d): %s", level, message);
 }
@@ -125,8 +126,7 @@ static bool mb_connect(void)
        if (modbus)
                return true;
 
        if (modbus)
                return true;
 
-       // FIXME: Find the right device. Reconnect if needed.
-       modbus = modbus_new_rtu("/dev/ttyUSB0", 9600, 'N', 8, 1);
+       modbus = modbus_new_rtu("/dev/modbus-power", 9600, 'N', 8, 1);
        if (!modbus) {
                mb_error("open");
                return false;
        if (!modbus) {
                mb_error("open");
                return false;
@@ -140,13 +140,14 @@ static bool mb_connect(void)
        }
 
        mb_is_open = true;
        }
 
        mb_is_open = true;
+       return true;
 }
 
 /*** Main loop ***/
 
 static u16 mb_regs[22];
 
 }
 
 /*** Main loop ***/
 
 static u16 mb_regs[22];
 
-s32 get_s32(uint i)
+static s32 get_s32(uint i)
 {
        if (mb_regs[i+1] < 0x8000)
                return (mb_regs[i+1] << 16) | mb_regs[i];
 {
        if (mb_regs[i+1] < 0x8000)
                return (mb_regs[i+1] << 16) | mb_regs[i];
@@ -168,8 +169,8 @@ static void scan_power_meter(time_t now)
        mqtt_publish("burrow/power/voltage/l3n", "%.1f %ld", get_s32(4) / 10., lnow);
 
        mqtt_publish("burrow/power/current/l1", "%.3f %ld", get_s32(6) / 1000., lnow);
        mqtt_publish("burrow/power/voltage/l3n", "%.1f %ld", get_s32(4) / 10., lnow);
 
        mqtt_publish("burrow/power/current/l1", "%.3f %ld", get_s32(6) / 1000., lnow);
-       mqtt_publish("burrow/power/current/l1", "%.3f %ld", get_s32(8) / 1000., lnow);
-       mqtt_publish("burrow/power/current/l1", "%.3f %ld", get_s32(10) / 1000., lnow);
+       mqtt_publish("burrow/power/current/l2", "%.3f %ld", get_s32(8) / 1000., lnow);
+       mqtt_publish("burrow/power/current/l3", "%.3f %ld", get_s32(10) / 1000., lnow);
 
        mqtt_publish("burrow/power/power", "%.1f %ld", get_s32(12) / 10., lnow);
        mqtt_publish("burrow/power/energy", "%.1f %ld", get_s32(14) / 10., lnow);
 
        mqtt_publish("burrow/power/power", "%.1f %ld", get_s32(12) / 10., lnow);
        mqtt_publish("burrow/power/energy", "%.1f %ld", get_s32(14) / 10., lnow);