add live send switch

This commit is contained in:
interfisch 2021-02-22 20:03:08 +01:00
parent a3b0eea2c6
commit 1e84da511c
1 changed files with 35 additions and 11 deletions

View File

@ -21,6 +21,8 @@ const int PIN_SELFENABLE = D1;
HX711 scale;
float weight_current=0; //last weight reading
#define MEASURE_INTERVAL 100 //ms
#define READING_FILTER_SIZE 40 //latency is about READING_FILTER_SIZE/2*MEASURE_INTERVAL
float weight_read[READING_FILTER_SIZE] = {0};
@ -36,6 +38,10 @@ unsigned long weight_sent_time=0;
#define MAXONTIME 60000*2 //turn off after ms
#define MQTT_SENDINTERVALL 500 //ms
unsigned long last_mqtt_send=0;
bool livesend=false; //if true, sends continuous data over mqtt
#define FW_NAME "scale"
#define FW_VERSION "0.0.1"
@ -71,7 +77,7 @@ void setup() {
scaleNode.advertise("raw");
scaleNode.advertise("max");
scaleNode.advertise("CMD").settable(cmdHandler); //function inputHandler gets called on new message on topic/input/set
scaleNode.advertise("cmd").settable(cmdHandler); //function inputHandler gets called on new message on topic/input/set
Homie.setup();
@ -140,7 +146,7 @@ void loopHandler() {
last_measure=loopmillis;
Serial.print("reading=");
float weight_current=0;
weight_current=0;
if (scale.wait_ready_timeout(1000)) { //for non blocking mode
weight_read_pos++;
weight_read_pos%=READING_FILTER_SIZE;
@ -157,12 +163,6 @@ void loopHandler() {
Serial.println(weight_current);
Serial.print("spread="); Serial.println(spread,3);
char charBuf[10];
dtostrf(weight_current,4, 3, charBuf);
scaleNode.setProperty("raw").send(charBuf);
dtostrf(spread,4, 3, charBuf);
scaleNode.setProperty("spread").send(charBuf);
@ -181,8 +181,6 @@ void loopHandler() {
}
}
dtostrf(weight_max-weight_tare,4, 3, charBuf);
scaleNode.setProperty("max").send(charBuf); //filtered and auto tared
if (!weight_sent) {
if (weight_max-weight_tare>MIN_WEIGHT_DIFFERENCE) {
@ -191,6 +189,26 @@ void loopHandler() {
}
}
if (livesend && (millis()>last_mqtt_send+MQTT_SENDINTERVALL)) {
last_mqtt_send=millis();
float weight_filtered=getFilteredWeight();
float spread=getWeightSpread();
char charBuf[10];
dtostrf(weight_current,4, 3, charBuf);
scaleNode.setProperty("raw").send(charBuf);
dtostrf(spread,4, 3, charBuf);
scaleNode.setProperty("spread").send(charBuf);
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();
@ -281,7 +299,13 @@ bool cmdHandler(const HomieRange& range, const String& value) {
Serial.println("HX711 not found.");
scaleNode.setProperty("cmd").send("HX711 not found");
}
}if (value=="live") {
livesend=!livesend;
if (livesend) {
scaleNode.setProperty("cmd").send("live data enabled");
}else{
scaleNode.setProperty("cmd").send("live data disabled");
}
}if (value=="off") {
powerOff();
scaleNode.setProperty("cmd").send("shutting down");