forked from SpectraCoder/ESP32_Novy_Commander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32_Novy_Controller.ino
302 lines (267 loc) · 10.4 KB
/
ESP32_Novy_Controller.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
#include <Arduino.h>
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <RCSwitch.h>
#include <PubSubClient.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "config.h"
RCSwitch transmitter = RCSwitch();
AsyncWebServer server(80);
WiFiClient espClient;
PubSubClient client(espClient);
const int bufferSize = 10; // Adjust this value based on the number of lines you want to show in logging on webpage
String logBuffer[bufferSize];
int logIndex = 0;
void PressLight(int channelIndex);
void PressPower(int channelIndex);
void PressPlus(int channelIndex);
void PressMinus(int channelIndex);
void PressNovy(int channelIndex);
void notFound(AsyncWebServerRequest *request) {
request->send(404, "text/plain", "Not found");
}
void addToLogBuffer(String message) {
logBuffer[logIndex] = message;
logIndex = (logIndex + 1) % bufferSize;
}
String getLogContent() {
String content = "";
for (int i = 0; i < bufferSize; i++) {
int index = (logIndex + i) % bufferSize;
content += logBuffer[index] + "<br>";
}
return content;
}
void connectToMQTT() {
while (!client.connected()) {
addToLogBuffer("Connecting to MQTT...");
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
addToLogBuffer("Connected to MQTT");
client.subscribe((String(HOSTNAME) + "/button/light").c_str());
client.subscribe((String(HOSTNAME) + "/button/power").c_str());
client.subscribe((String(HOSTNAME) + "/button/plus").c_str());
client.subscribe((String(HOSTNAME) + "/button/minus").c_str());
client.subscribe((String(HOSTNAME) + "/button/novy").c_str());
} else {
addToLogBuffer("Failed, rc=" + String(client.state()) + " Retrying in 5 seconds...");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(POWER_433MHZ_PIN, OUTPUT);
digitalWrite(POWER_433MHZ_PIN, HIGH);
transmitter.enableTransmit(TRANSMIT_433MHZ_PIN);
transmitter.setPulseLength(350);
transmitter.setProtocol(12);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.printf("WiFi Failed!\n");
return;
}
addToLogBuffer("IP Address: " + WiFi.localIP().toString());
client.setServer(MQTT_SERVER, MQTT_PORT);
client.setCallback(callback);
// Hostname dfor OTA image update
ArduinoOTA.setHostname(HOSTNAME.c_str());
// OTA password for updates image via WiFi
ArduinoOTA.setPassword(OTAPASSWORD.c_str());
// ArduinoOTA.setPassword("novy");
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();
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
String html = "<html><head>";
html += "<style>";
html += "body {";
html += " background-color: #333333; /* Dark mode background color */";
html += " color: #ffffff; /* Dark mode text color */";
html += " font-family: Arial, sans-serif;";
html += " margin: 0;";
html += "}";
html += "header {";
html += " background-color: #343a40;";
html += " padding: 15px;";
html += " text-align: center;";
html += " color: #fff;";
html += "}";
html += ".container {";
html += " max-width: 600px;";
html += " margin: auto;";
html += " padding: 20px;";
html += "}";
html += ".button {";
html += " display: block;";
html += " width: 100%;";
html += " padding: 10px;";
html += " margin-bottom: 10px;";
html += " background-color: #007aff;";
html += " color: #fff;";
html += " border: 1px solid #007aff;";
html += " border-radius: 5px;";
html += " text-align: center;";
html += " text-decoration: none;";
html += " font-size: 16px;";
html += "}";
html += ".button:hover {";
html += " background-color: #0056b3;";
html += " border: 1px solid #0056b3;";
html += "}";
html += ".log-container {";
html += " background-color: #000000; /* Black background color */";
html += " color: #ffffff; /* White text color */";
html += " padding: 10px;";
html += " margin-top: 20px;";
html += " width: 100%;";
html += " border: 1px solid #007aff;"; // Border color same as the button color
html += " border-radius: 5px;";
html += "}";
html += "</style>";
html += "<script>";
html += "function toggleLight() {";
html += " fetch('/toggleLight');";
html += "}";
html += "function togglePower() {";
html += " fetch('/togglePower');";
html += "}";
html += "function togglePlus() {";
html += " fetch('/togglePlus');";
html += "}";
html += "function toggleMinus() {";
html += " fetch('/toggleMinus');";
html += "}";
html += "function toggleNovy() {";
html += " fetch('/toggleNovy');";
html += "}";
html += "function updateLog() {";
html += " fetch('/getLog').then(response => response.text()).then(data => {";
html += " document.getElementById('log-container').innerHTML = data;";
html += " });";
html += "}";
html += "setInterval(updateLog, 1000);";
html += "</script>";
html += "</head><body>";
html += "<header>";
html += " <h1>" + HOSTNAME + "</h1>";
html += "</header>";
html += "<div class='container'>";
html += " <a class='button' href='#' onclick='toggleLight()'>Toggle Light</a>";
html += " <a class='button' href='#' onclick='togglePower()'>Toggle Power</a>";
html += " <a class='button' href='#' onclick='togglePlus()'>Toggle Plus</a>";
html += " <a class='button' href='#' onclick='toggleMinus()'>Toggle Minus</a>";
html += " <a class='button' href='#' onclick='toggleNovy()'>Toggle Novy</a>";
html += " <div id='log-container' class='log-container'>" + getLogContent() + "</div>";
html += "</div>";
html += "</body></html>";
request->send(200, "text/html", html);
});
server.on("/getLog", HTTP_GET, [](AsyncWebServerRequest *request) {
// Send the current log content as plain text
request->send(200, "text/plain", getLogContent());
});
server.on("/toggleLight", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Light toggled");
int channelIndex = 0;
PressLight(channelIndex);
client.publish((String(HOSTNAME) + "/button/light").c_str(), "ON");
});
server.on("/togglePower", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Power toggled");
int channelIndex = 0;
PressPower(channelIndex);
client.publish((String(HOSTNAME) + "/button/power").c_str(), "ON");
});
server.on("/togglePlus", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Plus toggled");
int channelIndex = 0;
PressPlus(channelIndex);
client.publish((String(HOSTNAME) + "/button/plus").c_str(), "ON");
});
server.on("/toggleMinus", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Minus toggled");
int channelIndex = 0;
PressMinus(channelIndex);
client.publish((String(HOSTNAME) + "/button/minus").c_str(), "ON");
});
server.on("/toggleNovy", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Novy toggled");
int channelIndex = 0;
PressNovy(channelIndex);
client.publish((String(HOSTNAME) + "/button/novy").c_str(), "ON");
});
server.onNotFound(notFound);
server.begin();
}
void PressLight(int channelIndex) {
transmitter.send((NOVY_DEVICE_CODE[channelIndex] + NOVY_PREFIX + NOVY_COMMAND_LIGHT).c_str());
}
void PressPower(int channelIndex) {
transmitter.send((NOVY_DEVICE_CODE[channelIndex] + NOVY_PREFIX + NOVY_COMMAND_POWER).c_str());
}
void PressPlus(int channelIndex) {
transmitter.send((NOVY_DEVICE_CODE[channelIndex] + NOVY_PREFIX + NOVY_COMMAND_PLUS).c_str());
}
void PressMinus(int channelIndex) {
transmitter.send((NOVY_DEVICE_CODE[channelIndex] + NOVY_PREFIX + NOVY_COMMAND_MINUS).c_str());
}
void PressNovy(int channelIndex) {
transmitter.send((NOVY_DEVICE_CODE[channelIndex] + NOVY_PREFIX + NOVY_COMMAND_NOVY).c_str());
}
void callback(char* topic, byte* payload, unsigned int length) {
String message = "MQTT Message arrived [" + String(topic) + "] " + String((char*)payload);
addToLogBuffer(message);
if (strcmp(topic, (String(HOSTNAME) + "/button/light").c_str()) == 0) {
transmitter.send((NOVY_DEVICE_CODE[0] + NOVY_PREFIX + NOVY_COMMAND_LIGHT).c_str());
}
if (strcmp(topic, (String(HOSTNAME) + "/button/power").c_str()) == 0) {
transmitter.send((NOVY_DEVICE_CODE[0] + NOVY_PREFIX + NOVY_COMMAND_POWER).c_str());
}
if (strcmp(topic, (String(HOSTNAME) + "/button/plus").c_str()) == 0) {
transmitter.send((NOVY_DEVICE_CODE[0] + NOVY_PREFIX + NOVY_COMMAND_PLUS).c_str());
}
if (strcmp(topic, (String(HOSTNAME) + "/button/minus").c_str()) == 0) {
transmitter.send((NOVY_DEVICE_CODE[0] + NOVY_PREFIX + NOVY_COMMAND_MINUS).c_str());
}
if (strcmp(topic, (String(HOSTNAME) + "/button/novy").c_str()) == 0) {
transmitter.send((NOVY_DEVICE_CODE[0] + NOVY_PREFIX + NOVY_COMMAND_NOVY).c_str());
}
}
void loop() {
if (!client.connected()) {
connectToMQTT();
}
client.loop();
ArduinoOTA.handle();
}