Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 94 additions & 80 deletions MQTT_LED_Controller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define NUMPIXELS 12

String address;
char address_str[5];
int r;
int g;
int b;
Expand All @@ -29,6 +30,9 @@ const char* password = "PASSWORD";
const char* mqtt_server = "192.168.1.1";
//Den MQTT-Topic können Sie in der Reconnect-Funktion ändern

//Wird Benutzerauthentifizierung verwendet, muss in der Reconnect-Funktion die andere Connect-Zeile aktiviert werden
//const char* mqtt_user = "MQTT-USER";
//const char* mqtt_password = "MQTT-PASS";

WiFiClient espClient;
PubSubClient client(espClient);
Expand Down Expand Up @@ -56,32 +60,61 @@ void callback(char* topic, byte* payload, unsigned int length) {


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();
if (address == "all") {
r = tv.toInt();
g = fov.toInt();
b = fiv.toInt();

for (int i = 0; i < NUMPIXELS; i++){
sprintf(address_str,"%d", i);
if (sv == "blink") {
JsonObject& led = blinkj.createNestedObject(address_str);
led["r"] = tv;
led["g"] = fov;
led["b"] = fiv;
}
else if (sv == "fastblink") {
JsonObject& led = fastblinkj.createNestedObject(address_str);
led["r"] = tv;
led["g"] = fov;
led["b"] = fiv;
}
else if (sv == "stop") {
blinkj.remove(address_str);
fastblinkj.remove(address_str);
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
pixels.show();
}
pixels.setPixelColor(i, pixels.Color(r, g, b));
pixels.show();
}
} else {
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();
Expand All @@ -91,8 +124,8 @@ void callback(char* topic, byte* payload, unsigned int length) {
if (address == "all") {
for (int i = 0; i < NUMPIXELS; i++){
pixels.setPixelColor(i, pixels.Color(r, g, b));
pixels.show();
}
pixels.show();

}
else {
Expand All @@ -106,6 +139,8 @@ void callback(char* topic, byte* payload, unsigned int length) {
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP8266 Client")) {
// if (client.connect("ESP8266 Client", mqtt_user, mqtt_password)) {
// diese Connect-Zeile ist fuer Verbindungen mit Benutzerauthentifizierung
//Der Default-Topic lautet ledbar/lights. Sie können Ihn beliebig ändern
client.subscribe("ledbar/lights");
pixels.setPixelColor(0, pixels.Color(0, 50, 0));
Expand All @@ -125,68 +160,47 @@ void reconnect() {
class blinktask : public Task {
protected:
void loop() {
Serial.printf("Blinktask startet - %d\n",state);
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);
// Fastblink bei jedem 2. Durchgang einschalten
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));
if ((state % 2) == 1) {
Serial.printf("f%d ",i);
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 {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
}
else {
delay(50);
// Normales Blink bei der oberen Haelfte der Durchgaenge einschalten
if (blinkj[index]) {
if ((state / 8) == 1) {
Serial.printf("b%d ",i);
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 {
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
}
}
}
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);
}
Serial.print(".\n");
if (state < 15) {
state = state + 1;
} else {
state = 0;
}
pixels.show();
delay(100);
delay(150);
Serial.printf("Blinktask fertig - %d\n", state);
}
private:
uint8_t state;
} fastblink_task;
} blink_task;

class MQTTtask : public Task {
protected:
Expand All @@ -201,9 +215,11 @@ private:

void setup()
{
Serial.begin(9600);
pixels.begin();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WIFI");

for (int i = 0; i < NUMPIXELS; i++)
pixels.setPixelColor(i, pixels.Color(0, 0, 0));
Expand All @@ -216,18 +232,16 @@ void setup()
pixels.setPixelColor(0, pixels.Color(0, 0, 0));
pixels.show();
delay(50);
Serial.print(".");
}


Serial.print("Connected\n");

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();

Expand Down