Running multiple Raspberry pi with Remote GPIO control

Is it possible to to run multiple raspberry pi’s and use the platform: remote_rpi_gpio integration? If someone has been able to do this can you share some yaml?

That platform has been a challenge even with a single RPI. I recently tried to get it working and could not get it stable.

Went to my own python code running on a RPI using MQTT to talk to HA.

Does it work well?

It is somewhat tricky to set up indeed.
My experience was good after i managed to set it up.

I ended up going a different way though.

I migrated my HA to an Asus Tinkerboard which has no GPIO support (and later to an old laptop), so all my GPIO needs were handled by, initially one and later multiple ESP32 chips flashed with ESPhome.

Sorry for going off-topic but i found that ESPs work flawlessly and i think its a little more power efficient.
Unless you use the slave RPi for other stuff too ofcourse

Yes I really like using ESPhome with Esp 8266 and esp32. However, I have a project in mind that would be a multisensor Node WITH built in Motioneye camera. The clarity of the Raspberry pi camera module tend to be better than that of the esp32.
I was thinking I could install standard RPIOS lite, install the needed packages for Platform Raspberry pi Remote GPIO. Then finally install motioneye, allowing all to work. But I want make a few of these and put around the outside of the house. Not sure if possible.

Well, as far as hassio remote goes, it think the trick was to disable the duckdns plug-ins (https) in order to make them work locally and with internal ips.

Other than that they worked well.

Works really well, does just what I want it to do. Code below if anyone wants to go that route.

#!/usr/bin/python

import RPi.GPIO as GPIO
import logging
import paho.mqtt.publish as publish
from time import sleep
from logging.handlers import RotatingFileHandler

logpath = '/var/log/'
filename = 'mqtt_alarm.log'

logging.basicConfig(handlers=[RotatingFileHandler(filename=logpath+filename,
                     mode='w', maxBytes=512000, backupCount=4)], level=logging.INFO,
                     format='%(levelname)s %(asctime)s %(message)s',
                     datefmt='%m/%d/%Y %I:%M:%S %p')

log = logging.getLogger('mqtt_alarm')
log.setLevel(logging.INFO)

MQTT_HOST = "192.168.XXX.YYY"
MQTT_TOPIC_PREFIX = "ha/"
MQTT_CLIENT_ID = ""
MQTT_PAYLOADS = {
  1: "OFF",
  0: "ON",
}

# Dictionary of GPIO PIN to mqtt topic
# gpio 27,22,23,24
PIN_MAP_BCM = {
  17: "front_door",
  22: "basement_slider",
  23: "garage_man_door",
  24: "mudroom_door",
}

GPIO.setmode(GPIO.BCM)

def state_change_handler(channel):
  sleep(0.1) # edge debounce of 100mSec
  state = GPIO.input(channel)

  publish_event(channel, state)

def publish_event(pin, state):
  topic = MQTT_TOPIC_PREFIX + PIN_MAP_BCM[pin]
  payload = MQTT_PAYLOADS[state]

  publish.single(topic, payload, hostname=MQTT_HOST, retain=True, qos=1)

  log.info("Published event, topic={}, payload={}, hostname={}".format(topic, payload, MQTT_HOST))
  print("Published event, topic={}, payload={}, hostname={}".format(topic, payload, MQTT_HOST))

log.info("Initializing.......")
print("Initializing.......")

for pin, name in PIN_MAP_BCM.items():
  GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  GPIO.add_event_detect(pin, GPIO.BOTH, callback=state_change_handler, bouncetime=250)

  state = GPIO.input(pin)
  publish_event(pin, state)

log.info("Running.......")
print("Running.......")

try:
  while True:
    sleep(0.01)
except KeyboardInterrupt:
  log.info("Stopping...")
finally:
  GPIO.cleanup()
1 Like

