What is the simplest way to synchronize GPIO state (states are: 1 or 0) between two Raspberry devices connected via Internet ?

What I need is immediately change GPIO state on Raspberry 1 if Raspberry 2 GPIO was changed before.

I'm thinking about bash script which read state on Raspberry 1 and send the same state to Raspberry 2 via ssh.
Something like:
ssh pi@remote-host.edu 'echo "1" > /sys/class/gpio/gpio4/value'

But I'm not sure about speed of this action.

  • immediately you are limited by the inherent "lag" that comes with the internet - so, what is immediate "enough"? 1s? 100ms? 10ms? – Jaromanda X Dec 13 '18 at 11:02
  • in my case enough is 1 second for all interaction. – Roman Koval Dec 13 '18 at 13:55

My pigpio may do what you want.

You could have the pigpio daemon running on each Pi.

You could then run a Python script on one Pi. The script will be able to read/write the local GPIO as well as read/write the GPIO on the other P.

#!/usr/bin/env python

import time
import pigpio


pi_local = pigpio.pi() # defaults to local Pi.
if not pi_local.connected:
   print("can't connect to local pi")

pi_remote = pigpio.pi("")
if not pi_remote.connected:
   print("can't connect to remote pi")

def callback_function(gpio, level, tick):
   pi_remote.write(gpio, level) # echo level changes to remote Pi.
   print("set {} to {}".format(gpio, level))

cb = pi_local.callback(GPIO, pigpio.EITHER_EDGE, callback_function)

   while True:
      time.sleep(60) # all the work is done in the callback function.
except KeyboardInterrupt:
   print("\nTidying up")

  • Is it possible to use your library with other Pi like Board ? Orange Pi, Banana Pi ? – Roman Koval Dec 13 '18 at 14:01
  • No, pigpio only runs on Raspberry Pis. It uses features unique to the Broadcom SoC. – joan Dec 13 '18 at 14:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.