Node-RED Support for Pimoroni Automation-HAT/pHAT

I’ve recently been looking at how to simplify how non-programmers can start to create home automation solutions and stumbled across Node-RED, something which provides some elements of flow-gramming that was available from Octoblu, (which looks to have fallen out of favour since acquired by Citrix). Along side the software elements which make it easier for the less tech-savvy amongst us to get started, we also have to consider the hardware. Not everyone is ready to pick up a soldering iron, breadboard, components and datasheets and design and build their own solutions. Instead they will turn to what’s available in the marketplace, a large market place where Raspberry Pi based addons exist today. This is where I found Pimoroni to have a solid standing. After building one of their projects with my then 10 year old son, using their Explorer HAT Pro and looking at some of their other offerings I picked up an Automation HAT to experiment with.

Automation HATs

The Automation HAT and it’s smaller brother the Automation pHAT provide a range of simple input and output interfaces which can be used for a variety of tasks needing simple switching, sinking or measuring.

The Automation pHAT offers:

  • 1 x 24V 2A Relay switching
  • 3 x 12-bit 0-24V ADC
  • 3 x 24V buffered inputs
  • 3 x 24V sinking outputs (max 500mA total over all 3 outputs)
  • SPI pins available via a breakout connection.
  • Compatible with: RPi 3B+, RPi3, RPi2, RPI B+, RPi A+, Zero and Zero W

The bigger brother the Automation HAT offers:

  • 3 x 24V 2A Relay switching
  • 3 x 12-bit 0-24V ADC
  • 3 x 24V buffered inputs
  • 3 x 24V sinking outputs (max 500mA total over all 3 outputs)
  • 1 x 12-bit 0-3.3V ADC (via breakout connector)
  • Power, Comms and Warn LED indicators
  • 15 x LED channel indicators
  • SPI, Tx, Rx and Pin #25 via a breakout connector
  • Compatible with: RPi 3B+, RPi3, RPi2, RPI B+, RPi A+, Zero and Zero W

The main difference between two versions, is the addition of 2 extra 24V @ 2A relays on the Automation HAT and a 0-3.3v ADC input, plus of course the size differences.

Controlling the HAT/pHAT is done through the same Python Library.

So this doesn’t control 120V/240V, but it could be an intermediate level of switching upto mains voltage levels, or these HATs could also drive loads which would be more than the standard GPIO pins could support. Such as this 12v Light stick:

Simplified Programming via Node-RED

Whilst Pimoroni have provided some Node-RED modules for some of their HAT’s (such as the Explorer HAT, Blinkt! and Unicorn HAT) sadly the Automation HAT hasn’t benefited from the attention. In addition the Automation HAT Python Library hasn’t had the same updates to the Explorer HAT to make it fully async, however the Explorer HAT Node-RED implementation provided a solid starting point for creating a working solution for the Automation-HAT and pHAT. So that’s what I’ve done.

I’ve release my 1st npm package (ever!) of the node-red-contrib-automation-hat. With the latest source available on GitHub. This initial release supports the main input and output connections, with the exception of those only available via the breakout connections (which would require some soldering for someone to use). However, this does satisfy my main goal to provide the basic interfaces in a Node-RED form for this HAT. Unless there is a strong need, I don’t foresee that I’ll start to rewrite the underlying python library to enable a more efficient solution. Any Python Async programmers want to have a crack at it, please feel free to start here.

The package provides two nodes, one for input events the other for outputs.

Automation-HAT Inputs

The input nodes are triggered when the input changes. In the future it may be necessary to make the analog input trigger threshold configurable, if 10mV is too sensitive for the intended use.

Input Events (Buffered Inputs)

Input events are generated by the three 5v tolerant protected inputs on Automation HAT/pHAT. A message will be sent at start-up as initial values are read, then every time an input changes. The topic is set to automationhat/input.<channel> and the payload is an number that’s either:

  • 1 : indicating the input is HIGH.
  • 0 : indicating the input is LOW.

Analog Events (ADC)

Analog events are generated by the three 0-24v tolerant analog inputs on Automation HAT/pHAT. A message will be sent when the analog value changes by 10mV or more, giving 500 steps of granularity. The topic is set to automationhat/analog.<channel>and the payload is a number ranging from 0 (0v) to 24 (24v):

For example, a topic of automationhat/analog.2 and a payload of 3.33 would indicate that 3.33v is present on Analog 2.

Automation-HAT Outputs

Digital Outputs

The three outputs on Automation HAT are 24V tolerant, sinking outputs. That means you should connect them between your load and ground. They act like a switch down to ground, toggling your load on and off. The outputs are driven by a ULN2003A Darlington Array driver.

Digital outputs are controlled by sending a message with the topic output.<channel>where channel is a number from 1 to 3 (corresponding to the numbers on the Automation HAT board) or a name from “one” to “three” if you prefer.

The payload of the message should be set to:

  • 1 or on or enable or true : to turn the output on.
  • 0 or off or disable or false : to turn the output off.
  • toggle : to toggle the output between each state.

Relays

The three relays on Automation HAT supply both NO (Normally Open) and NC (Normally Closed) terminals. You can use them to switch a single load, or alternate between two. The relays should be placed between the voltage supply and your load. The relays can switch a maximum load of 24V 2A per relay.

Relay outputs are controlled by sending a message with the topic relay.<channel>where channel is a number from 1 to 3 (corresponding to the numbers on the Automation HAT board) or a name from “one” to “three” if you prefer.

The payload of the message should be set to:

  • 1 or on or enable or true : to switch the relay contacts so that COM is connected to NO.
  • 0 or off or disable or false : to switch the relay contacts so that COM is connected to NC.
  • toggle : to toggle the relay from its previous state.

Lights

Automation HAT includes three user-controllable lights: POWER, COMMS and WARN.

Lights are controlled by sending a message with the topic light.<channel> where channel is a the light named on the board “power”, “comms”, or “warn”.

The payload of the message should be set to:

  • 1 or on or enable or true : to enable the automatic lights.
  • 0 or off or disable or false : to disable the automatic lights (lights already illuminated will remain lit).
  • toggle : to toggle the light from its previous state.

Additional Commands

By default the Automation HAT will trigger indicator lights on each input/output automatically.

This behaviour can be controlled by sending a message with the topic auto_lights

The payload of the message should be set to:

  • 1 or on or enable or true : to turn the light on.
  • 0 or off or disable or false : to turn the light off.

Simple Examples

Example 1: Check a URL periodically and turn on the WARN light if it doesn’t return statusCode 200.

Code:

