Skip to content

Instantly share code, notes, and snippets.

@diplix
Last active January 25, 2019 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save diplix/bc5c3be877dc850b33304e554bbd0e00 to your computer and use it in GitHub Desktop.
Save diplix/bc5c3be877dc850b33304e554bbd0e00 to your computer and use it in GitHub Desktop.
control a FC16 LED module via mqtt (via homie), credit for the heavy lifting goes to https://community.home-assistant.io/t/mqtt-enabled-led-sign/7475/7
switch:
- platform: mqtt
name: "LED Matrix Display"
state_topic: "devices/led-matrix/light/on"
command_topic: "devices/led-matrix/light/on/set"
availability_topic: "devices/led-matrix/$online"
qos: 0
payload_on: "on"
payload_off: "off"
optimistic: false
payload_availability_on: "true"
payload_availability_off: "false"
retain: true
light:
- platform: mqtt
name: "LED Matrix Display Light"
state_topic: "devices/led-matrix/light/on"
command_topic: "devices/led-matrix/light/on/set"
brightness_state_topic: "devices/led-matrix/light/brightness"
brightness_command_topic: "devices/led-matrix/light/brightness/set"
availability_topic: "devices/led-matrix/$online"
qos: 0
payload_on: "on"
payload_off: "off"
optimistic: false
payload_availability_on: "true"
payload_availability_off: "false"
retain: true
automation:
alias: Küchenaktionen nach Bewegung
trigger:
- platform: state
entity_id: binary_sensor.bewegungsmelder_3
to: 'on'
action:
- service: mqtt.publish
data:
topic: "devices/led-matrix/text/on/set"
payload: "AN"
#include <Homie.h>
#include <SPI.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
////////////////////////////////////////////////////////////////////////
//PAROLA
////////////////////////////////////////////////////////////////////////
#define MAX_DEVICES 4
#define CLK_PIN 12
#define DATA_PIN 13
#define CS_PIN 15
byte DisplayOn = 1;
int dimmlevel = 65 ;
MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
//MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);
HomieNode textNode("text", "sign");
HomieNode printNode("print", "sign");
HomieNode displayNode("light", "switch");
bool displayOnHandler(const HomieRange& range, const String& value) {
if (value == "on") {
DisplayOn = 1;
displayNode.setProperty("on").send("on");
}
else {
String text = "";
int value_len = text.length() + 1 ;
char txtbuffer[value_len] ;
text.toCharArray(txtbuffer, value_len );
P.displayText(txtbuffer, PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT );
DisplayOn = 0;
displayNode.setProperty("on").send("off");
}
return true;
}
bool displayBrightnessHandler(const HomieRange& range, const String& value) {
int newdimmlevel;
newdimmlevel = atoi(value.c_str());
if (newdimmlevel > 256) {
dimmlevel = 15;
} else if (newdimmlevel>0) {
dimmlevel = ceil(newdimmlevel / 17.06);
} else if (value == "0") {
dimmlevel=0;
} else {
//ungueltiger wert
//do nothing
return false;
}
P.setIntensity(dimmlevel);
displayNode.setProperty("brightness").send(value);
return true;
}
bool textOnHandler(const HomieRange& range, const String& value) {
String text = " +++++ " + value + " +++++ ";
int value_len = text.length() + 1 ;
char txtbuffer[value_len] ;
text.toCharArray(txtbuffer, value_len );
ESP.wdtFeed();
textNode.setProperty("on").send(value);
P.displayText(txtbuffer , PA_CENTER, 30, 0, PA_SCROLL_LEFT, PA_SCROLL_LEFT );
ESP.wdtFeed();
while (!P.displayAnimate()) {
delay(500);
ESP.wdtFeed();
}
ESP.wdtFeed();
Homie.getLogger() << "text is " << value << endl;
ESP.wdtFeed();
return true;
}
bool printOnHandler(const HomieRange& range, const String& value) {
String text = "" + value + "";
int value_len = text.length() + 1 ;
char txtbuffer[value_len] ;
text.toCharArray(txtbuffer, value_len );
ESP.wdtFeed();
printNode.setProperty("on").send(value);
P.displayText(txtbuffer , PA_CENTER, 30, 3000, PA_PRINT, PA_SCROLL_LEFT );
ESP.wdtFeed();
while (!P.displayAnimate()) {
delay(500);
ESP.wdtFeed();
}
ESP.wdtFeed();
Homie.getLogger() << "text is " << value << endl;
ESP.wdtFeed();
return true;
}
void setup() {
ESP.wdtDisable();
ESP.wdtEnable(WDTO_8S);
P.begin();
//Serial.begin(115200);
Serial << endl << endl;
Homie_setFirmware("awesome-sign", "1.0.1");
textNode.advertise("on").settable(textOnHandler);
printNode.advertise("on").settable(printOnHandler);
displayNode.advertise("on").settable(displayOnHandler);
displayNode.advertise("brightness").settable(displayBrightnessHandler);
Homie.setup();
P.displayText("Start", PA_CENTER, 1, 2000, PA_PRINT, PA_PRINT );
while (!P.displayAnimate());
NTP.onNTPSyncEvent([](NTPSyncEvent_t error) {
if (error) {
Serial.print("Time Sync error: ");
if (error == noResponse)
Serial.println("NTP server not reachable");
else if (error == invalidAddress)
Serial.println("Invalid NTP server address");
}
else {
Serial.print("Got NTP time: ");
Serial.println(NTP.getTimeDateString(NTP.getLastNTPSync()));
}
});
NTP.begin("pool.ntp.org", 1, true);
NTP.setInterval(1800);
}
void loop() {
Homie.loop();
char charBuf[100];
sprintf(charBuf, "%02u : %02u", hour(), minute());
if (DisplayOn == 1) {
P.displayText(charBuf, PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT );
}
while (!P.displayAnimate())
ESP.wdtFeed();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment