-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFireplace.ino
139 lines (120 loc) · 4.42 KB
/
Fireplace.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
#include "HAFireplace.h"
#include <ESP8266WiFi.h>
#include <HAMqtt.h>
#include <LED.h>
#include <Clock.h>
#include <Timer.h>
#include <ArduinoOTA.h>
#include "secrets.h"
#include <DatedVersion.h>
DATED_VERSION(0, 2)
#define LOG_REMOTE
#define LOG_LEVEL 2
#include <Logging.h>
////////////////////////////////////////////////////////////////////////////////////////////
// Configuration
const char* sta_ssid = STA_SSID;
const char* sta_pswd = STA_PASS;
const char* mqtt_server = "192.168.2.170"; // test.mosquitto.org"; //"broker.hivemq.com"; //6fee8b3a98cd45019cc100ef11bf505d.s2.eu.hivemq.cloud";
int mqtt_port = 1883; // 8883;
const char* mqtt_user = MQTT_USER;
const char *mqtt_passwd = MQTT_PASS;
////////////////////////////////////////////////////////////////////////////////////////////
// Global instances
WiFiClient socket;
HAFireplace fireplace; // The Fireplace remote with all of its sensors
HAMqtt mqtt(socket, fireplace, SENSOR_COUNT); // Home Assistant MTTQ
Clock rtc; // A real (software) time clock
LED led; //
////////////////////////////////////////////////////////////////////////////////////////////
// Send log remotely to MQTT
void LOG_CALLBACK(char *msg) {
LOG_REMOVE_NEWLINE(msg);
mqtt.publish("Fireplace/log", msg, true);
}
////////////////////////////////////////////////////////////////////////////////////////////
// Connect to the STA network
void wifi_connect()
{
if (((WiFi.getMode() & WIFI_STA) == WIFI_STA) && WiFi.isConnected())
return;
DEBUG("Wifi connecting to %s.", sta_ssid);
WiFi.begin(sta_ssid, sta_pswd);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
DEBUG(".");
}
DEBUG("\n");
INFO("WiFi connected with IP address: %s\n", WiFi.localIP().toString().c_str());
}
////////////////////////////////////////////////////////////////////////////////////////////
// MQTT Connect
void mqtt_connect() {
INFO("Fireplace Remote v%s saying hello\n", VERSION);
}
///////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////
void sync_clock()
{
rtc.ntp_sync();
INFO("Clock synchronized to %s\n", rtc.now().timestamp().c_str());
}
////////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
wifi_connect();
// start MQTT to enable remote logging asap
INFO("Connecting to MQTT server %s\n", mqtt_server);
uint8_t mac[6];
WiFi.macAddress(mac);
fireplace.begin(mac, &mqtt); // 5) make sure the device gets a unique ID (based on mac address)
mqtt.onConnected(mqtt_connect); // register function called when newly connected
mqtt.begin(mqtt_server, mqtt_port, mqtt_user, mqtt_passwd); //
sync_clock();
INFO("Initialize OTA\n");
ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname("Fireplace-Remote");
ArduinoOTA.setPassword(OTA_PASS);
ArduinoOTA.onStart([]() {
INFO("[%s] - Starting remote software update",
rtc.now().timestamp(DateTime::TIMESTAMP_TIME).c_str());
});
ArduinoOTA.onEnd([]() {
INFO("[%s] - Remote software update finished",
rtc.now().timestamp(DateTime::TIMESTAMP_TIME).c_str());
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
});
ArduinoOTA.onError([](ota_error_t error) {
ERROR("Error remote software update");
});
ArduinoOTA.begin();
INFO("Setup complete\n\n");
}
////////////////////////////////////////////////////////////////////////////////////////////
//
////////////////////////////////////////////////////////////////////////////////////////////
Timer relax;
int idx = 0;
void loop()
{
// ensure we are still connected (STA-mode)
wifi_connect();
// handle any OTA requests
ArduinoOTA.handle();
// handle MQTT
mqtt.loop();
// whats the timec:\Users\erikv\OneDrive\Archive\Erik\Hobby\Domotica\1. TraceLog\Fireplace.bat
DateTime now = rtc.now();
fireplace.loop();
if (!relax.passed())
return;
relax.set(1000);
led.blink();
}
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////