fix mqtt send

This commit is contained in:
interfisch 2019-10-12 23:40:56 +02:00
parent b401a2548c
commit 4b513bf6be
1 changed files with 52 additions and 31 deletions

View File

@ -5,7 +5,7 @@
#include <Homie.h> #include <Homie.h>
#define FW_NAME "tischlicht" #define FW_NAME "tischlicht"
#define FW_VERSION "1.0.1" #define FW_VERSION "1.0.3"
//#define DEBUG //turns on continuous serial debug printing //#define DEBUG //turns on continuous serial debug printing
@ -59,9 +59,9 @@ HomieNode lightNode("light", "light");
#define PWM_MAX 1023 //10 bit dac #define PWM_MAX 1023 //10 bit dac
#define PWM_FREQUENCY 1000 //default: 1000 Hz #define PWM_FREQUENCY 1000 //default: 1000 Hz
boolean sleep=true; //true turns lights off. like set_.. boolean enable=false;
float sleep_fadevalue=0; //0=off, 1=on float enable_fadevalue=0; //0=off, 1=on
float sleep_fadevalue_change_per_loop=0.01; //fixed value. For manual calculatoin: sleep_fadevalue_change_per_loop=_difference/fadetime*UPDATETIME; float enable_fadevalue_change_per_loop=0.01; //fixed value. For manual calculatoin: enable_fadevalue_change_per_loop=_difference/fadetime*UPDATETIME;
float set_brightness=2; //0 to 1 float set_brightness=2; //0 to 1
#define BRIGHTNESS_MIN 0.0 #define BRIGHTNESS_MIN 0.0
@ -104,6 +104,11 @@ boolean flag_updatePWM=false; //if manually set brightness or temperature, set t
long last_debugupdatetime=0; long last_debugupdatetime=0;
#define DEBUGUPDATETIME 500 #define DEBUGUPDATETIME 500
//To check if values changed (for mqtt response)
boolean known_enable=!enable; //start with differend known values, actual values will be send first
float known_set_brightness=set_brightness+1;
float known_set_temperature=set_temperature+1;
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
Serial.println("Hello"); Serial.println("Hello");
@ -123,7 +128,7 @@ void setup() {
lightNode.advertise("brightness").settable(brightnessHandler); lightNode.advertise("brightness").settable(brightnessHandler);
lightNode.advertise("temperature").settable(temperatureHandler); lightNode.advertise("temperature").settable(temperatureHandler);
lightNode.advertise("fadetime").settable(fadetimeHandler); lightNode.advertise("fadetime").settable(fadetimeHandler);
lightNode.advertise("sleep").settable(sleepHandler); lightNode.advertise("enable").settable(enableHandler);
Homie.setup(); Homie.setup();
@ -211,16 +216,16 @@ void loopHandler() {
//Button handling //Button handling
if (flag_btnApress || flag_btnBpress){ //short press either button if (flag_btnApress || flag_btnBpress){ //short press either button
sleep = !sleep; //switch on/off enable = !enable; //switch on/off
flag_updatePWM=true; //update pwm values flag_updatePWM=true; //update pwm values
} }
if (sleep && flag_btnAholdstart ) { //in sleep mode and brightness button held down if (!enable && flag_btnAholdstart ) { //in not enabled and brightness button held down
sleep=false; //enabled light enable=true; //enable light
set_brightness=0; //reset brightness set_brightness=0; //reset brightness
brightness=set_brightness; //immediately brightness=set_brightness; //immediately
} }
if (!sleep) { //only change values if not in sleep mode if (enable) { //only change values if enabled
// Button A Longpress Handling // Button A Longpress Handling
if (flag_btnAholdstart) { if (flag_btnAholdstart) {
/* //Change only hold direction at extremes /* //Change only hold direction at extremes
@ -302,14 +307,14 @@ void loopHandler() {
} }
//Sleep //Sleep
if ( (sleep && sleep_fadevalue>0) || (!sleep && sleep_fadevalue<1) ) { //not fully turned off or on if ( (!enable && enable_fadevalue>0) || (enable && enable_fadevalue<1) ) { //not fully turned off or on
if (sleep) { //turn off if (!enable) { //turn off
sleep_fadevalue-=sleep_fadevalue_change_per_loop; enable_fadevalue-=enable_fadevalue_change_per_loop;
}else{ //turn on }else{ //turn on
sleep_fadevalue+=sleep_fadevalue_change_per_loop; enable_fadevalue+=enable_fadevalue_change_per_loop;
} }
if (sleep_fadevalue>1) { sleep_fadevalue=1; } //limit if (enable_fadevalue>1) { enable_fadevalue=1; } //limit
if (sleep_fadevalue<0) { sleep_fadevalue=0; } if (enable_fadevalue<0) { enable_fadevalue=0; } //limit
flag_updatePWM=true; //force update flag_updatePWM=true; //force update
} }
@ -320,8 +325,8 @@ void loopHandler() {
uint16_t pwmCW; uint16_t pwmCW;
uint16_t pwmWW; uint16_t pwmWW;
float temp=mapFloat(temperature, TEMPERATURE_MIN, TEMPERATURE_MAX, 0.0,1.0); //0=warmwhite, 1=coldwhite float temp=mapFloat(temperature, TEMPERATURE_MIN, TEMPERATURE_MAX, 0.0,1.0); //0=warmwhite, 1=coldwhite
pwmCW=(brightness*sleep_fadevalue)*PWM_MAX*temp; pwmCW=(brightness*enable_fadevalue)*PWM_MAX*temp;
pwmWW=(brightness*sleep_fadevalue)*PWM_MAX*(1-temp); pwmWW=(brightness*enable_fadevalue)*PWM_MAX*(1-temp);
if (pwmCW>PWM_MAX) { pwmCW=PWM_MAX; } //limit if (pwmCW>PWM_MAX) { pwmCW=PWM_MAX; } //limit
if (pwmWW>PWM_MAX) { pwmWW=PWM_MAX; } //limit if (pwmWW>PWM_MAX) { pwmWW=PWM_MAX; } //limit
@ -330,7 +335,7 @@ void loopHandler() {
analogWrite(LED_CW, PWM_MAX-pwmCW); //full pwm is led off analogWrite(LED_CW, PWM_MAX-pwmCW); //full pwm is led off
/* /*
if (!sleep) { if (enable) {
analogWrite(LED_WW, PWM_MAX-pwmWW); //full pwm is led off analogWrite(LED_WW, PWM_MAX-pwmWW); //full pwm is led off
analogWrite(LED_CW, PWM_MAX-pwmCW); //full pwm is led off analogWrite(LED_CW, PWM_MAX-pwmCW); //full pwm is led off
}else{ }else{
@ -340,11 +345,27 @@ void loopHandler() {
} }
} }
//send new values back to broker
if (known_enable!=enable) {
lightNode.setProperty("enable").send(enable ? "true" : "false");
known_enable=enable;
}
if (known_set_brightness!=set_brightness) {
lightNode.setProperty("brightness").send(String(set_brightness));
known_set_brightness=set_brightness;
}
if (known_set_temperature!=set_temperature) {
lightNode.setProperty("temperature").send(String(set_temperature));
known_set_temperature=set_temperature;
}
#ifdef DEBUG #ifdef DEBUG
if (loopmillis >= last_debugupdatetime+DEBUGUPDATETIME ) { if (loopmillis >= last_debugupdatetime+DEBUGUPDATETIME ) {
last_debugupdatetime = loopmillis; last_debugupdatetime = loopmillis;
if (sleep) { Serial.print("Sleep. "); } if (!enable) { Serial.print("not enable. "); }
Serial.print("bright="); Serial.print("bright=");
Serial.print(brightness); Serial.print(brightness);
Serial.print(" set="); Serial.print(" set=");
@ -355,8 +376,8 @@ void loopHandler() {
Serial.print(set_temperature); Serial.print(set_temperature);
Serial.print(" change="); Serial.print(" change=");
Serial.print(brightness_change_per_loop); Serial.print(brightness_change_per_loop);
Serial.print(" sleep_fadevalue="); Serial.print(" enable_fadevalue=");
Serial.println(sleep_fadevalue); Serial.println(enable_fadevalue);
/* /*
uint16_t pwmCW; uint16_t pwmCW;
@ -368,7 +389,7 @@ void loopHandler() {
if (pwmCW>PWM_MAX) { pwmCW=PWM_MAX; } //limit if (pwmCW>PWM_MAX) { pwmCW=PWM_MAX; } //limit
if (pwmWW>PWM_MAX) { pwmWW=PWM_MAX; } //limit if (pwmWW>PWM_MAX) { pwmWW=PWM_MAX; } //limit
if (!sleep) { if (enable) {
Serial.print(PWM_MAX-pwmWW); Serial.print(PWM_MAX-pwmWW);
Serial.print(", "); Serial.print(", ");
Serial.println(PWM_MAX-pwmCW); Serial.println(PWM_MAX-pwmCW);
@ -391,7 +412,7 @@ bool brightnessHandler(const HomieRange& range, const String& value) {
return false; //if range is given but index is not in allowed range return false; //if range is given but index is not in allowed range
} }
Homie.getLogger() << "brightness " << ": " << value << endl; Homie.getLogger() << "brightness " << ": " << value << endl;
lightNode.setProperty("brightness").send(value); //lightNode.setProperty("brightness").send(value); //done in main loop
if (value.toFloat() >= BRIGHTNESS_MIN && value.toFloat() <= BRIGHTNESS_MAX) { if (value.toFloat() >= BRIGHTNESS_MIN && value.toFloat() <= BRIGHTNESS_MAX) {
set_brightness=value.toFloat(); set_brightness=value.toFloat();
}else { }else {
@ -414,7 +435,7 @@ bool temperatureHandler(const HomieRange& range, const String& value) {
return false; //if range is given but index is not in allowed range return false; //if range is given but index is not in allowed range
} }
Homie.getLogger() << "temperature " << ": " << value << endl; Homie.getLogger() << "temperature " << ": " << value << endl;
lightNode.setProperty("temperature").send(value); //lightNode.setProperty("temperature").send(value); //done in main loop
if (value.toFloat() >= TEMPERATURE_MIN && value.toFloat() <= TEMPERATURE_MAX) { if (value.toFloat() >= TEMPERATURE_MIN && value.toFloat() <= TEMPERATURE_MAX) {
set_temperature=value.toFloat(); set_temperature=value.toFloat();
@ -451,17 +472,17 @@ bool fadetimeHandler(const HomieRange& range, const String& value) { //fadetime
return true; return true;
} }
bool sleepHandler(const HomieRange& range, const String& value) { //change on off bool enableHandler(const HomieRange& range, const String& value) { //change on off
if (range.isRange) { if (range.isRange) {
return false; //if range is given but index is not in allowed range return false; //if range is given but index is not in allowed range
} }
Homie.getLogger() << "sleep " << ": " << value << endl; Homie.getLogger() << "enable " << ": " << value << endl;
lightNode.setProperty("sleep").send(value); //lightNode.setProperty("enable").send(value); //done in main loop
if (value.toInt() == 0) { if (value=="false") {
sleep=true; enable=false;
} else if(value.toInt() == 1) { } else if(value=="true") {
sleep=false; enable=true;
} else { } else {
Homie.getLogger() << "Value outside range" << endl; Homie.getLogger() << "Value outside range" << endl;
return false; return false;