-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_ledstrip.ino
360 lines (327 loc) · 11.5 KB
/
mqtt_ledstrip.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
#include "mqtt_ledstrip.h"
#ifdef USE_WIFI
const char *wifi_ssid = WIFI_SSID;
const char *wifi_pass = WIFI_PASS;
#endif
#ifndef NO_NETWORK
void mqtt_callback(char* topic, byte* payload, unsigned int length);
#ifdef ARTNET
void onDmxFrame(uint16_t universe, uint16_t length, uint8_t sequence, uint8_t* data, IPAddress remoteIP);
#endif
#endif
#ifdef USE_ETHERNET
byte mac[] = ETH_MAC;
byte broadcast[] = ETH_BROADCAST;
byte ip[] = ETH_IP;
#else
byte mac[6] = { };
byte broadcast[4] = { };
byte ip[4] = { };
#endif
CRGB leds[NUM_LEDS];
Light lights[NUM_LIGHTS];
TouchControl controls[NUM_CONTROLS];
int speed = GLOBAL_SPEED;
int count = 0;
#ifndef NO_NETWORK
#ifdef ARTNET
Artnet artnet;
CRGB artnet_leds [NUM_LEDS];
const int numberOfChannels = NUM_LEDS * 3;
const int startUniverse = 0;
const int maxUniverses = numberOfChannels / 512 + ((numberOfChannels % 512) ? 1 : 0);
bool universesReceived[maxUniverses];
bool sendFrame = 1;
int previousDataLength = 0;
#endif
#ifdef USE_ETHERNET
EthernetClient net_client;
#endif
#ifdef USE_WIFI
WiFiClient net_client;
#endif
const char* mqtt_server = MQTT_HOST;
const int mqtt_port = 1883;
const char* mqtt_username = MQTT_USER;
const char* mqtt_key = MQTT_PASS;
const char* mqtt_id = MQTT_CLID;
PubSubClient mqtt_client(net_client);
#endif
// SETUP
void setup() {
Serial.begin(115200);
Serial.println("Starting up LED Controller");
delay(10);
FastLED.setBrightness(BRIGHTNESS_SCALE);
#ifdef IS_APA102
FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, BGR, DATA_RATE_MHZ(24)>(leds, NUM_LEDS);
#endif
#ifdef IS_WS2801
FastLED.addLeds<WS2801, DATA_PIN, CLOCK_PIN, RGB>(leds, NUM_LEDS);
#endif
blink();
Serial.println("LEDs initialized");
#ifdef LIGHTS
lights = LIGHTS; // this doesn't work
#else
#ifdef BUMP_LED
lights[0] = Light("bump", &leds[0], 0, 1);
#endif
// lights[1] = Light("front", &leds[1], 0, 25); // FRONT
// lights[2] = Light("left", &leds[1], 25, 25, 1); // LEFT
// lights[3] = Light("right", &leds[1], 50, 25); // RIGHT
// lights[4] = Light("rear", &leds[1], 75, 25); // REAR
/* Test setup */
lights[1] = Light("front", &leds[1], 12, 11);
lights[2] = Light("left", &leds[1], 0, 10, 1);
lights[3] = Light("right", &leds[1], 25, 10);
CRGB* rearLeds[4] = { &leds[11], &leds[12], &leds[24], &leds[25] };
lights[4] = Light("rear", rearLeds);
lights[2].set_rgb(CRGB::Orange);
lights[3].set_rgb(CRGB::Orange);
lights[4].set_rgb(CRGB::Red);
lights[1].set_param(0,1);
lights[4].set_param(0,1);
#endif
for (Light light : lights) {
light.blink();
}
Serial.println("Light Mapping Initialized");
#ifdef TOUCH
controls[0] = TouchControl("left", T1, TOUCH_THRESHOLD,
[](int val) {
lights[2].set_program("chase");
lights[2].set_param(0,25);
lights[2].set_param(1,50);
lights[2].set_on(1); },
[](int val) { Serial.println("LEFT"); },
[](int val) { lights[2].turn_off(); }
);
controls[1] = TouchControl("right", T0, TOUCH_THRESHOLD,
[](int val) {
lights[3].set_program("chase");
lights[3].set_param(0,25);
lights[3].set_param(1,50);
lights[3].set_on(1);
},
[](int val) { Serial.println("RIGHT"); },
[](int val) { lights[3].turn_off(); }
);
controls[2] = TouchControl("Green", T4, TOUCH_THRESHOLD,
[](int val) { lights[1].turn_on(); },
[](int val) { Serial.println("GREEN"); },
[](int val) { lights[1].turn_off(); }
);
controls[3] = TouchControl("Red", T3, TOUCH_THRESHOLD,
[](int val) { lights[4].turn_on(); },
[](int val) { Serial.println("RED"); },
[](int val) { lights[4].turn_off(); }
);
#endif
#ifndef NO_NETWORK
#ifdef USE_ETHERNET
Serial.println(F("Connecting to Ethernet..."));
while (!Ethernet.begin(mac)) {
Serial.println(F("Ethernet configuration failed."));
delay(500);
}
IPAddress localip = Ethernet.localIP();
#endif
#ifdef USE_WIFI
WiFi.begin(wifi_ssid, wifi_pass);
while (WiFi.status() != WL_CONNECTED) {
Serial.println("Connecting to WiFi...");
delay(500);
}
IPAddress localip = WiFi.localIP();
#endif
mqtt_client.setServer(mqtt_server, mqtt_port);
mqtt_client.setCallback(mqtt_callback);
broadcast[0] = ip[0] = localip[0];
broadcast[1] = ip[1] = localip[1];
broadcast[2] = ip[2] = localip[2];
ip[3] = localip[3];
broadcast[3] = 0xff;
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
delay(100);
Serial.println(F("Network configured via DHCP"));
Serial.print("IP address: ");
Serial.println(localip);
#ifdef ARTNET
Serial.println("Initializing Artnet");
artnet.begin(mac, ip);
artnet.setBroadcast(broadcast);
artnet.setArtDmxCallback(onDmxFrame);
Serial.println("Artnet Initialized");
#endif
blink_rainbow();
delay(100);
blackout();
Serial.println("Setup complete, starting...");
#endif
delay(150);
}
// MAIN LOOP
void loop() {
#ifndef NO_NETWORK
if (!mqtt_client.connected()) {
Serial.println("Reconnecting...");
reconnect();
}
mqtt_client.loop();
#ifdef ARTNET
artnet.read();
#endif
#endif
#ifdef TOUCH
for (int i=0; i<NUM_CONTROLS; i++) controls[i].update();
#endif
for (int i=0; i<NUM_LIGHTS; i++) {
if ((count%(lights[i].get_param(0)||1)) == 0) {
Serial.println(lights[i].get_param(0));
lights[i].update();
}
}
FastLED.show();
count++;
Serial.println("======================LOOP======================");
#ifdef SLOW
delay(SLOW);
#endif
delay(1000/speed);
}
#ifndef NO_NETWORK
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
DynamicJsonDocument json(1024);
deserializeJson(json, (char *)payload);
payload[length] = '\0';
char* device = strtok(topic,"/");
char* name = strtok(NULL,"/");
if (strcmp(device, DEVICE_NAME) == 0
|| strcmp(device, "global") == 0
|| strcmp(device, "all" == 0)) {
for (int i=0; i<NUM_LIGHTS; i++) {
if (strcmp(name, lights[i].get_name()) == 0) {
if (json.containsKey("On")) {
bool OnOff = json["On"].as<bool>();
if(OnOff) {
Serial.println("Turning On");
lights[i].turn_on();
} else {
Serial.println("Turning Off");
lights[i].turn_off();
}
}
if (json.containsKey("Hue")) {
int val = json["Hue"].as<int>();
int hue = val * 17/24; // 255/360
Serial.print("Setting hue to ");
Serial.println(hue);
lights[i].set_hue(hue);
}
if (json.containsKey("Brightness")) {
int val = json["Brightness"].as<int>();
int bright = val * 51/20; // 255/100
Serial.print("Setting brightness to ");
Serial.println(bright);
lights[i].set_brightness(bright);
}
if (json.containsKey("Saturation")) {
int val = json["Saturation"].as<int>();
int sat = val * 51/20; // 255/100
Serial.print("Setting saturation to ");
Serial.println(sat);
lights[i].set_saturation(sat);
}
if (json.containsKey("Program")) {
const char* program = json["Program"];
lights[i].set_program(program);
}
if (json.containsKey("Speed")) { // deprecated, will be removed
int val = json["Speed"].as<int>();
if (device == "global") {
Serial.print("Setting speed to ");
Serial.println(val);
speed = val;
} else {
lights[i].set_param(0, val);
}
}
if (json.containsKey("Params")) {
Serial.println("Setting params");
JsonArray params = json["Params"].as<JsonArray>();
for (int p=0; p<sizeof(params); p++) {
int v = params[i].as<int>();
Serial.print("Setting param ");
Serial.print(p);
Serial.print(" to ");
Serial.print(v);
lights[i].set_param(p, v);
}
}
if (json.containsKey("Param")) {
int p = json["Param"]["p"];
int v = json["Param"]["v"];
lights[i].set_param(p, v);
}
}
}
}
}
void reconnect() {
// Loop until we're reconnected
while (!mqtt_client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqtt_client.connect(mqtt_id,mqtt_username,mqtt_key)) {
Serial.println("connected");
char feed[128];
sprintf(feed, "/%s/global", DEVICE_NAME);
mqtt_client.subscribe(feed);
sprintf(feed, "/%s/all", DEVICE_NAME);
mqtt_client.subscribe(feed);
for(int i=0; i<NUM_LIGHTS; i++) {
lights[i].subscribe(&mqtt_client);
}
} else {
Serial.print("failed, rc=");
Serial.print(mqtt_client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
#endif
// LED FUNCTIONS
void blink() {
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = CRGB::White;
}
FastLED.show();
delay(25);
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
}
void blink_rainbow() {
CHSV color;
for (int t=0; t<100; t++) {
color = CHSV((t*5)%255, 255, 100);
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = color;
}
FastLED.show();
delay(10);
}
}
void blackout() {
for (int i=0; i<NUM_LEDS; i++) {
leds[i] = CRGB::Black;
}
FastLED.show();
}