The SmartEdge IIOT GatewaySmartEdge IIOT Gateway is a Raspberry Pi 3 Compute based industrial Linux box. It runs Raspbian with some additions. There's a set of industrial hardware extensions. In this blog I revisit the industrial I/O. I create a Node-RED flow that controls the inputs and outputs. |
The industrial I/Os are controlled via the file system. I built 2 flows and an init subflow in Node-RED to exercise the system.
Two pins of DIGIO A are used. I1 as input. O1 as output.
Here's how they are wired:
Output flow
The process has two "buttons". They are Node-RED Inject nodes
Pressing a button sends 1 or 0 to the file that serves as interface for O1. That results in Industrial I/O O1 being activated or deactivated.
I'm showing the top Inject node here. The other node is the same, but generates a 0.
The payload is forwarded to a file node:
For info on the filenames used in this post, refer to the very first blog post of this series.
These are the commands executed, if you'd do this from the Linux prompt:
#switch O1 sudo echo 1 > /sys/class/gpio/gpio201/value sudo echo 0 > /sys/class/gpio/gpio201/value
Input flow
The "read" node is an Inject that does not add messages to the process. It's our trigger.
When pressing it, the I1 interface file gets read, and the output is logged to the debug window.
The two others nodes only have their name set. They do their default job.
In Linux, you'd execute:
#read I1 cat /sys/class/gpio/gpio201/value
Init Subflow
Before using them, the interfaces have to be exported and outputs have to be set.
You can do that at Linux startup if you want. I chose not to depend on that and made an init flow.
It exports the I1 and O1 (explained in the 1st post, including magic numbers and filenames) and sets O1 to output.
In essence: values 201 and 200 are written to the interface export file, and that activates I1 and O1.
Because this action may throw an error when the interface is already exported and buzy, I added a catch to avoid error messages in the debug window.
The output pin O1 is set to output:
written to the pin handler's direction file.
The Linux script would look like:
# enable DIGIO A I1 and O1 echo 200 >/sys/class/gpio/export echo 201 >/sys/class/gpio/export # make O1 an output pin sudo echo out > /sys/class/gpio/gpio201/direction
The Node-RED source
This is the code for flow and subflow. This can be imported in Node-RED.
When you deploy it, you can directly test I1 and O1.
[ {"id":"a17b32e7.2ec35","type":"subflow","name":"Subflow init DIGIO","info":"","category":"","in":[],"out":[],"env":[],"color":"#DDAA99"}, {"id":"d3a2f9b2.f39798","type":"inject","z":"a17b32e7.2ec35","name":"export O1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"201","payloadType":"str","x":200,"y":80,"wires":[["8fa6d153.718b8"]]}, {"id":"8fa6d153.718b8","type":"file","z":"a17b32e7.2ec35","name":"export","filename":"/sys/class/gpio/export","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":390,"y":80,"wires":[[]]}, {"id":"950ca3fe.56b6a","type":"file","z":"a17b32e7.2ec35","name":"direction","filename":"/sys/class/gpio/gpio201/direction","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":400,"y":160,"wires":[[]]}, {"id":"27bb2e8b.92bc52","type":"inject","z":"a17b32e7.2ec35","name":"out O1","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":"0.5","topic":"","payload":"out","payloadType":"str","x":200,"y":160,"wires":[["950ca3fe.56b6a"]]}, {"id":"68535b63.eed274","type":"catch","z":"a17b32e7.2ec35","name":"ignore export if it is buzy","scope":["8fa6d153.718b8"],"uncaught":false,"x":230,"y":200,"wires":[[]]},{"id":"a38d7b40.a85ac8","type":"inject","z":"a17b32e7.2ec35","name":"export I1","props":[{"p":"payload"}, {"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"200","payloadType":"str","x":200,"y":120,"wires":[["8fa6d153.718b8"]]},{"id":"2930abc4.06f094","type":"tab","label":"DIGIO","disabled":false,"info":""},{"id":"f3c94f23.3d446","type":"rpi-gpio out","z":"2930abc4.06f094","name":"DIGIO B O3","pin":"35","set":true,"level":"0","freq":"","out":"out","x":510,"y":720,"wires":[]},{"id":"5db3429d.3d146c","type":"inject","z":"2930abc4.06f094","name":"","props":[{"p":"payload"}, {"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":310,"y":720,"wires":[["f3c94f23.3d446"]]},{"id":"e3d92d70.e3861","type":"inject","z":"2930abc4.06f094","name":"","props":[{"p":"payload"}, {"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":310,"y":780,"wires":[["f3c94f23.3d446"]]},{"id":"4bf82d71.3d7654","type":"comment","z":"2930abc4.06f094","name":"modded to GPIOs","info":"","x":350,"y":660,"wires":[]}, {"id":"c8151694.4bc7d8","type":"comment","z":"2930abc4.06f094","name":"SmartEdge style","info":"","x":340,"y":860,"wires":[]}, {"id":"c65d4e31.7cff8","type":"inject","z":"2930abc4.06f094","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":310,"y":920,"wires":[["9ad9c626.44d138"]]},{"id":"b7e46153.4b49c","type":"inject","z":"2930abc4.06f094","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"str","x":310,"y":980,"wires":[["9ad9c626.44d138"]]}, {"id":"9ad9c626.44d138","type":"file","z":"2930abc4.06f094","name":"DIGIO A O1","filename":"/sys/class/gpio/gpio201/value","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":530,"y":920,"wires":[[]]}, {"id":"8f5ad274.395a1","type":"file in","z":"2930abc4.06f094","name":"DIGIO A I1","filename":"/sys/class/gpio/gpio200/value","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":530,"y":1040,"wires":[["2e3e8462.6c5fec"]]}, {"id":"2e3e8462.6c5fec","type":"debug","z":"2930abc4.06f094","name":"debug window","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":1040,"wires":[]}, {"id":"2335d9e3.e3dee6","type":"inject","z":"2930abc4.06f094","name":"read","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":310,"y":1040,"wires":[["8f5ad274.395a1"]]}, {"id":"29be1a09.ac6376","type":"subflow:a17b32e7.2ec35","z":"2930abc4.06f094","name":"Initialise DIGIO","env":[],"x":540,"y":860,"wires":[]} ]
todo: links to previous blogs...
Top Comments