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, bool need_close)
121 msg(L_ERROR, "MODBUS: %s failed: %s", operation, modbus_strerror(errno));
123 if (need_close && modbus) {
125 modbus_close(modbus);
133 static bool mb_connect(void)
138 modbus = modbus_new_rtu("/dev/modbus-aircon", 19200, 'E', 8, 1);
140 mb_error("open", true);
144 modbus_set_slave(modbus, 42);
146 if (modbus_connect(modbus) < 0) {
147 mb_error("connect", true);
156 static void scan_temperatures(time_t now)
158 static const char * const temp_names[] = {
167 if (modbus_read_input_registers(modbus, AIRCON_IREG_TEMP_FROM_INSIDE, 5, regs) < 0) {
168 mb_error("read", true);
172 for (uint i=0; i<5; i++) {
173 if (regs[i] != 0x8000) {
177 mqtt_publish(stk_printf("burrow/air/%s", temp_names[i]), "%.3f %llu", t / 100., (unsigned long long) now);
182 static void mqtt_msg_callback(struct mosquitto *mosq UNUSED, void *obj UNUSED, const struct mosquitto_message *m)
185 if (m->payloadlen >= sizeof(val) - 1) {
186 msg(L_ERROR, "Invalid value for topic %s", m->topic);
189 memcpy(val, m->payload, m->payloadlen);
190 val[m->payloadlen] = 0;
191 msg(L_DEBUG, "MQTT < %s %s", m->topic, val);
193 if (!strcmp(m->topic, "burrow/air/bypass")) {
196 if ((err = str_to_uint(&x, val, NULL, 10 | STN_WHOLE)) || x >= 2) {
197 msg(L_ERROR, "Received invalid bypass setting %s: %s", val, err);
200 if (modbus_write_bit(modbus, AIRCON_COIL_EXCHANGER_BYPASS, x) < 0)
201 mb_error("coil write", false);
202 } else if (!strcmp(m->topic, "burrow/air/exchanger-fan")) {
205 if ((err = str_to_uint(&x, val, NULL, 10 | STN_WHOLE)) || x >= 256) {
206 msg(L_ERROR, "Received invalid exchanger fan setting %s: %s", val, err);
209 if (modbus_write_register(modbus, AIRCON_HREG_EXCHANGER_FAN, x) < 0)
210 mb_error("fan register write", false);
211 } else if (!strcmp(m->topic, "burrow/air/aircon-remote")) {
212 if (strlen(val) != 1) {
213 msg(L_ERROR, "Received invalid aircon remote command %s", val);
216 if (modbus_write_register(modbus, AIRCON_HREG_REMOTE_CONTROL, val[0]) < 0)
217 mb_error("remote control register write", false);
221 static int use_daemon;
222 static int use_debug;
224 static struct opt_section options = {
226 OPT_HELP("A daemon for controlling the air conditioning controller via MQTT"),
228 OPT_HELP("Options:"),
229 OPT_BOOL('d', "debug", use_debug, 0, "\tLog debugging messages"),
230 OPT_BOOL(0, "daemon", use_daemon, 0, "\tDaemonize"),
237 int main(int argc UNUSED, char **argv)
240 opt_parse(&options, argv+1);
243 struct log_stream *ls = log_new_syslog("daemon", LOG_PID);
244 log_set_default_stream(ls);
247 log_default_stream()->levels &= ~(1U << L_DEBUG);
249 mosquitto_lib_init();
253 if (!mqtt_connect() || !mb_connect()) {
258 time_t now = time(NULL);
259 if (now < next_run) {
260 int err = mosquitto_loop(mosq, (next_run - now) * 1000, 1);
261 if (err != MOSQ_ERR_SUCCESS)
262 mqtt_error("loop", err, false);
267 scan_temperatures(now);