-
Notifications
You must be signed in to change notification settings - Fork 3
/
ha_mqtt_ledstrip.ino
212 lines (175 loc) · 4.86 KB
/
ha_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
// Based on
// https://github.com/kitesurfer1404/WS2812FX
// https://github.com/corbanmailloux/esp-mqtt-rgb-led
// TODO
// Use setSegment() and multiple colors
// Get rid of the String with some char
#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_GRBW + 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);
// Maintained state for reporting to HA
byte red = 60;
byte green = 0;
byte blue = 0;
byte white = 255;
byte brightness = 220;
uint16_t fxspeed = 1000;
bool stateOn = true;
const char* setEffect = NULL;
String currentEffect = "Static";
String allEffects[MODE_COUNT];
uint32_t getColorInt() {
return ((uint32_t)white << 24) | ((uint32_t)red << 16) | ((uint32_t)green << 8) | blue;
}
void setup() {
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);
}
}
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()) {
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("white_value")) {
white = root["white_value"];
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> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
JsonObject& color = root.createNestedObject("color");
color["r"] = red;
color["g"] = green;
color["b"] = blue;
root["white_value"] = white;
root["state"] = (stateOn) ? on_cmd : off_cmd;
root["brightness"] = brightness;
root["effect"] = currentEffect;
root["speed"] = fxspeed;
char buffer[root.measureLength() + 1];
root.printTo(buffer, sizeof(buffer));
client.publish(light_state_topic, buffer, true);
}
void loop() {
if (!client.connected()) {
if (client.connect(client_id, mqtt_username, mqtt_password)) {
client.subscribe(light_set_topic);
sendState();
}
}
client.loop();
ws2812fx.service();
ArduinoOTA.handle();
}