-
Notifications
You must be signed in to change notification settings - Fork 1
/
MQTT_LED_Controller.ino
236 lines (204 loc) · 5.39 KB
/
MQTT_LED_Controller.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
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_NeoPixel.h>
#include <Scheduler.h>
#include <ArduinoJson.h>
#define PIN 0
#define NUMPIXELS 12
String address;
int r;
int g;
int b;
String fv;
String sv;
String tv;
String fov;
String fiv;
StaticJsonBuffer<8192> jsonBuffer;
JsonObject& blinkj = jsonBuffer.createObject();
JsonObject& fastblinkj = jsonBuffer.createObject();
//Ergänzen Sie hier die Daten Ihres WLANs und MQTT-Servers
const char* ssid = "WIFI-SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "192.168.1.1";
//Den MQTT-Topic können Sie in der Reconnect-Funktion ändern
WiFiClient espClient;
PubSubClient client(espClient);
//Sollten Grün und Rot vertauscht sein, ändern Sie NEO_RGB auf NEO_GRB
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_RGB + NEO_KHZ800);
void callback(char* topic, byte* payload, unsigned int length) {
String spayload;
for (int i = 0; i < length; i++) {
spayload += (char)payload[i];
}
Serial.print("Nachricht empfangen: " + spayload + "\n");
int ci = spayload.indexOf(',');
int sci = spayload.indexOf(',', ci + 1);
int tci = spayload.indexOf(',', sci + 1);
int foci = spayload.indexOf(',', tci + 1);
fv = spayload.substring(0, ci);
sv = spayload.substring(ci + 1, sci);
tv = spayload.substring(sci + 1, tci);
fov = spayload.substring(tci + 1, foci);
fiv = spayload.substring(foci + 1, spayload.length());
address = fv;
if (sv == "blink" || sv == "fastblink" || sv == "stop"){
int firstaddress;
int iaddress = address.toInt();
r = tv.toInt();
g = fov.toInt();
b = fiv.toInt();
if (sv == "blink") {
JsonObject& led = blinkj.createNestedObject(address);
led["r"] = tv;
led["g"] = fov;
led["b"] = fiv;
}
else if (sv == "fastblink") {
JsonObject& led = fastblinkj.createNestedObject(address);
led["r"] = tv;
led["g"] = fov;
led["b"] = fiv;
}
else if (sv == "stop") {
blinkj.remove(address);
fastblinkj.remove(address);
pixels.setPixelColor(iaddress, pixels.Color(0, 0, 0));
pixels.show();
}
}
else {
int r = sv.toInt();
int g = tv.toInt();
int b = fov.toInt();
if (address == "all") {
for (int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, pixels.Color(r, g, b));
pixels.show();
}
}
else {
int lednum = address.toInt();
pixels.setPixelColor(lednum, pixels.Color(r, g, b));
pixels.show();
}
}
}
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP8266 Client")) {
//Der Default-Topic lautet ledbar/lights. Sie können Ihn beliebig ändern
client.subscribe("ledbar/lights");
pixels.setPixelColor(0, pixels.Color(0, 50, 0));
pixels.show();
delay(2000);
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
}
else {
pixels.setPixelColor(0, pixels.Color(0, 0, 50));
pixels.show();
delay(2000);
}
}
}
class blinktask : public Task {
protected:
void loop() {
for (int i = 0; i < NUMPIXELS; i++) {
String index = String(i);
if (blinkj[index]) {
r = blinkj[index]["r"].as<int>();
g = blinkj[index]["g"].as<int>();
b = blinkj[index]["b"].as<int>();
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
else {
delay(50);
}
}
pixels.show();
delay(750);
for (int i = 0; i < NUMPIXELS; i++) {
String index = String(i);
if (blinkj[index]) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
else {
delay(50);
}
}
pixels.show();
delay(750);
}
private:
uint8_t state;
} blink_task;
class fastblinktask : public Task {
protected:
void loop() {
for (int i = 0; i < NUMPIXELS; i++) {
String index = String(i);
if (fastblinkj[index]) {
r = fastblinkj[index]["r"].as<int>();
g = fastblinkj[index]["g"].as<int>();
b = fastblinkj[index]["b"].as<int>();
pixels.setPixelColor(i, pixels.Color(r, g, b));
}
else {
delay(50);
}
}
pixels.show();
delay(100);
for (int i = 0; i < NUMPIXELS; i++) {
String index = String(i);
if (fastblinkj[index]) {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
else {
delay(50);
}
}
pixels.show();
delay(100);
}
private:
uint8_t state;
} fastblink_task;
class MQTTtask : public Task {
protected:
void loop() {
if (!client.connected()) {
reconnect();}
client.loop();
}
private:
uint8_t state;
} mqtt_task;
void setup()
{
pixels.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
while (WiFi.status() != WL_CONNECTED) {
pixels.setPixelColor(0, pixels.Color(50, 0, 0));
pixels.show();
delay(50);
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
delay(50);
}
Serial.begin(9600);
delay(500);
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Scheduler.start(&mqtt_task);
Scheduler.start(&blink_task);
Scheduler.start(&fastblink_task);
Scheduler.begin();
}
void loop() {}