add commands

This commit is contained in:
interfisch 2021-02-22 19:34:18 +01:00
parent 5a99550e9c
commit a3b0eea2c6
1 changed files with 54 additions and 9 deletions

View File

@ -1,7 +1,6 @@
#include <Arduino.h> #include <Arduino.h>
#include <Homie.h> #include <Homie.h>
/* /*
* Wemos d1 mini * Wemos d1 mini
* Flash Size: 4M (1M SPIFFS) * Flash Size: 4M (1M SPIFFS)
@ -10,7 +9,9 @@
//Upload config: platformio run --target uploadfs //Upload config: platformio run --target uploadfs
#include "HX711.h" #include "HX711.h"
#define SCALE_CALIBRATION 23805 //calibrated with 2.25kg weight. devide adc reading by calibration weight (in kg) to get this value (or other way around)
// HX711 circuit wiring // HX711 circuit wiring
const int LOADCELL_DOUT_PIN = D2; const int LOADCELL_DOUT_PIN = D2;
@ -46,6 +47,8 @@ int sort_desc(const void *cmp1, const void *cmp2);
float getFilteredWeight(); float getFilteredWeight();
float getWeightSpread(); float getWeightSpread();
void sendWeight(float w); void sendWeight(float w);
bool cmdHandler(const HomieRange& range, const String& value);
void powerOff();
void setup() { void setup() {
@ -68,7 +71,7 @@ void setup() {
scaleNode.advertise("raw"); scaleNode.advertise("raw");
scaleNode.advertise("max"); scaleNode.advertise("max");
scaleNode.advertise("CMD").settable(cmdHandler); //function inputHandler gets called on new message on topic/input/set
Homie.setup(); Homie.setup();
@ -78,7 +81,8 @@ void setup() {
//calibration //calibration
Serial.println("setup"); Serial.println("setup");
scale.set_scale(23805); //calibrated with 2.25kg weight
scale.set_scale(SCALE_CALIBRATION);
delay(500); delay(500);
scale.tare(); scale.tare();
delay(2000); delay(2000);
@ -144,6 +148,7 @@ void loopHandler() {
weight_read[weight_read_pos]=weight_current; //one reading takes 91ms weight_read[weight_read_pos]=weight_current; //one reading takes 91ms
} else { } else {
Serial.println("HX711 not found."); Serial.println("HX711 not found.");
scaleNode.setProperty("cmd").send("HX711 not found"); //can be done in main loop
} }
float weight_filtered=getFilteredWeight(); float weight_filtered=getFilteredWeight();
@ -184,16 +189,11 @@ void loopHandler() {
sendWeight(weight_max-weight_tare); sendWeight(weight_max-weight_tare);
} }
} }
} }
#define STAYONTIME_AFTER_SENT 5000 #define STAYONTIME_AFTER_SENT 5000
if (millis() > MAXONTIME || (weight_sent && millis()>weight_sent_time+STAYONTIME_AFTER_SENT)) { if (millis() > MAXONTIME || (weight_sent && millis()>weight_sent_time+STAYONTIME_AFTER_SENT)) {
Serial.println("Turning Off"); powerOff();
Serial.flush();
delay(100);
digitalWrite(PIN_SELFENABLE, LOW);
} }
@ -254,3 +254,48 @@ void sendWeight(float w) {
weight_sent_time=millis(); weight_sent_time=millis();
Serial.print("Weight sent="); Serial.println(w,3); Serial.print("Weight sent="); Serial.println(w,3);
} }
bool cmdHandler(const HomieRange& range, const String& value) {
if (range.isRange) {
return false; //if range is given but index is not in allowed range
}
Homie.getLogger() << "cmd" << ": " << value << endl;
//boolean value
if (value=="reset") {
if (scale.wait_ready_timeout(1000)) { //for non blocking mode
scale.set_scale(0);
scale.tare();
scaleNode.setProperty("cmd").send("tared");
} else {
Serial.println("HX711 not found.");
scaleNode.setProperty("cmd").send("HX711 not found");
}
}if (value=="adc") {
if (scale.wait_ready_timeout(1000)) { //for non blocking mode
scaleNode.setProperty("cmd").send("adc="+scale.read_average(20));
} else {
Serial.println("HX711 not found.");
scaleNode.setProperty("cmd").send("HX711 not found");
}
}if (value=="off") {
powerOff();
scaleNode.setProperty("cmd").send("shutting down");
} else {
Homie.getLogger() << "cmd not recognized" << endl;
return false;
}
return true;
}
void powerOff() {
Serial.println("Turning Off");
Serial.flush();
delay(100);
digitalWrite(PIN_SELFENABLE, LOW);
}