我正在创建一个系统,每次温度传感器超出极限时都会发送文本。我只需要发送一次此文本,但它一直在发送。
法典:
if(temp > (userTemp + 5.00))
{
ledState2=1;
device.send("led2", ledState2);
local smsState = 0; //State, if sms has been sent yet or not
if(smsState==0)
{
smsState=1;
//This is where the sms script will be put
server.log("SMS should send: " + smsState);
}
}
输出:
2014-11-20 10:12:58 UTC+0 [Device] Set RED LED: 1
2014-11-20 10:13:08 UTC+0 [Device] 22.3245
2014-11-20 10:13:08 UTC+0 [Agent] SMS should send: 1
2014-11-20 10:13:09 UTC+0 [Device] Set RED LED: 1
2014-11-20 10:13:18 UTC+0 [Device] 22.2814
2014-11-20 10:13:18 UTC+0 [Agent] SMS should send: 1
2014-11-20 10:13:19 UTC+0 [Device] Set RED LED: 1
2014-11-20 10:13:28 UTC+0 [Device] 22.3245
2014-11-20 10:13:28 UTC+0 [Agent] SMS should send: 1
2014-11-20 10:13:29 UTC+0 [Device] Set RED LED: 1
2014-11-20 10:13:38 UTC+0 [Device] 22.2814
2014-11-20 10:13:39 UTC+0 [Agent] SMS should send: 1
2014-11-20 10:13:39 UTC+0 [Device] Set RED LED: 1
2014-11-20 10:13:48 UTC+0 [Device] 22.3245
2014-11-20 10:13:49 UTC+0 [Agent] SMS should send: 1
2014-11-20 10:13:49 UTC+0 [Device] Set RED LED: 1
2014-11-20 10:13:58 UTC+0 [Device] 22.2814
2014-11-20 10:13:59 UTC+0 [Agent] SMS should send: 1
2014-11-20 10:13:59 UTC+0 [Device] Set RED LED: 1
2014-11-20 10:14:08 UTC+0 [Device] 22.3029
2014-11-20 10:14:09 UTC+0 [Agent] SMS should send: 1
我不明白为什么会继续发送服务器.log当我输入应该只运行一次的 smsState if 语句时,因为我将 smsState 更改为 1这是在电动小鬼上,如果这改变了什么,但我认为它不会
这很简单,真的。只需添加一个变量来跟踪语句是否已运行。
local didSend = 0;
if(temp > (userTemp + 5.00) && !didSend)
{
didSend = 1;
ledState2=1;
device.send("led2", ledState2);
local smsState = 0; //State, if sms has been sent yet or not
if(smsState==0)
{
smsState=1;
//This is where the sms script will be put
server.log("SMS should send: " + smsState);
}
}
现在,if 语句将不会再次运行,直到您再次将 didSend 更改回 0。