UDP boardcast can Not be received

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
lgyer
Posts: 29
Joined: Fri Feb 26, 2021 8:09 am

UDP boardcast can Not be received

Post by lgyer » Wed Jun 16, 2021 1:05 pm

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))

lgyer
Posts: 29
Joined: Fri Feb 26, 2021 8:09 am

Re: UDP boardcast can Not be received

Post by lgyer » Fri Jun 18, 2021 5:34 am

Can anyone give any hints?

fe2o3
Posts: 10
Joined: Fri Oct 23, 2015 4:47 pm

Re: UDP boardcast can Not be received

Post by fe2o3 » 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-

gleria
Posts: 2
Joined: Fri Apr 09, 2021 1:28 pm

Re: UDP boardcast can Not be received

Post by gleria » Sat Jun 19, 2021 3:58 pm

Thanks for the interesting solution. I just have the same case. Agile software development companies https://mlsdev.com/services/custom-software-development usually employ both teams of programmers and developers. The developers create software applications by using software development tools and methods. Then, teams of testers test the applications to ensure that they work and are correct. Software tester jobs require a fair amount of programming skill, but they do not require a great deal of prior knowledge of software development in general. Usually, a software tester will be hired as a temporary employee and then need to be trained in order to take the job full-time.
Last edited by gleria on Tue Jun 22, 2021 5:10 pm, edited 1 time in total.

lgyer
Posts: 29
Joined: Fri Feb 26, 2021 8:09 am

Re: UDP boardcast can Not be received

Post by lgyer » Mon Jun 21, 2021 2:47 am

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))

lgyer
Posts: 29
Joined: Fri Feb 26, 2021 8:09 am

Re: UDP boardcast can Not be received

Post by lgyer » Mon Jun 21, 2021 2:49 am

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

fe2o3
Posts: 10
Joined: Fri Oct 23, 2015 4:47 pm

Re: UDP boardcast can Not be received

Post by fe2o3 » 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.

lgyer
Posts: 29
Joined: Fri Feb 26, 2021 8:09 am

Re: UDP boardcast can Not be received

Post by lgyer » Mon Jun 21, 2021 10:26 am

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.

gleria
Posts: 2
Joined: Fri Apr 09, 2021 1:28 pm

Re: UDP boardcast can Not be received

Post by gleria » Mon Jun 21, 2021 10:48 am

I solved this problem. Thanks one more time.

Post Reply