ESP’s are on my list as is esphome, but for these door sensors I wanted ethernet connectivity and not wifi for absolute speed and reliability as well as POE as all my switching is on a UPS that is generator backed so this keeps the door sensors up in a power failure.

Also the alarm panel terminated in a cinder block utility room in the basement and wifi coverage was challenging but ethernet plentiful as also where all the structured house wiring terminated.

I’ve been watching for ESP’s with ethernet and POE as a long list of things to build with those!

Considered Zigbee battery powered sensors?

Forgive me but im notvery good on the software side. Do I need to install anything a head of time before running the above? Like PahoMQTT?

YES very much an option, not familiar that much with Zigbee or zwave. Based on a limited half-ass search they seem more expensive?

You had multiple RPI work in this configuration?

Another approach would be to install node red on your raspberry pi. You can access the gpio ports and then use the node red mqtt node to send data to the mqtt broker in home assistant Or vice versa. I have this setup for a raspberry pi in the garage that controls a relay to open/close the garage door and sends contact sensor and temperature sensor data to home assistant.

Two, my main one and one to test as slave

Never would have thought of that. Very interesting idea. Thank you I will explore that also.

Hi,

Yes this will need a couple python modules - Paho and RPi.GPIO. Both can be installed with pip.

You also will need a MQTT server but assume you have that covered.

I use systemd to auto-run this on boot, but a quick google will find many options to do so based on how your system is setup and preference.

Thanks, Ill give it a go and see how it works out for me. I hope you dont mind if I reach out to you if I hit a roadblock or 8.

This approach was surprising simple and very effective! Thank you all for your help!

HI, Here’s some examples in node red. Also (if you import the flow) in each comment for each sensor I have added what you need to add to the HA Config.

