-
Notifications
You must be signed in to change notification settings - Fork 0
/
LED-Strip.ino
319 lines (267 loc) · 7.92 KB
/
LED-Strip.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
#include "ESP8266WiFi.h"
#include "ESP8266WebServer.h"
#include "ESP8266mDNS.h"
#include "FastLED.h"
#include "ArduinoJson.h"
#include "ESP8266HTTPClient.h"
#define NETWORK "Lydon"
#define PASSWORD "20221209"
#define NUMBER_LEDS 150
#define LED_PIN D1
WiFiClient WLAN_Client;
ESP8266WebServer server(80);
CRGB Leds[NUMBER_LEDS];
boolean blockedLeds[NUMBER_LEDS];
uint8_t Modus = 3;
unsigned long LoopTimer = 0;
uint8_t Brightness = 32;
CRGB Color = CRGB(50, 50, 50);
void setup() {
Serial.begin(115200);
delay(2000);
Serial.println();
Serial.print("Connect with WLAN: ");
Serial.println(NETWORK);
delay(100);
WiFi.begin(NETWORK, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
//Serial.print(WiFi.status());
//WiFi.printDiag(Serial);
}
Serial.println("");
Serial.print("Successfully connected. Own IP address: ");
Serial.println(WiFi.localIP());
registerController();
LEDS.addLeds<WS2812, LED_PIN, GRB>(Leds, NUMBER_LEDS);
LEDS.setBrightness(Brightness);
unblockLeds();
// Activate mDNS this is used to be able to connect to the server
// with local DNS hostmane ledstrip.local
if (MDNS.begin("ledstrip")) {
Serial.println("MDNS responder started: ledstrip[.local]");
}
// Set server routing
restServerRouting();
// Set not found response
server.onNotFound(handleNotFound);
// Start server
server.begin();
Serial.println("HTTP server started");
}
void loop() {
while (WiFi.status() != WL_CONNECTED) {
WiFi.disconnect();
WiFi.mode(WIFI_OFF);
WiFi.mode(WIFI_STA);
WiFi.begin(NETWORK, PASSWORD);
Serial.println("WLAN connection lost.");
delay(1000);
}
server.handleClient();
if (millis() > LoopTimer) {
switch (Modus) {
case 0: // Off
fadeToBlackBy(Leds, NUMBER_LEDS, 5);
LoopTimer = millis() + 50;
break;
case 1: // constant color
for (uint8_t i = 1; i < NUMBER_LEDS; i++) {
if (!blockedLeds[i]) Leds[i] = Color;
}
LoopTimer = millis() + 500;
break;
case 2: // waves
for (uint8_t i = 1; i < NUMBER_LEDS; i++) {
if (!blockedLeds[i]) {
byte Faktor = sin8(i + millis() / 50);
Leds[i] = CRGB(map(Color.red, 0, 255, 0, Faktor),
map(Color.green, 0, 255, 0, Faktor),
map(Color.blue, 0, 255, 0, Faktor));
}
}
LoopTimer = millis() + 50;
break;
case 3: // rainbow
for (uint8_t i = 1; i < NUMBER_LEDS; i++) {
if (!blockedLeds[i]) {
Leds[i] = CHSV(millis() / 50 + i, 255, 255);
}
}
LoopTimer = millis() + 50;
break;
}
FastLED.show();
}
}
void restServerRouting() {
server.on("/", HTTP_GET, []() {
server.send(200, F("text/html"), F("Welcome to the X-MAS tree LED-strip REST-API."));
});
server.on(F("/leds"), HTTP_GET, getLedStatus);
server.on(F("/modus"), HTTP_PUT, setModus);
server.on(F("/brightness"), HTTP_PUT, setBrightness);
server.on(F("/leds"), HTTP_PUT, setLedColor);
server.on(F("/unblockleds"), HTTP_PUT, setUnblockLeds);
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: " + server.uri();
message += "\nMethod: " + server.method();
message += "\nArguments: " + server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void unblockLeds() {
for (uint8_t i = 1; i < NUMBER_LEDS; i++) blockedLeds[i] = false;
}
void getLedStatus() {
DynamicJsonDocument doc(12400);
doc["modus"] = Modus;
doc["brightness"] = Brightness;
JsonArray LEDS = doc.createNestedArray("leds");
for (uint8_t i = 1; i <= NUMBER_LEDS; i++) {
if (blockedLeds[i]) {
JsonObject led = LEDS.createNestedObject();
led["led"] = i;
led["r"] = Leds[i].red;
led["g"] = Leds[i].green;
led["b"] = Leds[i].blue;
}
}
sendResponse200(doc);
}
void setModus() {
String body = server.arg("plain");
Serial.println(body);
DynamicJsonDocument doc(32);
DeserializationError error = deserializeJson(doc, body);
if (error) {
sendResponse400(error);
} else {
JsonObject bodyObj = doc.as<JsonObject>();
if (server.method() == HTTP_PUT) {
if (bodyObj.containsKey("modus")) {
Modus = bodyObj["modus"];
unblockLeds();
DynamicJsonDocument doc(16);
doc["modus"] = Modus;
sendResponse200(doc);
} else {
sendErrorIncorrectRequestBody();
}
}
}
}
void setBrightness() {
String body = server.arg("plain");
Serial.println(body);
DynamicJsonDocument doc(32);
DeserializationError error = deserializeJson(doc, body);
if (error) {
sendResponse400(error);
} else {
JsonObject bodyObj = doc.as<JsonObject>();
if (server.method() == HTTP_PUT) {
if (bodyObj.containsKey("brightness")) {
Brightness = bodyObj["brightness"];
LEDS.setBrightness(Brightness);
DynamicJsonDocument doc(16);
doc["brightness"] = Brightness;
sendResponse200(doc);
} else {
sendErrorIncorrectRequestBody();
}
}
}
}
void setLedColor() {
String body = server.arg("plain");
Serial.println(body);
DynamicJsonDocument doc(12400);
DeserializationError error = deserializeJson(doc, body);
if (error) {
sendResponse400(error);
} else {
JsonObject bodyObj = doc.as<JsonObject>();
if (server.method() == HTTP_PUT) {
if (bodyObj.containsKey("leds")) {
for (const auto& led : bodyObj["leds"].as<JsonArray>()) {
JsonObject ledOjb = led.as<JsonObject>();
if (ledOjb.containsKey("led")) {
uint8_t iLed = ledOjb["led"].as<uint8_t>();
blockedLeds[iLed] = true;
Leds[iLed] = CRGB(
ledOjb.containsKey("r") ? ledOjb["r"].as<uint8_t>() : Color.red,
ledOjb.containsKey("g") ? ledOjb["g"].as<uint8_t>() : Color.green,
ledOjb.containsKey("b") ? ledOjb["b"].as<uint8_t>() : Color.blue);
}
}
getLedStatus();
} else {
sendErrorIncorrectRequestBody();
}
}
}
}
void setUnblockLeds() {
String body = server.arg("plain");
Serial.println(body);
DynamicJsonDocument doc(32);
DeserializationError error = deserializeJson(doc, body);
if (error) {
sendResponse400(error);
} else {
if (server.method() == HTTP_PUT) {
unblockLeds();
DynamicJsonDocument doc(16);
sendResponse200(doc);
} else {
sendErrorIncorrectRequestBody();
}
}
}
void sendErrorIncorrectRequestBody() {
DynamicJsonDocument doc(96);
doc["status"] = "Error";
doc["message"] = F("No data found or incorrect!");
String buf;
serializeJson(doc, buf);
server.send(400, F("application/json"), buf);
}
void sendResponse200(DynamicJsonDocument doc) {
String buf;
serializeJson(doc, buf);
server.send(200, F("application/json"), buf);
}
void sendResponse400(DeserializationError error) {
// if the file didn't open, print an error:
Serial.print(F("Error while parsing JSON"));
Serial.println(error.c_str());
String msg = error.c_str();
server.send(400, F("text/html"), "Error while parsing json body! <br>" + msg);
}
void registerController() {
HTTPClient httpClient;
Serial.print("HTTP -> begin...\n");
if (httpClient.begin(WLAN_Client, "http://192.168.0.99:8080/register")) {
/*
httpClient.addHeader("Content-Type", "application/json");
httpClient.addHeader("api-key", "wf19sEt..........fOBWhP8Q");
*/
String payload = "{}";
int httpCode = httpClient.POST(payload);
if (httpCode > 0) {
Serial.printf("HTTP -> Code: %d\n", httpCode);
} else {
Serial.printf("HTTP -> GET... failed, error: %s\n", httpClient.errorToString(httpCode).c_str());
}
httpClient.end();
} else {
Serial.printf("HTTP -> Unable to connect\n");
}
}