Initial commit

This commit is contained in:
starcalc 2017-10-31 22:37:09 +01:00
commit 2994070e89
1 changed files with 181 additions and 0 deletions

181
esp-deckenlicht.ino Normal file
View File

@ -0,0 +1,181 @@
#include <Homie.h>
#include <ArduinoOTA.h>
#define PIN_LIGHT D5
#define PIN_LIGHT1 D6
#define PIN_LIGHT2 D7
#define PIN_LIGHT3 D8
#define FULL 255
#define LOWER 50
#define MINIMUM 1
#define OFF 0
int timeout = 50;
#define FW_NAME "esp-deckenlicht"
#define FW_VERSION "1.0.0"
int w0;
int w1;
int w2;
int w3;
int step = 0;
bool disco = false;
int lastEvent = 0;
HomieNode lightNode("strip", "strip");
bool speedHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "speed " << ": " << value << endl;
timeout = value.toInt();
lightNode.setProperty("speed").send(value);
return true;
}
bool discoHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "disco " << ": " << value << endl;
lightNode.setProperty("disco").send(value);
if (value.toInt() == 0)
{
disco = false;
} else
{
step = 0;
disco = true;
}
return true;
}
bool lightHandler(const HomieRange& range, const String& value) {
Homie.getLogger() << "light " << ": " << value << endl;
disco = false;
w0 = value.toInt();
w1 = value.toInt();
w2 = value.toInt();
w3 = value.toInt();
lightNode.setProperty("light").send(value);
output();
return true;
}
bool light0Handler(const HomieRange& range, const String& value) {
Homie.getLogger() << "light0 " << ": " << value << endl;
w0 = value.toInt();
disco = false;
lightNode.setProperty("light0").send(value);
output();
return true;
}
bool light1Handler(const HomieRange& range, const String& value) {
Homie.getLogger() << "light1 " << ": " << value << endl;
w1 = value.toInt();
disco = false;
lightNode.setProperty("light1").send(value);
output();
return true;
}
bool light2Handler(const HomieRange& range, const String& value) {
Homie.getLogger() << "light2 " << ": " << value << endl;
w2 = value.toInt();
disco = false;
lightNode.setProperty("light2").send(value);
output();
return true;
}
bool light3Handler(const HomieRange& range, const String& value) {
Homie.getLogger() << "light3 " << ": " << value << endl;
w3 = value.toInt();
disco = false;
lightNode.setProperty("light3").send(value);
output();
return true;
}
void output() {
// * 4 to scale the input up for ESP Arduino default 10 bit PWM
analogWrite(PIN_LIGHT, w0 * 4);
analogWrite(PIN_LIGHT1, w1 * 4);
analogWrite(PIN_LIGHT2, w2 * 4);
analogWrite(PIN_LIGHT3, w3 * 4);
}
void loopHandler()
{
if (disco)
{
if (millis() - lastEvent >= timeout || lastEvent == 0) {
lastEvent = millis();
if (step >= 4) {
step = 0;
}
// Cycle each light from 255 to 50 to 1 to off
switch (step)
{
case 0:
w0 = FULL;
w1 = OFF;
w2 = MINIMUM;
w3 = LOWER;
break;
case 1:
w1 = FULL;
w2 = OFF;
w3 = MINIMUM;
w0 = LOWER;
break;
case 2:
w2 = FULL;
w3 = OFF;
w0 = MINIMUM;
w1 = LOWER;
break;
case 3:
w3 = FULL;
w0 = OFF;
w1 = MINIMUM;
w2 = LOWER;
break;
default:
w0 = w1 = w2 = w3 = 0;
break;
}
output();
step++;
}
}
}
void setup() {
Serial.begin(115200);
Serial << endl << endl;
pinMode(PIN_LIGHT, OUTPUT);
pinMode(PIN_LIGHT1, OUTPUT);
pinMode(PIN_LIGHT2, OUTPUT);
pinMode(PIN_LIGHT3, OUTPUT);
Homie_setFirmware(FW_NAME, FW_VERSION);
Homie_setBrand(FW_NAME);
Homie.setLoopFunction(loopHandler);
lightNode.advertise("speed").settable(speedHandler);
lightNode.advertise("disco").settable(discoHandler);
lightNode.advertise("light").settable(lightHandler);
lightNode.advertise("light0").settable(light0Handler);
lightNode.advertise("light1").settable(light1Handler);
lightNode.advertise("light2").settable(light2Handler);
lightNode.advertise("light3").settable(light3Handler);
Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
}