Merge pull request #7188 from tobox/development

Make WeightDelta for JSON message configurable.
This commit is contained in:
Theo Arends 2019-12-12 14:40:24 +01:00 committed by GitHub
commit 07ce419f19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 3 deletions

View File

@ -58,6 +58,7 @@
#define D_JSON_WEIGHT_ITEM "WeightItem" #define D_JSON_WEIGHT_ITEM "WeightItem"
#define D_JSON_WEIGHT_CHANGE "WeightChange" #define D_JSON_WEIGHT_CHANGE "WeightChange"
#define D_JSON_WEIGHT_RAW "WeightRaw" #define D_JSON_WEIGHT_RAW "WeightRaw"
#define D_JSON_WEIGHT_DELTA "WeightDelta"
enum HxCalibrationSteps { HX_CAL_END, HX_CAL_LIMBO, HX_CAL_FINISH, HX_CAL_FAIL, HX_CAL_DONE, HX_CAL_FIRST, HX_CAL_RESET, HX_CAL_START }; enum HxCalibrationSteps { HX_CAL_END, HX_CAL_LIMBO, HX_CAL_FINISH, HX_CAL_FAIL, HX_CAL_DONE, HX_CAL_FIRST, HX_CAL_RESET, HX_CAL_START };
@ -81,6 +82,7 @@ struct HX {
uint8_t pin_dout; uint8_t pin_dout;
bool tare_flg = false; bool tare_flg = false;
bool weight_changed = false; bool weight_changed = false;
uint16_t weight_delta = 4;
} Hx; } Hx;
/*********************************************************************************************/ /*********************************************************************************************/
@ -149,6 +151,24 @@ void HxCalibrationStateTextJson(uint8_t msg_id)
if (msg_id < 3) { MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR("Sensor34")); } if (msg_id < 3) { MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR("Sensor34")); }
} }
void SetWeightDelta()
{
// backwards compatible: restore old default value of 4 grams
if (Settings.weight_change == 0) {
Hx.weight_delta = 4;
return;
}
// map upper values 101-255 to
if (Settings.weight_change > 100) {
Hx.weight_delta = (Settings.weight_change - 100) * 10 + 100;
return;
}
// map 1..100 to 0..99 grams
Hx.weight_delta = Settings.weight_change - 1;
}
/*********************************************************************************************\ /*********************************************************************************************\
* Supported commands for Sensor34: * Supported commands for Sensor34:
* *
@ -166,6 +186,7 @@ void HxCalibrationStateTextJson(uint8_t msg_id)
* Sensor34 7 - Save current weight to be used as start weight on restart * Sensor34 7 - Save current weight to be used as start weight on restart
* Sensor34 8 0 - Disable JSON weight change message * Sensor34 8 0 - Disable JSON weight change message
* Sensor34 8 1 - Enable JSON weight change message * Sensor34 8 1 - Enable JSON weight change message
* Sensor34 9 <weight code> - Set minimum delta to trigger JSON message
\*********************************************************************************************/ \*********************************************************************************************/
bool HxCommand(void) bool HxCommand(void)
@ -228,6 +249,13 @@ bool HxCommand(void)
} }
show_parms = true; show_parms = true;
break; break;
case 9: // WeightDelta
if (strstr(XdrvMailbox.data, ",") != nullptr) {
Settings.weight_change = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
SetWeightDelta();
}
show_parms = true;
break;
default: default:
show_parms = true; show_parms = true;
} }
@ -235,8 +263,10 @@ bool HxCommand(void)
if (show_parms) { if (show_parms) {
char item[33]; char item[33];
dtostrfd((float)Settings.weight_item / 10, 1, item); dtostrfd((float)Settings.weight_item / 10, 1, item);
Response_P(PSTR("{\"Sensor34\":{\"" D_JSON_WEIGHT_REF "\":%d,\"" D_JSON_WEIGHT_CAL "\":%d,\"" D_JSON_WEIGHT_MAX "\":%d,\"" D_JSON_WEIGHT_ITEM "\":%s,\"" D_JSON_WEIGHT_CHANGE "\":\"%s\"}}"), Response_P(PSTR("{\"Sensor34\":{\"" D_JSON_WEIGHT_REF "\":%d,\"" D_JSON_WEIGHT_CAL "\":%d,\"" D_JSON_WEIGHT_MAX "\":%d,\""
Settings.weight_reference, Settings.weight_calibration, Settings.weight_max * 1000, item, GetStateText(Settings.SensorBits1.hx711_json_weight_change)); D_JSON_WEIGHT_ITEM "\":%s,\"" D_JSON_WEIGHT_CHANGE "\":%s,\"" D_JSON_WEIGHT_DELTA "\":%d}}"),
Settings.weight_reference, Settings.weight_calibration, Settings.weight_max * 1000,
item, GetStateText(Settings.SensorBits1.hx711_json_weight_change), Settings.weight_change);
} }
return serviced; return serviced;
@ -261,6 +291,8 @@ void HxInit(void)
digitalWrite(Hx.pin_sck, LOW); digitalWrite(Hx.pin_sck, LOW);
SetWeightDelta();
if (HxIsReady(8 * HX_TIMEOUT)) { // Can take 600 milliseconds after power on if (HxIsReady(8 * HX_TIMEOUT)) { // Can take 600 milliseconds after power on
if (!Settings.weight_max) { Settings.weight_max = HX_MAX_WEIGHT / 1000; } if (!Settings.weight_max) { Settings.weight_max = HX_MAX_WEIGHT / 1000; }
if (!Settings.weight_calibration) { Settings.weight_calibration = HX_SCALE; } if (!Settings.weight_calibration) { Settings.weight_calibration = HX_SCALE; }
@ -358,7 +390,7 @@ void HxEvery100mSecond(void)
Hx.weight += Hx.last_weight; // grams Hx.weight += Hx.last_weight; // grams
if (Settings.SensorBits1.hx711_json_weight_change) { if (Settings.SensorBits1.hx711_json_weight_change) {
if (abs(Hx.weight - Hx.weight_diff) > 4) { // Use 4 gram threshold to decrease "ghost" weights if (abs(Hx.weight - Hx.weight_diff) > Hx.weight_delta) { // Use weight_delta threshold to decrease "ghost" weights
Hx.weight_diff = Hx.weight; Hx.weight_diff = Hx.weight;
Hx.weight_changed = true; Hx.weight_changed = true;
} }