-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheating_ctrl.ino
180 lines (155 loc) · 5.28 KB
/
heating_ctrl.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
#include "secrets.h"
#include "screen_setup.h"
#include "ui.h"
#include "constants.h"
#include <map>
#include <WiFi.h>
#include <PubSubClient.h>
std::map<String, Subscriber*> subscriptions;
WiFiClient client;
PubSubClient mqttClient(client);
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason){
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
void WiFiStationConnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Connected to AP successfully!");
}
void WiFiGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
UI::getInstance().connected(true);
connectToMqtt();
UI::getInstance().refreshTimeLabel();
}
void WiFiStationDisconnected(WiFiEvent_t event, WiFiEventInfo_t info){
Serial.println("Disconnected from WiFi access point");
Serial.print("WiFi lost connection. Reason: ");
Serial.println(info.wifi_sta_disconnected.reason);
Serial.println("Trying to Reconnect");
if (info.wifi_sta_disconnected.reason == WIFI_REASON_NO_AP_FOUND) {
Serial.print("Scanning networks ... ");
int n = WiFi.scanNetworks();
Serial.print("found ");
Serial.println(n);
for(int i=0; i<n; i++){
Serial.println(WiFi.SSID(i));
}
}
UI::getInstance().connected(false);
UI::getInstance().mqttConnected(false);
connectToWifi();
}
void onMqttConnect(bool sessionPresent){
Serial.println("Connected to MQTT broker");
UI::getInstance().mqttConnected(true);
for (auto it=subscriptions.begin(); it!=subscriptions.end(); ++it){
mqttClient.subscribe(it->first.c_str(), 1);
}
UI::getInstance().connected(true);
}
void onMqttMessage(char* topic, byte* payload, unsigned int len){
Serial.println(topic);
if(subscriptions.count(topic) == 0){
Serial.println("no subscription found");
}else{
auto dp = subscriptions[topic];
dp->parseData((char*)payload);
}
}
void connectToMqtt(){
Serial.println("Connecting to MQTT...");
if(mqttClient.connect("esp")){
onMqttConnect(true);
} else {
delay(250);
}
}
void connectToWifi(){
WiFi.mode(WIFI_MODE_STA);
char* ssid = WIFINAME;
char *password = PASSWORD;
WiFi.begin(ssid, password);
}
void create_subscription(RoomWidget *w, String wth_id, String drs_id){
String start = "device/status/";
if(!wth_id.isEmpty()){
w->setWthId(wth_id.c_str());
subscriptions[start + wth_id + ACTUALTEMPERATURE] = w->actTemp;
subscriptions[start + wth_id + SETPOINTTEMPERATURE] = w->spTemp;
subscriptions[start + wth_id + HUMIDITY] = w->hum;
subscriptions[start + wth_id + ACTIVEPROFILE] = w->actProfile;
subscriptions[start + wth_id + WINDOWSTATE] = w->win;
}
if(!drs_id.isEmpty()){
subscriptions[start + drs_id + DRSSTATE] = w->heat;
w->setDrsId(drs_id);
}
}
void setup(){
WiFi.onEvent(WiFiStationConnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(WiFiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent(WiFiStationDisconnected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
Serial.begin(115200);
while (!Serial);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setCallback(onMqttMessage);
// sorry for that terrible hack
// my 5" device has MAC ending with 0x74,
// the 4.3" device has a smaller touchscreen resolution
unsigned char mac[6];
WiFi.macAddress(mac);
bool small_screen = (mac[5] != 116);
if(!init_screen(small_screen)){
Serial.println("Panic");
delay(3000);
ESP.restart();
}
RoomWidget **rw = UI::getInstance().getRoomWidgets();
rw[0]->setRoomName(NAME_0);
rw[1]->setRoomName(NAME_1);
rw[2]->setRoomName(NAME_2);
rw[3]->setRoomName(NAME_3);
rw[4]->setRoomName(NAME_4);
rw[5]->setRoomName(NAME_5);
rw[6]->setRoomName(NAME_6);
rw[7]->setRoomName(NAME_7);
rw[8]->setRoomName(NAME_8);
rw[9]->setRoomName(NAME_9);
create_subscription(rw[0], WTH_0, DRS_0);
create_subscription(rw[1], WTH_1, DRS_1);
create_subscription(rw[2], WTH_2, DRS_2);
create_subscription(rw[3], WTH_3, DRS_3);
create_subscription(rw[4], WTH_4, DRS_4);
create_subscription(rw[5], WTH_5, DRS_5);
create_subscription(rw[6], WTH_6, DRS_6);
create_subscription(rw[7], WTH_7, DRS_7);
create_subscription(rw[8], WTH_8, DRS_8);
create_subscription(rw[9], WTH_9, DRS_9);
connectToWifi();
}
void loop(){
static uint32_t counter = 0;
lv_timer_handler(); /* let the GUI do its work */
delay(UI::getInstance().getRefreshTime());
if(mqttClient.connected()){
mqttClient.loop();
counter = 0;
} else {
UI::getInstance().mqttConnected(false);
counter += UI::getInstance().getRefreshTime();
if(counter > 5000){
counter = 0;
connectToMqtt();
}
}
}