-
Notifications
You must be signed in to change notification settings - Fork 0
/
Take One
212 lines (184 loc) · 5.16 KB
/
Take One
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
#include "config.h"
#include <ArduinoJson.h>
#include <WiFiUdp.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
#include <WS2812FX.h>
WS2812FX ws2812fx = WS2812FX(CONFIG_LED_COUNT, CONFIG_LED_PIN, NEO_GRB + NEO_KHZ800);
const char* ssid = CONFIG_WIFI_SSID;
const char* password = CONFIG_WIFI_PASS;
const char* mqtt_server = CONFIG_MQTT_HOST;
const char* mqtt_username = CONFIG_MQTT_USER;
const char* mqtt_password = CONFIG_MQTT_PASS;
const char* client_id = CONFIG_MQTT_CLIENT_ID;
const char* light_state_topic = CONFIG_MQTT_TOPIC_STATE;
const char* light_set_topic = CONFIG_MQTT_TOPIC_SET;
const char* on_cmd = CONFIG_MQTT_PAYLOAD_ON;
const char* off_cmd = CONFIG_MQTT_PAYLOAD_OFF;
const size_t BUFFER_SIZE = JSON_OBJECT_SIZE(20);
int ws2812fx_mode = 0;
uint8_t qossub = 0; // AMQTT can sub qos 0 or 1 or 2
uint8_t qospub = 0; // AMQTT can pub qos 0 or 1 or 2
// Maintained state for reporting to HA
byte red = 60;
byte green = 0;
byte blue = 0;
byte white = 0;
byte brightness = 15;
uint16_t fxspeed = 255;
bool stateOn = true;
const char* setEffect = NULL;
String currentEffect = "Multi Dynamic";
String allEffects[MODE_COUNT];
uint32_t getColorInt() {
return ((uint32_t)white << 24) | ((uint32_t)red << 16) | ((uint32_t)green << 8) | blue;
}
#define MQTT_MAX_PACKET_SIZE 1024
void setup() {
Serial.begin(9600);
ws2812fx.init();
ws2812fx.setBrightness(brightness);
ws2812fx.setSpeed(fxspeed);
ws2812fx.setMode(FX_MODE_STATIC);
ws2812fx.setColor(getColorInt());
ws2812fx.start();
if (stateOn) {
ws2812fx.service(); // turn on the leds immediatly
}
// Store all mode names (for faster lookup ?)
for (uint8_t i = 0; i < ws2812fx.getModeCount(); i++) {
allEffects[i] = ws2812fx.getModeName(i);
}
setup_ota();
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
sendState();
}
void setup_ota() {
ArduinoOTA.setHostname(CONFIG_OTA_NAME);
ArduinoOTA.setPassword((const char *)CONFIG_OTA_PASS);
ArduinoOTA.onStart([]() {
setMode("Static");
ws2812fx.setColor(0);
});
ArduinoOTA.onEnd([]() {
setMode(currentEffect);
ws2812fx.setColor(getColorInt());
});
ArduinoOTA.begin();
}
void setup_wifi() {
delay(10);
WiFi.mode(WIFI_STA);
WiFi.hostname(CONFIG_WIFI_HOST);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void callback(char* topic, byte* payload, unsigned int length) {
char message[length + 1];
for (int i = 0; i < length; i++) {
message[i] = (char)payload[i];
}
message[length] = '\0';
processJson(message);
sendState();
}
void processJson(char* message) {
StaticJsonBuffer < BUFFER_SIZE + 80 > jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(message);
if (!root.success()) {
Serial.println("parseObject() failed");
return;
}
if (root.containsKey("state")) {
if (strcmp(root["state"], on_cmd) == 0) {
stateOn = true;
if (!root.containsKey("effect")) {
setMode(currentEffect);
}
if (!root.containsKey("color")) {
ws2812fx.setColor(getColorInt());
}
}
else if (strcmp(root["state"], off_cmd) == 0) {
stateOn = false;
setMode("Static");
ws2812fx.setColor(0);
}
}
if (root.containsKey("effect")) {
setEffect = root["effect"];
currentEffect = setEffect;
setMode(currentEffect);
}
if (root.containsKey("color")) {
red = root["color"]["r"];
green = root["color"]["g"];
blue = root["color"]["b"];
ws2812fx.setColor(getColorInt());
}
if (root.containsKey("brightness")) {
brightness = root["brightness"];
ws2812fx.setBrightness(brightness);
}
if (root.containsKey("speed")) {
fxspeed = root["speed"];
ws2812fx.setSpeed(fxspeed);
}
}
void setMode(String _mode) {
int sizeEffects = sizeof(allEffects);
for (uint8_t i = 0; i < sizeEffects; i++) {
if (_mode == allEffects[i]) {
ws2812fx.setMode(i);
break;
}
}
}
void sendState() {
StaticJsonBuffer < BUFFER_SIZE + 80 > jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["state"] = (stateOn) ? on_cmd : off_cmd;
JsonObject& color = root.createNestedObject("color");
color["r"] = red;
color["g"] = green;
color["b"] = blue;;
root["effect"] = currentEffect;
root["speed"] = fxspeed;
root["brightness"] = brightness;
char buffer[root.measureLength() + 1];
root.printTo(buffer, sizeof(buffer));
Serial.println(buffer);
client.publish(light_state_topic, buffer, true);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect(CONFIG_MQTT_CLIENT_ID, CONFIG_MQTT_USER, CONFIG_MQTT_PASS)) {
Serial.println("connected");
client.subscribe(CONFIG_MQTT_TOPIC_SET);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
ws2812fx.service();
//ArduinoOTA.handle();
}