1

I'm working on a IoT project. The idea is basically: I have a notebook running a Python program that get frames from an IP camera, process it, and based on the result change a Raspberry Pi GPIO pin state. The computer, the camera and the Pi are connected to the same wireless router (by cables).

I was trying to use Paramiko to execute the commands on the raspPi, but I got problem with the velocity. The camera FPS is bigger than Paramiko could provide. I also tried parallel-ssh library (https://parallel-ssh.org/post/ssh2-python/) but still not fast enough.

Do you have an ideia to help me on this problem?

Thank you!

3
  • From your statements you want to send 1 bit of data to the Pi - so speed is irrelevant.
    – Milliways
    Mar 12, 2019 at 2:21
  • 1
    Hi @Milliways, the question is a little confusing, but I believe that Gabriel wants for the Raspberry Pi to change the state of a GPIO pin based on input from the laptop which is communicating with the Raspberry Pi using SSH. The real question is how frequently will the state of GPIO pin need to change? Could it change with every picture, meaning 30 times per second? If yes, I don't believe that the Pi could keep up with 30 input requests per second. Mar 12, 2019 at 3:26
  • @HeatfanJohn I can only answer the question asked - using sockets or remote GPIO it is simple to support 10s if not 100s of kHz. It should probably be closed as unclear.
    – Milliways
    Mar 12, 2019 at 3:52

2 Answers 2

4

Create a UDP "server" socket on the Pi, and use a client on the laptop to send commands to it. Just send more packages to be sure to get it. UDP won't need you to connect, and it has good latency. Or you can use TCP as well, it's not that different on the Python side.

Since you probably wrote your image recognition program in Python (and added a Python tag), I wrote a simple example (all right, copy-pasted from a project I copy-pasted from somewhere):

Server side:

import socket

UDP_IP = "0.0.0.0" # listen to everything
UDP_PORT = 12345 # port

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

while True:
  data, addr = sock.recvfrom(512) # random buffer size, doesn't matter here..
  print("received message:", data)
  #simplest way to react.. of course, a better parser should be used, and add GPIO code, etc..
  if data==b'LED=1\n':
    print("LED ON")
  elif data==b'LED=0\n':
    print("LED OFF")

Client:

import socket
import time

UDP_IP = "127.0.0.1" # set it to destination IP.. RPi in this case
UDP_PORT = 12345

print("UDP target IP:", UDP_IP)
print("UDP target port:", UDP_PORT)

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

while True:
  print("Turn ON")
  sock.sendto(b'LED=1\n', (UDP_IP, UDP_PORT))
  time.sleep(2)
  print("Turn OFF")
  sock.sendto(b'LED=0\n', (UDP_IP, UDP_PORT))
  time.sleep(2)

You could use broadcast to flood everything in your LAN as well.. but RPi gets your message whatever its address is. Also, I added newlines to the protocol, so you can simply test in in netcat: nc -u localhost 12345 then type LED=0 and press Enter.

5
  • 1
    How do you create an UDP "server" socket? And what client on the laptop to use?
    – Ingo
    Mar 12, 2019 at 23:20
  • 1
    @Ingo: added some code. I didn't think at first it was necessary (everything can be found on this site), but better have it in one place.
    – Nyos
    Mar 12, 2019 at 23:50
  • It is always better to have it in place :) Raspberry Pi is made to learn about hardware and software and we have many newbies which don't know what to search. Thanks +1
    – Ingo
    Mar 13, 2019 at 8:53
  • Hi @Nyos, it is working very good!! Thank you. I've send 20000 commands to raspberry within 5ms delay between each one and the Pi received all of them. I'll try TCP connection now to see the result. If the velocity is not too smaller, it would be good to have and feedback of the commands sent. Cheers!!! Mar 15, 2019 at 11:53
  • Oh, you can send feedback even in its current form, with UDP. Just copy the send code to the server side, and the receive checker to the client. Also, threads or nonblocking checks might come handy in that case. But TCP is nice too, just - instead of missing packets - you need to be sure to reconnect the socket if it's lost.
    – Nyos
    Mar 15, 2019 at 12:40
1

If you are looking for a workable solution using Python https://gpiozero.readthedocs.io/en/stable/remote_gpio.html has instructions for remote GPIO.

You could roll your own custom solution using any networking protocol, although sockets are probably the most common, but overkill for a simple task.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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