Simulate UDP Packet to Gateway Bridge with Python


I’m trying, to simulate UDP packet to Gateway bridge with python, and I plan after having passed this stage, to change manually the payload. I followed precisely the documentation below :

how encode the byte :

I modified the file. .toml configuration of the gateway bridge:


i try to see the example test in the link below :

My python script :

import socket
import time
import json
import binascii

UDP_PORT_NO = 1700

PAYLOAD = '{"rxpk":[{"time":"2013-03-31T16:21:17.532038Z","tmst":3316387610,"chan":0,"rfch":0,"freq":863.00981,"stat":1,"modu":"LORA","datr":"SF10BW125","codr":"4/7","rssi":-38,"lsnr":5.5,"size":32,"data":"ysgRl452xNLep9S1NTIg2lomKDxUgn3DJ7DE+b00Ass"}]}'.encode("utf-8")
MAC = "ABCDEF1010101010"
hex_mac =bytes.fromhex(MAC)

frame =  b'\2'       # 0 for protocole version
frame += b'\7'      # byte 1 random token
frame += b'\5'      # byte 2 random token
frame += b'\0'      # push data identifier
frame += hex_mac
frame += PAYLOAD

while True:	    
    clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    clientSock.sendto(frame, (UDP_IP_ADDRESS, UDP_PORT_NO))

unfortunately when i run the script, the gateway brige gives me a base 64 format error, see below :

      Jan 15 11:51:40 B827EBFFFEF65A01 chirpstack-gateway-bridge[3821]: time="2020-01-15T11:51:40Z" level=error msg="backend/semtechudp: could not handle packet" addr="" data_base64="AgcFAKvN7xAQEBAQeyJyeHBrIjpbeyJ0aW1lIjoiMjAxMy0wMy0zMVQxNjoyMToxNy41MzIwMzhaIiwidG1zdCI6MzMxNjM4NzYxMCwiY2hhbiI6MCwicmZjaCI6MCwiZnJlcSI6ODYzLjAwOTgxLCJzdGF0IjoxLCJtb2R1IjoiTE9SQSIsImRhdHIiOiJTRjEwQlcxMjUiLCJjb2RyIjoiNC83IiwicnNzaSI6LTM4LCJsc25yIjo1LjUsInNpemUiOjMyLCJkYXRhIjoieXNnUmw0NTJ4TkxlcDlTMU5USWcybG9tS0R4VWduM0RKN0RFK2IwMEFzcyJ9XX0=" error="illegal base64 data at input byte 40"

Can some one help me to make the good udp packet format !! thanks.

Here’s what I use, where data is a dictionary

def _push_data(self, data):
	token = os.urandom(2)
	packet = binascii.unhexlify('02') + token + binascii.unhexlify('00') + binascii.unhexlify(self.mac) + data
		self.sock.sendto(packet, self.server_ip)
	except Exception as ex:
		logger.exception('Failed to push uplink packet to server: {}', ex)
1 Like

Hi @gustavobsch !

Thanks for reply, i had try you solution, but i have the same error, can please let me show your variable " data ", it is a string ?
in my case it’s :

      PAYLOAD = '{"rxpk":[{"time":"2013-03-31T16:21:17.532038Z","tmst":3316387610,"chan":0,"rfch":0,"freq":863.00981,"stat":1,"modu":"LORA","datr":"SF10BW125","codr":"4/7","rssi":-38,"lsnr":5.5,"size":32,"data":"ysgRl452xNLep9S1NTIg2lomKDxUgn3DJ7DE+b00Ass"}]}'.encode("utf-8")



Data is not a String nor a Dictionary, Data is base64 data type. What error are you getting?

@gustavobsch, i resolve the bug ! i had mistake on my payload, thanks