Sun, 28 May 2017 14:19:57 +0200
Upgraded jqwidgets to version 4.5.2
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
506 | 2 | * Copyright (C) 2016-2017 |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * This is free software; you can redistribute it and/or modify it |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * along with ThermFerm; see the file COPYING. If not, write to the Free |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | #include "thermferm.h" |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | #include "xutil.h" |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | #include "mqtt.h" |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | extern sys_config Config; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern int debug; |
506 | 29 | extern const char UNITMODE[5][8]; |
30 | extern const char PROFSTATE[5][6]; | |
31 | extern const char TEMPSTATE[3][8]; | |
32 | ||
33 | int Sequence = 0; | |
34 | ||
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
35 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | #ifdef HAVE_MOSQUITTO_H |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
37 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
38 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
39 | /* Global variables for use in callbacks. */ |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | int mqtt_qos = 0; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
41 | int mqtt_status = STATUS_CONNECTING; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
42 | int mqtt_mid_sent = 0; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | int mqtt_last_mid = -1; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | int mqtt_last_mid_sent = -1; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
45 | int mqtt_connected = TRUE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
46 | int mqtt_disconnect_sent = FALSE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
47 | int mqtt_connect_lost = FALSE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
48 | int mqtt_my_shutdown = FALSE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
49 | int mqtt_use = FALSE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
50 | int keepalive = 60; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | unsigned int max_inflight = 20; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | struct mosquitto *mosq = NULL; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
53 | char *state = NULL; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
54 | char my_hostname[256]; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
55 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
56 | |
506 | 57 | |
58 | char *payload_header(void) | |
59 | { | |
60 | char *tmp, buf[128]; | |
61 | ||
62 | tmp = xstrcpy((char *)"{\"timestamp\":"); | |
63 | sprintf(buf, "%ld", time(NULL)); | |
64 | tmp = xstrcat(tmp, buf); | |
65 | tmp = xstrcat(tmp, (char *)",\"seq\":"); | |
66 | sprintf(buf, "%d", Sequence++); | |
67 | tmp = xstrcat(tmp, buf); | |
68 | tmp = xstrcat(tmp, (char *)",\"metric\":"); | |
69 | return tmp; | |
70 | } | |
71 | ||
72 | ||
73 | ||
74 | char *topic_base(char *msgtype) | |
75 | { | |
76 | char *tmp; | |
77 | ||
78 | tmp = xstrcpy((char *)"mbv1.0/fermenters/"); | |
79 | tmp = xstrcat(tmp, msgtype); | |
80 | tmp = xstrcat(tmp, (char *)"/"); | |
81 | tmp = xstrcat(tmp, my_hostname); | |
82 | return tmp; | |
83 | } | |
84 | ||
85 | ||
86 | ||
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
87 | void my_connect_callback(struct mosquitto *my_mosq, void *obj, int result) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
88 | { |
506 | 89 | char *topic = NULL; |
90 | ||
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
91 | if (mqtt_connect_lost) { |
506 | 92 | mqtt_connect_lost = FALSE; |
93 | syslog(LOG_NOTICE, "MQTT: reconnect: %s", mosquitto_connack_string(result)); | |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
94 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
95 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
96 | if (!result) { |
506 | 97 | topic = topic_base((char *)"NCMD"); |
98 | topic = xstrcat(topic, (char *)"/#"); | |
99 | mosquitto_subscribe(mosq, NULL, topic, 0); | |
100 | free(topic); | |
101 | topic = topic_base((char *)"DCMD"); | |
102 | topic = xstrcat(topic, (char *)"/#"); | |
103 | mosquitto_subscribe(mosq, NULL, topic, 0); | |
104 | free(topic); | |
105 | topic = NULL; | |
106 | mqtt_status = STATUS_CONNACK_RECVD; | |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
107 | } else { |
506 | 108 | syslog(LOG_NOTICE, "MQTT: my_connect_callback: %s\n", mosquitto_connack_string(result)); |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
109 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
110 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
111 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
112 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
113 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
114 | void my_disconnect_callback(struct mosquitto *my_mosq, void *obj, int rc) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
115 | { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
116 | if (mqtt_my_shutdown) { |
500
5aa914eb644e
Units now have an unique alias name so that the MQTT messages are more friendly.
Michiel Broek <mbroek@mbse.eu>
parents:
499
diff
changeset
|
117 | syslog(LOG_NOTICE, "MQTT: acknowledged DISCONNECT from %s", Config.mqtt_host); |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
118 | mqtt_connected = FALSE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
119 | } else { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
120 | /* |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
121 | * The remote server was brought down. We must keep running |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
122 | */ |
500
5aa914eb644e
Units now have an unique alias name so that the MQTT messages are more friendly.
Michiel Broek <mbroek@mbse.eu>
parents:
499
diff
changeset
|
123 | syslog(LOG_NOTICE, "MQTT: received DISCONNECT from %s, connection lost", Config.mqtt_host); |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
124 | mqtt_connect_lost = TRUE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
125 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
126 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
127 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
128 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
129 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
130 | void my_publish_callback(struct mosquitto *my_mosq, void *obj, int mid) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
131 | { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
132 | mqtt_last_mid_sent = mid; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
134 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
135 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
136 | |
506 | 137 | void my_subscribe_callback(struct mosquitto *my_mosq, void *userdata, int mid, int qos_count, const int *granted_qos) |
138 | { | |
139 | int i; | |
140 | ||
141 | syslog(LOG_NOTICE, "Subscribed (mid: %d): %d", mid, granted_qos[0]); | |
142 | for (i = 1; i < qos_count; i++) { | |
143 | syslog(LOG_NOTICE, " %d", granted_qos[i]); | |
144 | } | |
145 | } | |
146 | ||
147 | ||
148 | ||
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
149 | void my_log_callback(struct mosquitto *my_mosq, void *obj, int level, const char *str) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
150 | { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
151 | syslog(LOG_NOTICE, "MQTT: %s", str); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
152 | if (debug) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
153 | fprintf(stdout, "MQTT: %s\n", str); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
154 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
155 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
156 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
157 | |
506 | 158 | void my_message_callback(struct mosquitto *my_mosq, void *userdata, const struct mosquitto_message *message) |
159 | { | |
160 | if (message->payloadlen) { | |
161 | syslog(LOG_NOTICE, "MQTT: message callback %s :: %d", message->topic, message->payloadlen); | |
162 | // TODO: process subscribed topics here. | |
163 | ||
164 | } else { | |
165 | syslog(LOG_NOTICE, "MQTT: message callback %s (null)", message->topic); | |
166 | } | |
167 | } | |
168 | ||
169 | ||
170 | ||
171 | void publisher(struct mosquitto *my_mosq, char *topic, char *payload, bool retain) { | |
172 | // publish the data | |
173 | if (payload) | |
174 | mosquitto_publish(my_mosq, &mqtt_mid_sent, topic, strlen(payload), payload, mqtt_qos, retain); | |
175 | else | |
176 | mosquitto_publish(my_mosq, &mqtt_mid_sent, topic, 0, NULL, mqtt_qos, retain); | |
177 | } | |
178 | ||
179 | ||
180 | ||
181 | char *unit_data(units_list *unit, bool birth) | |
182 | { | |
183 | char *payload = NULL; | |
184 | char buf[128]; | |
185 | bool comma = false; | |
186 | profiles_list *profile; | |
187 | prof_step *pstep; | |
188 | ||
189 | payload = xstrcat(payload, (char *)"{"); | |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
190 | if (birth) { |
506 | 191 | payload = xstrcat(payload, (char *)"\"uuid\":\""); |
192 | payload = xstrcat(payload, unit->uuid); | |
193 | payload = xstrcat(payload, (char *)"\",\"alias\":\""); | |
194 | payload = xstrcat(payload, unit->alias); | |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
195 | payload = xstrcat(payload, (char *)"\","); |
506 | 196 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
197 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
198 | payload = xstrcat(payload, (char *)"\"name\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
199 | payload = xstrcat(payload, unit->name); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
200 | if (unit->air_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
201 | payload = xstrcat(payload, (char *)"\",\"air\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
202 | payload = xstrcat(payload, unit->air_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
203 | payload = xstrcat(payload, (char *)"\",\"state\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
204 | payload = xstrcat(payload, (char *)TEMPSTATE[unit->air_state]); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
205 | payload = xstrcat(payload, (char *)"\",\"temperature\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
206 | sprintf(buf, "%.3f", unit->air_temperature / 1000.0); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
207 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
208 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
209 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
210 | payload = xstrcat(payload, (char *)"\",\"air\":null"); |
506 | 211 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
212 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
213 | if (unit->beer_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
214 | payload = xstrcat(payload, (char *)",\"beer\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
215 | payload = xstrcat(payload, unit->beer_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
216 | payload = xstrcat(payload, (char *)"\",\"state\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
217 | payload = xstrcat(payload, (char *)TEMPSTATE[unit->beer_state]); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
218 | payload = xstrcat(payload, (char *)"\",\"temperature\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
219 | sprintf(buf, "%.3f", unit->beer_temperature / 1000.0); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
220 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
221 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
222 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
223 | payload = xstrcat(payload, (char *)",\"beer\":null"); |
506 | 224 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
225 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
226 | if (unit->heater_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
227 | payload = xstrcat(payload, (char *)",\"heater\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
228 | payload = xstrcat(payload, unit->heater_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
229 | payload = xstrcat(payload, (char *)"\",\"state\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
230 | sprintf(buf, "%d", unit->heater_state); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
231 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
232 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
233 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
234 | payload = xstrcat(payload, (char *)",\"heater\":null"); |
506 | 235 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
236 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
237 | if (unit->cooler_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
238 | payload = xstrcat(payload, (char *)",\"cooler\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
239 | payload = xstrcat(payload, unit->cooler_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
240 | payload = xstrcat(payload, (char *)"\",\"state\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
241 | sprintf(buf, "%d", unit->cooler_state); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
242 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
243 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
244 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
245 | payload = xstrcat(payload, (char *)",\"cooler\":null"); |
506 | 246 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
247 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
248 | if (unit->fan_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
249 | payload = xstrcat(payload, (char *)",\"fan\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
250 | payload = xstrcat(payload, unit->fan_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
251 | payload = xstrcat(payload, (char *)"\",\"state\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
252 | sprintf(buf, "%d", unit->fan_state); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
253 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
254 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
255 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
256 | payload = xstrcat(payload, (char *)",\"fan\":null"); |
506 | 257 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
258 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
259 | if (unit->door_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
260 | payload = xstrcat(payload, (char *)",\"door\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
261 | payload = xstrcat(payload, unit->door_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
262 | payload = xstrcat(payload, (char *)"\",\"state\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
263 | sprintf(buf, "%d", unit->door_state); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
264 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
265 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
266 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
267 | payload = xstrcat(payload, (char *)",\"door\":null"); |
506 | 268 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
269 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
270 | if (unit->light_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
271 | payload = xstrcat(payload, (char *)",\"light\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
272 | payload = xstrcat(payload, unit->light_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
273 | payload = xstrcat(payload, (char *)"\",\"state\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
274 | sprintf(buf, "%d", unit->light_state); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
275 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
276 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
277 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
278 | payload = xstrcat(payload, (char *)",\"light\":null"); |
506 | 279 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
280 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
281 | if (unit->psu_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
282 | payload = xstrcat(payload, (char *)",\"psu\":{\"address\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
283 | payload = xstrcat(payload, unit->psu_address); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
284 | payload = xstrcat(payload, (char *)"\",\"state\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
285 | sprintf(buf, "%d", unit->psu_state); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
286 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
287 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
288 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
289 | payload = xstrcat(payload, (char *)",\"psu\":null"); |
506 | 290 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
291 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
292 | payload = xstrcat(payload, (char *)",\"mode\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
293 | payload = xstrcat(payload, (char *)UNITMODE[unit->mode]); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
294 | payload = xstrcat(payload, (char *)"\",\"setpoint\":{\"low\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
295 | sprintf(buf, "%.1f", unit->PID_heat->SetP); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
296 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
297 | payload = xstrcat(payload, (char *)",\"high\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
298 | sprintf(buf, "%.1f", unit->PID_cool->SetP); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
299 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
300 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
301 | |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
302 | if (unit->mode == UNITMODE_PROFILE && unit->profile) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
303 | for (profile = Config.profiles; profile; profile = profile->next) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
304 | if (strcmp(unit->profile, profile->uuid) == 0) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
305 | payload = xstrcat(payload, (char *)",\"profile\":{\"uuid\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
306 | payload = xstrcat(payload, unit->profile); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
307 | payload = xstrcat(payload, (char *)",\"name\":\""); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
308 | payload = xstrcat(payload, profile->name); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
309 | payload = xstrcat(payload, (char *)"\",\"inittemp\":{\"low\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
310 | sprintf(buf, "%.1f", profile->inittemp_lo); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
311 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
312 | payload = xstrcat(payload, (char *)",\"high\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
313 | sprintf(buf, "%.1f", profile->inittemp_hi); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
314 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
315 | payload = xstrcat(payload, (char *)"},\"fridgemode\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
316 | sprintf(buf, "%d", profile->fridge_mode); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
317 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
318 | comma = false; |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
319 | if (profile->steps) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
320 | payload = xstrcat(payload, (char *)",\"steps\":["); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
321 | for (pstep = profile->steps; pstep; pstep = pstep->next) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
322 | if (comma) |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
323 | payload = xstrcat(payload, (char *)","); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
324 | payload = xstrcat(payload, (char *)"{\"resttime\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
325 | sprintf(buf, "%d", pstep->resttime); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
326 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
327 | payload = xstrcat(payload, (char *)",\"steptime\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
328 | sprintf(buf, "%d", pstep->steptime); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
329 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
330 | payload = xstrcat(payload, (char *)",\"target\":{\"low\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
331 | sprintf(buf, "%.1f", pstep->target_lo); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
332 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
333 | payload = xstrcat(payload, (char *)",\"high\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
334 | sprintf(buf, "%.1f", pstep->target_hi); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
335 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
336 | payload = xstrcat(payload, (char *)"},\"fridgemode\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
337 | sprintf(buf, "%d", pstep->fridge_mode); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
338 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
339 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
340 | comma = true; |
506 | 341 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
342 | payload = xstrcat(payload, (char *)"]"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
343 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
344 | payload = xstrcat(payload, (char *)",\"steps\":null"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
345 | } |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
346 | payload = xstrcat(payload, (char *)"}"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
347 | break; |
506 | 348 | } |
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
349 | } |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
350 | } else { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
351 | payload = xstrcat(payload, (char *)",\"profile\":null"); |
506 | 352 | } |
353 | payload = xstrcat(payload, (char *)"}"); | |
354 | ||
355 | return payload; | |
356 | } | |
357 | ||
358 | ||
359 | ||
360 | void publishDBirth(void) | |
361 | { | |
362 | char *payload = NULL; | |
363 | units_list *unit; | |
364 | int comma = FALSE; | |
365 | ||
366 | payload = payload_header(); | |
367 | payload = xstrcat(payload, (char *)"{\"units\":["); | |
368 | for (unit = Config.units; unit; unit = unit->next) { | |
369 | if (comma) | |
370 | payload = xstrcat(payload, (char *)","); | |
371 | payload = xstrcat(payload, unit_data(unit, true)); | |
372 | comma = TRUE; | |
373 | } | |
374 | payload = xstrcat(payload, (char *)"]}}"); | |
375 | publisher(mosq, topic_base((char *)"DBIRTH"), payload, true); | |
376 | free(payload); | |
377 | payload = NULL; | |
378 | } | |
379 | ||
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
380 | #endif |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
381 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
382 | |
506 | 383 | void publishDData(units_list *unit) |
384 | { | |
385 | #ifdef HAVE_MOSQUITTO_H | |
386 | ||
387 | char *payload = NULL, *topic = NULL; | |
388 | ||
389 | if (mqtt_use) { | |
390 | payload = payload_header(); | |
391 | payload = xstrcat(payload, unit_data(unit, false)); | |
392 | payload = xstrcat(payload, (char *)"}"); | |
393 | topic = xstrcat(topic_base((char *)"DDATA"), (char *)"/"); | |
394 | topic = xstrcat(topic, unit->alias); | |
395 | publisher(mosq, topic, payload, false); | |
396 | free(payload); | |
397 | payload = NULL; | |
398 | free(topic); | |
399 | topic = NULL; | |
400 | } | |
401 | #endif | |
402 | } | |
403 | ||
404 | ||
405 | ||
406 | void publishNData(bool birth, int flag) | |
407 | { | |
408 | #ifdef HAVE_MOSQUITTO_H | |
409 | char *payload = NULL, buf[64]; | |
410 | struct utsname ubuf; | |
411 | bool comma = false; | |
412 | ||
413 | payload = payload_header(); | |
414 | payload = xstrcat(payload, (char *)"{"); | |
415 | ||
416 | if (birth || flag & MQTT_NODE_CONTROL) { | |
417 | payload = xstrcat(payload, (char *)"\"nodecontrol\":{\"reboot\":false,\"rebirth\":false,\"nextserver\":false,\"scanrate\":3000}"); | |
418 | comma = true; | |
419 | } | |
420 | ||
421 | if (birth) { | |
422 | if (comma) | |
423 | payload = xstrcat(payload, (char *)","); | |
508
9e67c5f9c994
Add Raspberry support in MQTT init message
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
424 | #ifdef HAVE_WIRINGPI_H |
9e67c5f9c994
Add Raspberry support in MQTT init message
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
425 | payload = xstrcat(payload, (char *)"\"properties\":{\"hardwaremake\":\"Raspberry\",\"hardwaremodel\":\"Unknown\""); |
9e67c5f9c994
Add Raspberry support in MQTT init message
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
426 | #else |
506 | 427 | payload = xstrcat(payload, (char *)"\"properties\":{\"hardwaremake\":\"Unknown\",\"hardwaremodel\":\"Unknown\""); |
508
9e67c5f9c994
Add Raspberry support in MQTT init message
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
428 | #endif |
9e67c5f9c994
Add Raspberry support in MQTT init message
Michiel Broek <mbroek@mbse.eu>
parents:
506
diff
changeset
|
429 | if (uname(&ubuf) == 0) { |
506 | 430 | payload = xstrcat(payload, (char *)",\"os\":\""); |
431 | payload = xstrcat(payload, ubuf.sysname); | |
432 | payload = xstrcat(payload, (char *)"\",\"os_version\":\""); | |
433 | payload = xstrcat(payload, ubuf.release); | |
434 | payload = xstrcat(payload, (char *)"\""); | |
435 | } else { | |
436 | payload = xstrcat(payload, (char *)",\"os\":\"Unknown\",\"os_version\":\"Unknown\""); | |
437 | } | |
438 | ||
439 | payload = xstrcat(payload, (char *)",\"FW\":\""); | |
440 | payload = xstrcat(payload, (char *)VERSION); | |
441 | payload = xstrcat(payload, (char *)"\"}"); | |
442 | comma = true; | |
443 | } | |
444 | ||
510
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
445 | if (Config.temp_address || Config.hum_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
446 | if (comma) |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
447 | payload = xstrcat(payload, (char *)","); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
448 | payload = xstrcat(payload, (char *)"\"HT\":{"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
449 | if (Config.temp_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
450 | payload = xstrcat(payload, (char *)"\"temperature\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
451 | sprintf(buf, "%.1f", Config.temp_value / 1000.0); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
452 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
453 | } |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
454 | if (Config.temp_address && Config.hum_address) |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
455 | payload = xstrcat(payload, (char *)","); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
456 | if (Config.hum_address) { |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
457 | payload = xstrcat(payload, (char *)"\"humidity\":"); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
458 | sprintf(buf, "%.1f", Config.hum_value / 1000.0); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
459 | payload = xstrcat(payload, buf); |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
460 | } |
2da3d3340403
Version 0.6.0. The MQTT data messages are now allways the same length so that openhab will function with the message updates.
Michiel Broek <mbroek@mbse.eu>
parents:
509
diff
changeset
|
461 | payload = xstrcat(payload, (char *)"}"); |
506 | 462 | } |
463 | payload = xstrcat(payload, (char *)"}}"); | |
464 | ||
465 | if (birth) | |
466 | publisher(mosq, topic_base((char *)"NBIRTH"), payload, true); | |
467 | else | |
468 | publisher(mosq, topic_base((char *)"NDATA"), payload, false); | |
469 | ||
470 | free(payload); | |
471 | payload = NULL; | |
472 | #endif | |
473 | } | |
474 | ||
475 | ||
476 | ||
477 | void publishBirth(void) | |
478 | { | |
479 | publishNData(true, 0); | |
480 | publishDBirth(); | |
481 | } | |
482 | ||
483 | ||
484 | ||
485 | ||
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
486 | void mqtt_connect(void) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
487 | { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
488 | #ifdef HAVE_MOSQUITTO_H |
506 | 489 | char *id = NULL; |
490 | char err[1024]; | |
491 | int rc; | |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
492 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
493 | /* |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
494 | * Initialize mosquitto communication |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
495 | */ |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
496 | gethostname(my_hostname, 255); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
497 | mosquitto_lib_init(); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
498 | id = xstrcpy((char *)"thermferm/"); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
499 | id = xstrcat(id, my_hostname); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
500 | if (strlen(id) > MOSQ_MQTT_ID_MAX_LENGTH) { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
501 | /* |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
502 | * Enforce maximum client id length of 23 characters |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
503 | */ |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
504 | id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0'; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
505 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
506 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
507 | mosq = mosquitto_new(id, TRUE, NULL); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
508 | if (!mosq) { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
509 | switch(errno) { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
510 | case ENOMEM: |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
511 | syslog(LOG_NOTICE, "MQTT: mosquitto_new: Out of memory"); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
512 | break; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
513 | case EINVAL: |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
514 | syslog(LOG_NOTICE, "MQTT: mosquitto_new: Invalid id"); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
515 | break; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
516 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
517 | mosquitto_lib_cleanup(); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
518 | return; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
519 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
520 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
521 | /* |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
522 | * Set our will |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
523 | */ |
506 | 524 | if ((rc = mosquitto_will_set(mosq, topic_base((char *)"NDEATH"), 0, NULL, mqtt_qos, false))) { |
525 | if (rc > MOSQ_ERR_SUCCESS) | |
526 | syslog(LOG_NOTICE, "MQTT: mosquitto_will_set: %s", mosquitto_strerror(rc)); | |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
527 | mosquitto_lib_cleanup(); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
528 | return; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
529 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
530 | |
509 | 531 | if (debug) |
532 | mosquitto_log_callback_set(mosq, my_log_callback); | |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
533 | mosquitto_max_inflight_messages_set(mosq, max_inflight); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
534 | mosquitto_connect_callback_set(mosq, my_connect_callback); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
535 | mosquitto_disconnect_callback_set(mosq, my_disconnect_callback); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
536 | mosquitto_publish_callback_set(mosq, my_publish_callback); |
506 | 537 | mosquitto_message_callback_set(mosq, my_message_callback); |
538 | mosquitto_subscribe_callback_set(mosq, my_subscribe_callback); | |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
539 | |
500
5aa914eb644e
Units now have an unique alias name so that the MQTT messages are more friendly.
Michiel Broek <mbroek@mbse.eu>
parents:
499
diff
changeset
|
540 | if ((rc = mosquitto_connect(mosq, Config.mqtt_host, Config.mqtt_port, keepalive))) { |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
541 | if (rc == MOSQ_ERR_ERRNO) { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
542 | strerror_r(errno, err, 1024); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
543 | syslog(LOG_NOTICE, "MQTT: mosquitto_connect: error: %s", err); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
544 | } else { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
545 | syslog(LOG_NOTICE, "MQTT: mosquitto_connect: unable to connect (%d)", rc); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
546 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
547 | mosquitto_lib_cleanup(); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
548 | syslog(LOG_NOTICE, "MQTT: will run without an MQTT broker."); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
549 | } else { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
550 | mqtt_use = TRUE; |
500
5aa914eb644e
Units now have an unique alias name so that the MQTT messages are more friendly.
Michiel Broek <mbroek@mbse.eu>
parents:
499
diff
changeset
|
551 | syslog(LOG_NOTICE, "MQTT: connected with %s:%d", Config.mqtt_host, Config.mqtt_port); |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
552 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
553 | /* |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
554 | * Initialise is complete, report our presence state |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
555 | */ |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
556 | mosquitto_loop_start(mosq); |
506 | 557 | publishBirth(); |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
558 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
559 | #endif |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
560 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
561 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
562 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
563 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
564 | void mqtt_disconnect(void) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
565 | { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
566 | #ifdef HAVE_MOSQUITTO_H |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
567 | int rc; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
568 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
569 | if (mqtt_use) { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
570 | /* |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
571 | * Final publish 0 to clients/<hostname>/thermferm/state |
504
862de87f9f89
Added some MQTT publish clear functions. Version 0.5.8
Michiel Broek <mbroek@mbse.eu>
parents:
500
diff
changeset
|
572 | * After that, remove the retained topic. |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
573 | */ |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
574 | syslog(LOG_NOTICE, "MQTT disconnecting"); |
506 | 575 | publisher(mosq, topic_base((char *)"DBIRTH"), NULL, true); |
576 | publisher(mosq, topic_base((char *)"NBIRTH"), NULL, true); | |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
577 | mqtt_last_mid = mqtt_mid_sent; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
578 | mqtt_status = STATUS_WAITING; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
579 | mqtt_my_shutdown = TRUE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
580 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
581 | do { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
582 | if (mqtt_status == STATUS_WAITING) { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
583 | if (debug) |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
584 | fprintf(stdout, (char *)"Waiting\n"); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
585 | if (mqtt_last_mid_sent == mqtt_last_mid && mqtt_disconnect_sent == FALSE) { |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
586 | mosquitto_disconnect(mosq); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
587 | mqtt_disconnect_sent = TRUE; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
588 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
589 | usleep(100000); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
590 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
591 | rc = MOSQ_ERR_SUCCESS; |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
592 | } while (rc == MOSQ_ERR_SUCCESS && mqtt_connected); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
593 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
594 | mosquitto_loop_stop(mosq, FALSE); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
595 | mosquitto_destroy(mosq); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
596 | mosquitto_lib_cleanup(); |
506 | 597 | mqtt_use = FALSE; |
499
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
598 | syslog(LOG_NOTICE, "MQTT disconnected"); |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
599 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
600 | #endif |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
601 | } |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
602 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
603 | |
602d9968960f
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
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
604 |