Processing PIR sensor status from Arduino over serial: Timers

Hey All,
I have a PIR sensor attached to an Arduino sending data over serial to a Raspberry Pi to toggle a pin on the Arduino.

I have a flow that works when I use a trigger but when the PIR sensor sends the variable it doesn't work. I suspect this has to do with the fact that the PIR sensor sends a burst of message unlike the trigger which only sends one.

What's strange is when the PIR sensor sends a "1" i.e." turn on", the LED on the Arduino it works as expected. However the timer to turn off the LED doesn't make it back to the Arduino. Remember... this works from the trigger.

What's even stranger is if you look at my flow you'll see that the debug says the "0" i.e. "off message" was sent. I can verify the absence of this message in the Arduino serial monitor and the LED stays on.

How can I make this work properly?

Thanks!
Rich

[
    {
        "id": "956725a29433fd21",
        "type": "delay",
        "z": "cb58b0f2ac83d096",
        "name": "",
        "pauseType": "rate",
        "timeout": "1",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "allowrate": false,
        "outputs": 1,
        "x": 910,
        "y": 300,
        "wires": [
            [
                "b54ea3ccaaedef39",
                "9940544a9d513d71"
            ]
        ]
    },
    {
        "id": "116fa7f1d08b7242",
        "type": "function",
        "z": "cb58b0f2ac83d096",
        "name": "change payload to q",
        "func": "msg.payload = 'q';\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 300,
        "wires": [
            [
                "956725a29433fd21"
            ]
        ]
    },
    {
        "id": "b54ea3ccaaedef39",
        "type": "debug",
        "z": "cb58b0f2ac83d096",
        "name": "debug q",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1260,
        "y": 280,
        "wires": []
    },
    {
        "id": "9940544a9d513d71",
        "type": "serial out",
        "z": "cb58b0f2ac83d096",
        "name": "tell arduino what to do",
        "serial": "71aef95893b7db0d",
        "x": 1220,
        "y": 160,
        "wires": []
    },
    {
        "id": "52aba5ba36223615",
        "type": "switch",
        "z": "cb58b0f2ac83d096",
        "name": "aptPirPin2Status",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "aptPirPin2Status1",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 410,
        "y": 340,
        "wires": [
            [
                "79464535266ebe49",
                "116fa7f1d08b7242",
                "d8f4fe565f3f248f"
            ]
        ]
    },
    {
        "id": "5bb44f5b665759ac",
        "type": "ui_button",
        "z": "cb58b0f2ac83d096",
        "name": "",
        "group": "3d153cb1a380b89c",
        "order": 2,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Apt On",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "q",
        "payloadType": "str",
        "topic": "payload",
        "topicType": "msg",
        "x": 680,
        "y": 100,
        "wires": [
            [
                "9940544a9d513d71"
            ]
        ]
    },
    {
        "id": "3bdac78cb77c6f27",
        "type": "ui_button",
        "z": "cb58b0f2ac83d096",
        "name": "",
        "group": "3d153cb1a380b89c",
        "order": 5,
        "width": 0,
        "height": 0,
        "passthru": false,
        "label": "Apt Off",
        "tooltip": "",
        "color": "",
        "bgcolor": "",
        "className": "",
        "icon": "",
        "payload": "r",
        "payloadType": "str",
        "topic": "payload",
        "topicType": "msg",
        "x": 670,
        "y": 180,
        "wires": [
            [
                "9940544a9d513d71"
            ]
        ]
    },
    {
        "id": "63589c7ea493610b",
        "type": "inject",
        "z": "cb58b0f2ac83d096",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "r",
        "payloadType": "str",
        "x": 670,
        "y": 140,
        "wires": [
            [
                "9940544a9d513d71"
            ]
        ]
    },
    {
        "id": "419843c17619abcb",
        "type": "inject",
        "z": "cb58b0f2ac83d096",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "q",
        "payloadType": "str",
        "x": 670,
        "y": 60,
        "wires": [
            [
                "9940544a9d513d71"
            ]
        ]
    },
    {
        "id": "48bb5beeafc77d38",
        "type": "delay",
        "z": "cb58b0f2ac83d096",
        "name": "",
        "pauseType": "delay",
        "timeout": "2",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "3",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "allowrate": false,
        "outputs": 1,
        "x": 900,
        "y": 380,
        "wires": [
            [
                "7c349f0e2dc45027",
                "9940544a9d513d71"
            ]
        ]
    },
    {
        "id": "31342365bc543965",
        "type": "inject",
        "z": "cb58b0f2ac83d096",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "aptPirPin2Status1",
        "payloadType": "str",
        "x": 150,
        "y": 220,
        "wires": [
            [
                "52aba5ba36223615"
            ]
        ]
    },
    {
        "id": "faab90feb9a2e928",
        "type": "serial in",
        "z": "cb58b0f2ac83d096",
        "name": "listen to arduino",
        "serial": "71aef95893b7db0d",
        "x": 140,
        "y": 340,
        "wires": [
            [
                "52aba5ba36223615"
            ]
        ]
    },
    {
        "id": "79464535266ebe49",
        "type": "ui_text",
        "z": "cb58b0f2ac83d096",
        "group": "3d153cb1a380b89c",
        "order": 5,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "APT PIR Status",
        "format": "{{msg.payload}}",
        "layout": "row-spread",
        "className": "",
        "x": 1120,
        "y": 340,
        "wires": []
    },
    {
        "id": "d8f4fe565f3f248f",
        "type": "function",
        "z": "cb58b0f2ac83d096",
        "name": "change payload to r",
        "func": "msg.payload = \"r\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 670,
        "y": 380,
        "wires": [
            [
                "48bb5beeafc77d38"
            ]
        ]
    },
    {
        "id": "7c349f0e2dc45027",
        "type": "debug",
        "z": "cb58b0f2ac83d096",
        "name": "debug r",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1280,
        "y": 380,
        "wires": []
    },
    {
        "id": "71aef95893b7db0d",
        "type": "serial-port",
        "serialport": "/dev/ttyS0",
        "serialbaud": "115200",
        "databits": "8",
        "parity": "none",
        "stopbits": "1",
        "waitfor": "",
        "dtr": "none",
        "rts": "none",
        "cts": "none",
        "dsr": "none",
        "newline": "\\n",
        "bin": "bin",
        "out": "char",
        "addchar": "",
        "responsetimeout": "10000"
    },
    {
        "id": "3d153cb1a380b89c",
        "type": "ui_group",
        "name": "LED Switch",
        "tab": "c548a45fe690539c",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "c548a45fe690539c",
        "type": "ui_tab",
        "name": "Home",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": true
    }
]```

Experiment with the Trigger node instead of the Delay node

Test Flow that will disregard any multiple msgs coming from the PIR .. and it will just extend the OFF (r) msg

[{"id":"52aba5ba36223615","type":"switch","z":"54efb553244c241f","name":"aptPirPin2Status","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"aptPirPin2Status1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":550,"y":1800,"wires":[["b97d4f1e191e7c4d"]]},{"id":"31342365bc543965","type":"inject","z":"54efb553244c241f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"aptPirPin2Status1","payloadType":"str","x":350,"y":1800,"wires":[["52aba5ba36223615"]]},{"id":"b97d4f1e191e7c4d","type":"trigger","z":"54efb553244c241f","name":"","op1":"q","op2":"r","op1type":"str","op2type":"str","duration":"5","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":740,"y":1800,"wires":[["c381cac632325bb6"]]},{"id":"c381cac632325bb6","type":"debug","z":"54efb553244c241f","name":"debug 29","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":880,"y":1800,"wires":[]}]

Yes! Much more elegant. I'm loving Node-Red!

After15 years of coding for Arduino this is refreshing.

Thanks for the help!
Rich

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.