This commit is contained in:
Kekskurse 2020-01-17 16:03:43 +01:00
parent f0fff686f8
commit d386dc6699
3 changed files with 168 additions and 100 deletions

View file

@ -23,3 +23,6 @@ Simple MQTT-LED-Stripe Controller based on https://wiki.keks.cloud/doku.php?id=p
| Blink Code | Description |
| ---------- | ----------- |
| 4 short | MQTT Connection failed |
| 2 shourt, 2 long | WiFi Connection failed |
| fading | Try to connect to wifi |
| on | Try to connect to MQTT |

View file

@ -12,4 +12,4 @@
platform = espressif8266
board = d1
framework = arduino
monitor_speed = 115200

View file

@ -17,7 +17,7 @@
#define MQTT_CLIENT_ID "LED-Schrank-1"
const char* SSID = "WIFISSID";
const char* PSK = "WIDIPASS";
const char* PSK = "WIFIPASS";
WiFiClient espClient;
PubSubClient client(espClient);
@ -41,33 +41,39 @@ int Sync = 0; //Sync LED2 on the value of LED1
LEDStripe led1;
LEDStripe led2;
void connectWiFi() {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Start WiFi Connection");
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
int debugLoop = 0;
//BLink
//Blink
int fadeLevel = 0;
int fadeMode = 0;
void fade() {
if(fadeMode == 0) {
fadeLevel = fadeLevel + 10;
if(fadeLevel > 1023) {
fadeLevel = 1024;
fadeMode = 1;
}
}
if(fadeMode == 1) {
fadeLevel = fadeLevel - 10;
if(fadeLevel <= 0) {
fadeMode = 0;
fadeLevel = 0;
}
}
analogWrite(LED_BUILTIN, fadeLevel);
}
void blink_short() {
digitalWrite(LED_BUILTIN, LOW);
delay(500);
delay(200);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
delay(200);
}
void blink_long() {
digitalWrite(LED_BUILTIN, LOW);
delay(1500);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
delay(200);
}
void blink(int status) {
if(status == 1) {
@ -76,15 +82,60 @@ void blink(int status) {
blink_short();
blink_short();
}
if(status == 2) {
blink_short();
blink_short();
blink_long();
blink_long();
}
if(status == 3) {
blink_long();
blink_long();
blink_long();
blink_long();
}
}
void connectWiFi() {
digitalWrite(LED_BUILTIN, LOW);
Serial.println("Start WiFi Connection");
int wifiConnectionCount = 0;
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(5);
fade();
Serial.print(".");
wifiConnectionCount++;
if(wifiConnectionCount > 1000) {
Serial.println("No Connection found");
wifiConnectionCount = 0;
blink(2);
}
}
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
char* getTopicName(String topic) {
String getTopicNameString(String topic) {
String topicName = String(MQTT_TOPIC_START)+String(topic);
Serial.println("TopicName: "+topicName);
return topicName;
}
char* getTopicName(String topic) {
String topicName = getTopicNameString(topic);
Serial.println("TopicName: "+topicName);
int str_len = topicName.length() + 1;
char cc[str_len];
topicName.toCharArray(cc, str_len);
Serial.println(cc);
return cc;
}
@ -101,7 +152,10 @@ void connectMQTT() {
delay(3000);
}
digitalWrite(LED_BUILTIN, HIGH);
client.subscribe(getTopicName("/#"));
Serial.println("Subscribe");
//client.subscribe(getTopicName("/#"));
client.subscribe("led2/#");
client.publish(getTopicName("/status"), "1", true);
}
@ -113,104 +167,113 @@ void pulse_white_three_times() {
//Modes
//LED Fade
void fade(LEDStripe led) {
if(led.MOD_STEP == 0) {
led.RED ++;
led.GREEN = 0;
led.BLUE = 255;
if(led.RED >= 255) {
led.MOD_STEP++;
void fade(LEDStripe *led) {
if(led->MOD_STEP == 0) {
led->RED ++;
led->GREEN = 0;
led->BLUE = 255;
if(led->RED >= 255) {
led->MOD_STEP++;
}
}
if(led.MOD_STEP == 1) {
led.RED = 255;
led.GREEN = 0;
led.BLUE --;
if(led.BLUE <= 0) {
led.MOD_STEP++;
if(led->MOD_STEP == 1) {
led->RED = 255;
led->GREEN = 0;
led->BLUE --;
if(led->BLUE <= 0) {
led->MOD_STEP++;
}
}
if(led.MOD_STEP == 2) {
led.RED = 255;
led.GREEN ++;
led.BLUE = 0;
if(led.GREEN >= 255) {
led.MOD_STEP++;
if(led->MOD_STEP == 2) {
led->RED = 255;
led->GREEN ++;
led->BLUE = 0;
if(led->GREEN >= 255) {
led->MOD_STEP++;
}
}
if(led.MOD_STEP == 3) {
led.RED --;
led.GREEN = 255;
led.BLUE = 0;
if(led.RED <= 0) {
led.MOD_STEP++;
if(led->MOD_STEP == 3) {
led->RED --;
led->GREEN = 255;
led->BLUE = 0;
if(led->RED <= 0) {
led->MOD_STEP++;
}
}
if(led.MOD_STEP == 4) {
led.RED = 0;
led.GREEN = 255;
led.BLUE ++;
if(led.BLUE >= 255) {
led.MOD_STEP++;
if(led->MOD_STEP == 4) {
led->RED = 0;
led->GREEN = 255;
led->BLUE ++;
if(led->BLUE >= 255) {
led->MOD_STEP++;
}
}
if(led.MOD_STEP == 5) {
led.RED = 0;
led.GREEN --;
led.BLUE = 255;
if(led.GREEN <= 0) {
led.MOD_STEP = 0;
if(led->MOD_STEP == 5) {
led->RED = 0;
led->GREEN --;
led->BLUE = 255;
if(led->GREEN <= 0) {
led->MOD_STEP = 0;
}
}
}
void writeToLedStrips(LEDStripe l1, LEDStripe l2) {
analogWrite(REDPIN1, l1.RED);
analogWrite(REDPIN2, l2.RED);
analogWrite(GREENPIN1, l1.GREEN);
analogWrite(GREENPIN2, l2.GREEN);
analogWrite(BLUEPIN1, l1.BLUE);
analogWrite(BLUEPIN2, l2.BLUE);
Serial.println("LED1: "+String(l1.RED)+"|"+String(l1.GREEN)+"|"+String(l1.BLUE));
Serial.println("LED2: "+String(l2.RED)+"|"+String(l2.GREEN)+"|"+String(l2.BLUE));
void writeToLedStrips(LEDStripe *l1, LEDStripe *l2) {
analogWrite(REDPIN1, l1->RED);
analogWrite(REDPIN2, l2->RED);
analogWrite(GREENPIN1, l1->GREEN);
analogWrite(GREENPIN2, l2->GREEN);
analogWrite(BLUEPIN1, l1->BLUE);
analogWrite(BLUEPIN2, l2->BLUE);
debugLoop++;
if(debugLoop > 500) {
Serial.println("LED1: "+String(l1->RED)+"|"+String(l1->GREEN)+"|"+String(l1->BLUE));
Serial.println("LED2: "+String(l2->RED)+"|"+String(l2->GREEN)+"|"+String(l2->BLUE));
debugLoop = 0;
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println("CALLBACK");
String message = "";
for (int i=0;i<int(length);i++) {
message = message + String((char)payload[i]);
}
if(String(topic) == String(getTopicName("/1/mode"))) {
led1.MOD = message;
}
if(String(topic) == String(getTopicName("/2/mode"))) {
led2.MOD = message;
}
if(String(topic) == String(getTopicName("/1/speed"))) {
led1.SPEED = message.toInt();
}
if(String(topic) == String(getTopicName("/2/speed"))) {
led2.SPEED = message.toInt();
}
if(String(topic) == String(getTopicName("/1/color"))) {
led1.RED = message.substring(0,3).toInt();
led1.GREEN = message.substring(3,6).toInt();
led1.BLUE = message.substring(6,9).toInt();
}
if(String(topic) == String(getTopicName("/2/color"))) {
led2.RED = message.substring(0,3).toInt();
led2.GREEN = message.substring(3,6).toInt();
led2.BLUE = message.substring(6,9).toInt();
}
if(String(topic) == String(getTopicName("/sync"))) {
Sync = message.toInt();
}
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
Serial.print(message);
Serial.println();
Serial.println(getTopicNameString("/1/mode"));
if(String(topic) == getTopicNameString("/1/mode")) {
led1.MOD = message;
Serial.println("Set Mode");
}
if(String(topic) == getTopicNameString("/2/mode")) {
led2.MOD = message;
}
if(String(topic) == getTopicNameString("/1/speed")) {
led1.SPEED = message.toInt();
}
if(String(topic) == getTopicNameString("/2/speed")) {
led2.SPEED = message.toInt();
}
if(String(topic) == getTopicNameString("/1/color")) {
led1.RED = message.substring(0,3).toInt();
led1.GREEN = message.substring(3,6).toInt();
led1.BLUE = message.substring(6,9).toInt();
}
if(String(topic) == getTopicNameString("/2/color")) {
led2.RED = message.substring(0,3).toInt();
led2.GREEN = message.substring(3,6).toInt();
led2.BLUE = message.substring(6,9).toInt();
}
if(String(topic) == getTopicNameString("/sync")) {
Sync = message.toInt();
}
Serial.println("Message done");
}
void prepareMQTT() {
@ -220,6 +283,7 @@ void prepareMQTT() {
client.setServer(MQTT_BROKER, 1883);
client.setCallback(callback);
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
}
void setup() {
@ -256,7 +320,7 @@ void loop() {
client.loop();
if(led1.MOD == "fade") {
fade(led1);
fade(&led1);
}
if(Sync == 1) {
@ -265,11 +329,12 @@ void loop() {
led2.BLUE = led1.BLUE;
} else {
if(led2.MOD == "fade") {
fade(led2);
fade(&led2);
}
}
//Send calculated colors to led-strips
writeToLedStrips(led1, led2);
writeToLedStrips(&led1, &led2);
delay(10);
}