Created mqtt sourcefiles. Use flags to trigger publish messages. The main source does not know and does not care if MQTT messages will be sent. Version 0.5.5

Mon, 02 May 2016 16:15:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 02 May 2016 16:15:37 +0200
changeset 499
602d9968960f
parent 498
4903b4da9d40
child 500
5aa914eb644e

Created mqtt sourcefiles. Use flags to trigger publish messages. The main source does not know and does not care if MQTT messages will be sent. Version 0.5.5

configure file | annotate | diff | comparison | revisions
configure.ac file | annotate | diff | comparison | revisions
thermferm/Makefile file | annotate | diff | comparison | revisions
thermferm/mqtt.c file | annotate | diff | comparison | revisions
thermferm/mqtt.h file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
thermferm/thermferm.c file | annotate | diff | comparison | revisions
thermferm/thermferm.h file | annotate | diff | comparison | revisions
--- a/configure	Sat Apr 30 21:25:10 2016 +0200
+++ b/configure	Mon May 02 16:15:37 2016 +0200
@@ -2035,7 +2035,7 @@
 
 
 PACKAGE="mbsePi-apps"
-VERSION="0.5.4"
+VERSION="0.5.5"
 COPYRIGHT="Copyright (C) 2014-2016 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2016"
 
--- a/configure.ac	Sat Apr 30 21:25:10 2016 +0200
+++ b/configure.ac	Mon May 02 16:15:37 2016 +0200
@@ -8,7 +8,7 @@
 dnl General settings
 dnl After changeing the version number, run autoconf!
 PACKAGE="mbsePi-apps"
-VERSION="0.5.4"
+VERSION="0.5.5"
 COPYRIGHT="Copyright (C) 2014-2016 Michiel Broek, All Rights Reserved"
 CYEARS="2014-2016"
 AC_SUBST(PACKAGE)
--- a/thermferm/Makefile	Sat Apr 30 21:25:10 2016 +0200
+++ b/thermferm/Makefile	Mon May 02 16:15:37 2016 +0200
@@ -55,18 +55,19 @@
 # DO NOT DELETE THIS LINE - MAKE DEPEND RELIES ON IT
 # Dependencies generated by make depend
 rc-switch.o: thermferm.h xutil.h rc-switch.h
+mqtt.o: thermferm.h xutil.h mqtt.h
 slcd.o: thermferm.h slcd.h futil.h xutil.h
 panel.o: thermferm.h lcd-pcf8574.h slcd.h panel.h
 devices.o: thermferm.h devices.h rc-switch.h panel.h xutil.h
 lcd-buffer.o: thermferm.h lcd-buffer.h lcd-pcf8574.h slcd.h panel.h
 futil.o: thermferm.h futil.h
-thermferm.o: lock.h logger.h rdconfig.h devices.h server.h thermferm.h simulator.h lcd-pcf8574.h lcd-buffer.h slcd.h panel.h futil.h xutil.h pid.h
+thermferm.o: lock.h logger.h rdconfig.h devices.h server.h thermferm.h simulator.h lcd-pcf8574.h lcd-buffer.h slcd.h panel.h futil.h xutil.h pid.h mqtt.h
 lock.o: lock.h thermferm.h
 logger.o: logger.h thermferm.h futil.h xutil.h
 lcd-pcf8574.o: thermferm.h lcd-pcf8574.h slcd.h
 pid.o: thermferm.h pid.h
 xutil.o: thermferm.h xutil.h