[{"id":"1caf1fee.6e71c","type":"rpi-gpio out","z":"5ca5672f.9408","name":"","pin":"35","set":true,"level":"1","freq":"","out":"out","x":620,"y":180,"wires":[],"inputLabels":["hass-rgaragedoor"]},{"id":"64e40d29.b9aa6c","type":"mqtt in","z":"5ca5672f.9408","name":"MQTT Left Garage Door Switch","topic":"home/garage/lgaragedoor/set","qos":"2","broker":"","x":170,"y":220,"wires":[["1caf1fee.6e71c","3ee52337.c3256c"]]},{"id":"3ee52337.c3256c","type":"change","z":"5ca5672f.9408","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"str","to":"1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":240,"wires":[["c31b17eb.d4ff8"]]},{"id":"c31b17eb.d4ff8","type":"delay","z":"5ca5672f.9408","name":"","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":620,"y":240,"wires":[["1caf1fee.6e71c"]]},{"id":"a6797718.a4bfe8","type":"comment","z":"5ca5672f.9408","name":"Switch/Relay invoked from a button in home assistant","info":"Turns Relay on for 1 second and then off again \nto simulate a push button switch\n\nin Home Assistant Config\\Switch.yaml\n\n- platform: mqtt\n  name: \"Right Garage Door Switch\"\n  state_topic: \"home/garage/rgaragedoor/state\"\n  command_topic: \"home/garage/rgaragedoor/set\"\n  #availability_topic: \"home/bedroom/switch1/available\"\n  payload_on: \"0\"\n  payload_off: \"1\"\n  optimistic: false\n  qos: 0\n  retain: false","x":240,"y":180,"wires":[]},{"id":"b56f240.4f72f6","type":"mqtt out","z":"5ca5672f.9408","name":"MQTT Left Garage Door Contact","topic":"home/garage/lgaragedoorcontact","qos":"","retain":"","broker":"","x":540,"y":338,"wires":[]},{"id":"de20bcfd.dad178","type":"rpi-gpio in","z":"5ca5672f.9408","name":"","pin":"11","intype":"tri","debounce":"25","read":true,"x":90,"y":338,"wires":[["b56f240.4f72f6"]]},{"id":"f629a4a6.b5d348","type":"comment","z":"5ca5672f.9408","name":"REED Switch/Contact Senor","info":"Wired Contact sensor\n\nIn Home Assistant config/binary_sensor.yaml\n\n- platform: mqtt\n  name: Left Garage Door\n  state_topic: \"home/garage/lgaragedoorcontact\"\n  payload_on: \"1\"\n  payload_off: \"0\"\n  device_class: garage_door\n\n","x":160,"y":300,"wires":[]},{"id":"4f8079e5.85545","type":"ds18b20","z":"5ca5672f.9408","name":"Garage Temp Sensor","sensorid":"28-05170002ceff","timer":"0.2","x":140,"y":446,"wires":[["2eefaa73.5953fe"]]},{"id":"2eefaa73.5953fe","type":"mqtt out","z":"5ca5672f.9408","name":"MQTT Garage Temp","topic":"home/garage/gtemp","qos":"","retain":"","broker":"","x":560,"y":446,"wires":[]},{"id":"644f2086.c7e7a","type":"comment","z":"5ca5672f.9408","name":"DS18b20 Temp Sensor","info":"I use a DSB1820 sensor if you are using a different temp sensor search\nfor a node that is compatible with your sensor \n\nRequires node-red-contrib-ds18b20 installing in node red palette\n\nIn Home Assistant config/sensor.yaml\n\n- platform: mqtt\n  state_topic: \"home/garage/gtemp\"\n  name: \"Garage Temperature\"\n  unit_of_measurement: °C\n  value_template: \"{{ value | round(2) }}\"","x":140,"y":406,"wires":[]},{"id":"c910a222.590598","type":"rpi-srf","z":"5ca5672f.9408","name":"","topic":"SRF","pulse":"1","pins":"29,31","x":90,"y":553,"wires":[["c2e25e53.646b78"]]},{"id":"c2e25e53.646b78","type":"function","z":"5ca5672f.9408","name":"presence","func":"//Checks if distance is less than a metre and\n// if true returns a payload of 1.\n\nvar payload=msg.payload;\nif (Number(payload)<100){msg.payload=1;}\n  else {msg.payload=0;}\nreturn [msg,null];","outputs":1,"noerr":0,"x":300,"y":553,"wires":[["6794aca8.8e6b24"]]},{"id":"6794aca8.8e6b24","type":"mqtt out","z":"5ca5672f.9408","name":"MQTT Car Presence Sensor","topic":"home/garage/carpresence","qos":"","retain":"","broker":"","x":620,"y":553,"wires":[]},{"id":"ca785aad.2bbce","type":"comment","z":"5ca5672f.9408","name":"HCSR04 Ultrasonic Sensor","info":"requires node-red-node-pisrf installing in node red palette\n\nThis checks if distance is less than a metre and returns 1 as the payload if true\n\nIn Home Assistant config/binary_sensor.yaml\n\n- platform: mqtt\n  name: Car Presence\n  state_topic: \"home/garage/carpresence\"\n  payload_on: \"1\"\n  payload_off: \"0\"","x":150,"y":513,"wires":[]},{"id":"a7e2432.3e5424","type":"comment","z":"5ca5672f.9408","name":"****** Read Me First *********","info":"-Set the MQTT Server to your home assistant mqtt broker in all the MQTT Nodes.\n\n-Install the follwing nodes in your pallete\nnode-red-node-pisrf\nnode-red-contrib-ds18b20\n\nOpen each comment for each sensor/switch for the additional Home assistant Config","x":160,"y":115,"wires":[]}]

Hope this helps. Post back if you’re still unsure about anything

Hi @jamos , I’m experiencing troubles trying to let the pisrf node work in node-red inside home-assistant.
The error I get when I deploy is: node-red:rpi-gpio.status not available
So I’m wondering, how did you installed hass.io ?
Dockerized environment, python virtual-env or plain home-assistant ? I tried all with no success.
Thank you