Page 1 of 2

UDP boardcast can Not be received

Posted: Wed Jun 16, 2021 1:05 pm
by lgyer
Hi guys

Does micropython support UDP boardcast mode?
When I try to use udp boardcast to transfer some messages, but my server can't receive any message. But if I use unicast mode, it can work.
The code as following:
Server running in my hardware by using micropython

Code: Select all

import socket
import time

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect('Leo', '1234567890')
        while not sta_if.isconnected():
            time.sleep_ms(500)
    print('network config:', sta_if.ifconfig())
    return sta_if.ifconfig()[0]


local_ip = do_connect()

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP protocol
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((local_ip, 61234))
print("start Listening at 61234")

while True:
    data, client_addr = server.recvfrom(1000)
    print('client connected from', client_addr)
    print("data: ", data)
Client running in my PC by using python language

Code: Select all

import socket
import sys
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
network = '<broadcast>'
#network = '172.20.10.12'
port = 61234
sock.sendto('Client broadcast message!'.encode('utf-8'), (network, 61234))

Re: UDP boardcast can Not be received

Posted: Fri Jun 18, 2021 5:34 am
by lgyer
Can anyone give any hints?

Re: UDP boardcast can Not be received

Posted: Fri Jun 18, 2021 7:01 pm
by fe2o3
I thought I had posted this but...

For receiving broadcasts, change your variable 'local_ip' in the client to
either '0.0.0.0', which will permit broadcasts to be received on any IP address of the host or
set it to the broadcast address for the network the broadcast is sent.
For example, if your host is 192.168.1.25 then that network's broadcast
address is usually 192.168.1.255. The all-zeros address may be the easiest.

-Rusty-

Re: UDP boardcast can Not be received

Posted: Mon Jun 21, 2021 2:47 am
by lgyer
fe2o3 wrote:
Fri Jun 18, 2021 7:01 pm
I thought I had posted this but...

For receiving broadcasts, change your variable 'local_ip' in the client to
either '0.0.0.0', which will permit broadcasts to be received on any IP address of the host or
set it to the broadcast address for the network the broadcast is sent.
For example, if your host is 192.168.1.25 then that network's broadcast
address is usually 192.168.1.255. The all-zeros address may be the easiest.

-Rusty-
I have tried to change 'local_ip' to '0.0.0.0', but the result is same as before.
My lab environment as following:
1. There is a wifi hotspot created in my PC.
2. My esp32 is connected with this wifi hotspot.
3. Then I run my code to test UDP boardcast.

Am I right in these steps?

ESP32 code:

Code: Select all

import socket
import time

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.active(True)
        sta_if.connect('myy', '1234567890')
        while not sta_if.isconnected():
            time.sleep_ms(500)
    print('network config:', sta_if.ifconfig())
    return sta_if.ifconfig()[0]

local_ip = '0.0.0.0'
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP protocol
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((local_ip, 61234))
print("start Listening at 61234")

while True:
    data, client_addr = server.recvfrom(1000)
    print('client connected from', client_addr)
    print("data: ", data)
PC python code

Code: Select all

import socket
import sys
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
network = '<broadcast>'
port = 61234
sock.sendto('Client broadcast message!'.encode('utf-8'), (network, 61234))

Re: UDP boardcast can Not be received

Posted: Mon Jun 21, 2021 2:49 am
by lgyer
gleria wrote:
Sat Jun 19, 2021 3:58 pm
Thanks for the interesting solution. I just have the same case.
Hi gleria

Did your hardware receive the UDP boardcast in this way? Could you please share your code?

Thanks

Re: UDP boardcast can Not be received

Posted: Mon Jun 21, 2021 6:04 am
by fe2o3
Both code segments work for me although my PC is running Linux.
(Still not understanding the '<broadcast>' string)

Your hotspot may not be passing UDP broadcasts.

Re: UDP boardcast can Not be received

Posted: Mon Jun 21, 2021 10:26 am
by lgyer
fe2o3 wrote:
Mon Jun 21, 2021 6:04 am
Both code segments work for me although my PC is running Linux.
(Still not understanding the '<broadcast>' string)

Your hotspot may not be passing UDP broadcasts.
Ah,it's a good news to me. I'll try to use another wifi.

Re: UDP boardcast can Not be received

Posted: Mon Jun 21, 2021 10:48 am
by gleria
I solved this problem. Thanks one more time.

Re: UDP boardcast can Not be received

Posted: Fri Jul 30, 2021 8:44 am
by Luna
Thank you guys for hints. I found the solvation of my issue.

Re: UDP boardcast can Not be received

Posted: Sat Jul 31, 2021 4:51 pm
by williamhenrick
That's an old post and you didn't ask the question. :!: