ATARI_LIVE
Posts: 32
Joined: Sun Sep 20, 2020 6:55 pm

Data I/O between two RPI 4B...

Fri Oct 16, 2020 8:39 am

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!

User avatar
topguy
Posts: 6773
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Data I/O between two RPI 4B...

Fri Oct 16, 2020 8:57 am

What programming language should that code be in ?

ATARI_LIVE
Posts: 32
Joined: Sun Sep 20, 2020 6:55 pm

Re: Data I/O between two RPI 4B...

Fri Oct 16, 2020 11:34 am

topguy wrote:
Fri Oct 16, 2020 8:57 am
What programming language should that code be in ?
Python, Linux.

ATARI_LIVE
Posts: 32
Joined: Sun Sep 20, 2020 6:55 pm

Re: Data I/O between two RPI 4B...

Fri Oct 16, 2020 1:22 pm

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()

User avatar
thagrol
Posts: 3730
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Data I/O between two RPI 4B...

Fri Oct 16, 2020 2:32 pm

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
Arguing with strangers on the internet since 1993.

ATARI_LIVE
Posts: 32
Joined: Sun Sep 20, 2020 6:55 pm

Re: Data I/O between two RPI 4B...

Fri Oct 16, 2020 2:58 pm

thagrol wrote:
Fri Oct 16, 2020 2:32 pm
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
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!

User avatar
thagrol
Posts: 3730
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Data I/O between two RPI 4B...

Fri Oct 16, 2020 4:16 pm

ATARI_LIVE wrote:
Fri Oct 16, 2020 2:58 pm
thagrol wrote:
Fri Oct 16, 2020 2:32 pm
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
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!
No need to shout.

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
Instead of this:

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.
If you want to write your own network code look at python's socket module, it's http support (if you want a web api), MQTT, etc. Hell, you could read and write via stdin/stdout and pipe it over netcat. The documentation is all there so I'm not going to write your code for you.

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.

User avatar
Botspot
Posts: 1538
Joined: Thu Jan 17, 2019 9:47 pm
Location: Texas
Contact: Website

Re: Data I/O between two RPI 4B...

Sun Oct 18, 2020 1:04 am

thagrol wrote:
Fri Oct 16, 2020 4:16 pm
ATARI_LIVE wrote:
Fri Oct 16, 2020 2:58 pm
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!
No need to shout.
I fear you have missed my point.
Thagrol, you have the most fitting signature: "Arguing with strangers on the internet since 1993."
:lol:
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!

User avatar
thagrol
Posts: 3730
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Data I/O between two RPI 4B...

Sun Oct 18, 2020 11:22 am

Botspot wrote:
Sun Oct 18, 2020 1:04 am
Thagrol, you have the most fitting signature: "Arguing with strangers on the internet since 1993."
:lol:
Not really an argument there :) Just a detailed explantion of my early post following an agressive reply by the OP.
Arguing with strangers on the internet since 1993.

ATARI_LIVE
Posts: 32
Joined: Sun Sep 20, 2020 6:55 pm

Re: Data I/O between two RPI 4B...

Tue Oct 20, 2020 8:55 am

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.

User avatar
neilgl
Posts: 2544
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Data I/O between two RPI 4B...

Tue Oct 20, 2020 9:01 am

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.

User avatar
thagrol
Posts: 3730
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Data I/O between two RPI 4B...

Tue Oct 20, 2020 9:19 am

ATARI_LIVE wrote:
Tue Oct 20, 2020 8:55 am
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've already given you the answer.

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.

User avatar
davidcoton
Posts: 5491
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Data I/O between two RPI 4B...

Tue Oct 20, 2020 10:18 am

thagrol wrote:
Tue Oct 20, 2020 9:19 am
I've already given you the answer.
+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

Return to “General discussion”