-
Notifications
You must be signed in to change notification settings - Fork 0
/
RGBWW_LED_PROJECT.ino
200 lines (167 loc) · 4.96 KB
/
RGBWW_LED_PROJECT.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
#include <Redis.h>
#include "FastLED.h"
#include "FastLED_RGBW.h"
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
// Importing config
#include "config.h"
// Cycle count for restart purposes
int cycles = 0;
// JSON Object for LED's data
DynamicJsonDocument ledsData(20480); // This can handle up to 200 LEDs
//NTP timestamp string
String formattedDate;
void setup() {
// Setting up
Serial.begin(115200);
Serial.println();
Serial.println("Starting up...");
// Setting up WiFi Connection
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Assigned IP Address: ");
Serial.println(WiFi.localIP());
delay(500);
// Setup NTP Client
timeClient.begin();
timeClient.update();
timeClient.setTimeOffset(0); // Time zone GMT 0
// Setting up Redis
if (redisConnection.connect(REDIS_ADDR, REDIS_PORT))
{
Serial.println("Connected to the Redis server!");
} else {
Serial.println("Failed to connect to the Redis server! Restarting in 5 seconds...");
delay(5000);
ESP.restart();
}
// Redis Authentication
gRedis = new Redis (redisConnection);
auto connRet = gRedis->authenticate(REDIS_PASS);
if (connRet == RedisSuccess)
{
Serial.println("Successfull Redis Authentication!");
}
else
{
Serial.printf("Failed to authenticate to the Redis server! Errno: %d\n", (int)connRet, ". Restarting in 5 seconds...");
delay(5000);
ESP.restart();
}
Serial.println("====================> Configuration <====================");
// Registering device in redis
if(!gRedis->exists(KEY_NAME_CONFIG)){
DynamicJsonDocument doc(1024);
doc["MAC_ADDRESS"] = WiFi.macAddress();
doc["LED_TYPE"] = LED_TYPE;
doc["LOCATION"] = LOCATION;
doc["NUM_LEDS"] = NUM_LEDS;
doc["LOCAL_IP"] = WiFi.localIP().toString();
doc["NEW"] = true;
char config_string[256];
serializeJson(doc, config_string);
Serial.println("Registered as a new device, please complete setup in the dashboard.");
gRedis->set(KEY_NAME_CONFIG, config_string);
} else {
Serial.println("Device already registered!");
Serial.println("Current Configuration: " + gRedis->get(KEY_NAME_CONFIG));
}
// FastLED setup
FastLED.addLeds<WS2812B, DATA_PIN, RGB>(ledsRGB, getRGBWsize(NUM_LEDS));
FastLED.setBrightness(brightness);
// FastLED.show(); Show only when updating led's to prevent LEDs from flashing on restart
}
void loop(){
// Update time every "updateTimeCycle" number of cycles
if((cycles % updateTimeCycle) == 0){
// Update NTP time on each loop
timeClient.update();
formattedDate = timeClient.getEpochTime();
char timeStamp[12];
formattedDate.toCharArray(timeStamp, 12);
// Updating "last_connected";
gRedis->set(KEY_NAME_LC, timeStamp);
Serial.println("Updating time: "+formattedDate);
}
// Check if there is DATA for LED's
if(!gRedis->exists(KEY_NAME_DATA)){
// No data for LED's waiting for registration
Serial.println("No data for leds...");
colorFillHalf(CRGB::Red);
} else {
Serial.println("["+String(cycles)+"] Found data for LED's.");
deserializeJson(ledsData, gRedis->get(KEY_NAME_DATA));
int mode = ledsData["mode"].as<int>();
switch(mode){
case 0:
FillLEDs(ledsData);
break;
case 1:
rainbow();
break;
case 2:
rainbowLoop(ledsData);
break;
default:
Serial.println("Configuration exist but doesn't select any of the available modes.");
break;
}
}
// Restart cycle
cycles++;
if(cycles > maxCycles){
// When restarting, set the last connected to 0
char * zero = "0";
gRedis->set(KEY_NAME_LC, zero);
// Restart
Serial.println("Max Cycle count reached. Restarting in 1 second...");
delay(1000);
ESP.restart();
}
}
void FillLEDs(DynamicJsonDocument config){
// Processing data
for(int i = 0; i < NUM_LEDS; i++){
if(config["data"][i]){ // Does the config entry exist?
leds[i] = CRGBW(config["data"][i]["r"], config["data"][i]["g"], config["data"][i]["b"], config["data"][i]["w"]);
}
}
// Once the configuration has been done, show LED's from the new array.
FastLED.show();
}
void wipeLEDs(){
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CRGBW(0, 0, 0, 0);
FastLED.show();
}
}
void rainbow(){
static uint8_t hue;
for(int i = 0; i < NUM_LEDS; i++){
leds[i] = CHSV((i * 256 / NUM_LEDS) + hue, 255, 255);
}
FastLED.show();
hue++;
}
void rainbowLoop(DynamicJsonDocument config){
long millisIn = millis();
long loopTime = config["modeConfig"]["looptime"]; // 5 seconds
while(millis() < millisIn + loopTime){
rainbow();
delay(5);
}
}
void colorFillHalf(CRGB c){
for(int i = 0; i < NUM_LEDS; i += 2){
leds[i] = c;
FastLED.show();
}
}