#include #include //curl -X PUT http://homie.config/config -d @config.json --header "Content-Type: application/json" #define PIN_LAMP0_EDGE D5 #define PIN_LAMP0 D6 #define PIN_LAMP1_EDGE D7 #define PIN_LAMP1 D8 #define PIN_SENSOR D0 //#define FULL 255 //#define LOWER 50 //#define MINIMUM 1 //#define OFF 0 //int timeout = (1000 - 50); #define FW_NAME "ledstoffroehre" #define FW_VERSION "1.0.1" int lamp0e=0; int lamp0=0; int lamp1e=0; int lamp1=0; long fluorescentLastUpdated=0; //global #define FLUORESCENTUPDATEINTERVAL 20 long fluorescent0LastActivated = 0; long fluorescent1LastActivated = 0; int setbrightness0 = 0; int setbrightness1 = 0; bool fluorescent0Active=false; bool fluorescent1Active=false; int fluorescent0Age=10; int fluorescent1Age=10; #define FLUORESCENTAGEMAX 20 #define FLUORESCENTTEMPMAX 1000 int fluorescent0Temp=0; int fluorescent1Temp=0; #define GLOWBRIGHTNESS 5 bool lastSensorValue = false; HomieNode lightNode("lamp", "lamp"); HomieNode sensorNode("sensor", "sensor"); Bounce debouncer = Bounce(); 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 fluorescent0Active = false; //set effect off fluorescent1Active = false; //set effect off fluorescent0LastActivated = millis(); fluorescent1LastActivated = millis(); lamp0e=0; lamp0=0; lamp1e=0; lamp1=0; } else { //turned on //Initialization fluorescent0Active = true; //start effect fluorescent1Active = true; //start effect } output(); return true; } 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 fluorescent0Active = false; //set effect off fluorescent0LastActivated = millis(); lamp0e=0; lamp0 = 0; } else { //turned on //Initialization fluorescent0Active = true; //start effect } output(); return true; } 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 fluorescent1Active = false; //set effect off fluorescent1LastActivated = millis(); lamp1e=0; lamp1 = 0; } else { //turned on //Initialization fluorescent1Active = true; //start effect } output(); return true; } bool fluorescentAgeHandler(const HomieRange& range, const String& value) { Homie.getLogger() << "fluorescentAge " << ": " << value << endl; fluorescent0Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); fluorescent1Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); return true; } bool fluorescent0AgeHandler(const HomieRange& range, const String& value) { Homie.getLogger() << "fluorescent0Age " << ": " << value << endl; fluorescent0Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); return true; } bool fluorescent1AgeHandler(const HomieRange& range, const String& value) { Homie.getLogger() << "fluorescent1Age " << ": " << value << endl; fluorescent1Age = constrain(value.toInt(),0,FLUORESCENTAGEMAX); return true; } 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; output(); return true; } 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; output(); return true; } 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; output(); return true; } void output() { // * 4 to scale the input up for ESP Arduino default 10 bit PWM 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)); } void loopHandler() { //mosquitto_pub -h raum.ctdo.de -t "homie/esp-deckenlicht/strip/fluorescent/set" -m "255" if (millis() > fluorescentLastUpdated + FLUORESCENTUPDATEINTERVAL) { //Update values fluorescentLastUpdated = millis(); if (fluorescent0Active) { //long _time = millis() - fluorescent0LastActivated; //time since activated int fluorescentTempIncreaseMax=61-(fluorescent0Age*3); fluorescent0Temp +=1+ random(0,fluorescentTempIncreaseMax *fluorescent0Temp/FLUORESCENTTEMPMAX); if (random(0,80 )==0){ //ignite lamp0=setbrightness0*random(50,100)/100; } if (fluorescent0Temp>200){ //warm enough to glow if (lamp0<20){ //if under glow brightness lamp0+=5; //start glowing }else if (lamp0>50){ //too bright for glow lamp0-=random(0,30); //reduce intensity (flashing effect) } }else{ //not warm enough to glow if (lamp0>0){ //too bright for glow lamp0-=random(20,50); //reduce intensity (flashing effect) } } if(lamp0>GLOWBRIGHTNESS){ lamp0e=lamp0; } if (fluorescent0Temp>=FLUORESCENTTEMPMAX){ fluorescent0Active=false; lamp0=setbrightness0; lamp0e=setbrightness0; } lamp0=constrain(lamp0, 0,255); lamp0e=constrain(lamp0e, 0,255); output(); } if (fluorescent1Active) { //long _time = millis() - fluorescent0LastActivated; //time since activated int fluorescentTempIncreaseMax=61-(fluorescent1Age*3); fluorescent1Temp +=1+ random(0,fluorescentTempIncreaseMax *fluorescent1Temp/FLUORESCENTTEMPMAX); //+= 1+ random(0,20* fluorescent0Temp/FLUORESCENTTEMPMAX); if (random(0,80 )==0){ //ignite lamp1=setbrightness1*random(50,100)/100; } if (fluorescent1Temp>200){ //warm enough to glow if (lamp1<20){ //if under glow brightness lamp1+=5; //start glowing }else if (lamp1>50){ //too bright for glow lamp1-=random(0,30); //reduce intensity (flashing effect) } }else{ //not warm enough to glow if (lamp1>0){ //too bright for glow lamp1-=random(20,50); //reduce intensity (flashing effect) } } if(lamp1>GLOWBRIGHTNESS){ lamp1e=lamp1; } if (fluorescent1Temp>=FLUORESCENTTEMPMAX){ fluorescent1Active=false; lamp1=setbrightness1; lamp1e=setbrightness1; } lamp1=constrain(lamp1, 0,255); lamp1e=constrain(lamp1e, 0,255); output(); } } bool sensorValue = debouncer.read(); if (Homie.isConfigured() && Homie.isConnected() && sensorValue != lastSensorValue) { sensorNode.setProperty("motion").send(sensorValue ? "true" : "false"); lastSensorValue = sensorValue; } } void setup() { Serial.begin(115200); Serial << endl << endl; pinMode(PIN_LAMP0_EDGE, OUTPUT); pinMode(PIN_LAMP0, OUTPUT); pinMode(PIN_LAMP1_EDGE, OUTPUT); pinMode(PIN_LAMP1, OUTPUT); debouncer.attach(PIN_SENSOR, INPUT); debouncer.interval(50); Homie_setFirmware(FW_NAME, FW_VERSION); Homie_setBrand(FW_NAME); Homie.setLoopFunction(loopHandler); lightNode.advertise("lamp").settable(lampHandler); lightNode.advertise("lamp0").settable(lamp0Handler); lightNode.advertise("lamp1").settable(lamp1Handler); lightNode.advertise("fluorescent").settable(fluorescentHandler); lightNode.advertise("fluorescentage").settable(fluorescentAgeHandler); lightNode.advertise("fluorescent0").settable(fluorescent0Handler); lightNode.advertise("fluorescent0age").settable(fluorescent0AgeHandler); lightNode.advertise("fluorescent1").settable(fluorescent1Handler); lightNode.advertise("fluorescent1age").settable(fluorescent1AgeHandler); sensorNode.advertise("motion"); // Activate other PWM frequency. 1000 (1 KHz) is default analogWriteFreq(20000); // Restore last state output(); Homie.setup(); ArduinoOTA.setHostname(Homie.getConfiguration().deviceId); ArduinoOTA.begin(); } void loop() { Homie.loop(); debouncer.update(); ArduinoOTA.handle(); }