[{"id":"9665b53d.0ab958","type":"rpi-automation-hat out","z":"d83d3ba7.ad3b58","name":"","x":660,"y":280,"wires":[]},{"id":"4c138c65.4d2234","type":"http request","z":"d83d3ba7.ad3b58","name":"","method":"GET","ret":"txt","url":"","x":470,"y":80,"wires":[["76079885.d6b428"]]},{"id":"d3bb7963.cc88d8","type":"inject","z":"d83d3ba7.ad3b58","name":"My Blog","topic":"","payload":"https://www.martinrowan.co.uk","payloadType":"str","repeat":"30","crontab":"","once":true,"onceDelay":"0.1","x":120,"y":80,"wires":[["a2559634.ab9db8"]]},{"id":"a2559634.ab9db8","type":"change","z":"d83d3ba7.ad3b58","name":"","rules":[{"t":"set","p":"url","pt":"msg","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":290,"y":80,"wires":[["4c138c65.4d2234"]]},{"id":"76079885.d6b428","type":"switch","z":"d83d3ba7.ad3b58","name":"Status OK (200)","property":"statusCode","propertyType":"msg","rules":[{"t":"eq","v":"200","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":140,"y":280,"wires":[["48287f97.fb3ca","997847ae.03a008"],["c1cdf658.168268","4abf514b.7e33e"]]},{"id":"c1cdf658.168268","type":"change","z":"d83d3ba7.ad3b58","name":"Turn On - Warn Light","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"light.warn","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":320,"wires":[["9665b53d.0ab958"]]},{"id":"997847ae.03a008","type":"change","z":"d83d3ba7.ad3b58","name":"Turn Off - Warn Light","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"light.warn","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":240,"wires":[["9665b53d.0ab958"]]},{"id":"48287f97.fb3ca","type":"change","z":"d83d3ba7.ad3b58","name":"Turn On - Comms Light","rules":[{"t":"set","p":"payload","pt":"msg","to":"on","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"light.comms","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":200,"wires":[["9665b53d.0ab958"]]},{"id":"4abf514b.7e33e","type":"change","z":"d83d3ba7.ad3b58","name":"Turn Off - Comms Light","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"light.comms","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":360,"wires":[["9665b53d.0ab958"]]}]

Obviously instead of, or as well as triggering the on-board LEDs you could trigger an email, one of the other outputs. It’s also possible to replace the four “change” nodes which build the correct message to send to the Automation HAT node, with a single “function” node where you can use JavaScript to build and send out the necessary messages.

 

Example 2: Automatic Plant Watering System

Note this is a contrived example, not something I’ve actually built.

  • Input sensor: Soil Moisture Sensor (e.g. Sparkfun version). Connected to 5V, GND and ADC-1 on the Automation HAT. Determing the voltage levels to trigger at would require calibration, as per SparkFun guide.
  • Output: 12V Peristaltic Pump (e.g. This Pump). Connected using separate 12V DC supply and switched on via Relay 1 by connecting power between pump and HAT using COM and NO Connections.

The simple program will trigger when the reading from the sensor is lower than a threshold, activating the pump and warn LED for 30 seconds. To allow time for the water to be absorbed and detected by the sensor, the system includes some rate limiting too. It’s likely that a real implementation would need a few more smarts and you might want to also build a dashboard to record levels and activations. A more complete watering system not using this HAT can be found here.

Code:

[{"id":"9665b53d.0ab958","type":"rpi-automation-hat out","z":"d83d3ba7.ad3b58","name":"","x":760,"y":320,"wires":[]},{"id":"eff06e87.5182e","type":"rpi-automation-hat in","z":"d83d3ba7.ad3b58","name":"Analog Automation HAT Inputs","input":false,"analog":true,"x":150,"y":60,"wires":[["c6659f37.54b0f"]]},{"id":"c6659f37.54b0f","type":"switch","z":"d83d3ba7.ad3b58","name":"Pass ADC1","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"automationhat/analog.1","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":60,"wires":[["8960bdf0.fa8b2"]]},{"id":"8960bdf0.fa8b2","type":"switch","z":"d83d3ba7.ad3b58","name":"Check Moisture Level","property":"payload","propertyType":"msg","rules":[{"t":"lt","v":"0.5","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":560,"y":60,"wires":[["e9682136.4fe7e"],["795f0c79.7712b4"]]},{"id":"8dca354b.72a018","type":"change","z":"d83d3ba7.ad3b58","name":"Warn LED","rules":[{"t":"set","p":"topic","pt":"msg","to":"light.warn","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":160,"wires":[["9665b53d.0ab958"]]},{"id":"91e33825.05e638","type":"change","z":"d83d3ba7.ad3b58","name":"Turn off Warn LED","rules":[{"t":"set","p":"payload","pt":"msg","to":"off","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"light.warn","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":170,"y":320,"wires":[["9665b53d.0ab958"]]},{"id":"7d173d5b.0da224","type":"change","z":"d83d3ba7.ad3b58","name":"Run Pump","rules":[{"t":"set","p":"topic","pt":"msg","to":"relay.1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":240,"wires":[["9665b53d.0ab958"]]},{"id":"7005cdf2.72c6b4","type":"trigger","z":"d83d3ba7.ad3b58","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"30","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":330,"y":200,"wires":[["7d173d5b.0da224","8dca354b.72a018"]]},{"id":"ec5045e0.09c888","type":"delay","z":"d83d3ba7.ad3b58","name":"Limit 1 msg/min","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"minute","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":true,"x":160,"y":200,"wires":[["7005cdf2.72c6b4"]]},{"id":"e9682136.4fe7e","type":"link out","z":"d83d3ba7.ad3b58","name":"Water Me","links":["4b79a186.9f168"],"x":715,"y":40,"wires":[]},{"id":"4b79a186.9f168","type":"link in","z":"d83d3ba7.ad3b58","name":"Dry","links":["e9682136.4fe7e"],"x":35,"y":200,"wires":[["ec5045e0.09c888"]]},{"id":"795f0c79.7712b4","type":"link out","z":"d83d3ba7.ad3b58","name":"Wet","links":["5498ea2c.6afc34"],"x":715,"y":80,"wires":[]},{"id":"5498ea2c.6afc34","type":"link in","z":"d83d3ba7.ad3b58","name":"Moisture OK","links":["795f0c79.7712b4"],"x":35,"y":320,"wires":[["91e33825.05e638"]]}]

Quick explanation of the nodes in the diagram:

  • Analog Automation HAT Inputs – This is configured just to emit a payload on changes to analog inputs only.
  • Pass ADC1 – (switch node) – Only passes messages with a msg.topic of automationhat/analog.1 filtering all the other ADC inputs.
  • Check Moisture Level (switch node) – Passes the message to output 1 if the msg.payload is < 0.5. Otherwise message is passed to output 2. So output 1 means we need to water the plant, output 2 means we’re ok.
  • Limit 1 msg/min (delay node) – Restricts messges flowing downstream to a maximum of 1 per minute, to attempt to not run the pump for longer than necessary.
  • Trigger 20s (trigger node) – Sends a msg.payload of “1” waits for 30 seconds and then sends a “0”.
  • Warn LED and Run Pump (change nodes) – Respond to the trigger passing the “1” and “0” along to the Warn LED and the Relay controlling the pump.
  • Turn off Warn LED (change node) – Sends an “off” payload to the Warn LED.

 

Wrapping Up

I’m working on a few projects which will be using Node-RED and perhaps the Automation HAT, which I hope to publish soon. So keep checking back to see what’s new.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.