Stuff
This commit is contained in:
commit
3cf5706eee
2 changed files with 317 additions and 0 deletions
20
Readme.md
Normal file
20
Readme.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
# LED-Controller
|
||||
Simple MQTT-LED-Stripe Controller based on https://wiki.keks.cloud/doku.php?id=projects:ledcontroller
|
||||
|
||||
# MQTT Topics
|
||||
| Topic | Typ | Notice |
|
||||
|---|---|---|
|
||||
| led1/status | write-only | Retaines Message and Last will for MQTT Status, 1 = Online, 0 = Offline |
|
||||
| led1/sync | read-only | Ignore all Setting for LED2 and mirrow LED1 color to the secound stripe, 1 = On, 0 = OFF |
|
||||
| led1/1/mode | read-only | Set the Mode of LED1 |
|
||||
| led1/2/mode | read-only | Set the Mode of LED2 |
|
||||
| led1/1/speed | read-only | How fast the changes of the color will made, bigger number will be slower changes, for LED1 |
|
||||
| led1/2/speed | read-only | How fast the changes of the color will made, bigger number will be slower changes, for LED2 |
|
||||
| led1/1/color | read-only | Set Color for LED1, RRRGGGBBB, each color is a int between 000 and 255 |
|
||||
| led1/2/color | read-only | Set Color for LED2, RRRGGGBBB, each color is a int between 000 and 255 |
|
||||
|
||||
# MODE
|
||||
|
||||
* on = Just one color, color can changed via MQTT
|
||||
* fade = Fade
|
||||
* off = Off (color 000000000)
|
297
led-controller.ino
Normal file
297
led-controller.ino
Normal file
|
@ -0,0 +1,297 @@
|
|||
#include <ESP8266WiFi.h>
|
||||
#include <PubSubClient.h>
|
||||
|
||||
#define REDPIN1 5
|
||||
#define GREENPIN1 4
|
||||
#define BLUEPIN1 13
|
||||
|
||||
#define REDPIN2 12
|
||||
#define GREENPIN2 14
|
||||
#define BLUEPIN2 16
|
||||
|
||||
const char* SSID = "WIFISSID";
|
||||
const char* PSK = "WIDIPASS";
|
||||
const char* MQTT_BROKER = "mqtt.hostname.de";
|
||||
const char* MQTT_USER = "";
|
||||
const char* MQTT_PASS = "";
|
||||
|
||||
String LED1_MOD = "off";
|
||||
int LED1_SPEED = 10;
|
||||
int LED1_RED = 0;
|
||||
int LED1_GREEN = 0;
|
||||
int LED1_BLUE = 0;
|
||||
int LED1_LOOP = 0;
|
||||
int LED1_LOOPCOUNT = 0;
|
||||
int LED1_MODE_STEEP = 0;
|
||||
|
||||
String LED2_MOD = "off";
|
||||
int LED2_SPEED = 10;
|
||||
int LED2_RED = 0;
|
||||
int LED2_GREEN = 0;
|
||||
int LED2_BLUE = 0;
|
||||
int LED2_LOOP = 0;
|
||||
int LED2_LOOPCOUNT = 0;
|
||||
int LED2_MODE_STEEP = 0;
|
||||
|
||||
int LED_SYNC = 0;
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
void setup() {
|
||||
//Turn LED ON
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
Serial.begin(115200);
|
||||
Serial.println();
|
||||
|
||||
//LED Stripe Pins
|
||||
analogWrite(REDPIN1, 0);
|
||||
analogWrite(REDPIN2, 0);
|
||||
analogWrite(GREENPIN1, 0);
|
||||
analogWrite(GREENPIN2, 0);
|
||||
analogWrite(BLUEPIN1, 0);
|
||||
analogWrite(BLUEPIN2, 0);
|
||||
|
||||
//Connecting to WiFi
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(SSID);
|
||||
|
||||
WiFi.begin(SSID, PSK);
|
||||
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
//Prepair MQTT Connection
|
||||
client.setServer(MQTT_BROKER, 1883);
|
||||
client.setCallback(callback);
|
||||
|
||||
//Turn LED OFF
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
}
|
||||
|
||||
//Connect to MQTT
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
Serial.print("Reconnecting...");
|
||||
if (!client.connect("ESP8266Client-LED-Controller-Schrank", "user", "password", "led1/status", 1, true, "0")) {
|
||||
Serial.print("failed, rc=");
|
||||
Serial.print(client.state());
|
||||
Serial.println(" retrying in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
client.subscribe("led1/#");
|
||||
}
|
||||
client.publish("led1/status", "1", true);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
}
|
||||
int loopDebugCount = 0;
|
||||
void loop() {
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
|
||||
if(LED1_MOD == "off") {
|
||||
LED1_RED = 0;
|
||||
LED1_GREEN = 0;
|
||||
LED1_BLUE = 0;
|
||||
}
|
||||
if(LED2_MOD == "off") {
|
||||
LED2_RED = 0;
|
||||
LED2_GREEN = 0;
|
||||
LED2_BLUE = 0;
|
||||
}
|
||||
|
||||
if(LED1_MOD == "fade") {
|
||||
if(LED1_LOOP >= LED1_LOOPCOUNT)
|
||||
{
|
||||
fade_led_1();
|
||||
LED1_LOOP = 0;
|
||||
} else {
|
||||
LED1_LOOP = LED1_LOOP + 1;
|
||||
}
|
||||
}
|
||||
if(LED2_MOD == "fade") {
|
||||
if(LED2_LOOP >= LED2_LOOPCOUNT)
|
||||
{
|
||||
fade_led_2();
|
||||
LED2_LOOP = 0;
|
||||
} else {
|
||||
LED2_LOOP = LED2_LOOP + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(LED_SYNC==1) {
|
||||
LED2_RED = LED1_RED;
|
||||
LED2_GREEN = LED1_GREEN;
|
||||
LED2_BLUE = LED1_BLUE;
|
||||
}
|
||||
|
||||
analogWrite(REDPIN1, LED1_RED);
|
||||
analogWrite(REDPIN2, LED2_RED);
|
||||
analogWrite(GREENPIN1, LED1_GREEN);
|
||||
analogWrite(GREENPIN2, LED2_GREEN);
|
||||
analogWrite(BLUEPIN1, LED1_BLUE);
|
||||
analogWrite(BLUEPIN2, LED2_BLUE);
|
||||
/*if(loopDebugCount >= 200) {
|
||||
char cc[20];
|
||||
String("LED1:"+String(LED1_RED)+":"+String(LED1_GREEN)+":"+String(LED1_BLUE)).toCharArray(cc, 20);
|
||||
client.publish("led1/debug", cc);
|
||||
String("LED2:"+String(LED2_RED)+":"+String(LED2_GREEN)+":"+String(LED2_BLUE)).toCharArray(cc, 20);
|
||||
client.publish("led1/debug", cc);
|
||||
loopDebugCount = 0;
|
||||
} else {
|
||||
loopDebugCount = loopDebugCount + 1;
|
||||
}*/
|
||||
|
||||
|
||||
//delay(10);
|
||||
}
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
String message = "";
|
||||
for (int i=0;i<length;i++) {
|
||||
message = message + String((char)payload[i]);
|
||||
}
|
||||
if(String(topic) == "led1/1/mode") {
|
||||
LED1_MOD = message;
|
||||
}
|
||||
if(String(topic) == "led1/2/mode") {
|
||||
LED2_MOD = message;
|
||||
}
|
||||
if(String(topic) == "led1/1/speed") {
|
||||
LED1_LOOPCOUNT = message.toInt();
|
||||
}
|
||||
if(String(topic) == "led1/2/speed") {
|
||||
LED2_LOOPCOUNT = message.toInt();
|
||||
}
|
||||
|
||||
if(String(topic) == "led1/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) == "led1/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) == "led1/sync") {
|
||||
LED_SYNC = message.toInt();
|
||||
}
|
||||
Serial.print("Message arrived [");
|
||||
Serial.print(topic);
|
||||
Serial.print("] ");
|
||||
Serial.print(message);
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void fade_led_1() {
|
||||
if(LED1_MODE_STEEP == 0) {
|
||||
LED1_RED = LED1_RED + 1;
|
||||
LED1_GREEN = 0;
|
||||
LED1_BLUE = 255;
|
||||
if(LED1_RED >= 255) {
|
||||
LED1_MODE_STEEP = LED1_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED1_MODE_STEEP == 1) {
|
||||
LED1_RED = 255;
|
||||
LED1_GREEN = 0;
|
||||
LED1_BLUE = LED1_BLUE - 1;
|
||||
if(LED1_BLUE <= 0) {
|
||||
LED1_MODE_STEEP = LED1_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED1_MODE_STEEP == 2) {
|
||||
LED1_RED = 255;
|
||||
LED1_GREEN = LED1_GREEN + 1;
|
||||
LED1_BLUE = 0;
|
||||
if(LED1_GREEN >= 255) {
|
||||
LED1_MODE_STEEP = LED1_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED1_MODE_STEEP == 3) {
|
||||
LED1_RED = LED1_RED - 1;
|
||||
LED1_GREEN = 255;
|
||||
LED1_BLUE = 0;
|
||||
if(LED1_RED <= 0) {
|
||||
LED1_MODE_STEEP = LED1_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED1_MODE_STEEP == 4) {
|
||||
LED1_RED = 0;
|
||||
LED1_GREEN = 255;
|
||||
LED1_BLUE = LED1_BLUE + 1;
|
||||
if(LED1_BLUE >= 255) {
|
||||
LED1_MODE_STEEP = LED1_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED1_MODE_STEEP == 5) {
|
||||
LED1_RED = 0;
|
||||
LED1_GREEN = LED1_GREEN - 1;
|
||||
LED1_BLUE = 255;
|
||||
if(LED1_GREEN <= 0) {
|
||||
LED1_MODE_STEEP = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
void fade_led_2() {
|
||||
if(LED2_MODE_STEEP == 0) {
|
||||
LED2_RED = LED2_RED + 1;
|
||||
LED2_GREEN = 0;
|
||||
LED2_BLUE = 255;
|
||||
if(LED2_RED >= 255) {
|
||||
LED2_MODE_STEEP = LED2_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED2_MODE_STEEP == 1) {
|
||||
LED2_RED = 255;
|
||||
LED2_GREEN = 0;
|
||||
LED2_BLUE = LED2_BLUE - 1;
|
||||
if(LED2_BLUE <= 0) {
|
||||
LED2_MODE_STEEP = LED2_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED2_MODE_STEEP == 2) {
|
||||
LED2_RED = 255;
|
||||
LED2_GREEN = LED1_GREEN + 1;
|
||||
LED2_BLUE = 0;
|
||||
if(LED2_GREEN >= 255) {
|
||||
LED2_MODE_STEEP = LED2_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED2_MODE_STEEP == 3) {
|
||||
LED2_RED = LED1_RED - 1;
|
||||
LED2_GREEN = 255;
|
||||
LED2_BLUE = 0;
|
||||
if(LED2_RED <= 0) {
|
||||
LED2_MODE_STEEP = LED2_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED2_MODE_STEEP == 4) {
|
||||
LED2_RED = 0;
|
||||
LED2_GREEN = 255;
|
||||
LED2_BLUE = LED2_BLUE + 1;
|
||||
if(LED2_BLUE >= 255) {
|
||||
LED2_MODE_STEEP = LED2_MODE_STEEP + 1;
|
||||
}
|
||||
}
|
||||
if(LED2_MODE_STEEP == 5) {
|
||||
LED2_RED = 0;
|
||||
LED2_GREEN = LED2_GREEN - 1;
|
||||
LED2_BLUE = 255;
|
||||
if(LED2_GREEN <= 0) {
|
||||
LED2_MODE_STEEP = 0;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue