1

温度センサーが限界を超えるたびにテキストを送信するシステムを作成しています。このテキストを一度だけ送信する必要がありますが、送信し続けます。

コード:

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

server.log を送信し続ける理由がわかりません smsState を 1 に変更したため、一度だけ実行する必要がある smsState if ステートメントを入力すると、これは電気インプ上にあり、何かが変更されるとは思いませんが、そうは思いません

4

1 に答える 1

2

本当に簡単です。ステートメントが実行されたかどうかを追跡する変数を追加するだけです。

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

これで、didSend を再び 0 に変更するまで、if ステートメントは再び実行されなくなります。

于 2015-09-12T20:26:03.610 に答える