2 * A MQTT Gateway Daemon for the Air Conditioning Controller
4 * (c) 2019 Martin Mares <mj@ucw.cz>
10 #include <ucw/strtonum.h>
11 #include <ucw/stkstring.h>
22 #include <mosquitto.h>
24 #include "../firmware/registers.h"
28 static struct mosquitto *mosq;
29 static bool mqtt_connected;
31 static void mqtt_error(const char *operation, int err, bool teardown)
33 msg(L_ERROR, "Mosquitto: %s failed: error %d", operation, err);
36 mosquitto_destroy(mosq);
38 mqtt_connected = false;
39 } else if (err == MOSQ_ERR_NO_CONN || err == MOSQ_ERR_CONN_REFUSED || err == MOSQ_ERR_CONN_LOST) {
40 mqtt_connected = false;
44 static void mqtt_publish(const char *topic, const char *fmt, ...)
51 int l = vsnprintf(m, sizeof(m), fmt, args);
52 int err = mosquitto_publish(mosq, NULL, topic, l, m, 0, true);
53 if (err != MOSQ_ERR_SUCCESS)
54 mqtt_error("publish", err, false);
60 static void mqtt_log_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, int level, const char *message)
62 // msg(L_INFO, "MQTT(%d): %s", level, message);
65 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m);
67 static bool mqtt_connect(void)
75 mosq = mosquitto_new("aircond", 1, NULL);
77 die("Mosquitto: initialization failed");
79 mosquitto_log_callback_set(mosq, mqtt_log_callback);
80 mosquitto_message_callback_set(mosq, mqtt_msg_callback);
82 err = mosquitto_will_set(mosq, "status/aircon", 4, "dead", 0, true);
83 if (err != MOSQ_ERR_SUCCESS) {
84 mqtt_error("will_set", err, true);
88 err = mosquitto_connect(mosq, "127.0.0.1", 1883, 60);
89 if (err != MOSQ_ERR_SUCCESS) {
90 mqtt_error("connect", err, true);
94 err = mosquitto_reconnect(mosq);
95 if (err != MOSQ_ERR_SUCCESS) {
96 mqtt_error("reconnect", err, false);
101 err = mosquitto_subscribe(mosq, NULL, "burrow/air/#", 1);
102 if (err != MOSQ_ERR_SUCCESS) {
103 mqtt_error("subscribe", err, false);
107 mqtt_connected = true;
109 mqtt_publish("status/aircon", "ok");
111 return mqtt_connected;
116 static modbus_t *modbus;
117 static bool mb_is_open;
119 static void mb_error(const char *operation)
121 msg(L_ERROR, "MODBUS: %s failed: %s", operation, modbus_strerror(errno));
125 modbus_close(modbus);
133 static bool mb_connect(void)
138 // FIXME: Find the right device. Reconnect if needed.
139 modbus = modbus_new_rtu("/dev/ttyUSB1", 19200, 'E', 8, 1);
145 modbus_set_slave(modbus, 42);
147 if (modbus_connect(modbus) < 0) {
157 static void scan_temperatures(time_t now)
159 static const char * const temp_names[] = {
168 if (modbus_read_input_registers(modbus, AIRCON_IREG_TEMP_FROM_INSIDE, 5, regs) < 0) {
173 for (uint i=0; i<5; i++) {
174 if (regs[i] != 0x8000) {
178 mqtt_publish(stk_printf("burrow/air/%s", temp_names[i]), "%.3f %llu", t / 100., (unsigned long long) now);
183 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m)
186 if (m->payloadlen >= sizeof(val) - 1) {
187 msg(L_ERROR, "Invalid value for topic %s", m->topic);
190 memcpy(val, m->payload, m->payloadlen);
191 val[m->payloadlen] = 0;
192 msg(L_DEBUG, "MQTT < %s %s", m->topic, val);
194 if (!strcmp(m->topic, "burrow/air/bypass")) {
197 if ((err = str_to_uint(&x, val, NULL, 10 | STN_WHOLE)) || x >= 2) {
198 msg(L_ERROR, "Received invalid bypass setting %s: %s", val, err);
201 if (modbus_write_bit(modbus, AIRCON_COIL_EXCHANGER_BYPASS, x) < 0)
202 msg(L_ERROR, "Modbus write failed: %s", modbus_strerror(errno));
203 } else if (!strcmp(m->topic, "burrow/air/exchanger-fan")) {
206 if ((err = str_to_uint(&x, val, NULL, 10 | STN_WHOLE)) || x >= 256) {
207 msg(L_ERROR, "Received invalid exchanger fan setting %s: %s", val, err);
210 if (modbus_write_register(modbus, AIRCON_HREG_EXCHANGER_FAN, x) < 0)
211 msg(L_ERROR, "Modbus write failed: %s", modbus_strerror(errno));
215 static int use_daemon;
216 static int use_debug;
218 static struct opt_section options = {
220 OPT_HELP("A daemon for controlling the air conditioning controller via MQTT"),
222 OPT_HELP("Options:"),
223 OPT_BOOL('d', "debug", use_debug, 0, "\tLog debugging messages"),
224 OPT_BOOL(0, "daemon", use_daemon, 0, "\tDaemonize"),
231 int main(int argc UNUSED, char **argv)
234 opt_parse(&options, argv+1);
237 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
238 log_set_default_stream(ls);
241 log_default_stream()->levels &= ~(1U << L_DEBUG);
243 mosquitto_lib_init();
247 if (!mqtt_connect() || !mb_connect()) {
252 time_t now = time(NULL);
253 if (now < next_run) {
254 int err = mosquitto_loop(mosq, (next_run - now) * 1000, 1);
255 if (err != MOSQ_ERR_SUCCESS)
256 mqtt_error("loop", err, false);
261 scan_temperatures(now);