-server.o: rdconfig.h thermferm.h logger.h devices.h server.h lcd-buffer.h xutil.h
+server.o: rdconfig.h thermferm.h logger.h devices.h server.h lcd-buffer.h xutil.h mqtt.h
 simulator.o: thermferm.h simulator.h
 rdconfig.o: rdconfig.h thermferm.h pid.h futil.h xutil.h
 # End of generated dependencies
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/thermferm/mqtt.c	Mon May 02 16:15:37 2016 +0200
@@ -0,0 +1,270 @@
+/*****************************************************************************
+ * Copyright (C) 2016
+ *   
+ * Michiel Broek <mbroek at mbse dot eu>
+ *
+ * This file is part of the mbsePi-apps
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * mbsePi-apps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with ThermFerm; see the file COPYING.  If not, write to the Free
+ * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *****************************************************************************/
+
+#include "thermferm.h"
+#include "xutil.h"
+#include "mqtt.h"
+
+extern sys_config       Config;
+extern int		debug;
+
+#ifdef HAVE_MOSQUITTO_H
+
+
+/* Global variables for use in callbacks. */
+int              	mqtt_qos = 0;
+int              	mqtt_status = STATUS_CONNECTING;
+int              	mqtt_mid_sent = 0;
+int              	mqtt_last_mid = -1;
+int              	mqtt_last_mid_sent = -1;
+int              	mqtt_connected = TRUE;
+int              	mqtt_disconnect_sent = FALSE;
+int              	mqtt_connect_lost = FALSE;
+int              	mqtt_my_shutdown = FALSE;
+int              	mqtt_use = FALSE;
+int			keepalive = 60;
+unsigned int		max_inflight = 20;
+struct mosquitto	*mosq = NULL;
+char			*state = NULL;
+char			my_hostname[256];
+
+
+void my_connect_callback(struct mosquitto *my_mosq, void *obj, int result)
+{
+    if (mqtt_connect_lost) {
+       mqtt_connect_lost = FALSE;
+       syslog(LOG_NOTICE, "MQTT: reconnect: %s", mosquitto_connack_string(result));
+    }
+
+    if (!result) {
+       mqtt_status = STATUS_CONNACK_RECVD;
+    } else {
+       syslog(LOG_NOTICE, "MQTT: my_connect_callback: %s\n", mosquitto_connack_string(result));
+    }
+}
+
+
+
+void my_disconnect_callback(struct mosquitto *my_mosq, void *obj, int rc)
+{
+    if (mqtt_my_shutdown) {
+       syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mosq_host);
+       mqtt_connected = FALSE;
+    } else {
+       /*
+        * The remote server was brought down. We must keep running
+        */
+       syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mosq_host);
+       mqtt_connect_lost = TRUE;
+    }
+}
+
+
+
+void my_publish_callback(struct mosquitto *my_mosq, void *obj, int mid)
+{
+    mqtt_last_mid_sent = mid;
+}
+
+
+
+void my_log_callback(struct mosquitto *my_mosq, void *obj, int level, const char *str)
+{
+    syslog(LOG_NOTICE, "MQTT: %s", str);
+    if (debug)
+    	fprintf(stdout, "MQTT: %s\n", str);
+}
+
+
+
+#endif
+
+
+void mqtt_connect(void)
+{
+#ifdef HAVE_MOSQUITTO_H
+    char		*id = NULL;
+    char		err[1024];
+    int			rc;
+
+    /*
+     * Initialize mosquitto communication
+     */
+    gethostname(my_hostname, 255);
+    mosquitto_lib_init();
+    id = xstrcpy((char *)"thermferm/");
+    id = xstrcat(id, my_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, "MQTT: mosquitto_new: Out of memory");
+               break;
+           case EINVAL:
+               syslog(LOG_NOTICE, "MQTT: mosquitto_new: Invalid id");
+               break;
+       }
+       mosquitto_lib_cleanup();
+       return;
+    }
+
+    if (debug) {
+       mosquitto_log_callback_set(mosq, my_log_callback);
+    }
+
+    /*
+     * Set our will
+     */
+    state = xstrcpy((char *)"clients/");
+    state = xstrcat(state, my_hostname);
+    state = xstrcat(state, (char *)"/thermferm/state");
+    if ((rc = mosquitto_will_set(mosq, state, 1, (char *)"0", mqtt_qos, TRUE))) {
+        if (rc == MOSQ_ERR_INVAL) {
+            syslog(LOG_NOTICE, "MQTT: mosquitto_will_set: input parameters invalid");
+        } else if (rc == MOSQ_ERR_NOMEM) {
+            syslog(LOG_NOTICE, "MQTT: mosquitto_will_set: Out of Memory");
+        } else if (rc == MOSQ_ERR_PAYLOAD_SIZE) {
+            syslog(LOG_NOTICE, "MQTT: mosquitto_will_set: invalid payload size");
+        }
+        mosquitto_lib_cleanup();
+        return;
+    }
+
+    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, "MQTT: mosquitto_connect: error: %s", err);
+        } else {
+            syslog(LOG_NOTICE, "MQTT: mosquitto_connect: unable to connect (%d)", rc);
+        }
+        mosquitto_lib_cleanup();
+	syslog(LOG_NOTICE, "MQTT: will run without an MQTT broker.");
+    } 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);
+        mosquitto_publish(mosq, &mqtt_mid_sent, state, 1, (char *)"1", mqtt_qos, 1);
+    }
+#endif
+}
+
+
+
+void mqtt_disconnect(void)
+{
+#ifdef HAVE_MOSQUITTO_H
+    int		rc;
+    char	buf[128];
+
+    if (mqtt_use) {
+        /*
+         * Final publish 0 to clients/<hostname>/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;
+	mqtt_my_shutdown = TRUE;
+
+        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();
+	syslog(LOG_NOTICE, "MQTT disconnected");
+    }
+#endif
+}
+
+
+
+void mqtt_publish_int(char *uuid, char *tail, int value)
+{
+#ifdef HAVE_MOSQUITTO_H
+    char	topic[1024], buf[128];
+
+    if (mqtt_use) {
+	snprintf(topic, 1023, "fermenter/%s/%s/%s", my_hostname, uuid, tail);
+	snprintf(buf, 127, "%d", value);
+	mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1);
+    }
+#endif
+}
+
+
+
+void mqtt_publish_float(char *uuid, char *tail, float value, int decimals)
+{
+#ifdef HAVE_MOSQUITTO_H
+    char        topic[1024], buf[128];
+
+    if (mqtt_use) {
+	snprintf(topic, 1023, "fermenter/%s/%s/%s", my_hostname, uuid, tail);
+	snprintf(buf, 127, "%.*f", decimals, value);
+	mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1);
+    }
+#endif
+}
+
+
+
+void mqtt_publish_str(char *uuid, char *tail, char *value)
+{
+#ifdef HAVE_MOSQUITTO_H
+    char        topic[1024], buf[128];
+
+    if (mqtt_use) {
+	snprintf(topic, 1023, "fermenter/%s/%s/%s", my_hostname, uuid, tail);
+	snprintf(buf, 127, "%s", value);
+	mosquitto_publish(mosq, &mqtt_mid_sent, topic, strlen(buf), buf, mqtt_qos, 1);
+    }
+#endif
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/thermferm/mqtt.h	Mon May 02 16:15:37 2016 +0200
@@ -0,0 +1,21 @@
+#ifndef	_MQTT_H
+#define	_MQTT_H
+
+#ifdef HAVE_MOSQUITTO_H
+
+#define STATUS_CONNECTING 0
+#define STATUS_CONNACK_RECVD 1
+#define STATUS_WAITING 2
+
+#endif
+
+/*
+ * Public functions
+ */
+void mqtt_connect(void);
+void mqtt_disconnect(void);
+void mqtt_publish_int(char *, char *, int);
+void mqtt_publish_float(char *, char *, float, int);
+void mqtt_publish_str(char *, char *, char *);
+
+#endif
--- a/thermferm/server.c	Sat Apr 30 21:25:10 2016 +0200
+++ b/thermferm/server.c	Mon May 02 16:15:37 2016 +0200
@@ -27,6 +27,7 @@
 #include "server.h"
 #include "lcd-buffer.h"
 #include "xutil.h"
+#include "mqtt.h"
 
 
 extern int		my_shutdown;
@@ -2175,8 +2176,10 @@
 			     */
 			    if (val && (strcmp(kwd, (char *)"NAME") == 0)) {
 				if (unit->name) {
-				    if (strcmp(unit->name, val))
+				    if (strcmp(unit->name, val)) {
 					syslog(LOG_NOTICE, "Fermenter unit %s name `%s' to `%s'", unit->uuid, unit->name, val);
+					mqtt_publish_str(unit->uuid, (char *)"name", val);
+				    }
 				    free(unit->name);
 				}
 				unit->name = xstrcpy(val);
@@ -2356,6 +2359,7 @@
 					    initlog(unit->name);
 					syslog(LOG_NOTICE, "Fermenter unit %s mode %s to %s", unit->uuid, UNITMODE[unit->mode], UNITMODE[i]);
 					unit->mode = i;
+					unit->mqtt_flag |= (MQTT_FLAG_MODE | MQTT_FLAG_SP);
 					/* Allways turn everything off after a mode change */
 					unit->PID_cool->OutP = unit->PID_heat->OutP = 0.0;
 					unit->PID_cool->Mode = unit->PID_heat->Mode = PID_MODE_NONE;
@@ -2372,7 +2376,17 @@
 					     */
 					    unit->prof_target_lo = unit->prof_target_hi = 20.0;
 					    unit->prof_fridge_mode = 0;
+					    if (unit->profile) {
+						unit->mqtt_flag |= MQTT_FLAG_PROFILE;
+						unit->mqtt_flag |= MQTT_FLAG_SP;
+					    }
 					}
+					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);
 					break;
 				    }
 				}
