How to import subscribe message into C program?

I have a system designed using an ESP32 control board and Node-Red for the web interface. The system has an alarm that can be armed from either the Raspberry Pi running Node-Red or from the ESP32 board. There is an LED on the ESP32 and the Raspberry Pi that blinks fast when the system is armed and slow when unarmed. When I arm the system from the Pi, I can easily change the LED flash rate on the ESP32 by publishing a message that the ESP32 is subscribed to. When arming the system from the ESP32, changing the flash rate on the Pi is not so easy. The system cannot be armed or disarmed with the web interface. That may be a future addition.

I am able to change the Pi LED flash rate when the system is armed from the ESP32. The way I do this is with a C program that is running in the background. When the system is armed from the ESP32, it publishes to topic /esp32/door/arm with a message of either "one" or "two". One indicates armed and two indicates unarmed. The Raspberry Pi is subscribed to this topic and writes the "one" or "two" to a text file. The C program checks this text file every 5 seconds and retrieves the "one" or "two". If the value read is different than the previous value read from the file, it changes the flash rate on the Pi LED.

I just wanted input from anyone here that can think of a better way of doing it than the method I am currently using. The C program running on the Pi controls the Pi flash rate and also monitors for the arm push button being pressed to arm the system. I am using the wiringPi library to control the GPIO pins. Maybe when the subscribed topic comes in, have it run another program and pass the "one" or "two" to the program as an argv argument?? I don't know if this is possible or not.

This seems a bit convoluted not to mention the SD wear factor of writing to file. Why not have node-red subscribe to the topic as you do and simply operate the PI gpio pins from within node-red? The node-red nodes for controlling pins are installed and ready to be used as desired. I'm fairly certain they work perfectly fine.

Steve, yes I am concerned about the SD card wear. The arm/disarm state will not change more that 2 or 3 times a day so I am not writing to the card that often. The Pi system writes to the card a lot more than that. I do read the card every 10 seconds (was 5 seconds) to see if the state has changed. But that won't create wear on the card.

How can I use node-red to flash the light on the Pi pins without creating constant network traffic? I am using node-red to control a couple of GPIO pins for a relay and a buzzer. But that is a 1 or 0 condition unlike a blinking light would be.

You should be able to do everything within node red on the pi.

When MQTT receives the esp32/door/arm message it should store it in the flow context.

If the rapid blink rate is twice a second then set an inject node to inject a message every 0.5 seconds into a function node. The function node does the following:

  • Increments a counter that is stored in the node context
  • Retrieves the arm state from the flow context
  • If armed then sends a message to a trigger node
  • if disarmed and counter is odd then sends a message to a trigger node otherwise no message is sent.

The trigger node flashes the led.

The flows would look something this this:

(MQTT) ---> (Function) # Stores state in flow context

(Inject) ---->(Function) ----> (Trigger) -----> (RPI GPIO)

something like this...

image

[{"id":"ffce2231.6874c","type":"inject","z":"4d4cd124.3da23","name":"fake MQTT - begin blink","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":640,"wires":[["3b584930.bce316"]]},{"id":"3b584930.bce316","type":"change","z":"4d4cd124.3da23","name":"","rules":[{"t":"set","p":"doBlink","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":640,"wires":[[]]},{"id":"e7397459.402988","type":"inject","z":"4d4cd124.3da23","name":"fake MQTT - stop blink","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":680,"wires":[["3b584930.bce316"]]},{"id":"3d50a912.f76966","type":"inject","z":"4d4cd124.3da23","name":"Every 0.2 sec","topic":"","payload":"","payloadType":"date","repeat":"0.2","crontab":"","once":false,"onceDelay":"0.2","x":400,"y":720,"wires":[["79dad5c.94abe2c"]]},{"id":"79dad5c.94abe2c","type":"switch","z":"4d4cd124.3da23","name":"doBlink == true?","property":"doBlink","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":720,"wires":[["2b56d7e7.cb84d8"]]},{"id":"2b56d7e7.cb84d8","type":"debug","z":"4d4cd124.3da23","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":720,"wires":[]}]

Try this:
Screenshot_20190929_121246

[{"id":"ffce2231.6874c","type":"inject","z":"4d4cd124.3da23","name":"fake MQTT - begin blink","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":640,"wires":[["3b584930.bce316"]]},{"id":"3b584930.bce316","type":"change","z":"4d4cd124.3da23","name":"","rules":[{"t":"set","p":"doBlink","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":640,"wires":[[]]},{"id":"e7397459.402988","type":"inject","z":"4d4cd124.3da23","name":"fake MQTT - stop blink","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":680,"wires":[["3b584930.bce316"]]},{"id":"3d50a912.f76966","type":"inject","z":"4d4cd124.3da23","name":"Every 0.2 sec","topic":"","payload":"","payloadType":"date","repeat":"0.2","crontab":"","once":false,"onceDelay":"0.2","x":400,"y":720,"wires":[["79dad5c.94abe2c"]]},{"id":"79dad5c.94abe2c","type":"switch","z":"4d4cd124.3da23","name":"doBlink == true?","property":"doBlink","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":720,"wires":[["2b56d7e7.cb84d8"]]},{"id":"2b56d7e7.cb84d8","type":"debug","z":"4d4cd124.3da23","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":720,"wires":[]}]

Note: I don't have a Pi running Node-Red so haven't tested this.

This one is more flexible in that you can set the blink rates independent of each other.

Screenshot_20190929_124354

[{"id":"ffce2231.6874c","type":"inject","z":"4d4cd124.3da23","name":"fake MQTT - begin blink","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":640,"wires":[["3b584930.bce316"]]},{"id":"3b584930.bce316","type":"change","z":"4d4cd124.3da23","name":"","rules":[{"t":"set","p":"doBlink","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":640,"wires":[[]]},{"id":"e7397459.402988","type":"inject","z":"4d4cd124.3da23","name":"fake MQTT - stop blink","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":680,"wires":[["3b584930.bce316"]]},{"id":"3d50a912.f76966","type":"inject","z":"4d4cd124.3da23","name":"Every 0.2 sec","topic":"","payload":"","payloadType":"date","repeat":"0.2","crontab":"","once":false,"onceDelay":"0.2","x":400,"y":720,"wires":[["79dad5c.94abe2c"]]},{"id":"79dad5c.94abe2c","type":"switch","z":"4d4cd124.3da23","name":"doBlink == true?","property":"doBlink","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":600,"y":720,"wires":[["2b56d7e7.cb84d8"]]},{"id":"2b56d7e7.cb84d8","type":"debug","z":"4d4cd124.3da23","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":720,"wires":[]}]

Thanks SynAckFin and Steve. I will try these methods and report back with the final solution. I like the idea of doing it within node-red and not using a C program to control the LED.

This is what I ended up using. It is based on SynAckFin's last post and works perfectly. Thanks for the help guys.

Flow is attached as someone may find it useful.


[{"id":"d52f6b03.e02748","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"3388a817.9c12","type":"change","z":"d52f6b03.e02748","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"on","fromt":"str","to":"true","tot":"bool"},{"t":"change","p":"payload","pt":"msg","from":"off","fromt":"str","to":"false","tot":"bool"},{"t":"set","p":"doBlink","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":200,"wires":[[]]},{"id":"9e4fd81f.5d6d6","type":"inject","z":"d52f6b03.e02748","name":"Every 0.45 sec","topic":"","payload":"doBlink","payloadType":"flow","repeat":"0.45","crontab":"","once":false,"onceDelay":"0.2","x":280,"y":280,"wires":[["3df6948.a0bb26c"]]},{"id":"3df6948.a0bb26c","type":"switch","z":"d52f6b03.e02748","name":"doBlink == true","property":"doBlink","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":480,"y":280,"wires":[["db761fdc.a189d8"]]},{"id":"f916248f.a98d7","type":"mqtt in","z":"d52f6b03.e02748","name":"","topic":"esp32/door/arm","qos":"2","datatype":"auto","broker":"c3002906.2a182","x":260,"y":200,"wires":[["3388a817.9c12"]]},{"id":"d9a93dec.7985d8","type":"switch","z":"d52f6b03.e02748","name":"doBlink == false","property":"payload","propertyType":"msg","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":480,"y":360,"wires":[["9e308ef9.480578"]]},{"id":"a3bbebac.3fd5e8","type":"rpi-gpio out","z":"d52f6b03.e02748","name":"","pin":"32","set":"","level":"0","freq":"","out":"out","x":860,"y":320,"wires":[]},{"id":"db761fdc.a189d8","type":"trigger","z":"d52f6b03.e02748","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"200","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":680,"y":280,"wires":[["a3bbebac.3fd5e8"]]},{"id":"9e308ef9.480578","type":"trigger","z":"d52f6b03.e02748","op1":"1","op2":"0","op1type":"num","op2type":"str","duration":"600","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":680,"y":360,"wires":[["a3bbebac.3fd5e8"]]},{"id":"8b7dd130.7eacc8","type":"inject","z":"d52f6b03.e02748","name":"Every 1.15 sec","topic":"","payload":"doBlink","payloadType":"flow","repeat":"1.25","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":360,"wires":[["d9a93dec.7985d8"]]},{"id":"c3002906.2a182","type":"mqtt-broker","z":"","name":"MyPi","broker":"172.16.1.112","port":"1883","clientid":"pi","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"home/Relays","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]