Skip to content

Commit

Permalink
set desired water temperature without interrupting MQTT connection
Browse files Browse the repository at this point in the history
- PureSpaIO::setDesiredWaterTempCelsius: read back actual setpoint
- PureSpaIO::decodeButton: cancel temp up/down if buzzer is on
- PureSpaIO::decodeButton: reduced reply pulse length from 3 to 2 µs
- PureSpaIO::PRESS_COUNT: reduced from 21 to 19 button frames
  • Loading branch information
jnsbyr committed Aug 4, 2023
1 parent a1956e1 commit eae1344
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 36 deletions.
111 changes: 78 additions & 33 deletions PureSpaIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,20 +415,15 @@ void PureSpaIO::setDesiredWaterTempCelsius(int temp)
{
if (isPowerOn() && state.error == ERROR_NONE)
{
// try to get initial temp
#ifdef FORCE_WIFI_SLEEP
// try to get initial temp
WiFi.forceSleepBegin();
#endif

int setTemp = getDesiredWaterTempCelsius();
//DEBUG_MSG("\nBset %d", setTemp);
bool modifying = false;
if (setTemp == UNDEF::INT)
{
// trigger temp modification
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
#endif
changeWaterTemp(-1);
modifying = true;

Expand All @@ -441,18 +436,13 @@ void PureSpaIO::setDesiredWaterTempCelsius(int temp)
setTemp = getDesiredWaterTempCelsius();
tries--;
} while (setTemp == UNDEF::INT && tries);
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#endif

// check success
if (setTemp == UNDEF::INT)
{
// error, abort
#ifdef FORCE_WIFI_SLEEP
WiFi.forceSleepWake();
delay(1);
#endif
DEBUG_MSG("\naborted\n");
return;
}
Expand All @@ -467,13 +457,7 @@ void PureSpaIO::setDesiredWaterTempCelsius(int temp)
if (deltaTemp > 0)
{
//DEBUG_MSG("\nBU");
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
#endif
changeWaterTemp(1);
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#endif
if (modifying)
{
deltaTemp--;
Expand All @@ -483,13 +467,7 @@ void PureSpaIO::setDesiredWaterTempCelsius(int temp)
else
{
//DEBUG_MSG("\nBD");
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
#endif
changeWaterTemp(-1);
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#endif
if (modifying)
{
deltaTemp++;
Expand All @@ -499,9 +477,56 @@ void PureSpaIO::setDesiredWaterTempCelsius(int temp)
modifying = true;
}

#ifdef FORCE_WIFI_SLEEP
WiFi.forceSleepWake();
delay(1);
#else
// trigger temp modification
changeWaterTemp(-1);

int sleep = 100; // ms
int changeTries = WATER_TEMP::SET_MAX - WATER_TEMP::SET_MIN + 3;
int setTemp = UNDEF::INT;
do
{
// wait for temp readback (will take 2-3 blink durations)
int readTries = 5*BLINK::PERIOD/sleep;
int newSetTemp = UNDEF::INT;
ESP.wdtFeed();
delay(BLINK::PERIOD);
do
{
delay(sleep);
newSetTemp = getDesiredWaterTempCelsius();
readTries--;
} while (newSetTemp == setTemp && readTries);
DEBUG_MSG("\nst:%d (rt:%d)", newSetTemp, readTries);

// check success
if (newSetTemp == UNDEF::INT)
{
// error, abort
DEBUG_MSG("\naborted\n");
return;
}
else
{
// success, check direction and change
setTemp = newSetTemp;
if (temp > setTemp)
{
if (!changeWaterTemp(+1))
changeWaterTemp(+1);
changeTries--;
}
else if (temp < setTemp)
{
if (!changeWaterTemp(-1))
changeWaterTemp(-1);
changeTries--;
}
}
} while (temp != setTemp && changeTries);
DEBUG_MSG("\ncT:%d", changeTries);
#endif
}
}
Expand Down Expand Up @@ -662,11 +687,14 @@ bool PureSpaIO::waitBuzzerOff() const
*/
bool PureSpaIO::changeWaterTemp(int up)
{
if (isPowerOn() == true && state.error == ERROR_NONE)
if (isPowerOn() && state.error == ERROR_NONE)
{
// perform button action
waitBuzzerOff();
//DEBUG_MSG("\nP ");
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
#endif
int tries = BUTTON::ACK_TIMEOUT/BUTTON::ACK_CHECK_PERIOD;
if (up > 0)
{
Expand All @@ -686,6 +714,9 @@ bool PureSpaIO::changeWaterTemp(int up)
tries--;
}
}
#ifndef FORCE_WIFI_SLEEP
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#endif

if (tries && state.buzzer)
{
Expand Down Expand Up @@ -722,9 +753,9 @@ int PureSpaIO::convertDisplayToCelsius(uint32 value) const
ICACHE_RAM_ATTR void PureSpaIO::clockRisingISR(void* arg)
{
bool data = !digitalRead(PIN::DATA);
bool enable = digitalRead(PIN::LATCH) == LOW;
bool enabled = !digitalRead(PIN::LATCH);

if (enable || isrState.receivedBits == (FRAME::BITS - 1))
if (enabled || isrState.receivedBits == (FRAME::BITS - 1))
{
isrState.frameValue = (isrState.frameValue << 1) + data;
isrState.receivedBits++;
Expand Down Expand Up @@ -1133,17 +1164,31 @@ ICACHE_RAM_ATTR inline void PureSpaIO::decodeButton()
//DEBUG_MSG("U");
if (buttons.toggleTempUp)
{
isrState.reply = true;
buttons.toggleTempUp--;
if (state.buzzer)
{
buttons.toggleTempUp = 0;
}
else
{
isrState.reply = true;
buttons.toggleTempUp--;
}
}
}
else if (isrState.frameValue & FRAME_BUTTON::TEMP_DOWN)
{
//DEBUG_MSG("D");
if (buttons.toggleTempDown)
{
isrState.reply = true;
buttons.toggleTempDown--;
if (state.buzzer)
{
buttons.toggleTempDown = 0;
}
else
{
isrState.reply = true;
buttons.toggleTempDown--;
}
}
}
#ifdef MODEL_SJB_HS
Expand Down Expand Up @@ -1176,11 +1221,11 @@ ICACHE_RAM_ATTR inline void PureSpaIO::decodeButton()
if (isrState.reply)
{
// delay around 5 µs relative to rising edge of latch signal before pulsing
// pulse should be around 2 µs and must be completed before next falling edge of clock
// pulse should be around 2 µs and MUST be completed BEFORE next falling edge of clock
#if F_CPU == 160000000L
delayMicroseconds(1);
pinMode(PIN::DATA, OUTPUT);
delayMicroseconds(3);
delayMicroseconds(2);
pinMode(PIN::DATA, INPUT);
#else
#error 160 MHz CPU frequency required! Pulse timing not possible at 80 MHz, because the code above takes too long to reach this point.
Expand Down
2 changes: 1 addition & 1 deletion PureSpaIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class PureSpaIO
class BUTTON
{
public:
static const unsigned int PRESS_COUNT = BLINK::PERIOD/CYCLE::PERIOD - 2; // must be long enough to trigger, but short enough to avoid double trigger
static const unsigned int PRESS_COUNT = BLINK::PERIOD/CYCLE::PERIOD - 4; // must be long enough to trigger, but short enough to avoid double trigger
static const unsigned int ACK_CHECK_PERIOD = 10; // ms
static const unsigned int ACK_TIMEOUT = 2*PRESS_COUNT*CYCLE::PERIOD; // ms
};
Expand Down
4 changes: 2 additions & 2 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@
// if changing the water temperature setpoint does not work reliably, commenting
// in the following option may improve the behaviour at the cost of a short
// disconnect to the MQTT server
#define FORCE_WIFI_SLEEP
//#define FORCE_WIFI_SLEEP

//#define SERIAL_DEBUG

/*****************************************************************************/

namespace CONFIG
{
const char WIFI_VERSION[] = "1.0.5.2"; // 23.06.2023
const char WIFI_VERSION[] = "1.0.6.0"; // 23.07.2023

// WiFi parameters
const unsigned long WIFI_MAX_DISCONNECT_DURATION = 900000; // [ms] 5 min until reboot
Expand Down

0 comments on commit eae1344

Please sign in to comment.