2018-10-11 16:33:07 +01:00
|
|
|
/*
|
|
|
|
xsns_34_hx711.ino - HX711 load cell support for Sonoff-Tasmota
|
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2018-10-11 16:33:07 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_HX711
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* HX711 - Load cell as used in a scale
|
|
|
|
*
|
|
|
|
* Source: Sparkfun and https://github.com/bogde/HX711
|
|
|
|
*
|
|
|
|
* To reset the scale:
|
|
|
|
* - Execute command Sensor34 1
|
|
|
|
*
|
|
|
|
* To calibrate the scale perform the following tasks:
|
|
|
|
* - Set reference weight once using command Sensor34 3 <reference weight in gram>
|
|
|
|
* - Remove any weight from the scale
|
|
|
|
* - Execute command Sensor34 2 and follow messages shown
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-10-21 15:30:05 +01:00
|
|
|
#define XSNS_34 34
|
2018-10-11 16:33:07 +01:00
|
|
|
|
|
|
|
#ifndef HX_MAX_WEIGHT
|
2018-10-21 15:30:05 +01:00
|
|
|
#define HX_MAX_WEIGHT 20000 // Default max weight in gram
|
2018-10-11 16:33:07 +01:00
|
|
|
#endif
|
|
|
|
#ifndef HX_REFERENCE
|
2018-10-21 15:30:05 +01:00
|
|
|
#define HX_REFERENCE 250 // Default reference weight for calibration in gram
|
2018-10-11 16:33:07 +01:00
|
|
|
#endif
|
|
|
|
#ifndef HX_SCALE
|
2018-10-21 15:30:05 +01:00
|
|
|
#define HX_SCALE 120 // Default result of measured weight / reference weight when scale is 1
|
2018-10-11 16:33:07 +01:00
|
|
|
#endif
|
|
|
|
|
2018-10-21 15:30:05 +01:00
|
|
|
#define HX_TIMEOUT 120 // A reading at default 10Hz (pin RATE to Gnd on HX711) can take up to 100 milliseconds
|
|
|
|
#define HX_SAMPLES 10 // Number of samples for average calculation
|
|
|
|
#define HX_CAL_TIMEOUT 15 // Calibration step window in number of seconds
|
2018-10-11 16:33:07 +01:00
|
|
|
|
2018-10-21 15:30:05 +01:00
|
|
|
#define HX_GAIN_128 1 // Channel A, gain factor 128
|
|
|
|
#define HX_GAIN_32 2 // Channel B, gain factor 32
|
|
|
|
#define HX_GAIN_64 3 // Channel A, gain factor 64
|
|
|
|
|
|
|
|
#define D_JSON_WEIGHT_REF "WeightRef"
|
|
|
|
#define D_JSON_WEIGHT_CAL "WeightCal"
|
|
|
|
#define D_JSON_WEIGHT_MAX "WeightMax"
|
|
|
|
#define D_JSON_WEIGHT_ITEM "WeightItem"
|
2018-10-11 16:33:07 +01:00
|
|
|
|
|
|
|
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 };
|
|
|
|
|
|
|
|
const char kHxCalibrationStates[] PROGMEM = D_HX_CAL_FAIL "|" D_HX_CAL_DONE "|" D_HX_CAL_REFERENCE "|" D_HX_CAL_REMOVE;
|
|
|
|
|
|
|
|
long hx_weight = 0;
|
2019-05-19 16:49:00 +01:00
|
|
|
long hx_last_weight = 0;
|
2018-10-11 16:33:07 +01:00
|
|
|
long hx_sum_weight = 0;
|
|
|
|
long hx_offset = 0;
|
|
|
|
long hx_scale = 1;
|
|
|
|
uint8_t hx_type = 1;
|
|
|
|
uint8_t hx_sample_count = 0;
|
|
|
|
uint8_t hx_calibrate_step = HX_CAL_END;
|
|
|
|
uint8_t hx_calibrate_timer = 0;
|
|
|
|
uint8_t hx_calibrate_msg = 0;
|
|
|
|
uint8_t hx_pin_sck;
|
|
|
|
uint8_t hx_pin_dout;
|
2019-01-28 13:08:33 +00:00
|
|
|
bool hx_tare_flg = false;
|
2018-10-11 16:33:07 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
bool HxIsReady(uint16_t timeout)
|
|
|
|
{
|
|
|
|
// A reading can take up to 100 mS or 600mS after power on
|
|
|
|
uint32_t start = millis();
|
|
|
|
while ((digitalRead(hx_pin_dout) == HIGH) && (millis() - start < timeout)) { yield(); }
|
|
|
|
return (digitalRead(hx_pin_dout) == LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
long HxRead()
|
|
|
|
{
|
|
|
|
if (!HxIsReady(HX_TIMEOUT)) { return -1; }
|
|
|
|
|
|
|
|
uint8_t data[3] = { 0 };
|
|
|
|
uint8_t filler = 0x00;
|
|
|
|
|
|
|
|
// pulse the clock pin 24 times to read the data
|
|
|
|
data[2] = shiftIn(hx_pin_dout, hx_pin_sck, MSBFIRST);
|
|
|
|
data[1] = shiftIn(hx_pin_dout, hx_pin_sck, MSBFIRST);
|
|
|
|
data[0] = shiftIn(hx_pin_dout, hx_pin_sck, MSBFIRST);
|
|
|
|
|
|
|
|
// set the channel and the gain factor for the next reading using the clock pin
|
|
|
|
for (unsigned int i = 0; i < HX_GAIN_128; i++) {
|
|
|
|
digitalWrite(hx_pin_sck, HIGH);
|
|
|
|
digitalWrite(hx_pin_sck, LOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replicate the most significant bit to pad out a 32-bit signed integer
|
|
|
|
if (data[2] & 0x80) { filler = 0xFF; }
|
|
|
|
|
|
|
|
// Construct a 32-bit signed integer
|
|
|
|
unsigned long value = ( static_cast<unsigned long>(filler) << 24
|
|
|
|
| static_cast<unsigned long>(data[2]) << 16
|
|
|
|
| static_cast<unsigned long>(data[1]) << 8
|
|
|
|
| static_cast<unsigned long>(data[0]) );
|
|
|
|
|
|
|
|
return static_cast<long>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2019-05-19 16:49:00 +01:00
|
|
|
void HxResetPart(void)
|
2018-10-11 16:33:07 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
hx_tare_flg = true;
|
2018-10-11 16:33:07 +01:00
|
|
|
hx_sum_weight = 0;
|
|
|
|
hx_sample_count = 0;
|
2019-05-19 16:49:00 +01:00
|
|
|
hx_last_weight = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HxReset(void)
|
|
|
|
{
|
|
|
|
HxResetPart();
|
|
|
|
Settings.energy_frequency_calibration = 0;
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void HxCalibrationStateTextJson(uint8_t msg_id)
|
|
|
|
{
|
|
|
|
char cal_text[30];
|
|
|
|
|
|
|
|
hx_calibrate_msg = msg_id;
|
2019-03-23 16:57:31 +00:00
|
|
|
Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_34, GetTextIndexed(cal_text, sizeof(cal_text), hx_calibrate_msg, kHxCalibrationStates));
|
2018-10-11 16:33:07 +01:00
|
|
|
|
|
|
|
if (msg_id < 3) { MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR("Sensor34")); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Supported commands for Sensor34:
|
|
|
|
*
|
|
|
|
* Sensor34 1 - Reset display to 0
|
|
|
|
* Sensor34 2 - Start calibration
|
|
|
|
* Sensor34 2 <weight in gram> - Set reference weight and start calibration
|
|
|
|
* Sensor34 3 - Show reference weight in gram
|
|
|
|
* Sensor34 3 <weight in gram> - Set reference weight
|
|
|
|
* Sensor34 4 - Show calibrated scale value
|
|
|
|
* Sensor34 4 <scale value> - Set calibrated scale value
|
|
|
|
* Sensor34 5 - Show max weigth in gram
|
|
|
|
* Sensor34 5 <weight in gram> - Set max weight
|
|
|
|
* Sensor34 6 - Show item weigth in decigram
|
|
|
|
* Sensor34 6 <weight in decigram> - Set item weight
|
2019-05-19 16:49:00 +01:00
|
|
|
* Sensor34 7 - Save current weight to be used as start weight on restart
|
2018-10-11 16:33:07 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
bool HxCommand(void)
|
2018-10-11 16:33:07 +01:00
|
|
|
{
|
|
|
|
bool serviced = true;
|
|
|
|
bool show_parms = false;
|
|
|
|
char sub_string[XdrvMailbox.data_len +1];
|
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t ca = 0; ca < XdrvMailbox.data_len; ca++) {
|
2018-10-11 16:33:07 +01:00
|
|
|
if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; }
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (XdrvMailbox.payload) {
|
|
|
|
case 1: // Reset scale
|
|
|
|
HxReset();
|
2019-03-23 16:57:31 +00:00
|
|
|
Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_34, "Reset");
|
2018-10-11 16:33:07 +01:00
|
|
|
break;
|
|
|
|
case 2: // Calibrate
|
2019-03-26 17:26:50 +00:00
|
|
|
if (strstr(XdrvMailbox.data, ",") != nullptr) {
|
|
|
|
Settings.weight_reference = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
hx_scale = 1;
|
|
|
|
HxReset();
|
|
|
|
hx_calibrate_step = HX_CAL_START;
|
|
|
|
hx_calibrate_timer = 1;
|
|
|
|
HxCalibrationStateTextJson(3);
|
|
|
|
break;
|
2018-10-21 15:30:05 +01:00
|
|
|
case 3: // WeightRef to user reference
|
2019-03-26 17:26:50 +00:00
|
|
|
if (strstr(XdrvMailbox.data, ",") != nullptr) {
|
|
|
|
Settings.weight_reference = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
show_parms = true;
|
|
|
|
break;
|
|
|
|
case 4: // WeightCal to user calculated value
|
2019-03-26 17:26:50 +00:00
|
|
|
if (strstr(XdrvMailbox.data, ",") != nullptr) {
|
|
|
|
Settings.weight_calibration = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10);
|
2018-10-11 16:33:07 +01:00
|
|
|
hx_scale = Settings.weight_calibration;
|
|
|
|
}
|
|
|
|
show_parms = true;
|
|
|
|
break;
|
|
|
|
case 5: // WeightMax
|
2019-03-26 17:26:50 +00:00
|
|
|
if (strstr(XdrvMailbox.data, ",") != nullptr) {
|
|
|
|
Settings.weight_max = strtol(subStr(sub_string, XdrvMailbox.data, ",", 2), nullptr, 10) / 1000;
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
show_parms = true;
|
|
|
|
break;
|
|
|
|
case 6: // WeightItem
|
2019-03-26 17:26:50 +00:00
|
|
|
if (strstr(XdrvMailbox.data, ",") != nullptr) {
|
2019-07-01 17:20:43 +01:00
|
|
|
Settings.weight_item = (unsigned long)(CharToFloat(subStr(sub_string, XdrvMailbox.data, ",", 2)) * 10);
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
show_parms = true;
|
|
|
|
break;
|
2019-05-19 16:49:00 +01:00
|
|
|
case 7: // WeightSave
|
|
|
|
Settings.energy_frequency_calibration = hx_weight;
|
|
|
|
Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_34, D_JSON_DONE);
|
|
|
|
break;
|
2018-10-11 16:33:07 +01:00
|
|
|
default:
|
|
|
|
serviced = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show_parms) {
|
2018-12-21 15:17:06 +00:00
|
|
|
char item[33];
|
2018-10-11 16:33:07 +01:00
|
|
|
dtostrfd((float)Settings.weight_item / 10, 1, item);
|
2019-03-23 16:57:31 +00:00
|
|
|
Response_P(PSTR("{\"Sensor34\":{\"" D_JSON_WEIGHT_REF "\":%d,\"" D_JSON_WEIGHT_CAL "\":%d,\"" D_JSON_WEIGHT_MAX "\":%d,\"" D_JSON_WEIGHT_ITEM "\":%s}}"),
|
2018-10-11 16:33:07 +01:00
|
|
|
Settings.weight_reference, Settings.weight_calibration, Settings.weight_max * 1000, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2018-10-23 14:51:51 +01:00
|
|
|
long HxWeight()
|
|
|
|
{
|
|
|
|
return (hx_calibrate_step < HX_CAL_FAIL) ? hx_weight : 0;
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HxInit(void)
|
2018-10-11 16:33:07 +01:00
|
|
|
{
|
|
|
|
hx_type = 0;
|
|
|
|
if ((pin[GPIO_HX711_DAT] < 99) && (pin[GPIO_HX711_SCK] < 99)) {
|
|
|
|
hx_pin_sck = pin[GPIO_HX711_SCK];
|
|
|
|
hx_pin_dout = pin[GPIO_HX711_DAT];
|
|
|
|
|
|
|
|
pinMode(hx_pin_sck, OUTPUT);
|
|
|
|
pinMode(hx_pin_dout, INPUT);
|
|
|
|
|
|
|
|
digitalWrite(hx_pin_sck, LOW);
|
|
|
|
|
|
|
|
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_calibration) { Settings.weight_calibration = HX_SCALE; }
|
|
|
|
if (!Settings.weight_reference) { Settings.weight_reference = HX_REFERENCE; }
|
|
|
|
hx_scale = Settings.weight_calibration;
|
|
|
|
HxRead();
|
2019-05-19 16:49:00 +01:00
|
|
|
HxResetPart();
|
2018-10-11 16:33:07 +01:00
|
|
|
hx_type = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HxEvery100mSecond(void)
|
2018-10-11 16:33:07 +01:00
|
|
|
{
|
|
|
|
hx_sum_weight += HxRead();
|
|
|
|
|
|
|
|
hx_sample_count++;
|
|
|
|
if (HX_SAMPLES == hx_sample_count) {
|
|
|
|
long average = hx_sum_weight / hx_sample_count; // grams
|
|
|
|
long value = average - hx_offset; // grams
|
|
|
|
hx_weight = value / hx_scale; // grams
|
2019-05-19 16:49:00 +01:00
|
|
|
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;
|
|
|
|
}
|
2018-10-11 16:33:07 +01:00
|
|
|
|
|
|
|
if (hx_tare_flg) {
|
2019-01-28 13:08:33 +00:00
|
|
|
hx_tare_flg = false;
|
2018-10-11 16:33:07 +01:00
|
|
|
hx_offset = average; // grams
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hx_calibrate_step) {
|
|
|
|
hx_calibrate_timer--;
|
|
|
|
|
|
|
|
if (HX_CAL_START == hx_calibrate_step) { // Skip reset just initiated
|
|
|
|
hx_calibrate_step--;
|
|
|
|
hx_calibrate_timer = HX_CAL_TIMEOUT * (10 / HX_SAMPLES);
|
|
|
|
}
|
|
|
|
else if (HX_CAL_RESET == hx_calibrate_step) { // Wait for stable reset
|
|
|
|
if (hx_calibrate_timer) {
|
2018-12-24 17:14:25 +00:00
|
|
|
if (hx_weight < (long)Settings.weight_reference) {
|
2018-10-11 16:33:07 +01:00
|
|
|
hx_calibrate_step--;
|
|
|
|
hx_calibrate_timer = HX_CAL_TIMEOUT * (10 / HX_SAMPLES);
|
|
|
|
HxCalibrationStateTextJson(2);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hx_calibrate_step = HX_CAL_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (HX_CAL_FIRST == hx_calibrate_step) { // Wait for first reference weight
|
|
|
|
if (hx_calibrate_timer) {
|
2018-12-24 17:14:25 +00:00
|
|
|
if (hx_weight > (long)Settings.weight_reference) {
|
2018-10-11 16:33:07 +01:00
|
|
|
hx_calibrate_step--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
hx_calibrate_step = HX_CAL_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (HX_CAL_DONE == hx_calibrate_step) { // Second stable reference weight
|
2018-12-24 17:14:25 +00:00
|
|
|
if (hx_weight > (long)Settings.weight_reference) {
|
2018-10-11 16:33:07 +01:00
|
|
|
hx_calibrate_step = HX_CAL_FINISH; // Calibration done
|
|
|
|
Settings.weight_calibration = hx_weight / Settings.weight_reference;
|
|
|
|
hx_weight = 0; // Reset calibration value
|
|
|
|
HxCalibrationStateTextJson(1);
|
|
|
|
} else {
|
|
|
|
hx_calibrate_step = HX_CAL_FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HX_CAL_FAIL == hx_calibrate_step) { // Calibration failed
|
|
|
|
hx_calibrate_step--;
|
2019-01-28 13:08:33 +00:00
|
|
|
hx_tare_flg = true; // Perform a reset using old scale
|
2018-10-11 16:33:07 +01:00
|
|
|
HxCalibrationStateTextJson(0);
|
|
|
|
}
|
|
|
|
if (HX_CAL_FINISH == hx_calibrate_step) { // Calibration finished
|
|
|
|
hx_calibrate_step--;
|
|
|
|
hx_calibrate_timer = 3 * (10 / HX_SAMPLES);
|
|
|
|
hx_scale = Settings.weight_calibration;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hx_calibrate_timer) {
|
|
|
|
hx_calibrate_step = HX_CAL_END; // End of calibration
|
|
|
|
}
|
2019-05-19 16:49:00 +01:00
|
|
|
} else {
|
|
|
|
hx_weight += hx_last_weight; // grams
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hx_sum_weight = 0;
|
|
|
|
hx_sample_count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 16:49:00 +01:00
|
|
|
void HxSaveBeforeRestart()
|
|
|
|
{
|
|
|
|
Settings.energy_frequency_calibration = hx_weight;
|
|
|
|
hx_sample_count = HX_SAMPLES +1; // Stop updating hx_weight
|
|
|
|
}
|
|
|
|
|
2018-10-11 16:33:07 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_HX711_WEIGHT[] PROGMEM =
|
2018-10-11 16:33:07 +01:00
|
|
|
"{s}HX711 " D_WEIGHT "{m}%s " D_UNIT_KILOGRAM "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_HX711_COUNT[] PROGMEM =
|
2018-10-11 16:33:07 +01:00
|
|
|
"{s}HX711 " D_COUNT "{m}%d{e}";
|
2019-03-19 16:31:43 +00:00
|
|
|
const char HTTP_HX711_CAL[] PROGMEM =
|
2018-10-11 16:33:07 +01:00
|
|
|
"{s}HX711 %s{m}{e}";
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void HxShow(bool json)
|
2018-10-11 16:33:07 +01:00
|
|
|
{
|
|
|
|
char scount[30] = { 0 };
|
|
|
|
|
|
|
|
uint16_t count = 0;
|
|
|
|
float weight = 0;
|
|
|
|
if (hx_calibrate_step < HX_CAL_FAIL) {
|
|
|
|
if (hx_weight && Settings.weight_item) {
|
|
|
|
count = (hx_weight * 10) / Settings.weight_item;
|
|
|
|
if (count > 1) {
|
|
|
|
snprintf_P(scount, sizeof(scount), PSTR(",\"" D_JSON_COUNT "\":%d"), count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
weight = (float)hx_weight / 1000; // kilograms
|
|
|
|
}
|
2018-12-21 15:17:06 +00:00
|
|
|
char weight_chr[33];
|
2018-10-11 16:33:07 +01:00
|
|
|
dtostrfd(weight, Settings.flag2.weight_resolution, weight_chr);
|
|
|
|
|
|
|
|
if (json) {
|
2019-03-23 16:57:31 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"HX711\":{\"" D_JSON_WEIGHT "\":%s%s}"), weight_chr, scount);
|
2018-10-11 16:33:07 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_HX711_WEIGHT, weight_chr);
|
2018-10-11 16:33:07 +01:00
|
|
|
if (count > 1) {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_HX711_COUNT, count);
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
if (hx_calibrate_step) {
|
|
|
|
char cal_text[30];
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_HX711_CAL, GetTextIndexed(cal_text, sizeof(cal_text), hx_calibrate_msg, kHxCalibrationStates));
|
2018-10-11 16:33:07 +01:00
|
|
|
}
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:30:05 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
#ifdef USE_HX711_GUI
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Optional GUI
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define WEB_HANDLE_HX711 "s34"
|
|
|
|
|
|
|
|
const char S_CONFIGURE_HX711[] PROGMEM = D_CONFIGURE_HX711;
|
|
|
|
|
|
|
|
const char HTTP_BTN_MENU_MAIN_HX711[] PROGMEM =
|
2019-03-19 17:02:38 +00:00
|
|
|
"<p><form action='" WEB_HANDLE_HX711 "' method='get'><button name='reset'>" D_RESET_HX711 "</button></form></p>";
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
const char HTTP_BTN_MENU_HX711[] PROGMEM =
|
2019-02-13 15:05:25 +00:00
|
|
|
"<p><form action='" WEB_HANDLE_HX711 "' method='get'><button>" D_CONFIGURE_HX711 "</button></form></p>";
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
const char HTTP_FORM_HX711[] PROGMEM =
|
|
|
|
"<fieldset><legend><b> " D_CALIBRATION " </b></legend>"
|
|
|
|
"<form method='post' action='" WEB_HANDLE_HX711 "'>"
|
2019-05-31 17:24:56 +01:00
|
|
|
"<p><b>" D_REFERENCE_WEIGHT "</b> (" D_UNIT_KILOGRAM ")<br><input type='number' step='0.001' id='p1' placeholder='0' value='%s'></p>"
|
|
|
|
"<br><button name='calibrate' type='submit'>" D_CALIBRATE "</button>"
|
2018-10-21 15:30:05 +01:00
|
|
|
"</form>"
|
2019-05-31 17:24:56 +01:00
|
|
|
"</fieldset><br><br>"
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
"<fieldset><legend><b> " D_HX711_PARAMETERS " </b></legend>"
|
|
|
|
"<form method='post' action='" WEB_HANDLE_HX711 "'>"
|
2019-05-31 17:24:56 +01:00
|
|
|
"<p><b>" D_ITEM_WEIGHT "</b> (" D_UNIT_KILOGRAM ")<br><input type='number' max='6.5535' step='0.0001' id='p2' placeholder='0.0' value='%s'></p>";
|
2018-10-21 15:30:05 +01:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HandleHxAction(void)
|
2018-10-21 15:30:05 +01:00
|
|
|
{
|
2019-01-10 11:57:42 +00:00
|
|
|
if (!HttpCheckPriviledgedAccess()) { return; }
|
|
|
|
|
2018-10-21 15:30:05 +01:00
|
|
|
AddLog_P(LOG_LEVEL_DEBUG, S_LOG_HTTP, S_CONFIGURE_HX711);
|
|
|
|
|
|
|
|
if (WebServer->hasArg("save")) {
|
|
|
|
HxSaveSettings();
|
|
|
|
HandleConfiguration();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:16:07 +00:00
|
|
|
char stemp1[20];
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
if (WebServer->hasArg("reset")) {
|
2019-03-04 17:16:07 +00:00
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR("Sensor34 1")); // Reset
|
|
|
|
ExecuteWebCommand(stemp1, SRC_WEBGUI);
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
HandleRoot(); // Return to main screen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WebServer->hasArg("calibrate")) {
|
2019-03-04 17:16:07 +00:00
|
|
|
WebGetArg("p1", stemp1, sizeof(stemp1));
|
2019-07-01 17:20:43 +01:00
|
|
|
Settings.weight_reference = (!strlen(stemp1)) ? 0 : (unsigned long)(CharToFloat(stemp1) * 1000);
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
HxLogUpdates();
|
|
|
|
|
2019-03-04 17:16:07 +00:00
|
|
|
snprintf_P(stemp1, sizeof(stemp1), PSTR("Sensor34 2")); // Start calibration
|
|
|
|
ExecuteWebCommand(stemp1, SRC_WEBGUI);
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
HandleRoot(); // Return to main screen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-10 14:36:34 +00:00
|
|
|
WSContentStart_P(S_CONFIGURE_HX711);
|
2019-03-04 17:16:07 +00:00
|
|
|
WSContentSendStyle();
|
|
|
|
dtostrfd((float)Settings.weight_reference / 1000, 3, stemp1);
|
|
|
|
char stemp2[20];
|
|
|
|
dtostrfd((float)Settings.weight_item / 10000, 4, stemp2);
|
|
|
|
WSContentSend_P(HTTP_FORM_HX711, stemp1, stemp2);
|
2019-03-10 14:36:34 +00:00
|
|
|
WSContentSend_P(HTTP_FORM_END);
|
2019-03-11 09:38:41 +00:00
|
|
|
WSContentSpaceButton(BUTTON_CONFIGURATION);
|
2019-03-16 15:23:41 +00:00
|
|
|
WSContentStop();
|
2018-10-21 15:30:05 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HxSaveSettings(void)
|
2018-10-21 15:30:05 +01:00
|
|
|
{
|
|
|
|
char tmp[100];
|
|
|
|
|
|
|
|
WebGetArg("p2", tmp, sizeof(tmp));
|
2019-07-01 17:20:43 +01:00
|
|
|
Settings.weight_item = (!strlen(tmp)) ? 0 : (unsigned long)(CharToFloat(tmp) * 10000);
|
2018-10-21 15:30:05 +01:00
|
|
|
|
|
|
|
HxLogUpdates();
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void HxLogUpdates(void)
|
2018-10-21 15:30:05 +01:00
|
|
|
{
|
2018-12-21 15:17:06 +00:00
|
|
|
char weigth_ref_chr[33];
|
2018-10-21 15:30:05 +01:00
|
|
|
dtostrfd((float)Settings.weight_reference / 1000, Settings.flag2.weight_resolution, weigth_ref_chr);
|
2018-12-21 15:17:06 +00:00
|
|
|
char weigth_item_chr[33];
|
2018-10-21 15:30:05 +01:00
|
|
|
dtostrfd((float)Settings.weight_item / 10000, 4, weigth_item_chr);
|
|
|
|
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_WIFI D_JSON_WEIGHT_REF " %s, " D_JSON_WEIGHT_ITEM " %s"), weigth_ref_chr, weigth_item_chr);
|
2018-10-21 15:30:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_HX711_GUI
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2018-10-11 16:33:07 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns34(uint8_t function)
|
2018-10-11 16:33:07 +01:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2018-10-11 16:33:07 +01:00
|
|
|
|
|
|
|
if (hx_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_INIT:
|
|
|
|
HxInit();
|
|
|
|
break;
|
|
|
|
case FUNC_EVERY_100_MSECOND:
|
|
|
|
HxEvery100mSecond();
|
|
|
|
break;
|
2019-03-10 12:33:32 +00:00
|
|
|
case FUNC_COMMAND_SENSOR:
|
2018-10-11 16:33:07 +01:00
|
|
|
if (XSNS_34 == XdrvMailbox.index) {
|
|
|
|
result = HxCommand();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
HxShow(1);
|
|
|
|
break;
|
2019-05-19 16:49:00 +01:00
|
|
|
case FUNC_SAVE_BEFORE_RESTART:
|
|
|
|
HxSaveBeforeRestart();
|
|
|
|
break;
|
2018-10-11 16:33:07 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2018-10-11 16:33:07 +01:00
|
|
|
HxShow(0);
|
|
|
|
break;
|
2018-10-21 15:30:05 +01:00
|
|
|
#ifdef USE_HX711_GUI
|
|
|
|
case FUNC_WEB_ADD_MAIN_BUTTON:
|
2019-03-10 14:36:34 +00:00
|
|
|
WSContentSend_P(HTTP_BTN_MENU_MAIN_HX711);
|
2018-10-21 15:30:05 +01:00
|
|
|
break;
|
|
|
|
case FUNC_WEB_ADD_BUTTON:
|
2019-03-10 14:36:34 +00:00
|
|
|
WSContentSend_P(HTTP_BTN_MENU_HX711);
|
2018-10-21 15:30:05 +01:00
|
|
|
break;
|
|
|
|
case FUNC_WEB_ADD_HANDLER:
|
|
|
|
WebServer->on("/" WEB_HANDLE_HX711, HandleHxAction);
|
|
|
|
break;
|
|
|
|
#endif // USE_HX711_GUI
|
2018-10-11 16:33:07 +01:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_HX711
|