Skip to content

Commit

Permalink
Add support for second HX711
Browse files Browse the repository at this point in the history
e8d7458
author Fabian Sperrle <[email protected]> 1702580259 +0100
committer Fabian Sperrle <[email protected]> 1705696789 +0100

Start configuration for second HX711

WIP: start cleanup

Fix startup of second HX711

Brew by weight should not stop when time is reached

Bugfix: Set correct calibration weights for scale 2 and clean up output

Enable HX711 library feature for more consistent readings on ESP32

tldr: ESP32 is too fast for the library in default mode, so they added a flag that introduces timeouts and makes the readings more realiable
Starting point for further reading: olkal/HX711_ADC#35

Fix compile error for single HX711

Fix debug message: calibration instead of tare

Config override causes issues with pin numbers for some reason? disable for now

re-introduce constant for timeout and tare to avoid duplication

If we show decimal seconds we can also show decimal grams

Remove superfluous space

Re-add HX711 config flags and move scale interrupt up in main to avoid messing with timestamps
  • Loading branch information
FabianSperrle committed Jan 19, 2024
1 parent e8d7458 commit 4c5fb23
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 76 deletions.
2 changes: 1 addition & 1 deletion data/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h5 class="card-title">
<td>
<form action="/toggleCalibration" method="post">
<input type="hidden" id="varCALIBRATION_ON" name="varCALIBRATION_ON" />
<input type="submit" class="btn btn-primary" :value="param.value == 1 ? 'Off' : 'On'" id="calibration_toggle" />
<input type="submit" class="btn btn-primary" :value="param.value == 1 ? 'Off' : 'On'" id="calibration_toggle" @click.prevent="confirmSubmission"/>
</form>
</td>
</template>
Expand Down
6 changes: 6 additions & 0 deletions data/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const vueApp = Vue.createApp({
showPostSucceeded: false
}
},
el: '#calibrationForm',
methods: {
fetchParameters() {
fetch("/parameters")
Expand Down Expand Up @@ -72,6 +73,11 @@ const vueApp = Vue.createApp({
4: 'Scale Parameters'
}
return sectionNames[sectionId]
},
confirmSubmission() {
if(confirm('Are you sure you want to toggle the calibration?')) {
this.$el.submit();
}
}
},
computed: {
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ debug_init_break = tbreak setup
monitor_port = socket://silvia.local:23
upload_protocol = espota
upload_port = silvia.local
upload_flags = --auth=otapass
upload_flags = --auth=otapass
2 changes: 1 addition & 1 deletion src/EmbeddedWebserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void serverSetup() {
int calibrate = flipUintValue(calibrationON);

setCalibration(calibrate);
debugPrintf("Toggle tare mode: %i \n", calibrate);
debugPrintf("Toggle calibration mode: %i \n", calibrate);

request->redirect("/");
});
Expand Down
14 changes: 13 additions & 1 deletion src/Storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ typedef struct __attribute__((packed)) {
uint8_t freeToUse6[21];
uint8_t pidBdOn;
double pidKpBd;
#if SINGLE_HX711 == 1
uint8_t freeToUse7[2];
#else
float scale2Calibration;
#endif
double pidTnBd;
uint8_t freeToUse8[2];
double pidTvBd;
Expand Down Expand Up @@ -87,7 +91,8 @@ static const sto_data_t itemDefaults PROGMEM = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, // free to use
0, // STO_ITEM_USE_PID_BD
AGGBKP, // STO_ITEM_PID_KP_BD
{0xFF, 0xFF}, // free to use
SCALE2_CALIBRATION_FACTOR, //STO_ITEM_SCALE2_CALIBRATION_FACTOR
// {0xFF, 0xFF}, // free to use
AGGBTN, // STO_ITEM_PID_TN_BD
{0xFF, 0xFF}, // free to use
AGGBTV, // STO_ITEM_PID_TV_BD
Expand Down Expand Up @@ -279,6 +284,13 @@ static inline int32_t getItemAddr(sto_item_id_t itemId, uint16_t* maxItemSize =
size = STRUCT_MEMBER_SIZE(sto_data_t,scaleCalibration);
break;

#if SINGLE_HX711 == 0
case STO_ITEM_SCALE2_CALIBRATION_FACTOR:
addr = offsetof(sto_data_t,scale2Calibration );
size = STRUCT_MEMBER_SIZE(sto_data_t,scale2Calibration);
break;
#endif

case STO_ITEM_SCALE_KNOWN_WEIGHT:
addr = offsetof(sto_data_t,scaleKnownWeight );
size = STRUCT_MEMBER_SIZE(sto_data_t,scaleKnownWeight);
Expand Down
3 changes: 3 additions & 0 deletions src/Storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ typedef enum
STO_ITEM_STANDBY_MODE_ON, // Enable tandby mode
STO_ITEM_STANDBY_MODE_TIME, // Time until heater is turned off
STO_ITEM_SCALE_CALIBRATION_FACTOR, // Calibration factor for scale
#if SINGLE_HX711 == 0
STO_ITEM_SCALE2_CALIBRATION_FACTOR, // Calibration factor for scale 2
#endif
STO_ITEM_SCALE_KNOWN_WEIGHT, // Calibration weight for scale
STO_ITEM_RESERVED_30, // reserved
STO_ITEM_RESERVED_21, // reserved
Expand Down
9 changes: 4 additions & 5 deletions src/brewvoid.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ boolean brewPIDDisabled = false; // is PID disabled for delay
const unsigned long intervalWeight = 200; // weight scale
unsigned long previousMillisScale; // initialisation at the end of init()
HX711_ADC LoadCell(PIN_HXDAT, PIN_HXCLK);
#if SINGLE_HX711 == 0
HX711_ADC LoadCell2(PIN_HXDAT2, PIN_HXCLK);
#endif
#endif

/**
Expand Down Expand Up @@ -439,11 +442,7 @@ void brew() {
break;

case 41: // waiting time brew
if (timeBrewed > totalBrewTime || (weightBrew > (weightSetpoint - scaleDelayValue))) {
brewCounter = kBrewFinished;
}

if (timeBrewed > totalBrewTime) {
if (weightBrew > (weightSetpoint - scaleDelayValue)) {
brewCounter = kBrewFinished;
}

Expand Down
3 changes: 3 additions & 0 deletions src/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ int writeSysParamsToStorage(void);
#define TEMPOFFSET 0 // brew temperature setpoint
#define STEAMSETPOINT 120 // steam temperature setpoint
#define SCALE_CALIBRATION_FACTOR 1.00 // Raw data is divided by this value to convert to readable data
#if SINGLE_HX711 == 0
#define SCALE2_CALIBRATION_FACTOR 1.00 // Raw data is divided by this value to convert to readable data
#endif
#define SCALE_KNOWN_WEIGHT 267.00 // Calibration weight for scale (weight of the tray)
#define AGGKP 62 // PID Kp (regular phase)
#define AGGTN 52 // PID Tn (regular phase)
Expand Down
6 changes: 3 additions & 3 deletions src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ void displayShottimer(void) {
u8g2.print(timeBrewed / 1000, 1);
u8g2.print("s");
u8g2.setCursor(64, 38);
u8g2.print(weightBrew, 0);
u8g2.print(weightBrew, 1);
u8g2.print("g");
u8g2.setFont(u8g2_font_profont11_tf);
displayWaterIcon(119, 1);
Expand All @@ -261,8 +261,8 @@ void displayShottimer(void) {
u8g2.print(lastBrewTime/1000, 1);
u8g2.print("s");
u8g2.setCursor(64, 38);
u8g2.print(weightBrew, 0);
u8g2.print(" g");
u8g2.print(weightBrew, 1);
u8g2.print("g");
u8g2.setFont(u8g2_font_profont11_tf);
displayWaterIcon(119, 1);
u8g2.sendBuffer();
Expand Down
48 changes: 42 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ hw_timer_t *timer = NULL;
#endif

#if (BREWMODE == 2 || ONLYPIDSCALE == 1)
#define HX711_ADC_config_h
#define SAMPLES 32
#define IGN_HIGH_SAMPLE 1
#define IGN_LOW_SAMPLE 1
#define SCK_DELAY 1
#define SCK_DISABLE_INTERRUPTS 0
#include <HX711_ADC.h>
#endif

Expand Down Expand Up @@ -177,6 +183,9 @@ double brewTempOffset = TEMPOFFSET;
double setpoint = brewSetpoint;
double steamSetpoint = STEAMSETPOINT;
float scaleCalibration = SCALE_CALIBRATION_FACTOR;
#if SINGLE_HX711 == 0
float scale2Calibration = SCALE_CALIBRATION_FACTOR;
#endif
float scaleKnownWeight = SCALE_KNOWN_WEIGHT;
uint8_t usePonM = 0; // 1 = use PonM for cold start PID, 0 = use normal PID for cold start
double steamKp = STEAMKP;
Expand Down Expand Up @@ -241,6 +250,9 @@ SysPara<double> sysParaWeightSetpoint(&weightSetpoint, WEIGHTSETPOINT_MIN, WEIGH
SysPara<uint8_t> sysParaStandbyModeOn(&standbyModeOn, 0, 1, STO_ITEM_STANDBY_MODE_ON);
SysPara<double> sysParaStandbyModeTime(&standbyModeTime, STANDBY_MODE_TIME_MIN, STANDBY_MODE_TIME_MAX, STO_ITEM_STANDBY_MODE_TIME);
SysPara<float> sysParaScaleCalibration(&scaleCalibration, -100000, 100000, STO_ITEM_SCALE_CALIBRATION_FACTOR);
#if SINGLE_HX711 == 0
SysPara<float> sysParaScale2Calibration(&scale2Calibration, -100000, 100000, STO_ITEM_SCALE2_CALIBRATION_FACTOR);
#endif
SysPara<float> sysParaScaleKnownWeight(&scaleKnownWeight, 0, 2000, STO_ITEM_SCALE_KNOWN_WEIGHT);

// Other variables
Expand Down Expand Up @@ -2008,7 +2020,7 @@ void setup() {
};

editableVars["SCALE_CALIBRATION"] = {
.displayName = F("Calibration factor"),
.displayName = F("Calibration factor scale 1"),
.hasHelpText = false,
.helpText = "",
.type = kFloat,
Expand All @@ -2019,6 +2031,21 @@ void setup() {
.maxValue = 100000,
.ptr = (void *)&scaleCalibration
};

#if SINGLE_HX711 == 0
editableVars["SCALE2_CALIBRATION"] = {
.displayName = F("Calibration factor scale 2"),
.hasHelpText = false,
.helpText = "",
.type = kFloat,
.section = sScaleSection,
.position = 32,
.show = [] { return true; },
.minValue = -100000,
.maxValue = 100000,
.ptr = (void *)&scale2Calibration
};
#endif
#endif

editableVars["VERSION"] = {
Expand Down Expand Up @@ -2062,6 +2089,9 @@ void setup() {
if (ONLYPIDSCALE == 1 || BREWMODE == 2) {
mqttVars["weightSetpoint"] = []{ return &editableVars.at("SCALE_WEIGHTSETPOINT"); };
mqttVars["scaleCalibration"] = []{ return &editableVars.at("SCALE_CALIBRATION"); };
#if SINGLE_HX711 == 0
mqttVars["scale2Calibration"] = []{ return &editableVars.at("SCALE2_CALIBRATION"); };
#endif
mqttVars["scaleKnownWeight"] = []{ return &editableVars.at("SCALE_KNOWN_WEIGHT"); };
mqttVars["tareON"] = []{ return &editableVars.at("TARE_ON"); };
mqttVars["calibrationON"] = []{ return &editableVars.at("CALIBRATION_ON"); };
Expand Down Expand Up @@ -2219,6 +2249,11 @@ void setup() {

temperature -= brewTempOffset;

// Init Scale by BREWMODE 2 or SHOTTIMER 2
#if (BREWMODE == 2 || ONLYPIDSCALE == 1)
initScale();
#endif

// Initialisation MUST be at the very end of the init(), otherwise the
// time comparision in loop() will have a big offset
unsigned long currentTime = millis();
Expand All @@ -2236,11 +2271,6 @@ void setup() {
previousMillisPressure = currentTime;
#endif

// Init Scale by BREWMODE 2 or SHOTTIMER 2
#if (BREWMODE == 2 || ONLYPIDSCALE == 1)
initScale();
#endif

setupDone = true;

enableTimer1();
Expand Down Expand Up @@ -2648,6 +2678,9 @@ int readSysParamsFromStorage(void) {
if (sysParaStandbyModeOn.getStorage() != 0) return -1;
if (sysParaStandbyModeTime.getStorage() != 0) return -1;
if (sysParaScaleCalibration.getStorage() != 0) return -1;
#if SINGLE_HX711 == 0
if (sysParaScale2Calibration.getStorage() != 0) return -1;
#endif
if (sysParaScaleKnownWeight.getStorage() != 0) return -1;

return 0;
Expand Down Expand Up @@ -2686,6 +2719,9 @@ int writeSysParamsToStorage(void) {
if (sysParaStandbyModeOn.setStorage() != 0) return -1;
if (sysParaStandbyModeTime.setStorage() != 0) return -1;
if (sysParaScaleCalibration.setStorage() != 0) return -1;
#if SINGLE_HX711 == 0
if (sysParaScale2Calibration.setStorage() != 0) return -1;
#endif
if (sysParaScaleKnownWeight.setStorage() != 0) return -1;

return storageCommit();
Expand Down
Loading

0 comments on commit 4c5fb23

Please sign in to comment.