diff -r 4903b4da9d40 -r 602d9968960f thermferm/thermferm.c --- a/thermferm/thermferm.c Sat Apr 30 21:25:10 2016 +0200 +++ b/thermferm/thermferm.c Mon May 02 16:15:37 2016 +0200 @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (C) 2014-2015 + * Copyright (C) 2014-2016 * * Michiel Broek * @@ -34,6 +34,7 @@ #include "futil.h" #include "xutil.h" #include "pid.h" +#include "mqtt.h" int my_shutdown = FALSE; @@ -67,26 +68,6 @@ unsigned char RevHeatONOFF[8] = { 0b11111, 0b10101, 0b10101, 0b10001, 0b10001, 0b10101, 0b10101, 0b11111 }; // [6] reverse HEAT symbol -#ifdef HAVE_MOSQUITTO_H - -#define STATUS_CONNECTING 0 -#define STATUS_CONNACK_RECVD 1 -#define STATUS_WAITING 2 - -/* Global variables for use in callbacks. */ -static int mqtt_qos = 0; -static int mqtt_status = STATUS_CONNECTING; -static int mqtt_mid_sent = 0; -static int mqtt_last_mid = -1; -static int mqtt_last_mid_sent = -1; -static int mqtt_connected = TRUE; -static int mqtt_disconnect_sent = FALSE; -static int mqtt_connect_lost = FALSE; -static int mqtt_my_shutdown = FALSE; -static int mqtt_use = FALSE; - -#endif - int server(void); void help(void); void die(int); @@ -97,6 +78,21 @@ #endif +#ifdef HAVE_MOSQUITTO_H + +extern int mqtt_qos; +extern int mqtt_last_mid; +extern int mqtt_last_mid_sent; +extern int mqtt_mid_sent; +extern int mqtt_disconnect_sent; +extern int mqtt_connected; +extern int mqtt_status; +extern int mqtt_use; +extern struct mosquitto *mosq; +extern char *state; + +#endif + void help(void) { @@ -399,6 +395,7 @@ initlog(current_unit->name); syslog(LOG_NOTICE, "Mode from %s to %s via panel interface", UNITMODE[current_unit->mode], UNITMODE[mode]); current_unit->mode = mode; + current_unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_MODE); /* Allways turn everything off after a mode change */ current_unit->PID_cool->OutP = current_unit->PID_heat->OutP = 0.0; current_unit->PID_cool->Mode = current_unit->PID_heat->Mode = PID_MODE_NONE; @@ -613,6 +610,7 @@ if (temp_temp != current_unit->fridge_set) { syslog(LOG_NOTICE, "Fridge temperature changed from %.1f to %.1f degrees from the panel", current_unit->fridge_set, temp_temp); current_unit->fridge_set = temp_temp; + current_unit->mqtt_flag |= MQTT_FLAG_SP; } go_menu(MENU_MODE_FRIDGE); } @@ -653,6 +651,7 @@ if (temp_temp != current_unit->beer_set) { syslog(LOG_NOTICE, "Beer temperature changed from %.1f to %.1f degrees from the panel", current_unit->beer_set, temp_temp); current_unit->beer_set = temp_temp; + current_unit->mqtt_flag |= MQTT_FLAG_SP; } go_menu(MENU_MODE_BEER); } @@ -734,6 +733,7 @@ current_unit->prof_paused = current_unit->prof_primary_done = 0; current_unit->prof_peak_abs = current_unit->prof_peak_rel = 0.0; syslog(LOG_NOTICE, "Profile started from the panel"); + current_unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT); go_menu(MENU_MODE_PROFILE); } break; @@ -745,6 +745,7 @@ go_menu(MENU_PROFILE_ABORT); if (key == KEY_ENTER) { current_unit->prof_state = PROFILE_PAUSE; + current_unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT); syslog(LOG_NOTICE, "Profile pause from the panel"); go_menu(MENU_MODE_PROFILE); } @@ -763,6 +764,7 @@ current_unit->prof_state = PROFILE_OFF; current_unit->prof_started = 0; syslog(LOG_NOTICE, "Profile aborted from the panel"); + current_unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT); go_menu(MENU_MODE_PROFILE); } break; @@ -775,6 +777,7 @@ if (key == KEY_ENTER) { current_unit->prof_state = PROFILE_RUN; syslog(LOG_NOTICE, "Profile resume from the panel"); + current_unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT); go_menu(MENU_MODE_PROFILE); } break; @@ -785,6 +788,7 @@ if (key == KEY_ENTER) { if (current_unit->prof_state == PROFILE_DONE) { current_unit->prof_state = PROFILE_OFF; + current_unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT); syslog(LOG_NOTICE, "Profile from done to off from the panel"); } } @@ -962,56 +966,6 @@ } -#ifdef HAVE_MOSQUITTO_H - -void my_connect_callback(struct mosquitto *mosq, void *obj, int result) -{ - if (mqtt_connect_lost) { - mqtt_connect_lost = FALSE; - syslog(LOG_NOTICE, "Reconnect: %s", mosquitto_connack_string(result)); - } - - if (!result) { - mqtt_status = STATUS_CONNACK_RECVD; - } else { - syslog(LOG_NOTICE, "my_connect_callback: %s\n", mosquitto_connack_string(result)); - } -} - - - -void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) -{ - if (mqtt_my_shutdown) { - syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host); - mqtt_connected = FALSE; - } else { - /* - * The remote server was brought down. We must keep running - */ - syslog(LOG_NOTICE, "Received DISCONNECT from %s, connection lost", Config.mosq_host); - mqtt_connect_lost = TRUE; - } -} - - -void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) -{ - mqtt_last_mid_sent = mid; -} - - - -void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str) -{ - syslog(LOG_NOTICE, "MQTT: %s", str); - printf("MQTT: %s\n", str); - mqtt_my_shutdown = TRUE; -} - - -#endif - int server(void) { @@ -1032,97 +986,13 @@ float LCDair, LCDbeer, LCDspL, LCDspH; unsigned char LCDstatC, LCDstatH; int LCDunit; -#ifdef HAVE_MOSQUITTO_H - char *id = NULL, *state = NULL, hostname[256], topic[1024], err[1024]; - struct mosquitto *mosq = NULL; - int keepalive = 60; - unsigned int max_inflight = 20; -#endif if (lockprog((char *)"thermferm")) { syslog(LOG_NOTICE, "Can't lock"); return 1; } - -#ifdef HAVE_MOSQUITTO_H - /* - * Initialize mosquitto communication - */ - gethostname(hostname, 255); - mosquitto_lib_init(); - id = xstrcpy((char *)"thermferm/"); - id = xstrcat(id, hostname); - if(strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { - /* - * Enforce maximum client id length of 23 characters - */ - id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; - } - - mosq = mosquitto_new(id, true, NULL); - if(!mosq) { - switch(errno) { - case ENOMEM: - syslog(LOG_NOTICE, "mosquitto_new: Out of memory"); - break; - case EINVAL: - syslog(LOG_NOTICE, "mosquitto_new: Invalid id"); - break; - } - mosquitto_lib_cleanup(); - return 1; - } - - if (debug) { - mosquitto_log_callback_set(mosq, my_log_callback); - } - - /* - * Set our will - */ - state = xstrcpy((char *)"clients/"); - state = xstrcat(state, hostname); - state = xstrcat(state, (char *)"/thermferm/state"); - sprintf(buf, "0"); - if ((rc = mosquitto_will_set(mosq, state, strlen(buf), buf, mqtt_qos, TRUE))) { - if (rc == MOSQ_ERR_INVAL) { - syslog(LOG_NOTICE, "mosquitto_will_set: input parameters invalid"); - } else if (rc == MOSQ_ERR_NOMEM) { - syslog(LOG_NOTICE, "mosquitto_will_set: Out of Memory"); - } else if (rc == MOSQ_ERR_PAYLOAD_SIZE) { - syslog(LOG_NOTICE, "mosquitto_will_set: invalid payload size"); - } - mosquitto_lib_cleanup(); - return rc; - } - - mosquitto_max_inflight_messages_set(mosq, max_inflight); - mosquitto_connect_callback_set(mosq, my_connect_callback); - mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); - mosquitto_publish_callback_set(mosq, my_publish_callback); - - if ((rc = mosquitto_connect(mosq, Config.mosq_host, Config.mosq_port, keepalive))) { - if (rc == MOSQ_ERR_ERRNO) { - strerror_r(errno, err, 1024); - syslog(LOG_NOTICE, "mosquitto_connect: error: %s", err); - } else { - syslog(LOG_NOTICE, "mosquitto_connect: unable to connect (%d)", rc); - } - mosquitto_lib_cleanup(); - } else { - mqtt_use = TRUE; - syslog(LOG_NOTICE, "MQTT connected with %s:%d", Config.mosq_host, Config.mosq_port); - - /* - * Initialise is complete, report our presence state - */ - mosquitto_loop_start(mosq); - sprintf(buf, "1"); - rc = mosquitto_publish(mosq, &mqtt_mid_sent, state, strlen(buf), buf, mqtt_qos, 1); - } -#endif - + mqtt_connect(); if ((rc = devices_detect())) { syslog(LOG_NOTICE, "Detected %d new devices", rc); @@ -1195,43 +1065,36 @@ /* * Safety, turn everything off */ + unit->mqtt_flag = MQTT_FLAG_MODE; unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->light_state = 0; unit->heater_wait = unit->cooler_wait = unit->fan_wait = unit->light_wait = 0; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && (unit->mode != UNITMODE_OFF)) { - sprintf(buf, "1"); - snprintf(topic, 1023, "fermenter/%s/%s/state", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - sprintf(buf, "%s", unit->name); - snprintf(topic, 1023, "fermenter/%s/%s/name", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - sprintf(buf, "0"); - if (unit->heater_address) { - snprintf(topic, 1023, "fermenter/%s/%s/heater", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } - if (unit->cooler_address) { - snprintf(topic, 1023, "fermenter/%s/%s/cooler", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } - if (unit->fan_address) { - snprintf(topic, 1023, "fermenter/%s/%s/fan", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } - snprintf(topic, 1023, "fermenter/%s/%s/mode", hostname, unit->uuid); - sprintf(buf, "%s", UNITMODE[unit->mode]); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + mqtt_publish_int(unit->uuid, (char *)"state", (unit->mode != UNITMODE_OFF) ? 1 : 0); + if (unit->name) + mqtt_publish_str(unit->uuid, (char *)"name", unit->name); + if (unit->heater_address) + mqtt_publish_int(unit->uuid, (char *)"heater", 0); + if (unit->cooler_address) + mqtt_publish_int(unit->uuid, (char *)"cooler", 0); + if (unit->fan_address) + mqtt_publish_int(unit->uuid, (char *)"fan", 0); + if (unit->air_address) + unit->mqtt_flag |= MQTT_FLAG_AIR; + if (unit->beer_address) + unit->mqtt_flag |= MQTT_FLAG_BEER; if (unit->mode == UNITMODE_PROFILE) { if (!unit->profile) syslog(LOG_NOTICE, "Starting unit `%s' in profile mode, no profile defined.", unit->name); - else + else { syslog(LOG_NOTICE, "Starting unit `%s' in profile state %s.", unit->name, PROFSTATE[unit->prof_state]); + unit->mqtt_flag |= MQTT_FLAG_SP; + unit->mqtt_flag |= MQTT_FLAG_PROFILE; + } } else if (unit->mode == UNITMODE_BEER) { syslog(LOG_NOTICE, "Starting unit `%s' beer cooler at %.1f degrees", unit->name, unit->beer_set); + unit->mqtt_flag |= MQTT_FLAG_SP; } else if (unit->mode == UNITMODE_FRIDGE) { syslog(LOG_NOTICE, "Starting unit `%s' as refridgerator at %.1f degrees", unit->name, unit->fridge_set); + unit->mqtt_flag |= MQTT_FLAG_SP; } else if (unit->mode == UNITMODE_NONE) { syslog(LOG_NOTICE, "Starting unit `%s' in inactive state", unit->name); } else { @@ -1316,13 +1179,8 @@ if (Config.temp_address) { rc = device_in(Config.temp_address, &temp); if (rc == DEVPRESENT_YES) { -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && (Config.temp_value != temp)) { - sprintf(buf, "%.1f", temp / 1000.0); - snprintf(topic, 1023, "fermenter/%s/room/temperature", hostname); - rc = mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (Config.temp_value != temp) + mqtt_publish_float((char *)"room", (char *)"temperature", temp / 1000.0, 1); Config.temp_value = temp; Config.temp_state = 0; #ifdef HAVE_WIRINGPI_H @@ -1351,13 +1209,8 @@ if (Config.hum_address) { rc = device_in(Config.hum_address, &temp); if (rc == DEVPRESENT_YES) { -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && (Config.hum_value != temp)) { - sprintf(buf, "%.1f", temp / 1000.0); - snprintf(topic, 1023, "fermenter/%s/room/humidity", hostname); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (Config.hum_value != temp) + mqtt_publish_float((char *)"room", (char *)"humidity", temp / 1000.0, 1); Config.hum_value = temp; Config.hum_state = 0; #ifdef HAVE_WIRINGPI_H @@ -1392,20 +1245,12 @@ deviation = 40000; if ((unit->air_temperature == 0) || (unit->air_temperature && (temp > (int)unit->air_temperature - deviation) && (temp < ((int)unit->air_temperature + deviation)))) { -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && (unit->air_temperature != temp)) { - sprintf(buf, "%.3f", temp / 1000.0); - snprintf(topic, 1023, "fermenter/%s/%s/air/temperature", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->air_temperature != temp) + unit->mqtt_flag |= MQTT_FLAG_AIR; unit->air_temperature = temp; unit->air_state = 0; } else { syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->air_temperature, temp); - if (debug) { - fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->air_temperature, temp); - } } } else if (rc == DEVPRESENT_ERROR) { unit->air_state = 1; @@ -1420,20 +1265,12 @@ deviation = 40000; if ((unit->beer_temperature == 0) || (unit->beer_temperature && (temp > (int)unit->beer_temperature - deviation) && (temp < ((int)unit->beer_temperature + deviation)))) { -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && (unit->beer_temperature != temp)) { - sprintf(buf, "%.3f", temp / 1000.0); - snprintf(topic, 1023, "fermenter/%s/%s/beer/temperature", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->beer_temperature != temp) + unit->mqtt_flag |= MQTT_FLAG_BEER; unit->beer_temperature = temp; unit->beer_state = 0; } else { syslog(LOG_NOTICE, "deviation error deviation=%d, old=%d new=%d", deviation, unit->beer_temperature, temp); - if (debug) { - fprintf(stdout, "deviation error deviation=%d, old=%d new=%d\n", deviation, unit->beer_temperature, temp); - } } } else if (rc == DEVPRESENT_ERROR) { unit->beer_state = 1; @@ -1449,30 +1286,17 @@ if (unit->door_address) { rc = device_in(unit->door_address, &temp); if (rc == DEVPRESENT_YES) { -#ifdef HAVE_MOSQUITTO_H - snprintf(topic, 1023, "fermenter/%s/%s/door", hostname, unit->uuid); -#endif if (temp) { if (unit->door_state == 0) { syslog(LOG_NOTICE, "Unit `%s' door closed", unit->name); unit->door_state = 1; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use) { - sprintf(buf, "closed"); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + mqtt_publish_str(unit->uuid, (char *)"door", (char *)"closed"); } } else { if (unit->door_state) { syslog(LOG_NOTICE, "Unit `%s' door opened", unit->name); unit->door_state = 0; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use) { - sprintf(buf, "open"); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + mqtt_publish_str(unit->uuid, (char *)"door", (char *)"open"); } } } @@ -1489,11 +1313,13 @@ if (unit->psu_state == 0) { syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is on", unit->name); unit->psu_state = 1; + mqtt_publish_str(unit->uuid, (char *)"12volt", (char *)"on"); } } else { if (unit->psu_state) { syslog(LOG_NOTICE, "Unit `%s' PSU (12 volt) is off", unit->name); unit->psu_state = 0; + mqtt_publish_str(unit->uuid, (char *)"12volt", (char *)"off"); } } } @@ -1642,6 +1468,7 @@ profile->name, run_hours / 24, run_hours % 24, run_minutes % 60, current_step, unit->prof_percent, unit->prof_fridge_mode ? (char *)"air":(char *)"beer", unit->prof_target_lo, unit->prof_target_hi); + unit->mqtt_flag |= (MQTT_FLAG_PERCENT | MQTT_FLAG_PROFILE | MQTT_FLAG_SP); } } else { /* @@ -1650,6 +1477,7 @@ unit->prof_state = PROFILE_DONE; unit->prof_percent = 100; syslog(LOG_NOTICE, "Profile `%s' is done", profile->name); + unit->mqtt_flag |= (MQTT_FLAG_PERCENT | MQTT_FLAG_PROFILE | MQTT_FLAG_SP); } break; @@ -1824,13 +1652,8 @@ if (unit->heater_state != power) { syslog(LOG_NOTICE, "Unit `%s' heater %d%% => %d%%", unit->name, unit->heater_state, power); unit->heater_state = power; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && unit->heater_address) { - sprintf(buf, "%d", unit->heater_state); - snprintf(topic, 1023, "fermenter/%s/%s/heater", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->heater_address) + mqtt_publish_int(unit->uuid, (char *)"heater", unit->heater_state); } } } else { @@ -1840,13 +1663,8 @@ if (unit->heater_state) { syslog(LOG_NOTICE, "Unit `%s' heater On => Off", unit->name); unit->heater_state = 0; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && unit->heater_address) { - sprintf(buf, "0"); - snprintf(topic, 1023, "fermenter/%s/%s/heater", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->heater_address) + mqtt_publish_int(unit->uuid, (char *)"heater", 0); } } } @@ -1865,13 +1683,8 @@ if (unit->cooler_state != power) { syslog(LOG_NOTICE, "Unit `%s' cooler %d%% => %d%%", unit->name, unit->cooler_state, power); unit->cooler_state = power; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && unit->cooler_address) { - sprintf(buf, "%d", unit->cooler_state); - snprintf(topic, 1023, "fermenter/%s/%s/cooler", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->cooler_address) + mqtt_publish_int(unit->uuid, (char *)"cooler", unit->cooler_state); } } } else { @@ -1881,13 +1694,8 @@ if (unit->cooler_state) { syslog(LOG_NOTICE, "Unit `%s' cooler On => Off", unit->name); unit->cooler_state = 0; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && unit->cooler_address) { - sprintf(buf, "0"); - snprintf(topic, 1023, "fermenter/%s/%s/cooler", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->cooler_address) + mqtt_publish_int(unit->uuid, (char *)"cooler", 0); } } } @@ -1912,13 +1720,8 @@ if (! unit->fan_state) { syslog(LOG_NOTICE, "Unit `%s' Fan Off => On", unit->name); unit->fan_state = 100; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && unit->fan_address) { - sprintf(buf, "100"); - snprintf(topic, 1023, "fermenter/%s/%s/fan", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->fan_address) + mqtt_publish_int(unit->uuid, (char *)"fan", 100); } } } else { @@ -1928,13 +1731,8 @@ if (unit->fan_state) { syslog(LOG_NOTICE, "Unit `%s' Fan On => Off", unit->name); unit->fan_state = 0; -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && unit->fan_address) { - sprintf(buf, "0"); - snprintf(topic, 1023, "fermenter/%s/%s/fan", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } -#endif + if (unit->fan_address) + mqtt_publish_int(unit->uuid, (char *)"fan", 0); } } } @@ -1974,16 +1772,9 @@ LCDspH = unit->prof_target_hi; } } -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && (seconds == 60) && ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE))) { - sprintf(buf, "%.1f", LCDspH); - snprintf(topic, 1023, "fermenter/%s/%s/setpoint/high", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - sprintf(buf, "%.1f", LCDspL); - snprintf(topic, 1023, "fermenter/%s/%s/setpoint/low", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); + if ((seconds == 60) && ((unit->mode == UNITMODE_FRIDGE) || (unit->mode == UNITMODE_BEER) || (unit->mode == UNITMODE_PROFILE))) { + unit->mqtt_flag |= MQTT_FLAG_SP; } -#endif #ifdef HAVE_WIRINGPI_H piLock(LOCK_LCD); #endif @@ -1997,6 +1788,31 @@ #ifdef HAVE_WIRINGPI_H piUnlock(LOCK_LCD); #endif + + /* + * Publish MQTT messages set in flag + */ + if (unit->mqtt_flag & MQTT_FLAG_SP) { + mqtt_publish_float(unit->uuid, (char *)"setpoint/high", LCDspH, 1); + mqtt_publish_float(unit->uuid, (char *)"setpoint/low", LCDspL, 1); + } + if (unit->mqtt_flag & MQTT_FLAG_AIR) { + mqtt_publish_float(unit->uuid, (char *)"air/temperature", unit->air_temperature / 1000.0, 3); + } + if (unit->mqtt_flag & MQTT_FLAG_BEER) { + mqtt_publish_float(unit->uuid, (char *)"beer/temperature", unit->beer_temperature / 1000.0, 3); + } + if (unit->mqtt_flag & MQTT_FLAG_MODE) { + mqtt_publish_str(unit->uuid, (char *)"mode", (char *)UNITMODE[unit->mode]); + } + if (unit->mqtt_flag & MQTT_FLAG_PROFILE) { + mqtt_publish_str(unit->uuid, (char *)"profile/uuid", unit->profile); + mqtt_publish_str(unit->uuid, (char *)"profile/state", (char *)PROFSTATE[unit->prof_state]); + } + if (unit->mqtt_flag & MQTT_FLAG_PERCENT) { + mqtt_publish_int(unit->uuid, (char *)"profile/percent", unit->prof_percent); + } + unit->mqtt_flag = 0; } /* for units */ #ifdef HAVE_WIRINGPI_H @@ -2110,25 +1926,15 @@ */ for (unit = Config.units; unit; unit = unit->next) { -#ifdef HAVE_MOSQUITTO_H - if (mqtt_use && (unit->mode != UNITMODE_OFF)) { - sprintf(buf, "0"); - if (unit->heater_address) { - snprintf(topic, 1023, "fermenter/%s/%s/heater", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } - if (unit->cooler_address) { - snprintf(topic, 1023, "fermenter/%s/%s/cooler", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } - if (unit->fan_address) { - snprintf(topic, 1023, "fermenter/%s/%s/fan", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); - } - snprintf(topic, 1023, "fermenter/%s/%s/state", hostname, unit->uuid); - mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1); + if (unit->mode != UNITMODE_OFF) { + if (unit->heater_address) + mqtt_publish_int(unit->uuid, (char *)"heater", 0); + if (unit->cooler_address) + mqtt_publish_int(unit->uuid, (char *)"cooler", 0); + if (unit->fan_address) + mqtt_publish_int(unit->uuid, (char *)"fan", 0); + mqtt_publish_int(unit->uuid, (char *)"state", 0); } -#endif /* * Turn everything off @@ -2142,36 +1948,7 @@ syslog(LOG_NOTICE, "Unit `%s' stopped in mode %s", unit->name, UNITMODE[unit->mode]); } -#ifdef HAVE_MOSQUITTO_H - - if (mqtt_use) { - /* - * Final publish 0 to clients//thermferm/state - */ - syslog(LOG_NOTICE, "MQTT disconnecting"); - sprintf(buf, "0"); - mosquitto_publish(mosq, &mqtt_mid_sent, state, strlen(buf), buf, mqtt_qos, true); - mqtt_last_mid = mqtt_mid_sent; - mqtt_status = STATUS_WAITING; - - do { - if (mqtt_status == STATUS_WAITING) { - if (debug) - fprintf(stdout, (char *)"Waiting\n"); - if (mqtt_last_mid_sent == mqtt_last_mid && mqtt_disconnect_sent == FALSE) { - mosquitto_disconnect(mosq); - mqtt_disconnect_sent = TRUE; - } - usleep(100000); - } - rc = MOSQ_ERR_SUCCESS; - } while (rc == MOSQ_ERR_SUCCESS && mqtt_connected); - - mosquitto_loop_stop(mosq, FALSE); - mosquitto_destroy(mosq); - mosquitto_lib_cleanup(); - } -#endif + mqtt_disconnect(); syslog(LOG_NOTICE, "Out of loop"); if (debug)