Skip to content

Commit

Permalink
catch invalid temperatures from ADC
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-godinez committed Aug 16, 2024
1 parent a912aa0 commit ee756cb
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,13 +451,23 @@ export class OpenSaunaAccessory {

const isRunning =
system === 'sauna' ? this.saunaRunning : this.steamRunning;
const otherSystemRunning =
system === 'sauna' ? this.steamRunning : this.saunaRunning;
const service = this.accessory.getService(`${system}-thermostat`);

this.platform.log.info(
`${system.charAt(0).toUpperCase() + system.slice(1)} Mode Request:`,
value ? 'Heat' : 'Off',
);

if (otherSystemRunning && value === this.platform.Characteristic.TargetHeatingCoolingState.HEAT) {
this.platform.log.warn(
`${system.charAt(0).toUpperCase() + system.slice(1)
} cannot be started because the other system is already running.`,
);
return;
}

if (service) {
service
.getCharacteristic(
Expand Down Expand Up @@ -993,6 +1003,12 @@ export class OpenSaunaAccessory {
private calculateTemperature(adcValue: number, resistanceAt25C: number, bValue: number): number {
const pullUpResistor = 10000; // 10k ohm pull-up resistor

// Avoid division by zero and ensure adcValue is within a valid range
if (adcValue <= 0 || adcValue >= 1023) {
this.platform.log.warn(`Invalid adcValue: ${adcValue}`);
return NaN; // Return NaN to indicate an invalid temperature reading
}

// Calculate the resistance of the thermistor based on the ADC value
let resistance = (1023 / adcValue) - 1;
resistance = pullUpResistor / resistance;
Expand All @@ -1005,6 +1021,12 @@ export class OpenSaunaAccessory {
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to Celsius

// Ensure the temperature is a valid number before returning
if (isNaN(steinhart) || !isFinite(steinhart)) {
this.platform.log.warn(`Calculated temperature is invalid: ${steinhart}`);
return NaN;
}

return steinhart;
}

Expand Down

0 comments on commit ee756cb

Please sign in to comment.