B4R Question Wemos D1 R2 - UDP sending to broadcast address 255.255.255.255 fails to send

qle

Member
Licensed User
Longtime User
Hi,
WEMOS D1 R2, B4R 1.80, Arduino v1.8.1

UDP sending to broadcast address fails to send

Just trying to send a udp packet on broadcast (255.255.255.255=>port)
But monitoring the network packets (tcpdump) i dont see it even try to attempt to send..

'this does not seem to work?
udp.BeginPacket("255.255.255.255",4101)
udp.Write("test".GetBytes)
udp.SendPacket

Note: The code below is just a sample and not the full working code.

Any assistance is appreciated

EDIT: all udp seems to fail, so using
udp.BeginPacket("192.168.1.22",5211)
also fails to send... mmm... what am i missing?

Lee.

B4X:
Sub Process_Globals
        Private udp As WiFiUDP
End Sub

Private Sub AppStart

    Serial1.Initialize(115200)
    Log("AppStart")

        udp.Initialize(4102,"udp_PacketArrived")

    DoConnect ' connects fine, and i can see the Bootp/DHCP request

End Sub

Sub timer1_tick
    If wifi.IsConnected  = True Then
            Log("MyIP ",wifi.LocalIp )

            'this does not seem to work?
            udp.BeginPacket("255.255.255.255",4101)
            udp.Write("test".GetBytes)
            udp.SendPacket

        Else
            Log("Wifi not connected")
    End If
End Sub

Sub udp_PacketArrived (Data() As Byte, Ip() As Byte, Port As UInt)

    Log("PacketArrived from:")
    PrintIp(Ip)
    log(Data.Length)
End Sub

Sub PrintIp(ip() As Byte)
    Log(ip(0),".", ip(1),".", ip(2),".", ip(3))
End Sub

Sub DoConnect

        Log("Wifi trying to connect")
        If WifiConnect Then '(use Connect2 if a password is required).
            Log("Connected to wireless network.")

        Else
            Log("Failed to connect.")

        End If

End Sub


Sub WifiConnect() As Boolean
    Dim l As Int
    Log("Wifi ssid: digger ")
    If wifi.Connect2("digger","apassword") Then Return True
    Return False 'can not connect
End Sub
 
Last edited:

rbghongade

Active Member
Licensed User
Longtime User
Dear friend,
I think that udp socket should be initialised after connection to the network is established. I may be wrong but this is the sequence I am successfully using.
regards,
 
Upvote 0

qle

Member
Licensed User
Longtime User
Dear friend,
I think that udp socket should be initialised after connection to the network is established. I may be wrong but this is the sequence I am successfully using.
regards,

Hi
Thank your for your reply.

I have moved the initialize to after the connect...

ie:
B4X:
Sub DoConnect

        Log("Wifi trying to connect")
        If WifiConnect Then '(use Connect2 if a password is required).
            Log("Connected to wireless network.")
            udp.Initialize(4102,"udp_PacketArrived")
        Else
            Log("Failed to connect.")

        End If

End Sub

But still not work..
I have test code for TCP to connect to and send data a listening ip and port and that works ok... just cannot get UDP to work.


Lee.
 
Last edited:
Upvote 0

jboavida

Member
Licensed User
Longtime User
Some routers block UDP broadcast... I had a problem once with a WizNet Serial to Ethernet module. The module cannot be found by the configuration software when connected to the network.
A direct cable from the PC worked fine... With another router worked also.
In your case is wifi, so is not possible a direct cable :) but try with a different router.
 
Last edited:
Upvote 0

qle

Member
Licensed User
Longtime User
Thank you for your reply.
Other devices (Android) that i write for using B4A that are on the same wireless network are working fine.
I have dropped trying to use UDP, and now just use TCP connections.. works fine..

Thank you.

Lee.
 
Upvote 0

qle

Member
Licensed User
Longtime User
yes your quite right, i missed that.
will try that later..

thank you very much for your reply.
 
Upvote 0
Top