forked from thehookup/E131-MQTT-RGBW-Lights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
E131_MQTT_B1_CONFIGURE.ino
261 lines (240 loc) · 7.89 KB
/
E131_MQTT_B1_CONFIGURE.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
//requires ESPAsyncUDP: https://github.com/me-no-dev/ESPAsyncUDP
#include <ESP8266WiFi.h> //If you get an error here you need to install the ESP8266 board manager
#include <ESPAsyncE131.h> //https://github.com/forkineye/ESPAsyncE131
#include <my92xx.h> //https://github.com/xoseperez/my92xx
#include <ArduinoOTA.h> //ArduinoOTA is included in the ArduinoIDE now
#include <PubSubClient.h> //https://github.com/knolleary/pubsubclient
#define MY92XX_MODEL MY92XX_MODEL_MY9231 // The MY9291 is a 4-channel driver, usually for RGBW lights
#define MY92XX_CHIPS 2 // No daisy-chain
#define MY92XX_DI_PIN 12 // DI GPIO
#define MY92XX_DCKI_PIN 14 // DCKI GPIO
#define UNIVERSE 1 // First DMX Universe to listen for
#define UNIVERSE_COUNT 1 // Total number of Universes to listen for, starting at UNIVERSE
my92xx _my92xx = my92xx(MY92XX_MODEL, MY92XX_CHIPS, MY92XX_DI_PIN, MY92XX_DCKI_PIN, MY92XX_COMMAND_DEFAULT);
ESPAsyncE131 e131(UNIVERSE_COUNT);
WiFiClient espClient;
PubSubClient client(espClient);
//USER CONFIGURED SECTION START//
#define MQTT_CLIENT_NAME "B1_2" //This name will be used for all MQTT Messages
const char* ssid = "YourSSID";
const char* password = "YourWiFi_PW";
const char* mqtt_server = "192.168.X.XXX";
const int mqtt_port = 1883;
const char *mqtt_user = "YourMQTTUserName";
const char *mqtt_pass = "YourMQTTPassword";
//USER CONFIGURED SECTION END//
int r = 0;
int g = 0;
int b = 0;
int w = 0;
uint8_t r_show = 0;
uint8_t g_show = 0;
uint8_t b_show = 0;
uint8_t w_show = 0;
int brightness = 255;
bool boot = true;
String effect = "MQTT";
char charPayload[50];
const char *mqtt_client_name = MQTT_CLIENT_NAME;
void setup_wifi()
{
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
}
void reconnect()
{
int retries = 0;
while (!client.connected()) {
if(retries < 5)
{
Serial.print("Attempting MQTT connection...");
if (client.connect(mqtt_client_name, mqtt_user, mqtt_pass))
{
Serial.println("connected");
if(boot == false)
{
client.publish(MQTT_CLIENT_NAME "/checkIn", "Reconnected");
}
if(boot == true)
{
client.publish(MQTT_CLIENT_NAME "/checkIn", "Rebooted");
boot = false;
}
client.subscribe(MQTT_CLIENT_NAME"/power");
client.subscribe(MQTT_CLIENT_NAME"/color");
client.subscribe(MQTT_CLIENT_NAME"/brightness");
client.subscribe(MQTT_CLIENT_NAME"/white");
client.subscribe(MQTT_CLIENT_NAME"/effect");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
retries++;
delay(5000);
}
}
if(retries > 5)
{
ESP.restart();
}
}
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
String newTopic = topic;
Serial.print(topic);
Serial.print("] ");
payload[length] = '\0';
String newPayload = String((char *)payload);
int intPayload = newPayload.toInt();
Serial.println(newPayload);
Serial.println();
newPayload.toCharArray(charPayload, newPayload.length() + 1);
if (newTopic == MQTT_CLIENT_NAME "/color")
{
client.publish(MQTT_CLIENT_NAME "/colorState", charPayload);
// get the position of the first and second commas
uint8_t firstIndex = newPayload.indexOf(',');
uint8_t lastIndex = newPayload.lastIndexOf(',');
uint8_t rgb_red = newPayload.substring(0, firstIndex).toInt();
if (rgb_red < 0 || rgb_red > 255) {
return;
} else {
r = rgb_red;
Serial.println(r);
}
uint8_t rgb_green = newPayload.substring(firstIndex + 1, lastIndex).toInt();
if (rgb_green < 0 || rgb_green > 255) {
return;
} else {
g = rgb_green;
Serial.println(g);
}
uint8_t rgb_blue = newPayload.substring(lastIndex + 1).toInt();
if (rgb_blue < 0 || rgb_blue > 255) {
return;
} else {
b = rgb_blue;
Serial.println(b);
}
updateLights();
}
if (newTopic == MQTT_CLIENT_NAME"/brightness")
{
client.publish(MQTT_CLIENT_NAME"/brightnessState", charPayload);
brightness = intPayload;
updateLights();
}
if (newTopic == MQTT_CLIENT_NAME"/white")
{
client.publish(MQTT_CLIENT_NAME"/whiteState", charPayload);
w = intPayload;
updateLights();
}
if (newTopic == MQTT_CLIENT_NAME"/effect")
{
client.publish(MQTT_CLIENT_NAME"/effectState", charPayload);
effect = newPayload;
updateLights();
}
if (newTopic == MQTT_CLIENT_NAME "/power")
{
client.publish(MQTT_CLIENT_NAME "/powerState", charPayload);
if (newPayload == "ON")
{
updateLights();
}
if (newPayload == "OFF")
{
_my92xx.setChannel(0, 0);
_my92xx.setChannel(1, 0);
_my92xx.setChannel(2, 0);
_my92xx.setChannel(3, 0);
_my92xx.setChannel(4, 0);
_my92xx.setChannel(5, 0);
_my92xx.setChannel(6, 0);
_my92xx.setState(true);
_my92xx.update();
}
}
}
void updateLights()
{
r_show = (r * brightness / 255);
g_show = (g * brightness / 255);
b_show = (b * brightness / 255);
w_show = (w * brightness / 255);
_my92xx.setChannel(4, r_show);
_my92xx.setChannel(3, g_show);
_my92xx.setChannel(5, b_show);
_my92xx.setChannel(0, w_show);
_my92xx.setChannel(1, w_show);
_my92xx.setState(true);
_my92xx.update();
}
void setup() {
Serial.begin(115200);
delay(10);
// Make sure you're in station mode
WiFi.mode(WIFI_STA);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
setup_wifi();
Serial.println("");
Serial.print(F("Connecting to "));
Serial.print(ssid);
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
ArduinoOTA.setHostname(MQTT_CLIENT_NAME);
ArduinoOTA.begin();
// Choose one to begin listening for E1.31 data
if (e131.begin(E131_UNICAST)) // Listen via Unicast
//if (e131.begin(E131_MULTICAST, UNIVERSE, UNIVERSE_COUNT)) // Listen via Multicast
Serial.println(F("Listening for data..."));
else
Serial.println(F("*** e131.begin failed ***"));
}
void loop() {
if(effect == "E131")
{
if (!e131.isEmpty()) {
e131_packet_t packet;
e131.pull(&packet); // Pull packet from ring buffer
Serial.printf("Universe %u / %u Channels | Packet#: %u / Errors: %u / CH1: %u\n",
htons(packet.universe), // The Universe for this packet
htons(packet.property_value_count) - 1, // Start code is ignored, we're interested in dimmer data
e131.stats.num_packets, // Packet counter
e131.stats.packet_errors, // Packet error counter
packet.property_values[1]); // Dimmer data for Channel 1
r_show = packet.property_values[1];
g_show = packet.property_values[2];
b_show = packet.property_values[3];
}
_my92xx.setChannel(4, r_show);
_my92xx.setChannel(3, g_show);
_my92xx.setChannel(5, b_show);
_my92xx.setChannel(1, 0);
_my92xx.setChannel(0, 0);
_my92xx.setState(true);
_my92xx.update();
}
if (!client.connected())
{
reconnect();
}
client.loop();
ArduinoOTA.handle();
}