fix mqtt handling

This commit is contained in:
interfisch 2019-10-12 16:56:30 +02:00
parent efa1f58785
commit 2306f019b3
1 changed files with 52 additions and 8 deletions

View File

@ -5,7 +5,7 @@
#include <Homie.h>
#define FW_NAME "tischlicht"
#define FW_VERSION "1.0.0"
#define FW_VERSION "1.0.1"
/*
* To Update configuration (wifi credentials) from data/homie/config.json:
@ -116,6 +116,7 @@ void setup() {
lightNode.advertise("brightness").settable(brightnessHandler);
lightNode.advertise("temperature").settable(temperatureHandler);
lightNode.advertise("fadetime").settable(fadetimeHandler);
lightNode.advertise("sleep").settable(sleepHandler);
Homie.setup();
@ -272,12 +273,22 @@ void loopHandler() {
last_updatetime = loopmillis;
float old_brightness = brightness; //store last brightness
if ( (brightness_change_per_loop<0 && brightness>set_brightness) || (brightness_change_per_loop>0 && brightness<set_brightness)) { //if brightness not reached
brightness += brightness_change_per_loop;
if ( ( old_brightness <= set_brightness && set_brightness <= brightness ) || ( old_brightness >= set_brightness && set_brightness >= brightness ) ) { //overshot set value
brightness = set_brightness;
}
flag_updatePWM=true; //force update
}
float old_temperature = temperature; //store last temperature
if ( (temperature_change_per_loop<0 && temperature>set_temperature) || (temperature_change_per_loop>0 && temperature<set_temperature)) { //if temperature not reached
temperature += temperature_change_per_loop;
if ( ( old_temperature <= set_temperature && set_temperature <= temperature ) || ( old_temperature >= set_temperature && set_temperature >= temperature ) ) { //overshot set value
temperature = set_temperature;
}
flag_updatePWM=true; //force update
}
//calculate and update pwm
@ -307,17 +318,20 @@ void loopHandler() {
last_debugupdatetime = loopmillis;
if (sleep) { Serial.print("Sleep. "); }
/*Serial.print("bright=");
Serial.print("bright=");
Serial.print(brightness);
Serial.print(" set=");
Serial.print(set_brightness);
Serial.print("| temp=");
Serial.print(temperature);
Serial.print(" set=");
Serial.println(set_temperature);
*/
Serial.print(set_temperature);
Serial.print(" change=");
Serial.println(brightness_change_per_loop);
/*
uint16_t pwmCW;
uint16_t pwmWW;
float temp=mapFloat(temperature, TEMPERATURE_MIN, TEMPERATURE_MAX, 0.0,1.0); //0=warmwhite, 1=coldwhite
@ -336,6 +350,7 @@ void loopHandler() {
Serial.print(", ");
Serial.println(0);
}
*/
}
@ -357,7 +372,11 @@ bool brightnessHandler(const HomieRange& range, const String& value) {
}
float _difference=set_brightness-brightness;
if (fadetime>0) {
brightness_change_per_loop = _difference/fadetime*UPDATETIME;
} else { //special case for instant change
brightness_change_per_loop = _difference;
}
return true;
}
@ -377,7 +396,11 @@ bool temperatureHandler(const HomieRange& range, const String& value) {
}
float _difference=set_temperature-temperature;
if (fadetime>0) {
temperature_change_per_loop = _difference/fadetime*UPDATETIME;
} else { //special case for instant change
temperature_change_per_loop = _difference;
}
return true;
@ -400,6 +423,27 @@ bool fadetimeHandler(const HomieRange& range, const String& value) { //fadetime
return true;
}
bool sleepHandler(const HomieRange& range, const String& value) { //change on off
if (range.isRange) {
return false; //if range is given but index is not in allowed range
}
Homie.getLogger() << "sleep " << ": " << value << endl;
lightNode.setProperty("sleep").send(value);
if (value.toInt() == 0) {
sleep=true;
} else if(value.toInt() == 1) {
sleep=false;
} else {
Homie.getLogger() << "Value outside range" << endl;
return false;
}
flag_updatePWM=true; //force update
return true;
}
float mapFloat(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}