@@ -2382,6 +2396,7 @@
 				    if (unit->fridge_set != fval)
 				    	syslog(LOG_NOTICE, "Fermenter unit %s fridge temperature %.1f to %.1f", unit->uuid, unit->fridge_set, fval);
 				    unit->fridge_set = fval;
+				    unit->mqtt_flag |= MQTT_FLAG_SP;
 				}
 
 			    } else if (val && (strcmp(kwd, (char *)"BEER_SET") == 0)) {
@@ -2389,6 +2404,7 @@
 				    if (unit->beer_set != fval)
 				    	syslog(LOG_NOTICE, "Fermenter unit %s beer temperature %.1f to %.1f", unit->uuid, unit->beer_set, fval);
 				    unit->beer_set = fval;
+				    unit->mqtt_flag |= MQTT_FLAG_SP;
 				}
 
 			    } else if (val && (strcmp(kwd, (char *)"PIDC_IMAX") == 0)) {
@@ -2486,6 +2502,13 @@
 				    device_out(unit->cooler_address, unit->cooler_state);
 				    device_out(unit->fan_address, unit->fan_state);
 				    device_out(unit->light_address, unit->light_state);
+				    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);
+				    unit->mqtt_flag |= (MQTT_FLAG_PROFILE | MQTT_FLAG_SP);
 				}
 
 			    } else if (val && (strcmp(kwd, (char *)"PROF_STATE") == 0)) {
@@ -2495,14 +2518,17 @@
 						case PROFILE_OFF:	if (unit->prof_state == PROFILE_DONE) {
 									    unit->prof_state = PROFILE_OFF;
 									    syslog(LOG_NOTICE, "Fermenter unit %s profile to OFF", unit->uuid);
+									    mqtt_publish_str(unit->uuid, (char *)"profile/state", (char *)PROFSTATE[i]);
 									}
 									break;
 						case PROFILE_PAUSE:	if (unit->prof_state == PROFILE_RUN) {
 									    unit->prof_state = PROFILE_PAUSE;
 									    syslog(LOG_NOTICE, "Fermenter unit %s profile PAUSE", unit->uuid);
+									    mqtt_publish_str(unit->uuid, (char *)"profile/state", (char *)PROFSTATE[i]);
 									} else if (unit->prof_state == PROFILE_PAUSE) {
 									    unit->prof_state = PROFILE_RUN;
 									    syslog(LOG_NOTICE, "Fermenter unit %s profile RESUME", unit->uuid);
+									    mqtt_publish_str(unit->uuid, (char *)"profile/state", (char *)PROFSTATE[PROFILE_RUN]);
 									}
 									break;
 						case PROFILE_RUN:	if (unit->prof_state == PROFILE_OFF) {
@@ -2511,6 +2537,7 @@
 									    unit->prof_paused = unit->prof_primary_done = 0;
 									    unit->prof_peak_abs = unit->prof_peak_rel = 0.0;
 									    syslog(LOG_NOTICE, "Fermenter unit %s profile to RUN", unit->uuid);
+									    mqtt_publish_str(unit->uuid, (char *)"profile/state", (char *)PROFSTATE[i]);
 									}
 									break;
 						case PROFILE_DONE:	break;	/* Command is illegal */
@@ -2518,9 +2545,11 @@
 									    unit->prof_state = PROFILE_OFF;
 									    unit->prof_started = 0;
 									    syslog(LOG_NOTICE, "Fermenter unit %s profile ABORT", unit->uuid);
+									    mqtt_publish_str(unit->uuid, (char *)"profile/state", (char *)PROFSTATE[i]);
 									}
 									break;
 					}
