Page 1 of 1

Read data udp/tcp

Posted: Sun May 21, 2017 5:36 pm
by elfresno
Hi, I'm new to rpi. I need to be able to read the data that comes to my rpi through the udp port.
I'm using the next statement
nc -u -l 2115
The incoming messages I see in the terminal, but I can not use them for example stop a program.
How can I do to read and use those incoming messages?

Raspbian jesse lite.
Work in bash
I use netcat for udp connection

Re: Read data udp/tcp

Posted: Sun May 21, 2017 11:38 pm
by lmarmisa
You can redirect the standard output of the command nc -u -l 2115 to other script or program that will process the incoming data. The redirected output will become the standard input of your program or script. Use the pipe character | for such purpose:

Code: Select all

nc -u -l 2115 | my_program_or_script
Other alternative would be to develop a script using python:

https://wiki.python.org/moin/UdpCommunication

Re: Read data udp/tcp

Posted: Mon May 22, 2017 1:31 am
by SurferTim
I have C code for UDP, both server (receive, then send reply) and client (send, then wait for reply). Interested?

Re: Read data udp/tcp

Posted: Wed May 24, 2017 10:24 pm
by elfresno
Thanks all for your help. I use now this python script



import socket

UDP_IP = "192.168.0.7"
UDP_PORT = 9933

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

while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data

It work great. But i need that when i send for example: sudo killall omxplayer.bin. This execute and stop the application. Any idea?

And can i receive udp data for mi local ip and broadcast ip? For example i have 2 rpi and send the same udp data for the broadcast ip.

Thanks

Re: Read data udp/tcp

Posted: Wed May 24, 2017 10:33 pm
by elfresno
SurferTim wrote:I have C code for UDP, both server (receive, then send reply) and client (send, then wait for reply). Interested?

Thank you but i use now a python script. But this script no work for me.

I need receive a data and execute this. For example receive the data sudo killall omxplayer.bin and stop the application

Thanks anyway

Re: Read data udp/tcp

Posted: Thu May 25, 2017 7:42 am
by lmarmisa
If you wish to call a system comand, you will need to use the subprocess module:

https://docs.python.org/2/library/subprocess.html

Code: Select all

import socket
import subprocess

UDP_IP = "192.168.0.7"
UDP_PORT = 9933

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

while True:
  data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
  print "received message:", data
  subprocess.call(data.split())

In relation with your second question, take a look at the first answer of this link:

https://stackoverflow.com/questions/228 ... -in-python

BTW, you could consider using MQTT (a publish & subscribe solution) instead of UDP.

Code: Select all

sudo apt-get install mosquitto mosquitto-clients
sudo pip install paho-mqtt
https://en.wikipedia.org/wiki/MQTT

https://www.baldengineer.com/mqtt-tutorial.html

Re: Read data udp/tcp

Posted: Thu May 25, 2017 12:56 pm
by elfresno
lmarmisa wrote:If you wish to call a system comand, you will need to use the subprocess module:

https://docs.python.org/2/library/subprocess.html

Code: Select all

import socket
import subprocess

UDP_IP = "192.168.0.7"
UDP_PORT = 9933

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

while True:
  data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
  print "received message:", data
  subprocess.call(data.split())

In relation with your second question, take a look at the first answer of this link:

https://stackoverflow.com/questions/228 ... -in-python

BTW, you could consider using MQTT (a publish & subscribe solution) instead of UDP.

Code: Select all

sudo apt-get install mosquitto mosquitto-clients
sudo pip install paho-mqtt
https://en.wikipedia.org/wiki/MQTT

https://www.baldengineer.com/mqtt-tutorial.html
thanks for you answer, today i read and try it. I'll tell you the result later