Automation PHat - and node-red?


#1

I’ve been developing stuff in node-red suing ( for example p3 and piface 2 ), I’ve now jumped onto a pi zero w and automation phat but I can’t find a node-red node for the automation phat, only some python drivers.
Just wondered if anybody had any ideas how to drive the auto phat from node - I don’t mind getting my feet wet trying to create a node, but I’m not even sure where to start - I did think to get the piface 2 node-red source code and modify it . . . except that I only have python drivers for the phat to start from ??
any ideas, suggestions, sympathy etc. appreciated,
Gav


#2

I started piecing together Node RED nodes, but since I don’t use the platform very often myself it was difficult to build something that felt right. I know that sounds wishy washy, but I think people using Node RED have certain expectations about how something should work. Since I had no idea what those expectations really were, it was tricky to design an API that felt right. (also Node is really, really painful to develop against!)

Anyway a few nodes were more or less completed which you can find here: https://github.com/pimoroni/node-red-nodes

They represent what seems to be the established pattern for connecting Node-RED to HATs on the Pi - just use the Python library and make Node-RED communicate with Python binary to interact with it.

If I were making an Automation pHAT node, I’d start by duplicating the Explorer HAT code: https://github.com/pimoroni/node-red-nodes/tree/master/explorerhat


#3

Hi, thanks for the very prompt response !
I’m really pleased to see the approach you’ve described - simply because from a quick wander around I’d sort of figured that was an approach that could work ( ie: use the python lib and drive it from NR ) - so I’ll check out the Exp code as you suggest,
thanks v much
Gav


#4

so just to close the loop on this I got something working in a pretty hacko way, but its just prototyping and my focus is on the node-red side so I’ll go with hacko :)
in case anybody is remotely interested what I did was use the redis NoSQL db as an intermediary ( because i use redis all the time, so it seemed like a good idea ! )
so for example, to write out to the relay :-

  • node-red function node to write to a redis key
  • redis node to write the actual key out to the redis store
  • node-red python-function node to run python script ( taken from your example )to read the redis key and write to the relay

It all works, its a hack but there you go, here’s the embarassingly awful code :-

first the function node :-

if (flow.get(“relaystate”) === null ) {
_ flow.set(“relaystate”, 0);_
}
if (flow.get(“relaystate”) === 0 ) {
_ flow.set(“relaystate”, 1);_
} else {
_ flow.set(“relaystate”, 0); _
}
msg.payload = [‘witsfd.bo1’, flow.get(“relaystate”)];

return msg;

this links to a redis node which simply writes to the kay called ‘witsfd.bo1’

then the node which lets me run python from node-red, which is a pretty damn powerful option ! :-

#!/usr/bin/env python

import redis
import automationhat

r = redis.StrictRedis()
bo1 = r.get(“witsfd.bo1”)

if bo1 == ‘1’:
_ automationhat.relay.one.write(1)_

if bo1 == ‘0’:
_ automationhat.relay.one.write(0)_

return