-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeatCable_MQTT.ino
402 lines (297 loc) · 14.2 KB
/
HeatCable_MQTT.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Heat Cable control system using external temperature sensor (DB18B20) and a predefined temperature treshold (SetTemp)
* when relay to heatcable shall be activated.
*
* LCD display (Omilex sheild-LCD16x2) for visualize external(OutTemp) and treshold(SetTemp) temperature with relay status
*
* Pressed button(1) incresses the treshold and button(2) decreasess
*
*/
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <EEPROM.h>
#include <LCD16x2.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Define Wemos D1 Pin and EEPROM
#define One_Wire_Bus D5 // The pin location of the Thermometer sensor
#define SDA_PIN D1
#define SCL_PIN D2
#define LCD_RST_PIN D3
#define RELAY_PIN D7
#define EEPROM_SIZE 2
//Constants
const char *wifi_ssid = "xxx";
const char *wifi_pwd = "xxx";
const char *wifi_hostname = "HeatCable";
const char* mqtt_server = "xxx"; // MQTT Boker IP, your home MQTT server eg Mosquitto on RPi, or some public MQTT
const int mqtt_port = 1883; // MQTT Broker PORT, default is 1883 but can be anything.
const char *mqtt_user = "xxx"; // MQTT Broker User Name
const char *mqtt_pwd = "xxx"; // MQTT Broker Password
String clientId = "HeatCable : " + String(ESP.getChipId(), HEX);
const char *ota_hostname = "HeatCable";
const char *ota_pwd = "xxx";
// Global variables
float HeatCable_version = 1.0; // Heat Cable program version
float OutTemp = 0; // Temperature sensor
float previousOutTemp = 0;
signed char SetTemp = 0; // Treshold level temperature to activate the RELAY_PIN (Heat Cable)
signed char previousSetTemp = 0;
int EEPROMaddr = 0;
bool Backlight = false; // Turn on and off the LCD backlight
bool debug = true; // If true activate debug values to write to serial port
bool relayStatus = false; // Status on relay
unsigned long setBacklightMillis = 0; // Time when button was presed
const unsigned long backlightTime = 10000; // Time in milliseconds the backlight is on after pressed button (10 sec)
unsigned long readTempPrevMillis = 0; // Store previous millis
const unsigned long readTempTime = 10000; // Time in milliseconds the temperature sensor update value (30 sec)
unsigned long ReportTimerStatusPrevMillis = 0; // Store the previous millis
const unsigned long ReportTimerStatus = 60000; // Time in milliseconds (1 min) to report MQTT alive status
unsigned long ReportTimerLongPrevMillis = 0; // Store the previous millis
const unsigned long ReportTimerLong = 600000; // Timer in milliseconds (5 min) to report MQTT status
// MQTT Constants
const char *Status_topic = "HeatCable/Status";
const char *RelayStatus_topic = "HeatCable/RelayStatus";
const char *OutTemp_topic = "HeatCable/OutTemp";
const char *SetTemp_topic = "HeatCable/SetTemp";
OneWire oneWire(One_Wire_Bus); // Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature sensor
WiFiClient espClient; // Setup WiFi client definition WiFi
PubSubClient client(espClient); // Setup MQTT client
LCD16x2 lcd; // Setup a LCD16x2 instance to communicate to display
/**************************************************************************/
/* Setup */
/**************************************************************************/
void setup(){
setup_wifi();
setup_ota();
if (debug) { Serial.begin(115200); Serial.println("HeatCable"); }
EEPROM.begin(EEPROM_SIZE);
sensors.begin(); // Start up the Dallas Temperature library
Wire.begin(SDA_PIN, SCL_PIN); // join i2c bus
pinMode(RELAY_PIN, OUTPUT);
pinMode(LCD_RST_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
digitalWrite(LCD_RST_PIN, HIGH);
sensors.requestTemperatures();
OutTemp = sensors.getTempCByIndex(0);
initLCD();
}
/**************************************************************************/
/* Setup WiFi connection */
/**************************************************************************/
void setup_wifi() {
/* WiFi status return values and meaning
WL_IDLE_STATUS = 0,
WL_NO_SSID_AVAIL = 1,
WL_SCAN_COMPLETED = 2,
WL_CONNECTED = 3,
WL_CONNECT_FAILED = 4,
WL_CONNECTION_LOST = 5,
WL_DISCONNECTED = 6 */
if (debug){ Serial.print("WiFi.status(): "); Serial.println(WiFi.status()); }
int WiFi_retry_counter = 0;
WiFi.hostname(wifi_hostname);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_pwd);
// Loop until reconnected or max retry then restart
while (WiFi.status() != WL_CONNECTED){
WiFi_retry_counter ++;
if (WiFi_retry_counter == 30) {ESP.restart();}
if (debug){ Serial.print("WiFi retry: "); Serial.println(WiFi_retry_counter); }
delay(1000);
}
if (debug){ Serial.print("WiFi connected: ");Serial.println(WiFi.localIP()); }
}
/**************************************************************************/
/* Setup OTA connection */
/**************************************************************************/
void setup_ota() {
ArduinoOTA.setHostname(ota_hostname);
ArduinoOTA.setPassword(ota_pwd);
ArduinoOTA.onStart([]() {});
ArduinoOTA.onEnd([]() {});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {});
ArduinoOTA.onError([](ota_error_t error) {
// (void)error;
// ESP.restart();
});
ArduinoOTA.begin();
}
/**************************************************************************/
/* Setup MQTT connection */
/**************************************************************************/
void reconnect() {
int MQTT_retry_counter = 0;
// Loop until reconnected or max retry then leave
while (!client.connected() && MQTT_retry_counter < 30) {
client.setServer(mqtt_server, mqtt_port);
if (debug){ Serial.print("Connecting to MQTT server "); Serial.println(MQTT_retry_counter); }
client.setServer(mqtt_server, mqtt_port);
client.connect(clientId.c_str(), mqtt_user, mqtt_pwd);
MQTT_retry_counter ++;
delay (1000);
}
if (debug && client.connected()){ Serial.println(" MQTT connected"); }
}
/**************************************************************************/
/* Publish status to MQTT Broker */
/**************************************************************************/
void publishStatusMQTT(){
// Report status to MQTT broker every reportInterval
if(millis() - ReportTimerStatusPrevMillis > ReportTimerStatus) {
ReportTimerStatusPrevMillis = millis();
client.publish(Status_topic, String("online").c_str());
}
if((millis() - ReportTimerLongPrevMillis) > ReportTimerLong ) {
ReportTimerLongPrevMillis = millis();
client.publish(RelayStatus_topic, String(relayStatus).c_str());
client.publish(OutTemp_topic, String(OutTemp).c_str());
client.publish(SetTemp_topic, String(SetTemp).c_str());
}
}
/**************************************************************************/
/* Main loop */
/**************************************************************************/
void loop(){
ArduinoOTA.handle();
client.loop();
if (WiFi.status() != WL_CONNECTED){ setup_wifi(); } // Check WiFi connnection reconnect otherwise
if (!client.connected()) { reconnect(); } // Check MQTT connnection reconnect otherwise
publishStatusMQTT(); // Publish MQTT status
getOutTemp(); // Read temperature sensor
writeOutTemp(); // Write temperature sensor value on LCD
writeSetTemp(); // Write stored temperature treshold value
setRelay(); // Set relay according to threshoild values
setBacklight(); // Enlight LCD backlight if any buttons is pressed during a defined time
checkButtons(); // Check if any buttons is pressed and
}
/**************************************************************************/
/* Init LCD */
/**************************************************************************/
void initLCD(){
Wire.begin(SDA_PIN, SCL_PIN); // join i2c bus
delay(1000);
lcd.lcdSetBlacklight(1000);
lcd.lcdClear();
lcd.lcdGoToXY(1,1);
lcd.lcdWrite("Welcome to");
lcd.lcdGoToXY(1,2);
lcd.lcdWrite("HeatCable");
lcd.lcdGoToXY(11,2);
lcd.lcdWrite(HeatCable_version,1);
delay(2000);
lcd.lcdClear();
lcd.lcdGoToXY(1,1);
lcd.lcdWrite("Out: ");
lcd.lcdGoToXY(1,2);
lcd.lcdWrite("Set: ");
}
/**************************************************************************/
/* Read temperature sensor data */
/**************************************************************************/
void getOutTemp(){
if(millis() - readTempPrevMillis > readTempTime ) {
readTempPrevMillis = millis();
sensors.requestTemperatures();
OutTemp = sensors.getTempCByIndex(0);
}
}
void writeOutTemp(){
if (previousOutTemp != OutTemp){ // don't update LCD if same value
lcd.lcdGoToXY(6,1);
lcd.lcdWrite(" ");
lcd.lcdWrite(OutTemp, 1);
lcd.lcdGoToXY(11,1);
lcd.lcdWrite("C");
}
previousOutTemp = OutTemp;
}
/**************************************************************************/
/* Write LCD treshold tempoerature, when relay should activate/deactivate */
/**************************************************************************/
void writeSetTemp(){
SetTemp = EEPROM.read(EEPROMaddr);
if (previousSetTemp != SetTemp){ // don't update LCD if same value
lcd.lcdGoToXY(6,2);
lcd.lcdWrite(" ");
lcd.lcdWrite(SetTemp, 1);
lcd.lcdGoToXY(11,2);
lcd.lcdWrite("C");
}
previousSetTemp = SetTemp;
}
/**************************************************************************/
/* Update LCD and activate/deactivate relay */
/**************************************************************************/
void setRelay(){
if (OutTemp < SetTemp) { // Activate Heat cable
relayStatus = true;
digitalWrite(RELAY_PIN, HIGH);
lcd.lcdGoToXY(14,1);
lcd.lcdWrite("ON ");
}
if (OutTemp > SetTemp) { // Deactivate Heat cable
relayStatus = false;
digitalWrite(RELAY_PIN, LOW);
lcd.lcdGoToXY(14,1);
lcd.lcdWrite("OFF");
}
}
/**************************************************************************/
/* Turn on and off LCD backlight */
/**************************************************************************/
void setBacklight(){
unsigned long currentMillis = millis();
if (Backlight == true) {
lcd.lcdSetBlacklight(1000);
if(currentMillis - setBacklightMillis > backlightTime) {
Backlight = false;
}
}
else {
lcd.lcdSetBlacklight(0);
}
}
/**************************************************************************/
/* Check if any buttons on LCD display sheild is pressed */
/**************************************************************************/
void checkButtons(){
int buttons;
buttons = lcd.readButtons();
if (buttons == 14) { // Button 1
SetTemp++;
EEPROM.write(EEPROMaddr, SetTemp);
EEPROM.commit();
Backlight = true;
setBacklightMillis = millis();
}
if (buttons == 13) { // Button 2
SetTemp--;
EEPROM.write(EEPROMaddr, SetTemp);
EEPROM.commit();
Backlight = true;
setBacklightMillis = millis();
}
if (buttons == 11){ // Button 3
Backlight = true;
setBacklightMillis = millis();
}
if (buttons == 7) { // Button 4
Backlight = true;
setBacklightMillis = millis();
}
if (buttons == 0) { // Can not read status from LCD => trigg a restart via LCD reset PIN
if (debug) { Serial.println(" RESTART "); }
digitalWrite(LCD_RST_PIN, LOW);
delay(3000);
digitalWrite(LCD_RST_PIN, HIGH);
delay(1000);
ESP.restart();
}
}