As the title says... Chat on...

User avatar
By dana
#31340 I'm working on a home automation project and I need to receive UDP broadcast messages first with the NodeMCU configured as an access point and then with the NodeMCU connect to the local WiFi network.

I can receive UDP messages directed at a specific IP address like 192.168.1.100, but I can't seem to receive broadcast messages either configured as an access point or connected to the local network.

Here's a sample based on the example code:

wifi.setmode(wifi.STATION)
wifi.sta.config("myssid","mypassword")
wifi.sta.connect()
wifi.sta.setip({ip="192.168.1.255",netmask="255.255.255.0",gateway="192.168.1.1"})
srv=net.createServer(net.UDP)
srv:on("receive",function(s,c) print(c) end)
srv:listen(10000)

This works if I set the IP to 192.168.1.100, but not if the IP is 192.168.1.255 or 255.255.255.255.

I'm sure I'm making a simple mistake, but I can't see it. Any suggestions?
User avatar
By jcheger
#38951 I've made some tests. Regarding the following example, it will answer to its own IP address, to the subnet broadcast and the global broadcast. In other words, if the ESP8266 is set to 192.168.1.100 / 255.255.255.0, it will answer to 192.168.1.100, 192.168.1.255 and 255.255.255.255.

On Linux, you can try it with « socat » - type « info » + ENTER to trigger an answer
Code: Select allsocat - UDP-DATAGRAM:255.255.255.255:1234,broadcast
info
{serial:16335237,mac:"18:fe:34:f9:41:85",ip:"192.168.1.100"}

bc-discover.lua:
Code: Select allbc=net.createServer(net.UDP)
bc:on("receive", function(c, p1)
    if string.sub(p1, 1, -2) == "info" then
        c:send("serial:"..node.chipid()..",mac:\""..wifi.sta.getmac().."\",ip:\""..wifi.sta.getip().."\"}\n")
    end
end)
bc:listen(1234)