Folks, I am looking for the coding to set a communicate on two of the Raspberry Pi 4B 4 bit in and 4 bit out. see the picture for an example:
https://www.dropbox.com/s/u17zihd0pqu58vp/I_O.png?dl=0
Just small example code so I can figure it out.
Thanks for your helps!
-
- Posts: 32
- Joined: Sun Sep 20, 2020 6:55 pm
Re: Data I/O between two RPI 4B...
What programming language should that code be in ?
-
- Posts: 32
- Joined: Sun Sep 20, 2020 6:55 pm
-
- Posts: 32
- Joined: Sun Sep 20, 2020 6:55 pm
Re: Data I/O between two RPI 4B...
Here code for example what I wish...
Code: Select all
# THIS IS FOR EXAMPLE ONLY this code is not working, just example again:
import network
import GPIO
GPIO.OUTPUT(1,2,3,4)
GPIO.INPUT(5,6,7,8,pull_up)
data1 = 0
data2 = 0
data3 = 0
data4 = 0
data5 = 0
data6 = 0
data7 = 0
data8 = 0
def receiver():
data1=net.get()
data2=net.get()
data3=net.get()
data4=net.get()
x.after(1,receiver)
def transmitter():
net.send(data5)
net.send(data6)
net.send(data7)
net.send(data8)
x.after(1,transmitter)
def connect_net():
open(net, 192.168.1.10) # on other board use 192.168.1.11
net.lock() #keep it open forever.
def read_write_IO():
GPIO.OUT(1,data1)
GPIO.OUT(2,data2)
GPIO.OUT(3,data3)
GPIO.OUT(4,data4)
data5 = GPIO.IN(5)
data6 = GPIO.IN(6)
data7 = GPIO.IN(7)
data8 = GPIO.IN(8)
x.after(1,read_write_IO)
connect.net()
receiver()
transmitter()
read_write_IO()
- thagrol
- Posts: 4024
- Joined: Fri Jan 13, 2012 4:41 pm
- Location: Darkest Somerset, UK
- Contact: Website
Re: Data I/O between two RPI 4B...
I'd use the remote GPIO feature of joan's pigpio library http://abyz.me.uk/rpi/pigpio/ It's going to be a lot simpler than writing your own network code and protocols.
For python, gpiozero supports it: https://gpiozero.readthedocs.io/en/stab ... _gpio.html
For python, gpiozero supports it: https://gpiozero.readthedocs.io/en/stab ... _gpio.html
Arguing with strangers on the internet since 1993.
-
- Posts: 32
- Joined: Sun Sep 20, 2020 6:55 pm
Re: Data I/O between two RPI 4B...
Sir, I know the GPIO code works I have another project with the GPIO works good. my issue is the network to connect to other RPI board. THANK YOU!thagrol wrote: ↑Fri Oct 16, 2020 2:32 pmI'd use the remote GPIO feature of joan's pigpio library http://abyz.me.uk/rpi/pigpio/ It's going to be a lot simpler than writing your own network code and protocols.
For python, gpiozero supports it: https://gpiozero.readthedocs.io/en/stab ... _gpio.html
- thagrol
- Posts: 4024
- Joined: Fri Jan 13, 2012 4:41 pm
- Location: Darkest Somerset, UK
- Contact: Website
Re: Data I/O between two RPI 4B...
No need to shout.ATARI_LIVE wrote: ↑Fri Oct 16, 2020 2:58 pmSir, I know the GPIO code works I have another project with the GPIO works good. my issue is the network to connect to other RPI board. THANK YOU!thagrol wrote: ↑Fri Oct 16, 2020 2:32 pmI'd use the remote GPIO feature of joan's pigpio library http://abyz.me.uk/rpi/pigpio/ It's going to be a lot simpler than writing your own network code and protocols.
For python, gpiozero supports it: https://gpiozero.readthedocs.io/en/stab ... _gpio.html
I fear you have missed my point.
If you use pigpio as I suggested you don't need to write any network code of your own. The library takes care of that and hides it from your code. Once created you can treat the remote GPIO as you would any local one.
Very roughly your code becomes this:
Code: Select all
# create remote gpio
remote = ...
# create local gpio
local = ...
while True:
remote.value = local.value
Code: Select all
define protocol
define some method for your threads to communicate with each other.
configure gpio
start thread to monitor gpio
create and open sockets
start thread to handle communications (you may want two threads, one to send and one to recieve)
# don't forget to use non blocking sockets (hint: select.select()) and write your code so that it can cope with network outages.
# don't forget to clean up your sockets on exit (even when exiting due to an error)
# if you don't you may find next time you try to run it the desired socket isn't available.
I was explainging how I'd do it and trying to make things easier for you. Guess that's not what you wanted.
Arguing with strangers on the internet since 1993.
Re: Data I/O between two RPI 4B...
Thagrol, you have the most fitting signature: "Arguing with strangers on the internet since 1993."thagrol wrote: ↑Fri Oct 16, 2020 4:16 pmNo need to shout.ATARI_LIVE wrote: ↑Fri Oct 16, 2020 2:58 pmSir, I know the GPIO code works I have another project with the GPIO works good. my issue is the network to connect to other RPI board. THANK YOU!
I fear you have missed my point.

