I am creating this temp monitoring system

I am creating this temp monitoring system...Therefore, i want to get messages/alerts when the temperature are below 6 and again when they come back to above 6. Note: I don't want the alert (sendMailNormalValue():wink: to come when the system boots up....How do I deactivate the sendMailNormalValue(); and activate it only when the temp are below 6 (only to alert me when comes back to above 6)..


#include <OneWire.h>
#include <DallasTemperature.h>

// GPIO where the DS18B20 is connected to
const int oneWireBus = 5;     

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor 
DallasTemperature sensors(&oneWire);
//========================================
float t;
int period = 30000;
unsigned long time_now = 0;
bool DisplayFirstTime = true;

int period1 = 30000;
unsigned long time_now1 = 0;

void sendMailBelowValue(){
  
  }
  
void sendMailNormalValue(){
  
  }
void setup() {
// put your setup code here, to run once:
Serial.begin (9600);

}

void loop() {
// put your main code here, to run repeatedly:
sensors.requestTemperatures(); 
  t = sensors.getTempCByIndex(0);
  float p=t-2;
  // Check if any reads failed and exit early (to try again).
  if (isnan(t)) {
    Serial.println("Failed to read from sensor !");
    delay(500);
    return;
  }

  if (t>6){
    if(millis() > time_now + period){
    time_now = millis();
    sendMailNormalValue();
    Serial.print ("You got messagae");
    }
  }
if (t<6){
    if(millis() > time_now1 + period1 || DisplayFirstTime){
      DisplayFirstTime = false;
    time_now = millis();
    sendMailBelowValue();
    }
   }
}

What is the largest number you can have in an int (integer)


Only read the temperature once in a while, say 1-5 seconds.

Not sure but ...But I am sure that only 3 min.

https://www.arduino.cc/reference/en/language/variables/data-types/int/

This is my question. I do not want to send the alert once the system starts up

Thanks for this noted

You set a Flag variable to false at power up.

Set it to true when messages are allowed to be sent as per your decision.

The Flag variable is used as a qualifier to sending messages.

Please help doing that in the code attached...It will be helpful...
I just want it to be sent to notify the system is fine or back to normal after the error

Note:

The DS18B20 takes a bit of time to get your temperature.

See the examples that come with the IDE how to get temperature after a wait period.

@LarryD can you help on how to do that in the code?

After supper :slight_smile:

Ok.. Thanks in advance

The IDE examples are written in code.

@aarg haha.. Ok. Help edit the code to do what I have explained above

This should be a good starting point you can build on:

#include <OneWire.h>
#include <DallasTemperature.h>     //library from temp sensor 18b20

//DS18B20 pin number
const byte oneWireBus             = 5;

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

#define ENABLED                     true
#define DISABLED                    false

bool highFlag                     = ENABLED;
bool lowFlag                      = ENABLED;

const byte heartbeatLED           = 13;

int period                        = 30000;
int period1                       = 30000;

float currentTemperature;

const float lowTrigger            = 24.0;
const float highTrigger           = 24.5;

unsigned long time_now1           = 0;

//timing stuff
unsigned long heartbeatMillis;
unsigned long tempMillis;

unsigned long time_now;



//***********************************************************************
void sendMailBelowValue()
{
}


//***********************************************************************
void sendMailNormalValue()
{
}


//***********************************************************************
void setup()
{
  // put your setup code here, to run once:
  Serial.begin (9600);

  pinMode(heartbeatLED, OUTPUT);

  sensors.begin();

  //start a new conversion request
  sensors.setWaitForConversion(false);
  sensors.requestTemperatures();
  sensors.setWaitForConversion(true);

} //END of setup()


//***********************************************************************
void loop()
{
  //*************************************                   heartbeat TIMER
  //used to see if our code is blocking
  //the heartbeat LED should regularly toggle, every 1/2 second
  //is it time to toggle the LED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the LED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }


  //*************************************                   temperature TIMER
  //is it time to read the temperature ?
  if (millis() - tempMillis >= 1000)
  {
    //restart this TIMER
    tempMillis = millis();

    //the last conversion request will be finished by now
    //get the temperature
    currentTemperature = sensors.getTempCByIndex(0);

    Serial.println(currentTemperature, 1);

    //start a new conversion request
    sensors.setWaitForConversion(false);
    sensors.requestTemperatures();
    sensors.setWaitForConversion(true);
  }

  //*************************************
  //have we gone over the HIGH trigger point ?
  if (highFlag == ENABLED && currentTemperature > highTrigger)
  {
    highFlag = DISABLED;
    lowFlag = ENABLED;

    sendMailNormalValue();
    Serial.println("High Trigger");
  }

  //*************************************
  //have we gone under the LOW trigger point ?
  if (lowFlag == ENABLED && currentTemperature < lowTrigger)
  {
    lowFlag = DISABLED;
    highFlag = ENABLED;

    sendMailBelowValue();
    Serial.println("Low Trigger");
  }

  //*************************************
  //other non-blocking code goes here
  //*************************************

} //END of loop()

@mutwirijosh, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

First, have you done this?
"See the examples that come with the IDE how to get temperature after a wait period."
?

Unless you add some hysteresis, you are going to have an avalanche of alerts when the temperature is in the whereabouts of 6.

That's actually worse than lack of hysteresis. Negative hysteresis, perhaps.