fix scale not working without mqtt

This commit is contained in:
interfisch 2021-03-16 20:39:56 +01:00
parent 1f58d49295
commit c5f99bd86a
1 changed files with 52 additions and 36 deletions

View File

@ -18,6 +18,8 @@ float vbat_calib2_voltage=8.0;
int vbat_raw_filtered=-1; //-1 flags it as initialized
bool vbatSent=false;
bool weight_updated=false;
#include <TM1637Display.h>
#define TM1637_CLK D5
@ -115,6 +117,7 @@ void setup() {
digitalWrite(LED_BUILTIN, HIGH);
Serial.begin(115200);
Serial.println("Hello");
display.setBrightness(displaybrightness, true); //brightness 0 to 7
@ -125,7 +128,9 @@ void setup() {
Homie_setFirmware(FW_NAME, FW_VERSION);
Homie_setBrand(FW_NAME);
Serial.println("setLoopFunction");
Homie.setLoopFunction(loopHandler);
scaleNode.advertise("human");
@ -139,6 +144,8 @@ void setup() {
hardwareNode.advertise("vbat");
hardwareNode.advertise("vbatraw");
Serial.println("homie setup");
Homie.setup();
@ -158,11 +165,6 @@ void setup() {
}
void loop() {
Homie.loop();
}
void loopHandler() {
unsigned long loopmillis=millis();
static unsigned long last_measure=0;
@ -190,8 +192,53 @@ void loopHandler() {
//Serial.print("spread="); Serial.println(spread,3);
weight_updated=true;
}
#define STAYONTIME_AFTER_SENT 5000
if (millis() > MAXONTIME || (weight_sent && millis()>weight_sent_time+STAYONTIME_AFTER_SENT)) {
powerOff();
}
if ( (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL) | (update_display && (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL_MIN) )) {
last_displayupdate=loopmillis;
update_display=false; //reset flag
if ((loopmillis >= last_new_display_custom) && (loopmillis < last_new_display_custom+display_custom_duration)) {
display.setBrightness(displaybrightness,true); //enable display
display.setSegments(display_custom);
}else{
displayNumber(weight_filtered);
display.setSegments(display_data);
if ((loopmillis >= last_display_blink) && (loopmillis < last_display_blink+display_blink_duration)) {
display.setBrightness(displaybrightness,false);
}else{
display.setBrightness(displaybrightness,true);
}
}
}
/*
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();*/
Homie.loop();
}
void loopHandler() {
unsigned long loopmillis=millis();
if (weight_updated) {
weight_updated=false;
#define MAXSPREAD 0.2 //in kg
if (spread<MAXSPREAD) { //if reading is stable
@ -231,11 +278,8 @@ void loopHandler() {
Serial.println("Cannot send vbat because mqtt not connected!");
}
}
}
if (Homie.getMqttClient().connected() && livesend && (millis()>last_mqtt_send+MQTT_SENDINTERVALL)) {
last_mqtt_send=millis();
@ -252,34 +296,6 @@ void loopHandler() {
dtostrf(weight_max-weight_tare,4, 3, charBuf);
scaleNode.setProperty("max").send(charBuf); //filtered and auto tared
}
#define STAYONTIME_AFTER_SENT 5000
if (millis() > MAXONTIME || (weight_sent && millis()>weight_sent_time+STAYONTIME_AFTER_SENT)) {
powerOff();
}
if ( (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL) | (update_display && (loopmillis > last_displayupdate + DISPLAYUPDATEINTERVAL_MIN) )) {
last_displayupdate=loopmillis;
update_display=false; //reset flag
if ((loopmillis >= last_new_display_custom) && (loopmillis < last_new_display_custom+display_custom_duration)) {
display.setBrightness(displaybrightness,true); //enable display
display.setSegments(display_custom);
}else{
displayNumber(weight_filtered);
display.setSegments(display_data);
if ((loopmillis >= last_display_blink) && (loopmillis < last_display_blink+display_blink_duration)) {
display.setBrightness(displaybrightness,false);
}else{
display.setBrightness(displaybrightness,true);
}
}
}
/*
scale.power_down(); // put the ADC in sleep mode
delay(5000);
scale.power_up();*/
}