Want to run Minecraft Java on your RPi? Easiest way is with Pi-Apps - just click Install.
https://github.com/Botspot/pi-apps
Pi-Apps also includes Zoom, Visual Studio, Tor browser, Windows 10 theme, and 33 more.
Over 50,000 users!
https://github.com/Botspot/pi-apps
Pi-Apps also includes Zoom, Visual Studio, Tor browser, Windows 10 theme, and 33 more.
Over 50,000 users!
- thagrol
- Posts: 4024
- Joined: Fri Jan 13, 2012 4:41 pm
- Location: Darkest Somerset, UK
- Contact: Website
Re: Data I/O between two RPI 4B...
Not really an argument there

Arguing with strangers on the internet since 1993.
-
- Posts: 32
- Joined: Sun Sep 20, 2020 6:55 pm
Re: Data I/O between two RPI 4B...
Alright, I have an idea to explain this:
I have two the RASPBERRY PI 4B as 'A' and 'B' on each RPI board.
The 'A' board has a button connected to the GPIO.
The 'B' board has a LED connected to the GPIO.
Both boards are connected with the network with IP address.
I would like to press the button from the board 'A' and the LED go light on from the board 'B'
My question is how do a code for communicate between two boards via the network with socketio or else?
Thank You again.
I have two the RASPBERRY PI 4B as 'A' and 'B' on each RPI board.
The 'A' board has a button connected to the GPIO.
The 'B' board has a LED connected to the GPIO.
Both boards are connected with the network with IP address.
I would like to press the button from the board 'A' and the LED go light on from the board 'B'
My question is how do a code for communicate between two boards via the network with socketio or else?
Thank You again.
Re: Data I/O between two RPI 4B...
I would install mqtt (mosquitto) on both. On A publish a message when the button is pressed. On B, listen for that message and light the LED.
- thagrol
- Posts: 4024
- Joined: Fri Jan 13, 2012 4:41 pm
- Location: Darkest Somerset, UK
- Contact: Website
Re: Data I/O between two RPI 4B...
I've already given you the answer.ATARI_LIVE wrote: ↑Tue Oct 20, 2020 8:55 amAlright, I have an idea to explain this:
I have two the RASPBERRY PI 4B as 'A' and 'B' on each RPI board.
The 'A' board has a button connected to the GPIO.
The 'B' board has a LED connected to the GPIO.
Both boards are connected with the network with IP address.
I would like to press the button from the board 'A' and the LED go light on from the board 'B'
My question is how do a code for communicate between two boards via the network with socketio or else?
Thank You again.
Install pigpio and pigpiod on both PI (it should be there in a default RPiOS install), Use its remote GPIO feature. Either run your code on A and have it switch the LED on B or run your code on B and have it monitor the button on A. Not going to write it for you especially as I 've already linked the documentation.
No need for MQTT or other protocols whether home grown or not.
Arguing with strangers on the internet since 1993.
- davidcoton
- Posts: 5665
- Joined: Mon Sep 01, 2014 2:37 pm
- Location: Cambridge, UK
- Contact: Website
Re: Data I/O between two RPI 4B...
+1 for that as the quick, easy and simple answer.
Use MQTT if you want to make it harder.
Write your own socket server in Python (good library support) to make it harder still.
Use C++ to make it harder yet.
Use bare metal and assembler for a real challenge.
Location: 345th cell on the right of the 210th row of L2 cache