我正在创建这个温度监控系统…因此,我想在温度低于6度时收到消息/警报,当温度回到6度以上时再次收到消息/警报。注意:我不希望警报(sendMailNormalValue():wink:当系统启动....时出现如何停用sendMailNormalValue();只有当温度低于6时才激活它(只有当温度高于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();
}
}
}
我想我明白你的意思了。发生错误(temp <6),你想每30秒发送一封电子邮件;当温度达到6或模式,发送一个电子邮件,说错误条件已被修复。在启动时,只有在出错时才发送电子邮件。
如果是这样,您需要使用全局标志来跟踪错误条件。
// ...
bool tempError; // initialized in setup()
void setup()
{
// ...
float t;
for(;;) // loop until we get a valid reading.
{
t = sensors.getTempCByIndex(0);
if (!isnan(t))
break;
Serial.println("Failed to read from sensor !");
delay(500);
}
tempError = (t < 6);
}
void loop()
{
// read temp and check sensor...
// ...
if (t < 6)
{
tempError = true;
// send error email, set timer, etc...
}
else if (tempError)
{
// temp is now fine, after being too low.
tempError = false; // Clear error flag.
// send OK email, only once.
// Don't forget to clear the 30 second email timer !!!
}
}
通常,你希望在警报系统中有一些滞后。你应该考虑在温度"固定"后等几秒钟再发送"OK"电子邮件。否则,当温度在6度左右时,你可能会收到很多失败/修复的邮件。这通常是通过一个简单的状态机引擎完成的,但这超出了这个问题的范围。