+					unit->mqtt_flag |= (MQTT_FLAG_SP | MQTT_FLAG_PROFILE | MQTT_FLAG_PERCENT);
 					break;
 				    }
 				}
--- 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 <mbroek at mbse dot eu>
  *
@@ -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/<hostname>/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)
--- a/thermferm/thermferm.h	Sat Apr 30 21:25:10 2016 +0200
+++ b/thermferm/thermferm.h	Mon May 02 16:15:37 2016 +0200
@@ -180,6 +180,7 @@
     int			prof_fridge_mode;	/* Profile fridge/beer mode	*/
     pid_var		*PID_cool;		/* PID cooler			*/
     pid_var		*PID_heat;		/* PID heater			*/
+    int			mqtt_flag;		/* MQTT print values flag	*/
 } units_list;
 
 #define	UNITMODE_OFF		0		/* Unit turned off		*/
@@ -188,6 +189,13 @@
 #define	UNITMODE_BEER		3		/* Unit acts as beer cooler	*/
 #define	UNITMODE_PROFILE	4		/* Unit runs in profile mode	*/
 
+#define	MQTT_FLAG_SP		0x0001		/* Show setpoint values		*/
+#define	MQTT_FLAG_AIR		0x0002		/* Show air temperature		*/
+#define	MQTT_FLAG_BEER		0x0004		/* Show beer temperature	*/
+#define	MQTT_FLAG_MODE		0x0008		/* Show unit mode		*/
+#define	MQTT_FLAG_PROFILE	0x0010		/* Show profile settings	*/
+#define	MQTT_FLAG_PERCENT	0x0020		/* Show profile percent		*/
+
 
 /*
  * Fermenting steps

mercurial