This is part of a series of posts related to My Internet of Things and MobileFirst adventure. An index to all posts can be found at the end of the first post.
In previous posts, I have setup the hardware and software on my Raspberry Pi. Now it is time to create a Node-RED flow to manage my hardware.
Node-RED flow editor
Node-RED’s flow editor is browser based. Once you have Node.js running with the Node-RED package installed, browse to your flow editor at http://your.pi.address:1880. The Node-RED flow editor is quite simple to use. On the left is a palette of nodes that can be added to your flow. In the center is the canvas where you will create your flow. On the right you see two tabs. The Info tab will display documentation for any selected node. The Debug tab will become really important shortly as we start putting together a flow and want to see what is going on.
The first flow I created was a simple one to verify my Node-RED environment was seeing the button state changes.
- I dragged the rpi-gpio in node to the canvas. This node isn’t in the out-of-the-box Node-RED but was added by the node-red-contrib-gpio node I added. Note all the information about the node displayed in the Info tab on the right.
- Double-click the node to open its configuration dialog. Set the values as below and close it
- Drag the debug output node to the canvas and connect them as below.
The blue dots on the nodes indicate that these changes have not yet been deployed.
- Select the debug tab on the right side of the screen.
- Click the red Deploy button in the upper right. You should see a ‘0’ payload displayed in the debug window. The input node was configured to read the initial state of the pin when the flow was deployed. That caused a message to be sent from the input pin node to the debug node which displayed the payload in the debug window.
- Press and release the button on the breadboard. You should see a transition to a ‘1’, and then a transition back to ‘0’.
Next I wanted to create a simple flow that would enable me to manually control the LED state from within Node-RED.
- Drag the Inject node onto the canvas. The inject node enables you to manually interact with the flow. By default, it simply sends the current timestamp as a payload in the outgoing message.
- Drag the Trigger node onto the canvas. Change the node’s settings to send a 1 then wait for 2 seconds before sending the 0.
- Finally, drag an rpi-gpio out node to the canvas and configure it for pin 22.
- Connect the nodes together like this:
- Deploy the flow.
- Click the little tab on the left side of the Inject node. Your LED should light for 2 seconds, then go off.
Now, let’s combine them into a single flow.
- Connect the output of the Pin 12 node to the input of the trigger node.
Now the LED can be triggered by either clicking on the timestamp node or by pressing the pushbutton connected to pin 12.
So now I have a Raspberry Pi with a pushbutton and LED that I’m controlling with Node-RED. In the next post, I will install the camera and see what I need to do to control it with a Node-RED flow.