-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_hotpot.ino
360 lines (305 loc) · 11.7 KB
/
mqtt_hotpot.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
//#include <WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include "mqtt_hotpot.h"
#include "mqtt.h"
#include "filter.h"
void lowpass(float *filter, float temp);
float temperature_change_threshold = 0.2;
const float freeze_threshold = 2.0;
const float fire_threshold = 5.0;
#define UPDATE_RATE_SECONDS 1
// MQTT Broker
const char *topic_prefix = "homeassistant";
const char *config_suffix = "/config";
WiFiClient espClient;
PubSubClient client(espClient);
// MQTT Topics
char mqtt_message[512];
char topic[60];
const char *temperature_state_topic = "homeassistant/sensor/sensor_hotpot/state";
const char *fire_state_topic = "homeassistant/binary_sensor/sensor_fire_hotpot/state";
const char *freeze_state_topic = "homeassistant/binary_sensor/sensor_freeze_hotpot/state";
const char *filter_state_topic = "homeassistant/binary_sensor/sensor_filter_hotpot/state";
// Temperature sensors
#define ONE_WIRE_BUS D4
//#define ONE_WIRE_BUS 26
// Filter pump
#define FILTER_RELAY 16
unsigned long filterOnSince = 0, filterOffSince = 0;
unsigned int filterIntervalHours = 6;
unsigned int filterDurationMinutes = 30;
// Setup a OneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass OneWire reference to Dallas Temperature DallasTemperature
DallasTemperature sensors(&oneWire);
#define HISTORY_LENGTH 6
float temp_vor[HISTORY_LENGTH] = { }, temp_rueck[HISTORY_LENGTH] = { }, temp_difference = -5.0;
float temp_vor_last_transmitted = 0.0, temp_rueck_last_transmitted = 0.0;
// Tendency calculation
float temp_vor_avg = 0.0, temp_rueck_avg = 0.0;
float vor_change = 0.0, rueck_change = 0.0;
float vorlauf_filter[HISTORY_LENGTH];
unsigned long previousMillis;
bool fire = false;
void setup(void) {
// Connect to serial interface
Serial.begin(9600);
Serial.println("Starting...");
// -------------------------------------------------------------------------
// Connecting to a Wi-Fi network
WiFi.setHostname(device_name);
connectWifi();
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// OTA Updates
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.setHostname("Hotpot_OTA");
ArduinoOTA.setPassword(ota_password);
ArduinoOTA.begin();
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// Connect to MQTT broker
client.setServer(mqtt_broker, mqtt_port);
client.setCallback(mqtt_callback);
while (!client.connected()) {
connectMQTT();
}
// -------------------------------------------------------------------------
// Increase MQTT Buffer size
client.setBufferSize(512);
// Create Vorlauf Sensor
createNewSensor("Vorlauf", "temperature", temperature_state_topic, "ui_vor", "hotpot_temperature", device_name, manufacturer, model, model_id, "sensor", "{{ value_json.vorlauf }}");
// Create Ruecklauf Sensor
createNewSensor("Rücklauf", "temperature", temperature_state_topic, "ui_rueck", "hotpot_temperature", device_name, manufacturer, model, model_id, "sensor", "{{ value_json.ruecklauf }}");
// Create Difference Sensor
createNewSensor("Differenz", "temperature", temperature_state_topic, "ui_diff", "hotpot_temperature", device_name, manufacturer, model, model_id, "sensor", "{{ value_json.difference }}");
// Create Fire state Sensor
createNewSensor("Feuer", "heat", fire_state_topic, "ui_fire_state", "hotpot_temperature", device_name, manufacturer, model, model_id, "binary_sensor", "{{ value_json.fire }}");
// Create Freeze state Sensor
createNewSensor("Frost", "cold", freeze_state_topic, "ui_freeze_state", "hotpot_temperature", device_name, manufacturer, model, model_id, "binary_sensor", "{{ value_json.freeze }}");
// Create Filter state Sensor
createNewSensor("Filter", "running", filter_state_topic, "ui_filter_state", "hotpot_temperature", device_name, manufacturer, model, model_id, "binary_sensor", "{{ value_json.filter }}");
client.setBufferSize(256);
// Connect to sensors
pinMode(ONE_WIRE_BUS, INPUT_PULLUP);
sensors.begin();
pinMode(FILTER_RELAY, OUTPUT);
}
void loop(void) {
ArduinoOTA.handle();
client.loop();
// Check WIFI connection
if (WiFi.status() != WL_CONNECTED)
{
// if disconnected, reconnect
connectWifi();
}
// -------------------------------------------------------------
// TEMPERATURES
// -------------------------------------------------------------
if ((millis() - previousMillis) > (UPDATE_RATE_SECONDS*1000))
{
// digitalWrite(FILTER_RELAY, LOW);
// delay(1000);
// digitalWrite(FILTER_RELAY, HIGH);
// delay(1000);
// digitalWrite(FILTER_RELAY, LOW);
// delay(1000);
// digitalWrite(FILTER_RELAY, HIGH);
// Check MQTT connection
if (!client.connected())
{
//reconnect();
connectMQTT();
}
// Store timestamp
previousMillis = millis();
// Tendency
temp_vor_avg = 0.0;
temp_rueck_avg = 0.0;
for (int i = 0; i < HISTORY_LENGTH; i++)
{
temp_vor_avg += temp_vor[i];
temp_rueck_avg += temp_rueck[i];
}
temp_vor_avg /= HISTORY_LENGTH;
temp_rueck_avg /= HISTORY_LENGTH;
// Get temperature updates
sensors.requestTemperatures();
for (int i = HISTORY_LENGTH-1; i > 0; i--)
{
temp_vor[i] = temp_vor[i-1];
temp_rueck[i] = temp_rueck[i-1];
}
temp_vor[0] = sensors.getTempCByIndex(0);
temp_rueck[0] = sensors.getTempCByIndex(1);
vor_change = temp_vor[0] - temp_vor_last_transmitted;
rueck_change = temp_rueck[0] - temp_rueck_last_transmitted;
//DEBUG
// for (int i = 0; i < HISTORY_LENGTH; i++)
// {
// Serial.print(temp_vor[i]);
// Serial.print(" ");
// }
//Serial.println("");
// Serial.println(temp_vor[0]);
// Serial.println(temp_rueck[0]);
// Serial.println(vor_change);
// Serial.println(rueck_change);
// if (vor_change > 0) {
// Serial.println("Vorlauf temperature rising");
// } else {
// Serial.println("Vorlauf temperature falling");
// }
//
// if (rueck_change > 0) {
// Serial.println("Ruecklauf temperature rising");
// } else {
// Serial.println("Ruecklauf temperature falling");
// }
// Update Temperatures if significant change
if ( (fabs(vor_change) >= temperature_change_threshold) || (fabs(rueck_change) >= temperature_change_threshold) )
{
// Calculate difference
temp_difference = temp_rueck[0] - temp_vor[0];
// Publish temperatures
sprintf(mqtt_message, "{\"vorlauf\": %.1f, \"ruecklauf\": %.1f , \"difference\": %.1f}", temp_vor[0], temp_rueck[0], temp_difference);
client.publish(temperature_state_topic, mqtt_message);
temp_vor_last_transmitted = temp_vor[0];
temp_rueck_last_transmitted = temp_rueck[0];
Serial.println(mqtt_message);
// Publish Fire state
fire = temp_difference > fire_threshold;
updateBinarysensor(fire_state_topic, fire);
// Publish Freeze state
updateBinarysensor(freeze_state_topic, ((temp_rueck[0] < freeze_threshold) | (temp_vor[0] < freeze_threshold)));
}
}
// lowpass(vorlauf_filter, temp_vor);
// -------------------------------------------------------------
// FILTER
// -------------------------------------------------------------
// if (fire)
// {
// // Turn off filter
// switchFilter(false);
// }
// if ((millis() - filterOffSince) > (filterIntervalHours*60*60*1000))
// {
// // Turn on filter
// switchFilter(true);
// }
// if ((millis() - filterOnSince) > (filterDurationMinutes*60*1000))
// {
// // Turn off filter
// switchFilter(false);
// }
}
void connectWifi()
{
Serial.print("Connecting to WiFi...");
WiFi.begin((char *)ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("... connected");
}
void connectMQTT()
{
String client_id = "hotpot_mqtt-client";
Serial.print("Connecting to MQTT broker...");
while (!client.connected()) {
Serial.print(".");
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
client.subscribe("hotpot/temp_threshold");
client.subscribe("hotpot/filter_duration");
client.subscribe("hotpot/filter");
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void createNewSensor(const char *name,
const char *device_class,
const char *state_topic,
const char *unique_id,
const char *identifiers,
const char *device_name,
const char *manufacturer,
const char *model,
const char *model_id,
const char *sensor_type,
const char *value_template)
{
// Create MQTT payload
if (sensor_type == "sensor")
{
sprintf(mqtt_message,
"{\"name\": \"%s\", \"device_class\": \"%s\", \"state_topic\": \"%s\", \"unit_of_measurement\": \"°C\", \"value_template\": \"%s\", \"unique_id\": \"%s\", \"device\": {\"identifiers\": [\"%s\"], \"name\": \"%s\", \"manufacturer\": \"%s\", \"model\": \"%s\", \"model_id\": \"%s\"} }",\
name, device_class, state_topic, value_template, unique_id, identifiers, device_name, manufacturer, model, model_id);
} else {
sprintf(mqtt_message,
"{\"name\": \"%s\", \"device_class\": \"%s\", \"state_topic\": \"%s\", \"unique_id\": \"%s\", \"device\": {\"identifiers\": [\"%s\"], \"name\": \"%s\", \"manufacturer\": \"%s\", \"model\": \"%s\", \"model_id\": \"%s\"} }",\
name, device_class, state_topic, unique_id, identifiers, device_name, manufacturer, model, model_id);
}
// Create MQTT topic
sprintf(topic, "%s/%s/%s/config", topic_prefix, sensor_type, unique_id);
// Serial.print("Creating sensor: ");
// Serial.println(topic);
// Serial.println(mqtt_message);
// Publish
client.publish(topic, mqtt_message, true);
}
void updateBinarysensor(const char* topic, bool state)
{
if (state) {
client.publish(topic, "ON");
} else {
client.publish(topic, "OFF");
}
}
void lowpass(float *filter, float temp)
{
unsigned int length = sizeof(filter);
for (int i = length; i > 0; i--)
{
filter[i] = filter[i-1];
}
filter[0] = temp;
float output = 0.0;
for (int i = 0; i < length; i++)
{
output = output + filter[i];
}
output = output / length;
}