2017-10-31 21:37:09 +00:00
|
|
|
#include <Homie.h>
|
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
//curl -X PUT http://homie.config/config -d @config.json --header "Content-Type: application/json"
|
|
|
|
|
2019-01-26 22:51:30 +00:00
|
|
|
// homie/ledstoffroehre/lamp/fluorescent0/set
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
#define PIN_LAMP0_EDGE D5
|
|
|
|
#define PIN_LAMP0 D6
|
|
|
|
#define PIN_LAMP1_EDGE D7
|
|
|
|
#define PIN_LAMP1 D8
|
2017-11-18 19:43:30 +00:00
|
|
|
#define PIN_SENSOR D0
|
2017-10-31 21:37:09 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
//#define FULL 255
|
|
|
|
//#define LOWER 50
|
|
|
|
//#define MINIMUM 1
|
|
|
|
//#define OFF 0
|
|
|
|
//int timeout = (1000 - 50);
|
2017-10-31 21:37:09 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
#define FW_NAME "ledstoffroehre"
|
2018-04-08 18:14:38 +00:00
|
|
|
#define FW_VERSION "1.0.1"
|
2019-01-22 23:25:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
int lamp0e=0;
|
|
|
|
int lamp0=0;
|
|
|
|
int lamp1e=0;
|
|
|
|
int lamp1=0;
|
|
|
|
|
|
|
|
long fluorescentLastUpdated=0; //global
|
2018-11-18 21:37:27 +00:00
|
|
|
#define FLUORESCENTUPDATEINTERVAL 20
|
2019-01-22 23:25:51 +00:00
|
|
|
long fluorescent0LastActivated = 0;
|
|
|
|
long fluorescent1LastActivated = 0;
|
|
|
|
|
|
|
|
int setbrightness0 = 0;
|
|
|
|
int setbrightness1 = 0;
|
|
|
|
bool fluorescent0Active=false;
|
|
|
|
bool fluorescent1Active=false;
|
2019-01-26 22:51:30 +00:00
|
|
|
int fluorescent0Quality=50; // 0 to 100
|
|
|
|
int fluorescent1Quality=50;
|
|
|
|
#define FLUORESCENQUALITYMAX 100
|
2019-01-22 23:25:51 +00:00
|
|
|
#define FLUORESCENTTEMPMAX 1000
|
|
|
|
int fluorescent0Temp=0;
|
|
|
|
int fluorescent1Temp=0;
|
2019-01-26 22:51:30 +00:00
|
|
|
#define GLOWBRIGHTNESS 10
|
|
|
|
|
|
|
|
|
|
|
|
int flashprobability0=300; //the higher the lesser
|
|
|
|
int flashprobabilitymin0=100; //the higher the lesser (at peak)
|
|
|
|
int tempincreasemax0=10; //the higher the faster lightup
|
|
|
|
|
|
|
|
int flashprobability1=300; //the higher the lesser
|
|
|
|
int flashprobabilitymin1=100; //the higher the lesser (at peak)
|
|
|
|
int tempincreasemax1=10; //the higher the faster lightup
|
|
|
|
|
|
|
|
#define FLASHPROBABILITY_MIN 100
|
|
|
|
#define FLASHPROBABILITY_MAX 600
|
|
|
|
#define FLASHPROBABILITYMIN_MIN 50
|
|
|
|
#define FLASHPROBABILITYMIN_MAX 100 //should not be more than FLASHPROBABILITY_MIN
|
|
|
|
#define TEMPINCREASEMAX_MIN 5
|
|
|
|
#define TEMPINCREASEMAX_MAX FLUORESCENTTEMPMAX/10
|
2019-01-22 23:25:51 +00:00
|
|
|
|
2018-11-15 23:06:58 +00:00
|
|
|
|
2017-11-18 19:43:30 +00:00
|
|
|
bool lastSensorValue = false;
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
HomieNode lightNode("lamp", "lamp");
|
2017-11-18 19:43:30 +00:00
|
|
|
HomieNode sensorNode("sensor", "sensor");
|
|
|
|
Bounce debouncer = Bounce();
|
2017-10-31 21:37:09 +00:00
|
|
|
|
|
|
|
|
2017-11-25 12:43:59 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
bool fluorescentHandler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "fluorescent " << ": " << value << endl;
|
|
|
|
lightNode.setProperty("fluorescent").send(value);
|
|
|
|
setbrightness0 = value.toInt();
|
|
|
|
setbrightness1 = value.toInt();
|
|
|
|
fluorescent0Temp=0;
|
|
|
|
fluorescent1Temp=0;
|
|
|
|
|
|
|
|
if (setbrightness0 == 0 || setbrightness1 == 0) { // turned off
|
2019-01-26 22:51:30 +00:00
|
|
|
resetLamp0();
|
|
|
|
resetLamp1();
|
2019-01-22 23:25:51 +00:00
|
|
|
} else { //turned on
|
|
|
|
//Initialization
|
|
|
|
fluorescent0Active = true; //start effect
|
|
|
|
fluorescent1Active = true; //start effect
|
2017-11-25 12:43:59 +00:00
|
|
|
}
|
|
|
|
output();
|
2019-01-22 23:25:51 +00:00
|
|
|
return true;
|
2017-11-25 12:43:59 +00:00
|
|
|
}
|
2019-01-22 23:25:51 +00:00
|
|
|
bool fluorescent0Handler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "fluorescent0 " << ": " << value << endl;
|
|
|
|
lightNode.setProperty("fluorescent0").send(value);
|
|
|
|
setbrightness0 = value.toInt();
|
|
|
|
fluorescent0Temp=0;
|
|
|
|
|
|
|
|
if (setbrightness0 == 0) { // turned off
|
2019-01-26 22:51:30 +00:00
|
|
|
resetLamp0();
|
2019-01-22 23:25:51 +00:00
|
|
|
} else { //turned on
|
|
|
|
//Initialization
|
|
|
|
fluorescent0Active = true; //start effect
|
2017-10-31 21:37:09 +00:00
|
|
|
}
|
2019-01-22 23:25:51 +00:00
|
|
|
output();
|
2017-10-31 21:37:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-22 23:25:51 +00:00
|
|
|
bool fluorescent1Handler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "fluorescent1 " << ": " << value << endl;
|
|
|
|
lightNode.setProperty("fluorescent1").send(value);
|
|
|
|
setbrightness1 = value.toInt();
|
|
|
|
fluorescent1Temp=0;
|
|
|
|
|
|
|
|
if (setbrightness1 == 0) { // turned off
|
2019-01-26 22:51:30 +00:00
|
|
|
resetLamp1();
|
2019-01-22 23:25:51 +00:00
|
|
|
} else { //turned on
|
|
|
|
//Initialization
|
|
|
|
fluorescent1Active = true; //start effect
|
2018-11-15 23:06:58 +00:00
|
|
|
}
|
|
|
|
output();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-26 22:51:30 +00:00
|
|
|
|
|
|
|
void resetLamp0(){
|
|
|
|
fluorescent0Active = false; //set effect off
|
|
|
|
fluorescent0LastActivated = millis();
|
|
|
|
lamp0e=0;
|
|
|
|
lamp0=0;
|
|
|
|
float speedpercent=fluorescent0Quality*1.0/FLUORESCENQUALITYMAX;
|
|
|
|
flashprobability0=random(FLASHPROBABILITY_MIN,FLASHPROBABILITY_MAX);
|
|
|
|
flashprobabilitymin0=random(FLASHPROBABILITYMIN_MIN,FLASHPROBABILITYMIN_MAX);
|
|
|
|
tempincreasemax0=TEMPINCREASEMAX_MIN+speedpercent*(TEMPINCREASEMAX_MAX-TEMPINCREASEMAX_MIN);//random(TEMPINCREASEMAX_MIN,TEMPINCREASEMAX_MAX);
|
|
|
|
}
|
|
|
|
void resetLamp1(){
|
|
|
|
fluorescent1Active = false; //set effect off
|
|
|
|
fluorescent1LastActivated = millis();
|
|
|
|
lamp1e=0;
|
|
|
|
lamp1=0;
|
|
|
|
float speedpercent=fluorescent1Quality*1.0/FLUORESCENQUALITYMAX;
|
|
|
|
flashprobability1=random(FLASHPROBABILITY_MIN,FLASHPROBABILITY_MAX);
|
|
|
|
flashprobabilitymin1=random(FLASHPROBABILITYMIN_MIN,FLASHPROBABILITYMIN_MAX);
|
|
|
|
tempincreasemax0=TEMPINCREASEMAX_MIN+speedpercent*(TEMPINCREASEMAX_MAX-TEMPINCREASEMAX_MIN);
|
|
|
|
}
|
|
|
|
|
2018-11-23 00:49:05 +00:00
|
|
|
bool fluorescentAgeHandler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "fluorescentAge " << ": " << value << endl;
|
2019-01-26 22:51:30 +00:00
|
|
|
fluorescent0Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
|
|
|
|
fluorescent1Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
|
2018-11-23 00:49:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-26 22:51:30 +00:00
|
|
|
bool fluorescent0QualityHandler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "fluorescent0Quality " << ": " << value << endl;
|
|
|
|
fluorescent0Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
|
2017-10-31 21:37:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-01-26 22:51:30 +00:00
|
|
|
bool fluorescent1QualityHandler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "fluorescent1Quality " << ": " << value << endl;
|
|
|
|
fluorescent1Quality = constrain(value.toInt(),0,FLUORESCENQUALITYMAX);
|
2017-10-31 21:37:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
|
|
|
|
bool lampHandler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "lamp0,1 " << ": " << value << endl;
|
|
|
|
lamp0 = value.toInt();
|
|
|
|
lamp0e = value.toInt();
|
|
|
|
lamp1 = value.toInt();
|
|
|
|
lamp1e = value.toInt();
|
|
|
|
lightNode.setProperty("lamp0").send(value);
|
|
|
|
lightNode.setProperty("lamp1").send(value);
|
|
|
|
fluorescent0Active = false;
|
|
|
|
fluorescent1Active = false;
|
2017-10-31 21:37:09 +00:00
|
|
|
output();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
bool lamp0Handler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "lamp0 " << ": " << value << endl;
|
|
|
|
lamp0 = value.toInt();
|
|
|
|
lamp0e = value.toInt();
|
|
|
|
lightNode.setProperty("lamp0").send(value);
|
|
|
|
fluorescent0Active = false;
|
2017-10-31 21:37:09 +00:00
|
|
|
output();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
bool lamp1Handler(const HomieRange& range, const String& value) {
|
|
|
|
Homie.getLogger() << "lamp1 " << ": " << value << endl;
|
|
|
|
lamp1 = value.toInt();
|
|
|
|
lamp1e = value.toInt();
|
|
|
|
lightNode.setProperty("lamp1").send(value);
|
|
|
|
fluorescent1Active = false;
|
2017-10-31 21:37:09 +00:00
|
|
|
output();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
|
2017-10-31 21:37:09 +00:00
|
|
|
void output() {
|
|
|
|
// * 4 to scale the input up for ESP Arduino default 10 bit PWM
|
2019-01-22 23:25:51 +00:00
|
|
|
analogWrite(PIN_LAMP0_EDGE, constrain(lamp0e * 4,0,1023));
|
|
|
|
analogWrite(PIN_LAMP0, constrain(lamp0 * 4,0,1023));
|
|
|
|
analogWrite(PIN_LAMP1_EDGE, constrain(lamp1e * 4,0,1023));
|
|
|
|
analogWrite(PIN_LAMP1, constrain(lamp1 * 4,0,1023));
|
2017-10-31 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loopHandler()
|
|
|
|
{
|
2019-01-26 22:51:30 +00:00
|
|
|
//mosquitto_pub -h raum.ctdo.de -t "homie/ledstoffroehre/lamp/fluorescent0/set" -m "255"
|
2018-11-18 21:37:27 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
|
|
|
|
if (millis() > fluorescentLastUpdated + FLUORESCENTUPDATEINTERVAL) { //Update values
|
|
|
|
fluorescentLastUpdated = millis();
|
|
|
|
|
|
|
|
if (fluorescent0Active) {
|
|
|
|
//long _time = millis() - fluorescent0LastActivated; //time since activated
|
2018-11-18 21:37:27 +00:00
|
|
|
|
2019-01-26 22:51:30 +00:00
|
|
|
fluorescent0Temp+=random(1,tempincreasemax0);
|
|
|
|
if (random(0,flashprobability0-constrain(fluorescent0Temp*flashprobability0/FLUORESCENTTEMPMAX,0,flashprobability0-flashprobabilitymin0))==0)
|
|
|
|
{
|
|
|
|
lamp0=setbrightness0*random(30,100)/10;
|
|
|
|
lamp0e=lamp0; //flash everything
|
2018-11-18 21:37:27 +00:00
|
|
|
}
|
2019-01-26 22:51:30 +00:00
|
|
|
lamp0-=40;
|
|
|
|
if (random(0,100)>95){
|
|
|
|
lamp0e+=constrain(random(0,fluorescent0Temp*10/FLUORESCENTTEMPMAX) , 0, 10); //start glowing slowly
|
2018-11-20 23:33:56 +00:00
|
|
|
}
|
2019-01-26 22:51:30 +00:00
|
|
|
|
|
|
|
if (lamp0e>GLOWBRIGHTNESS)
|
|
|
|
{
|
|
|
|
lamp0e-=constrain(lamp0e-GLOWBRIGHTNESS,0,20); //make sides darker until glowbrightness
|
2019-01-22 23:25:51 +00:00
|
|
|
}
|
2018-11-20 23:33:56 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
if (fluorescent0Temp>=FLUORESCENTTEMPMAX){
|
|
|
|
fluorescent0Active=false;
|
|
|
|
lamp0=setbrightness0;
|
|
|
|
lamp0e=setbrightness0;
|
|
|
|
}
|
2018-11-23 00:49:05 +00:00
|
|
|
|
2018-11-20 23:33:56 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
lamp0=constrain(lamp0, 0,255);
|
|
|
|
lamp0e=constrain(lamp0e, 0,255);
|
|
|
|
output();
|
|
|
|
}
|
2018-11-20 23:33:56 +00:00
|
|
|
|
2018-11-23 00:49:05 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
if (fluorescent1Active) {
|
|
|
|
//long _time = millis() - fluorescent0LastActivated; //time since activated
|
2018-11-23 00:49:05 +00:00
|
|
|
|
2019-01-26 22:51:30 +00:00
|
|
|
fluorescent1Temp+=random(1,tempincreasemax1);
|
|
|
|
if (random(0,flashprobability1-constrain(fluorescent1Temp*flashprobability1/FLUORESCENTTEMPMAX,0,flashprobability1-flashprobabilitymin1))==0)
|
|
|
|
{
|
|
|
|
lamp1=setbrightness1*random(30,100)/10;
|
|
|
|
lamp1e=lamp1; //flash everything
|
2018-11-18 21:37:27 +00:00
|
|
|
}
|
2019-01-26 22:51:30 +00:00
|
|
|
lamp1-=40;
|
|
|
|
if (random(0,100)>95){
|
|
|
|
lamp1e+=constrain(random(0,fluorescent1Temp*10/FLUORESCENTTEMPMAX) , 0, 10); //start glowing slowly
|
2018-11-18 21:37:27 +00:00
|
|
|
}
|
2019-01-26 22:51:30 +00:00
|
|
|
|
|
|
|
if (lamp1e>GLOWBRIGHTNESS)
|
|
|
|
{
|
|
|
|
lamp1e-=constrain(lamp1e-GLOWBRIGHTNESS,0,20); //make sides darker until glowbrightness
|
2019-01-22 23:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fluorescent1Temp>=FLUORESCENTTEMPMAX){
|
|
|
|
fluorescent1Active=false;
|
|
|
|
lamp1=setbrightness1;
|
|
|
|
lamp1e=setbrightness1;
|
|
|
|
}
|
2018-11-23 00:49:05 +00:00
|
|
|
|
2019-01-26 22:51:30 +00:00
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
lamp1=constrain(lamp1, 0,255);
|
|
|
|
lamp1e=constrain(lamp1e, 0,255);
|
2018-11-18 21:37:27 +00:00
|
|
|
output();
|
2018-11-15 23:06:58 +00:00
|
|
|
}
|
2019-01-22 23:25:51 +00:00
|
|
|
|
2018-11-15 23:06:58 +00:00
|
|
|
}
|
2019-01-22 23:25:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-18 19:43:30 +00:00
|
|
|
bool sensorValue = debouncer.read();
|
|
|
|
if (Homie.isConfigured() && Homie.isConnected() && sensorValue != lastSensorValue) {
|
2019-01-22 23:25:51 +00:00
|
|
|
sensorNode.setProperty("motion").send(sensorValue ? "true" : "false");
|
|
|
|
lastSensorValue = sensorValue;
|
2017-11-18 19:43:30 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 21:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
Serial << endl << endl;
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
pinMode(PIN_LAMP0_EDGE, OUTPUT);
|
|
|
|
pinMode(PIN_LAMP0, OUTPUT);
|
|
|
|
pinMode(PIN_LAMP1_EDGE, OUTPUT);
|
|
|
|
pinMode(PIN_LAMP1, OUTPUT);
|
|
|
|
|
|
|
|
debouncer.attach(PIN_SENSOR, INPUT);
|
2017-11-18 19:43:30 +00:00
|
|
|
debouncer.interval(50);
|
|
|
|
|
2017-10-31 21:37:09 +00:00
|
|
|
|
|
|
|
Homie_setFirmware(FW_NAME, FW_VERSION);
|
|
|
|
Homie_setBrand(FW_NAME);
|
|
|
|
Homie.setLoopFunction(loopHandler);
|
|
|
|
|
2019-01-22 23:25:51 +00:00
|
|
|
lightNode.advertise("lamp").settable(lampHandler);
|
|
|
|
lightNode.advertise("lamp0").settable(lamp0Handler);
|
|
|
|
lightNode.advertise("lamp1").settable(lamp1Handler);
|
2018-11-15 23:06:58 +00:00
|
|
|
lightNode.advertise("fluorescent").settable(fluorescentHandler);
|
2018-11-23 00:49:05 +00:00
|
|
|
lightNode.advertise("fluorescentage").settable(fluorescentAgeHandler);
|
2019-01-22 23:25:51 +00:00
|
|
|
lightNode.advertise("fluorescent0").settable(fluorescent0Handler);
|
2019-01-26 22:51:30 +00:00
|
|
|
lightNode.advertise("fluorescent0Quality").settable(fluorescent0QualityHandler);
|
2019-01-22 23:25:51 +00:00
|
|
|
lightNode.advertise("fluorescent1").settable(fluorescent1Handler);
|
2019-01-26 22:51:30 +00:00
|
|
|
lightNode.advertise("fluorescent1Quality").settable(fluorescent1QualityHandler);
|
2019-01-22 23:25:51 +00:00
|
|
|
|
2017-11-18 19:43:30 +00:00
|
|
|
sensorNode.advertise("motion");
|
2017-10-31 21:37:09 +00:00
|
|
|
|
2018-09-04 18:23:12 +00:00
|
|
|
// Activate other PWM frequency. 1000 (1 KHz) is default
|
2019-01-22 23:25:51 +00:00
|
|
|
analogWriteFreq(20000);
|
2018-09-04 18:23:12 +00:00
|
|
|
|
2018-04-30 23:06:11 +00:00
|
|
|
// Restore last state
|
|
|
|
output();
|
|
|
|
|
2017-10-31 21:37:09 +00:00
|
|
|
Homie.setup();
|
|
|
|
|
|
|
|
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
|
|
|
|
ArduinoOTA.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
Homie.loop();
|
2017-11-18 19:43:30 +00:00
|
|
|
debouncer.update();
|
2017-10-31 21:37:09 +00:00
|
|
|
ArduinoOTA.handle();
|
|
|
|
}
|