Scott0123
Posts: 5
Joined: Fri Feb 01, 2019 5:11 pm

Controlling 100 LED's over TCP/IP between two Pi's

Mon Feb 11, 2019 3:16 pm

Hello, I'm working on a project where I need to control nearly 100 LED's between two Raspberry Pi's. For this, I'm using two B+ Pi's, seven IO Pi Plus boards from AB Electronics (https://www.abelectronics.co.uk/p/54/io-pi-plus), one Ethernet cable (standard cable, a cross-over is not needed) and several 16-channel relay boards from SainSmart. Everything is connected and I can locally control the relays from each Pi with no problem. I have the main Pi set up as the "client" and the other one set up as the "server" waiting for commands over the socket connection.

Ideally, I would like to control the entire system through one Pi (likely the client) and have no perceptible delay between boards. This will allow me to create the needed effects with the LED's. Unfortunately, I haven't been able to find much information on doing so. Everything that I've found so far talks about simple communication between Pi's over the socket connection. While this is obviously important, it stops short of the commands needed to control the LED's.

Assuming people have done this, I'm hoping someone can direct me to a video or provide the needed code for this type of communication. Because there are so many LED's, I'd like to simplify the process to something as basic as "turn on 62" and let the program take it from there. That would allow be to build the LED effects I need.

Any help is appreciated.

Scott

User avatar
rpdom
Posts: 13712
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Controlling 100 LED's over TCP/IP between two Pi's

Mon Feb 11, 2019 4:23 pm

Socket stuff isn't too hard. Just google it (it's not Pi specific, just search for Linux and python).

Otherwise you could look at MQTT, which can send messages between systems. You just have to write something that will use those messages once received.
Signature is on holiday.

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

Re: Controlling 100 LED's over TCP/IP between two Pi's

Mon Feb 11, 2019 5:58 pm

Doesn't Joan's GPIO library allow remote access to GPIO on another Pi?
Signature retired

Scott0123
Posts: 5
Joined: Fri Feb 01, 2019 5:11 pm

Re: Controlling 100 LED's over TCP/IP between two Pi's

Tue Feb 12, 2019 12:18 am

Thank you for the replies.

Primarily, I'm trying to figure out how to progress past a simple "trigger" in the Python script over the socket connection to more of a data statement where I can have more control over the LED's. Ideally, all the commands will come from the primary (client) Pi with minimal scripting on the server Pi. It would be helpful to know how much of a command is supported over the socket connection. Can it be the same as directly from the Pi?

I'm sure the information is out there but I have been unable to find it. If any of you have more guidance, it would be appreciated.

Scott

User avatar
rpdom
Posts: 13712
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Controlling 100 LED's over TCP/IP between two Pi's

Tue Feb 12, 2019 7:02 am

You can send whatever you like over a socket connection. What you do with it depends on your code at each end.
Signature is on holiday.

ghans
Posts: 7839
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Controlling 100 LED's over TCP/IP between two Pi's

Tue Feb 12, 2019 8:05 am

pigpio already supports remote control over a network. That might be easier than writing network code yourself.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

Scott0123
Posts: 5
Joined: Fri Feb 01, 2019 5:11 pm

Re: Controlling 100 LED's over TCP/IP between two Pi's

Tue Feb 12, 2019 4:11 pm

Thank you for the help. I think I'm very close to figuring this out. Here is part of the current "server" code:

led61on()
time.sleep(2)
led61off()

s.listen(1)
print ('socketawaiting messages')
(conn, addr) = s.accept()
print ('connected')
while True:
print ('doing it')
data = conn.recv(1024)
data = data.decode('utf-8')
print('data received= ' + data)
break
data

-----------------------------------
The top three statements turn the led on and off with no problem. So, I know the defined statements work fine.
I'm having the client send "led61on()". The server is clearly receiving the string. This is confirmed with the message, "data received= led61on()" in the shell area.

how do get the server to execute that variable? I was hoping the "data" statement at the bottom would run it but it does not. How would I execute multiple commands?

Thank you for the help.

Scott

User avatar
rpdom
Posts: 13712
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Controlling 100 LED's over TCP/IP between two Pi's

Tue Feb 12, 2019 9:04 pm

If you post your code in [code] [/code] tags it will preserve the formatting which is critical in python (use the [code] button when you post.)

It is not a good idea to have your code blindly run any command that it receives. It should check for the command and decide if it is valid or not, something like:
if data starts with "turn_on_led " then gpio(on, data_part_2)
That's not valid code, but you get the idea.
Signature is on holiday.

Scott0123
Posts: 5
Joined: Fri Feb 01, 2019 5:11 pm

Re: Controlling 100 LED's over TCP/IP between two Pi's

Tue Feb 12, 2019 10:11 pm

Yes, I'm sorry about that. I'm new to the forum and didn't preview the code part before submitting it.

Code: Select all

led61on()
time.sleep(2)
led61off()

s.listen(1)
print ('socketawaiting messages')
(conn, addr) = s.accept()
print ('connected')
while True:
	print ('doing it')
	data = conn.recv(1024)
	data = data.decode('utf-8')
	print('data received=  ' + data)
	break
data
This one should look better.

I understand the issues with blindly running the code but I still cannot figure out how to get my "led61on()" into action. I will have many statements like that and I'm trying to minimize the coding on the server side. There will be no wifi and only a single Ethernet between the two Pi's so I'm thinking the chances of strange code getting in there will be rare.

If you could suggest some code to allow the system to execute the "led61on()" after is comes over the socket connection, I'd really appreciate it.

Scott

Return to “General programming discussion”