-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStrangerThings.ino
347 lines (276 loc) · 8.99 KB
/
StrangerThings.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
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "FastLED.h"
/************ WIFI and MQTT Information (CHANGE THESE FOR YOUR SETUP) ******************/
const char* ssid = "YOURSSID"; //type your WIFI information inside the quotes
const char* password = "YOURSSIDPASSWORD";
const char* mqtt_server = "192.168.0.100"; //Local MQTT Server Address
const char* mqtt_username = "yourMQTTusername";
const char* mqtt_password = "yourMQTTpassword";
const int mqtt_port = 1883;
#define SENSORNAME "StrangerThings" //change this to whatever you want to call your device
/************* MQTT TOPICS (change these topics as you wish) **************************/
const char* light_state_topic = "lights/strangerthings";
const char* light_set_topic = "lights/strangerthings/set";
const char* wordChar = "I Am Here";
String wordString = "I Am Here";
int current_position = 0;
int wait_time = 0;
int animation_speed = 50;
/****************************************FOR JSON***************************************/
const int BUFFER_SIZE = JSON_OBJECT_SIZE(10);
#define MQTT_MAX_PACKET_SIZE 512
const char* on_cmd = "ON";
const char* off_cmd = "OFF";
/*********************************** FastLED Defintions ********************************/
#define NUM_LEDS 186
#define DATA_PIN 5
//#define CLOCK_PIN 5
#define CHIPSET WS2811
#define COLOR_ORDER RBG
WiFiClient espClient;
PubSubClient client(espClient);
struct CRGB leds[NUM_LEDS];
struct CRGB leds_rgb[NUM_LEDS];
bool stateOn = false;
/********************************** START SETUP*****************************************/
void setup() {
Serial.begin(115200);
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
Serial.println("Ready");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
setColor(0,0,0);
}
/********************************** START SETUP WIFI*****************************************/
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
/********************************** START CALLBACK*****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
char message[length + 1];
for (int i = 0; i < length; i++) {
message[i] = (char)payload[i];
}
message[length] = '\0';
Serial.println(message);
if (!processJson(message)) {
return;
}
sendState();
}
/********************************** START PROCESS JSON*****************************************/
bool processJson(char* message) {
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(message);
if (!root.success()) {
Serial.println("parseObject() failed");
return false;
}
if (root.containsKey("state")) {
if (strcmp(root["state"], on_cmd) == 0) {
Serial.println("Recieved new state command, turn ON.");
stateOn = true;
current_position = 0;
wait_time = 0;
}
else if (strcmp(root["state"], off_cmd) == 0) {
Serial.println("Recieved new state command, turn OFF.");
stateOn = false;
setColor(0,0,0);
}
}
if (root.containsKey("strangerthingslight")) {
wordChar = root["strangerthingslight"];
wordString = String(wordChar);
Serial.print("Received new stranger things light word: ");
Serial.println(wordString);
setColor(0,0,0);
current_position = 0;
wait_time = 0;
}
if (root.containsKey("strangerthingsspeed")) {
animation_speed = root["strangerthingsspeed"];
Serial.print("Received new stranger things light speed: ");
Serial.println(animation_speed);
setColor(0,0,0);
current_position = 0;
wait_time = 0;
}
return true;
}
/********************************** START SEND STATE*****************************************/
void sendState() {
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["state"] = (stateOn) ? on_cmd : off_cmd;
root["speed"] = animation_speed;
root["strangerthingslight"] = wordString;
char buffer[root.measureLength() + 1];
root.printTo(buffer, sizeof(buffer));
client.publish(light_state_topic, buffer, true);
}
/********************************** START RECONNECT*****************************************/
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(SENSORNAME, mqtt_username, mqtt_password)) {
Serial.println("connected");
client.subscribe(light_set_topic);
sendState();
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/********************************** START Set Color*****************************************/
void setColor(int inR, int inG, int inB) {
for (int i = 0; i < NUM_LEDS; i++) {
leds[i].red = inR;
leds[i].green = inG;
leds[i].blue = inB;
}
FastLED.show();
Serial.print("Setting LEDs: ");
Serial.print("r: ");
Serial.print(inR);
Serial.print(", g: ");
Serial.print(inG);
Serial.print(", b: ");
Serial.println(inB);
//setup leds_rgb
int color_temp = 0;
for ( int i = 0; i < NUM_LEDS; i++){
if (color_temp==0)
{
leds_rgb[i].red = 255;
leds_rgb[i].green = 0;
leds_rgb[i].blue = 0;
}
else if (color_temp==1)
{
leds_rgb[i].red = 0;
leds_rgb[i].green = 255;
leds_rgb[i].blue = 0;
}
else if (color_temp==2)
{
leds_rgb[i].red = 0;
leds_rgb[i].green = 0;
leds_rgb[i].blue = 255;
}
color_temp++;
if (color_temp==3) color_temp=0;
}
}
/********************************** START MAIN LOOP*****************************************/
void loop() {
if (!client.connected()) {
reconnect();
}
if (WiFi.status() != WL_CONNECTED) {
delay(1);
Serial.print("WIFI Disconnected. Attempting reconnection.");
setup_wifi();
return;
}
client.loop();
int temp;
int temp_r;
int temp_g;
int temp_b;
if (current_position > wordString.length()) current_position = 0;
wait_time++;
if (wait_time == (1500*animation_speed) && stateOn) {
temp=returnIndexForChar(wordString.charAt(current_position));
Serial.print("Char #");
Serial.print(current_position);
Serial.print(":");
Serial.print(wordString.charAt(current_position));
Serial.print("=");
Serial.print(temp);
if (temp>=0) {
temp_r=leds_rgb[temp].red;
temp_g=leds_rgb[temp].green;
temp_b=leds_rgb[temp].blue;
leds[temp].setRGB(temp_r,temp_g,temp_b);
Serial.print("... r=");
Serial.print(temp_r);
Serial.print(",g=");
Serial.print(temp_g);
Serial.print(",b=");
Serial.println(temp_b);
FastLED.show();
current_position++;
}
else{
Serial.println(" .. Invalid Char, No lights.");
current_position++;
}
}
if (wait_time == (3000*animation_speed)) {
temp=returnIndexForChar(wordString.charAt(current_position-1));
Serial.print("Turning off bulb#:");
Serial.println(temp);
leds[temp].setRGB(0,0,0);
FastLED.show();
wait_time = 0;
}
}
//mapping letters to light position, assuming a 50 light string
int returnIndexForChar(char temp) {
if (temp == 'A' || temp == 'a') return 49;
if (temp == 'B' || temp == 'b') return 48;
if (temp == 'C' || temp == 'c') return 47;
if (temp == 'D' || temp == 'd') return 45;
if (temp == 'E' || temp == 'e') return 44;
if (temp == 'F' || temp == 'f') return 42;
if (temp == 'G' || temp == 'g') return 40;
if (temp == 'H' || temp == 'h') return 38;
if (temp == 'I' || temp == 'i') return 21;
if (temp == 'J' || temp == 'j') return 22;
if (temp == 'K' || temp == 'k') return 24;
if (temp == 'L' || temp == 'l') return 25;
if (temp == 'M' || temp == 'm') return 27;
if (temp == 'N' || temp == 'n') return 29;
if (temp == 'O' || temp == 'o') return 31;
if (temp == 'P' || temp == 'p') return 33;
if (temp == 'Q' || temp == 'q') return 35;
if (temp == 'R' || temp == 'r') return 17;
if (temp == 'S' || temp == 's') return 16;
if (temp == 'T' || temp == 't') return 14;
if (temp == 'U' || temp == 'u') return 12;
if (temp == 'V' || temp == 'v') return 10;
if (temp == 'W' || temp == 'w') return 7;
if (temp == 'X' || temp == 'x') return 5;
if (temp == 'Y' || temp == 'y') return 2;
if (temp == 'Z' || temp == 'z') return 0;
return -1;
}