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() 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();
}
}
}
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
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()