forked from netzbasteln/syncenlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syncenlight0.6-nb.ino
351 lines (295 loc) · 9.74 KB
/
syncenlight0.6-nb.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
// SYNCENLIGHT BY NETZBASTELN
// ZF18 Workshop version
#define VERSION "0.6-nb"
#include <FS.h> // File system, this needs to be first.
#include <ESP8266WiFi.h> // ESP8266 Core WiFi Library
#include <DNSServer.h> // Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> // Local WebServer used to serve the configuration portal
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <Adafruit_NeoPixel.h> // LED
#include <PubSubClient.h> // MQTT client
#include <ArduinoJson.h> // https://github.com/bblanchon/ArduinoJson
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <Ticker.h>
//---------------------------------------------------------
// Pins
#define BUTTON_PIN 0 // D3
#define PIXEL_PIN 4 // D2
// Defaults
char mqtt_server[40] = "netzbasteln.de";
char publish_topic[40] = "syncenlight";
char mqtt_user[32] = "";
char mqtt_pass[32] = "";
unsigned int brightness = 255; // 0-255
//---------------------------------------------------------
bool shouldSaveConfig = false;
void saveConfigCallback () {
Serial.println("Should save config");
shouldSaveConfig = true;
}
WiFiManager wifiManager;
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
Adafruit_NeoPixel leds = Adafruit_NeoPixel(2, PIXEL_PIN, NEO_RGBW + NEO_KHZ800); // NEO_RGBW for Wemos Mini LED Modules, NEO_GRB for most Stripes
uint16_t hue = 0; // 0-359
extern const uint8_t gamma8[];
bool buttonState;
bool lastButtonState;
Ticker blinkTicker;
bool blinkTickerOn = false;
uint32_t blinkColor;
String chipId = String(ESP.getChipId(), HEX);
char chip_id_char_arr[7];
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println(".");
Serial.println(".");
pinMode(BUTTON_PIN, INPUT_PULLUP);
leds.begin();
leds.setBrightness(brightness);
blinkColor = leds.Color(10, 10, 10);
blinkTicker.attach(1, blinkLed);
chipId.toUpperCase();
chipId.toCharArray(chip_id_char_arr, 7);
String ssid = "SYNCENLIGHT-" + chipId;
int ssid_char_arr_size = ssid.length() + 1;
char ssid_char_arr[ssid_char_arr_size];
ssid.toCharArray(ssid_char_arr, ssid_char_arr_size);
Serial.println("Hi!");
Serial.print("Version: Syncenlight ");
Serial.println(VERSION);
Serial.print("Chip ID: ");
Serial.println(chipId);
// Read configuration from FS json.
Serial.println("Mounting FS ...");
// Clean FS, for testing
//SPIFFS.format();
if (SPIFFS.begin()) {
Serial.println("Mounted file system.");
if (SPIFFS.exists("/config.json")) {
// File exists, reading and loading.
Serial.println("Reading config file.");
File configFile = SPIFFS.open("/config.json", "r");
if (configFile) {
Serial.println("Opened config file.");
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
json.printTo(Serial);
if (json.success()) {
Serial.println("\nParsed json.");
strcpy(mqtt_server, json["mqtt_server"]);
strcpy(publish_topic, json["publish_topic"]);
strcpy(mqtt_user, json["mqtt_user"]);
strcpy(mqtt_pass, json["mqtt_pass"]);
} else {
Serial.println("Failed to load json config.");
}
configFile.close();
}
}
} else {
Serial.println("Failed to mount FS.");
}
//end read
// The extra parameters to be configured.
WiFiManagerParameter custom_mqtt_server("Server", "mqtt server", mqtt_server, 40);
WiFiManagerParameter custom_publish_topic("Channel", "channel", publish_topic, 40);
WiFiManagerParameter custom_mqtt_user("MQTT User", "mqtt username", mqtt_user, 40);
WiFiManagerParameter custom_mqtt_pass("MQTT Password", "mqtt password", mqtt_pass, 40);
wifiManager.setSaveConfigCallback(saveConfigCallback);
// Add all parameters.
wifiManager.addParameter(&custom_mqtt_server);
wifiManager.addParameter(&custom_publish_topic);
wifiManager.addParameter(&custom_mqtt_user);
wifiManager.addParameter(&custom_mqtt_pass);
// When button is pressed on start, go into config portal.
if (digitalRead(BUTTON_PIN) == LOW) {
blinkTicker.attach(0.1, blinkLed);
wifiManager.startConfigPortal(ssid_char_arr);
blinkTicker.attach(1, blinkLed);
}
else {
wifiManager.autoConnect(ssid_char_arr);
}
// We are connected.
Serial.println("WiFi Connected.");
// Read updated parameters.
strcpy(mqtt_server, custom_mqtt_server.getValue());
strcpy(publish_topic, custom_publish_topic.getValue());
strcpy(mqtt_user, custom_mqtt_user.getValue());
strcpy(mqtt_pass, custom_mqtt_pass.getValue());
// Save the custom parameters to FS.
if (shouldSaveConfig) {
Serial.println("Saving config.");
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["mqtt_server"] = mqtt_server;
json["publish_topic"] = publish_topic;
json["mqtt_user"] = mqtt_user;
json["mqtt_pass"] = mqtt_pass;
File configFile = SPIFFS.open("/config.json", "w");
if (!configFile) {
Serial.println("Failed to open config file for writing.");
}
json.printTo(Serial);
json.printTo(configFile);
configFile.close();
}
// End save.
// Start MQTT client.
mqttClient.setServer(mqtt_server, 1883);
mqttClient.setCallback(mqtt_callback);
Serial.println("MQTT client started.");
}
void loop() {
// Debounce button
buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW) {
hue = (hue + 1) % 360;
updateLed();
delay(15);
}
if (lastButtonState == LOW && buttonState == HIGH) {
Serial.print("New Color: ");
Serial.print(hue);
Serial.println();
// Send.
char payload[1];
itoa(hue, payload, 10);
mqttClient.publish(publish_topic, payload, true);
}
lastButtonState = buttonState;
if (!mqttClient.connected()) {
blinkTicker.attach(1, blinkLed);
mqtt_reconnect();
}
mqttClient.loop();
}
void mqtt_callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Received [");
Serial.print(topic);
Serial.print("]: ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.print(" (length ");
Serial.print(length);
Serial.print(")");
Serial.println();
// Update
if (length <= 3) {
payload[length] = '\0';
String s = String((char*)payload);
unsigned int newHue = s.toInt();
if (newHue >= 0 && newHue < 360) {
hue = newHue;
updateLed();
}
}
}
void mqtt_reconnect() {
while (!mqttClient.connected()) {
Serial.println("Connecting MQTT...");
if (mqttClient.connect(chip_id_char_arr,mqtt_user,mqtt_pass)) {
blinkTicker.detach();
Serial.println("MQTT connected.");
mqttClient.subscribe(publish_topic, 1); // QoS level 1
}
else {
Serial.print("Error, rc=");
Serial.print(mqttClient.state());
delay(5000);
}
}
}
void updateLed() {
uint32_t color = HSVtoRGB(hue, 255, 255);
for (uint16_t i=0; i<leds.numPixels(); i++) {
leds.setPixelColor(i, color);
}
leds.show();
}
void blinkLed() {
if (blinkTickerOn) {
leds.clear();
}
else {
for (uint16_t i=0; i<leds.numPixels(); i++) {
leds.setPixelColor(i, blinkColor);
}
}
leds.show();
blinkTickerOn = !blinkTickerOn;
}
// hue: 0-359, sat: 0-255, val (lightness): 0-255
uint32_t HSVtoRGB(unsigned int hue, unsigned int sat, unsigned int val) {
int r, g, b, base;
if (sat == 0) {
r = g = b = val;
}
else {
base = ((255 - sat) * val) >> 8;
switch (hue / 60) {
case 0:
r = val;
g = (((val - base) * hue) / 60) + base;
b = base;
break;
case 1:
r = (((val - base) * (60 - (hue % 60))) / 60) + base;
g = val;
b = base;
break;
case 2:
r = base;
g = val;
b = (((val - base) * (hue % 60)) / 60) + base;
break;
case 3:
r = base;
g = (((val - base) * (60 - (hue % 60))) / 60) + base;
b = val;
break;
case 4:
r = (((val - base) * (hue % 60)) / 60) + base;
g = base;
b = val;
break;
case 5:
r = val;
g = base;
b = (((val - base) * (60 - (hue % 60))) / 60) + base;
break;
}
}
return leds.Color(
pgm_read_byte(&gamma8[r]),
pgm_read_byte(&gamma8[g]),
pgm_read_byte(&gamma8[b]));
}
// Gamma correction curve
// https://learn.adafruit.com/led-tricks-gamma-correction/the-quick-fix
const uint8_t PROGMEM gamma8[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10,
10, 10, 11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16,
17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
25, 26, 27, 27, 28, 29, 29, 30, 31, 32, 32, 33, 34, 35, 35, 36,
37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 50,
51, 52, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 68,
69, 70, 72, 73, 74, 75, 77, 78, 79, 81, 82, 83, 85, 86, 87, 89,
90, 92, 93, 95, 96, 98, 99,101,102,104,105,107,109,110,112,114,
115,117,119,120,122,124,126,127,129,131,133,135,137,138,140,142,
144,146,148,150,152,154,156,158,160,162,164,167,169,171,173,175,
177,180,182,184,186,189,191,193,196,198,200,203,205,208,210,213,
215,218,220,223,225,228,231,233,236,239,241,244,247,249,252,255
};