Python Forum

Full Version: help on udp response analysis
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

i'm new on python and need some help on best practise for the below:

I have a device working with ESP32 with a Gui for Android but i need to control the device from my raspberry pi

in general i'm sending a udp string to the device in order to get back the status and then take action to control

here the script to request the status

# Echo client program
import socket
ON = "mobile\03\04\01\r\n"
STATUS = "mobile\01\r\n"
HOST = '192.168.0.20'    # The remote host
PORT = 4000              # The same port as used by the server
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((HOST, PORT))
sock.sendall(STATUS)
data = sock.recv(1024)
print('Received', repr(data))
the output looks like that

Output:
('Received', "'master\\x03\\x01\\t\\x00\\x0c\\x00\\x13\\x00\\r\\x00\\x1a\\x00\\x04\\x01\\x05\\x00\\x06\\x01\\x08)\\x0e\\x00\\x00\\x00\\x12\\x00\\x14\\x00%\\x00'")
now i need to analyze the output in order to map the status for example

\x03\\x01\ represents Parameter#03 with value=01

what is the best approach to get this done ?
i tried to load it into array but without success :-(

best would be to get an output as followed

Parameter01=Value01
Parameter02=Value02
Parameter03=Value03
Parameter04=Value04
.
.
.

Thanks in advance
Thomas

The Parameters starts at x03 not at 01 !
no answer :-(
ok, i have done some modification and for the begining i want to analyze only 3 fields from the output string

main issue now is to manipulate the ouput in order to use them afterwards

like '\x01' should be translated to 1 (need to strip the unrelevant chars)

any idea ? tried a lot but without success :-(

thanks
Thomas


# Echo client program
import socket
import re
#ON = "mobile\03\04\01\r\n"
STATUS = "mobile\01\r\n"
HOST = '192.168.0.20'    # The remote host
PORT = 4000              # The same port as used by the server
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((HOST, PORT))
sock.sendall(STATUS)
data = sock.recv(2048)
ONOFF = data[7] 		# 0=OFF,1=ON
SPEED = data[19]		# 0=LOW,1=MED,2=HIGH
MODE = data[23]			# 0=VENTILATE,1=RECOVERY,2=INTAKE
print(r'Received', repr(data))
print('POWER', (ONOFF))
print('SPEED', (SPEED))
print('MODE', (MODE))
Output:
python Status_test.py ('Received', '"master\\x03\\x01\\t\\x00\\x0c\\x00\\x13\\x00\\r\\x00\\x1a\\x00\\x04\\x01\\x05\\x00\\x06\\x02\\x08\'\\x0e\\x00\\x00\\x00\\x12\\x00\\x14\\x00%\\x00"') ('POWER', '\x01') ('SPEED', '\x01') ('MODE', '\x02')