Always showing low temp alarm and it doesn't return to normal

Hello again,

I’m struggling to see my mistake. Basically using a temp sensor and slider to create a low setpoint. When my project runs, I’m always getting a low alarm. Despite the temp coming back into the normal range I cannot loose my alarm.

I really can’t see what I’m doing wrong.

Would really appreciate another eye.
Thanks Joe


/********************************************************************/
// First we include the libraries
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS 2  // on pin 4 of wemos d1 using virtual pin 0 for readout on app
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/ 

// Widget colors for alerts
#define ALERT_COLOR_OK   "#23C48E" // Green
#define ALERT_COLOR_LOW  "#5F7CD8" // Dark Blue
#define ALERT_COLOR_HIGH "#D3435C" // Red

// Alert messages
char alertMessageLow[] = "Temperature is LOW!";
char alertMessageHigh[] = "Temperature is HIGH!";

int alertTempLow, alertTempHigh;
boolean lowAlertOn = false;
boolean highAlertOn = false;

// Hysteresis in degrees for alerts
#define ALERT_HYSTERESIS 1

BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,60), 8080);
  sensors.begin();
  // Setup a function to be called every second
  timer.setInterval(1000L, TempSensorRead);
}

float currentTemp = 0;

void TempSensorRead(){
sensors.requestTemperatures();
Blynk.virtualWrite(V0, sensors.getTempCByIndex(0));

// Low temperature alerts
    if ((currentTemp <= alertTempLow) && !lowAlertOn) {
      Blynk.setProperty(V0, "color", ALERT_COLOR_LOW);
      Blynk.notify(alertMessageLow);
      lowAlertOn = true;
    }
    else if (lowAlertOn && (currentTemp > alertTempLow + ALERT_HYSTERESIS)) {
      Blynk.setProperty(V0, "color", ALERT_COLOR_OK);
      lowAlertOn = false;
    }

}

BLYNK_WRITE(V1) {
  alertTempLow = param.asInt();
  Blynk.virtualWrite(V2, alertTempLow);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  
}

I think it’s your currentTemp value.
You’re setting it to zero (I’d move that line of code up so that it’s with your other declarations) then it never changes.

I’d say that this:

void TempSensorRead(){
  sensors.requestTemperatures();
  Blynk.virtualWrite(V0, sensors.getTempCByIndex(0));

should be more like this:

void TempSensorRead(){
  sensors.requestTemperatures();
  currentTemp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(V0, currentTemp);

Pete.

Thanks Peter - what would I be doing with float currentTemp = 0;

Would I just do float currentTemp;?

The float currentTemp = 0; is fine, it’s just that it’s “floating” :grinning: in the middle of your sketch.

You have your global variables defined up here:

int alertTempLow, alertTempHigh;
boolean lowAlertOn = false;
boolean highAlertOn = false;

so just move it up with them, so it doesn’t feel lonely and you can easily find it when you want it.
My personal taste is not to have multiple declarations on each line, and to add some comments to help later debugging and code changes, so my declarations would look something like this:

int alertTempLow;                // Temperature at/below which alerts will be sent
int alertTempHigh;               // Temperature above which alerts will be sent
boolean lowAlertOn = false;      // flag to show that low alert message has been sent via Blynk 
boolean highAlertOn = false;     // flag to show that highalert message has been sent via Blynk 
float currentTemp = 0;           // Current temp reading (if we ever assign it a value :-)
#define ALERT_HYSTERESIS 1       // Hysteresis in degrees for alerts

A quick tip about debugging…
Using serial print statements is a great way to see snapshots of variable values, and to see which branches of for…else statements are being executed.
This is your original code, with some serial print statements added…

void TempSensorRead()
{
  sensors.requestTemperatures();
  Blynk.virtualWrite(V0, sensors.getTempCByIndex(0));

  // Low temperature alerts
  if ((currentTemp <= alertTempLow) && !lowAlertOn)
  {
    Serial.println("We're in branch 1 of the if statement");
    Serial.print("currentTemp = ");
    Serial.println(currentTemp);
    Serial.print("alertTempLow = ");
    Serial.println(alertTempLow);
    Serial.print("lowAlertOn = ");
    Serial.println(lowAlertOn);
    
    Blynk.setProperty(V0, "color", ALERT_COLOR_LOW);
    Blynk.notify(alertMessageLow);
    lowAlertOn = true;
  }
  else if (lowAlertOn && (currentTemp > alertTempLow + ALERT_HYSTERESIS))
  {
    Serial.println("We're in branch 2 of the if statement");    
    Serial.print("lowAlertOn = ");
    Serial.println(lowAlertOn);        
    Serial.print("currentTemp = ");
    Serial.println(currentTemp);
    Serial.print("alertTempLow = ");
    Serial.println(alertTempLow);
    Serial.print("ALERT_HYSTERESIS = ");
    Serial.println(ALERT_HYSTERESIS);
    Blynk.setProperty(V0, "color", ALERT_COLOR_OK);
    lowAlertOn = false;
  }
}

I think it would have shown that ```currentTemp``` is always zero.

Pete.

Thanks again Peter. I will give it a go.

Peter your a star - you’ve solved it for me.

Final code for anyone else to use:

   /********************************************************************/
// First we include the libraries
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
/********************************************************************/
// Data wire is plugged into pin 2 on the Arduino 
#define ONE_WIRE_BUS 2  // on pin 4 of wemos d1 using virtual pin 0 for readout on app
/********************************************************************/
// Setup a oneWire instance to communicate with any OneWire devices  
// (not just Maxim/Dallas temperature ICs) 
OneWire oneWire(ONE_WIRE_BUS);
/********************************************************************/
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
/********************************************************************/ 

// Widget colors for alerts
#define ALERT_COLOR_OK   "#23C48E" // Green
#define ALERT_COLOR_LOW  "#5F7CD8" // Dark Blue
#define ALERT_COLOR_HIGH "#D3435C" // Red

// Alert messages
char alertMessageLow[] = "Temperature is LOW!";
char alertMessageHigh[] = "Temperature is HIGH!";

int alertTempLow;                // Temperature at/below which alerts will be sent
int alertTempHigh;               // Temperature above which alerts will be sent
boolean lowAlertOn = false;      // flag to show that low alert message has been sent via Blynk 
boolean highAlertOn = false;     // flag to show that highalert message has been sent via Blynk 
float currentTemp = 0;           // Current temp reading (if we ever assign it a value :-)
#define ALERT_HYSTERESIS 1       // Hysteresis in degrees for alerts

BlynkTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,60), 8080);
  sensors.begin();
  // Setup a function to be called every second
  timer.setInterval(1000L, TempSensorRead);
}


void TempSensorRead(){
  sensors.requestTemperatures();
  currentTemp = sensors.getTempCByIndex(0);
  Blynk.virtualWrite(V0, currentTemp);

// Low temperature alerts
    if ((currentTemp <= alertTempLow) && !lowAlertOn) {
      Blynk.setProperty(V0, "color", ALERT_COLOR_LOW);
      Blynk.notify(alertMessageLow);
      lowAlertOn = true;
    }
    else if (lowAlertOn && (currentTemp > alertTempLow + ALERT_HYSTERESIS)) {
      Blynk.setProperty(V0, "color", ALERT_COLOR_OK);
      lowAlertOn = false;
    }

}

BLYNK_WRITE(V1) {
  alertTempLow = param.asInt();
  Blynk.virtualWrite(V2, alertTempLow);
}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  
}
1 Like

:star_struck: :star_struck: :star_struck: