-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESP32_Novy_Commander.ino
436 lines (365 loc) · 12.1 KB
/
ESP32_Novy_Commander.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/// The main goal of this script is to control the lights of the Novy cooking hood when the Zigbee kitchen lights are turned on or off.
/// This script looks at the API of Home Assistant to determine the on or off state of the kitchen lights, and sends a 433mhz signal to the connected 433mhz transmitter.
/// The ip address of the ESP32 shows a basic status web page with the ability to reboot the device.
/// SpectraCoder 01-12-2023
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <RCSwitch.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
/// IMPORTANT! This file holds all credentials and settings.
/// Make a copy of config.example.h and rename it to config.h to make this work with your own setup.
#include "config.h"
#include "favicon.h"
bool isKitchenLightsOn = false;
int kitchenLightBrightness = 0;
bool isNovyLightOn = false;
WebServer server(80);
RCSwitch transmitter = RCSwitch();
String returnedAPIString = "";
void setup() {
Serial.begin(115200);
pinMode(POWER_433MHZ_PIN, OUTPUT);
digitalWrite(POWER_433MHZ_PIN, HIGH);
connectWifi();
startServer();
transmitter.enableTransmit(TRANSMIT_433MHZ_PIN);
transmitter.setPulseLength(350);
transmitter.setProtocol(12);
ArduinoWifiUpdater();
}
///This function allows for firmware uploads through wifi,
///instead of connecting the ESP32 directly via usb.
void ArduinoWifiUpdater() {
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);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.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.begin();
}
void startServer()
{
server.stop();
server.on("/", webpage);
server.on("/reboot", reboot);
server.on("/favicon.ico", HTTP_GET, getFavicon);
server.begin();
}
void connectWifi() {
delay(4000); //Delay needed before calling the WiFi.begin
WiFi.mode(WIFI_STA);
WiFi.disconnect(true);
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE, INADDR_NONE);
WiFi.setHostname(HOSTNAME.c_str());
WiFi.begin(SSID, PASSWORD, 0, ROUTER_TO_CONNECT_TO);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network " + String(SSID));
Serial.println(WiFi.localIP());
}
void loop() {
//Check the current connection status
if ((WiFi.status() == WL_CONNECTED)) {
ArduinoOTA.handle();
returnedAPIString = getHomeAssistantAPIString();
isKitchenLightsOn = convertJsonToBool(returnedAPIString);
kitchenLightBrightness = convertJsonToBrightness(returnedAPIString);
Serial.println(isKitchenLightsOn ? "The kitchen lights are on" : "The kitchen lights are off");
if (isKitchenLightsOn && !isNovyLightOn)
{
/// The Novy cooking hood lights always start up in full brightness. To dim them down, you need to press and hold the dim button.
/// It is possible to simulate that button hold by sending the LightOn signal multiple times in a row.
/// I decided against that, and just leave them off when the other kitchen lights are dimmed below the threshold. (e.g. at night)
/// Otherwise there would be a flash of light from the cooking hood before it dimmed down to a good level.
if (kitchenLightBrightness > 200)
{
LightOn();
isNovyLightOn = true;
Serial.println("Turned on Novy light");
}
}
if (!isKitchenLightsOn && isNovyLightOn)
{
LightOff();
isNovyLightOn = false;
Serial.println("Turned off Novy light");
}
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Lost connection. Reconnecting...");
connectWifi();
startServer();
}
server.handleClient();
delay(REFRESH_TIME);
}
String getHomeAssistantAPIString() {
HTTPClient http;
String string = "";
//Url of the Home Assistant API kitchen lights
http.begin(HOME_ASSISTANT_LIGHT_URL);
http.addHeader("Authorization", HOME_ASSISTANT_TOKEN); //Adds the authorization header
int httpCode = http.GET(); //Makes the request
if (httpCode > 0) {
string = http.getString();
Serial.print("WiFi signal strength: ");
Serial.println(WiFi.RSSI());
} else {
Serial.println("Error on HTTP request");
}
http.end(); //Frees the resources
return string;
}
/// Checks to which wifi router the ESP32 is connected by comparing the mac addresses of the configured routers in config.h.
/// It returns a friendly name to know which one it is connected to.
String GetFriendlyRouterName()
{
/// This compares the first n bytes (where n is the third argument, in this case 6) of
/// the block of memory pointed by bssid with the first n bytes pointed by bssid_bedroom or bssid_livingroom.
/// It returns 0 if the two blocks of memory are identical.
String routerName = "";
uint8_t* bssid = WiFi.BSSID();
if (memcmp(bssid, BSSID_BEDROOM, 6) == 0)
{
routerName = "bedroom";
}
else if (memcmp(bssid, BSSID_LIVINGROOM, 6) == 0)
{
routerName = "living room";
}
else
{
routerName = "unknown BSSID";
}
return routerName;
}
void getFavicon ()
{
server.sendHeader("Content-Type", "image/x-icon");
server.send_P(200, "image/x-icon", (const char*)FAVICON, sizeof(FAVICON));
}
void webpage() {
int rssi = WiFi.RSSI();
int signalStrength = map(rssi, -100, -50, 0, 100);
signalStrength = constrain(signalStrength, 0, 100);
String routerName = GetFriendlyRouterName();
String html = R"(
<html>
<head>
<title>)" + String(HOSTNAME)
+ R"(</title>
<meta http-equiv="refresh" content=")"
+ String(REFRESH_TIME / 1000) + R"(">
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<style>
body {
background-color: #111111;
}
h1{
font-family: verdana;
color: white;
}
p {
font-family: verdana;
color: white;
}
pre{
color: lightgray;
}
.json{
margin-top: 50px;
background-color: black;
}
.button {
background-color: blue;
border: none;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>)" + String(HOSTNAME)
+ R"(</h1>
<div>
<p>Connected to )"
+ WiFi.SSID() + R"( ()" + routerName + R"()</p>
<p>WiFi Signal Strength: )"
+ String(signalStrength) + R"(% ()" + String(rssi) + R"()</p>
<p>)" + String(isKitchenLightsOn ? "The kitchen lights are on" : "The kitchen lights are off")
+ R"(</p>
<p>Brightness: )"
+ String(kitchenLightBrightness) + R"(</p>
<p>Used Heap Size: )"
+ getUsedHeapPercentage() + R"(</p>
<p>Uptime: )"
+ getUptime() + R"(</p>
<button class="button" onclick="location.href='/reboot'">Reboot</button>
</div>
<div class='json'>
<pre>)" + beautifyJson(returnedAPIString)
+ R"(</pre>
</div>
</body>
</html>
)";
server.send(200, "text/html", html);
}
void reboot() {
String html = R"(<html>
<head>
<title>Rebooting...</title>
<meta http-equiv='refresh' content='10; url=/'>
<style>
body {
background-color: #111111;
}
p {
font-family: verdana;
color: white;
}
</style>
</head>
<body>
<p>Rebooting...</p>
</body>
</html>)";
server.send(200, "text/html", html);
delay(1000);
ESP.restart();
}
String getUptime()
{
uint64_t timeSinceBoot = esp_timer_get_time();
uint64_t seconds = timeSinceBoot / 1000000;
uint64_t minutes = seconds / 60;
uint64_t hours = minutes / 60;
uint64_t days = hours / 24;
seconds = seconds % 60;
minutes = minutes % 60;
hours = hours % 24;
String uptime = String(days) + " days, " + String(hours) + " hours, " + String(minutes) + " minutes, " + String(seconds) + " seconds";
return uptime;
}
/// Function to calculate the percentage of used memory
String getUsedHeapPercentage()
{
// Get the free heap size
uint32_t freeHeap = esp_get_free_heap_size();
// Define the total heap size
uint32_t totalHeap = 320 * 1024; // 320KB
// Calculate the used heap size
uint32_t usedHeap = (freeHeap > totalHeap) ? 0 : (totalHeap - freeHeap);
// Calculate the percentage of used heap
float usedHeapPercentage = ((float)usedHeap / totalHeap) * 100;
// Return the used heap size and percentage as a string
return String(usedHeap) + " bytes (" + String(usedHeapPercentage) + "%)";
}
String beautifyJson(String jsonString)
{
//Create an empty Json object
StaticJsonDocument<1500> jsonDoc;
//Load the received data into a JSON object
DeserializationError error = deserializeJson(jsonDoc, jsonString);
if (!error)
{
String prettyJson;
serializeJsonPretty(jsonDoc, prettyJson);
return prettyJson;
}
else
{
return String("ERROR: Json could not be read");
}
}
/// Converts the "brightness" property inside the json (returned by Home Assistant) to an int.
/// Returns an int from 0 to 255.
int convertJsonToBrightness(String jsonString)
{
//Create an empty Json object
StaticJsonDocument<1500> jsonDoc;
//Load the received data into a JSON object
DeserializationError error = deserializeJson(jsonDoc, jsonString);
// Test if parsing succeeds.
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return 0;
}
JsonObject root = jsonDoc.as<JsonObject>();
JsonObject attributes = root["attributes"].as<JsonObject>();
int brightness = attributes["brightness"];
return brightness;
}
///Converts the "state" property inside the json (returned by Home Assistant) to a bool.
///Returns true if the state is "on", false if it is "off".
bool convertJsonToBool(String jsonString)
{
//Create an empty Json object
StaticJsonDocument<1500> jsonDoc;
//Load the received data into a JSON object
DeserializationError error = deserializeJson(jsonDoc, jsonString);
// Test if parsing succeeds.
if (error)
{
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.f_str());
return false;
}
// Get the state value inside the JSON object
const char* state = jsonDoc["state"];
// Convert to boolean
bool isOn = strcmp(state, "on") == 0;
return isOn;
}
void PressLight(int channelIndex)
{
Serial.print("Pressing light on channel ");
Serial.println(channelIndex);
Serial.print((NOVY_DEVICE_CODE[channelIndex] + NOVY_PREFIX + NOVY_COMMAND_LIGHT).c_str());
Serial.println();
//010101010111010001
transmitter.send((NOVY_DEVICE_CODE[channelIndex] + NOVY_PREFIX + NOVY_COMMAND_LIGHT).c_str());
}
void LightOn()
{
PressLight(0);
}
void LightOff()
{
PressLight(0);
}