Add HX711 weight restore after controlled restart or after power restore just before executing command Sensor34 7 (#5367, #5786)

Add HX711 weight restore after controlled restart or after power restore just before executing command Sensor34 7 (#5367, #5786)
This commit is contained in:
Theo Arends 2019-05-19 17:49:00 +02:00
parent 25feabb921
commit 9c0bd2a665
3 changed files with 38 additions and 5 deletions

View File

@ -2,6 +2,7 @@
* Add command SetOption64 0/1 to switch between "-" or "_" as sensor index separator impacting DS18X20, DHT, BMP and SHT3X sensor names (#5689) * Add command SetOption64 0/1 to switch between "-" or "_" as sensor index separator impacting DS18X20, DHT, BMP and SHT3X sensor names (#5689)
* Add initial support for Scripts as replacement for Rules. Default disabled but can be enabled in my_user_config.h (#5689) * Add initial support for Scripts as replacement for Rules. Default disabled but can be enabled in my_user_config.h (#5689)
* Add rule System#Save executed just before a planned restart * Add rule System#Save executed just before a planned restart
* Add HX711 weight restore after controlled restart or after power restore just before executing command Sensor34 7 (#5367, #5786)
* *
* 6.5.0.10 20190513 * 6.5.0.10 20190513
* Enable ADC0 by default in my_user_config.h (#5671) * Enable ADC0 by default in my_user_config.h (#5671)

View File

@ -345,7 +345,7 @@ struct SYSCFG {
uint16_t weight_max; // 7BE Total max weight in kilogram uint16_t weight_max; // 7BE Total max weight in kilogram
unsigned long weight_reference; // 7C0 Reference weight in gram unsigned long weight_reference; // 7C0 Reference weight in gram
unsigned long weight_calibration; // 7C4 unsigned long weight_calibration; // 7C4
unsigned long energy_frequency_calibration; // 7C8 unsigned long energy_frequency_calibration; // 7C8 also used by HX711 to save last weight
uint16_t web_refresh; // 7CC uint16_t web_refresh; // 7CC
char mems[MAX_RULE_MEMS][10]; // 7CE char mems[MAX_RULE_MEMS][10]; // 7CE
char rules[MAX_RULE_SETS][MAX_RULE_SIZE]; // 800 uses 512 bytes in v5.12.0m, 3 x 512 bytes in v5.14.0b char rules[MAX_RULE_SETS][MAX_RULE_SIZE]; // 800 uses 512 bytes in v5.12.0m, 3 x 512 bytes in v5.14.0b

View File

@ -62,6 +62,7 @@ enum HxCalibrationSteps { HX_CAL_END, HX_CAL_LIMBO, HX_CAL_FINISH, HX_CAL_FAIL,
const char kHxCalibrationStates[] PROGMEM = D_HX_CAL_FAIL "|" D_HX_CAL_DONE "|" D_HX_CAL_REFERENCE "|" D_HX_CAL_REMOVE; const char kHxCalibrationStates[] PROGMEM = D_HX_CAL_FAIL "|" D_HX_CAL_DONE "|" D_HX_CAL_REFERENCE "|" D_HX_CAL_REMOVE;
long hx_weight = 0; long hx_weight = 0;
long hx_last_weight = 0;
long hx_sum_weight = 0; long hx_sum_weight = 0;
long hx_offset = 0; long hx_offset = 0;
long hx_scale = 1; long hx_scale = 1;
@ -116,11 +117,18 @@ long HxRead()
/*********************************************************************************************/ /*********************************************************************************************/
void HxReset(void) void HxResetPart(void)
{ {
hx_tare_flg = true; hx_tare_flg = true;
hx_sum_weight = 0; hx_sum_weight = 0;
hx_sample_count = 0; hx_sample_count = 0;
hx_last_weight = 0;
}
void HxReset(void)
{
HxResetPart();
Settings.energy_frequency_calibration = 0;
} }
void HxCalibrationStateTextJson(uint8_t msg_id) void HxCalibrationStateTextJson(uint8_t msg_id)
@ -147,6 +155,7 @@ void HxCalibrationStateTextJson(uint8_t msg_id)
* Sensor34 5 <weight in gram> - Set max weight * Sensor34 5 <weight in gram> - Set max weight
* Sensor34 6 - Show item weigth in decigram * Sensor34 6 - Show item weigth in decigram
* Sensor34 6 <weight in decigram> - Set item weight * Sensor34 6 <weight in decigram> - Set item weight
* Sensor34 7 - Save current weight to be used as start weight on restart
\*********************************************************************************************/ \*********************************************************************************************/
bool HxCommand(void) bool HxCommand(void)
@ -199,6 +208,10 @@ bool HxCommand(void)
} }
show_parms = true; show_parms = true;
break; break;
case 7: // WeightSave
Settings.energy_frequency_calibration = hx_weight;
Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_34, D_JSON_DONE);
break;
default: default:
serviced = false; serviced = false;
} }
@ -238,8 +251,7 @@ void HxInit(void)
if (!Settings.weight_reference) { Settings.weight_reference = HX_REFERENCE; } if (!Settings.weight_reference) { Settings.weight_reference = HX_REFERENCE; }
hx_scale = Settings.weight_calibration; hx_scale = Settings.weight_calibration;
HxRead(); HxRead();
HxReset(); HxResetPart();
hx_type = 1; hx_type = 1;
} }
} }
@ -254,7 +266,16 @@ void HxEvery100mSecond(void)
long average = hx_sum_weight / hx_sample_count; // grams long average = hx_sum_weight / hx_sample_count; // grams
long value = average - hx_offset; // grams long value = average - hx_offset; // grams
hx_weight = value / hx_scale; // grams hx_weight = value / hx_scale; // grams
if (hx_weight < 0) { hx_weight = 0; } if (hx_weight < 0) {
if (Settings.energy_frequency_calibration) {
long difference = Settings.energy_frequency_calibration + hx_weight;
hx_last_weight = difference;
if (difference < 0) { HxReset(); } // Cancel last weight as there seems to be no more weight on the scale
}
hx_weight = 0;
} else {
hx_last_weight = Settings.energy_frequency_calibration;
}
if (hx_tare_flg) { if (hx_tare_flg) {
hx_tare_flg = false; hx_tare_flg = false;
@ -313,6 +334,8 @@ void HxEvery100mSecond(void)
if (!hx_calibrate_timer) { if (!hx_calibrate_timer) {
hx_calibrate_step = HX_CAL_END; // End of calibration hx_calibrate_step = HX_CAL_END; // End of calibration
} }
} else {
hx_weight += hx_last_weight; // grams
} }
hx_sum_weight = 0; hx_sum_weight = 0;
@ -320,6 +343,12 @@ void HxEvery100mSecond(void)
} }
} }
void HxSaveBeforeRestart()
{
Settings.energy_frequency_calibration = hx_weight;
hx_sample_count = HX_SAMPLES +1; // Stop updating hx_weight
}
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
const char HTTP_HX711_WEIGHT[] PROGMEM = const char HTTP_HX711_WEIGHT[] PROGMEM =
"{s}HX711 " D_WEIGHT "{m}%s " D_UNIT_KILOGRAM "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr> "{s}HX711 " D_WEIGHT "{m}%s " D_UNIT_KILOGRAM "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
@ -484,6 +513,9 @@ bool Xsns34(uint8_t function)
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
HxShow(1); HxShow(1);
break; break;
case FUNC_SAVE_BEFORE_RESTART:
HxSaveBeforeRestart();
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR: case FUNC_WEB_SENSOR:
HxShow(0); HxShow(0);