nodemcu-ssdp icon indicating copy to clipboard operation
nodemcu-ssdp copied to clipboard

UPnP failing on latest NodeMCU builds

Open FrankX0 opened this issue 8 years ago • 2 comments

I have been using your piece of code successfully on the NodeMCU firmware, until the latest modifications to the network layer. I modified your code to make it compatible with the changes to the UDP commands: changing from net.createServer to net.createUDPSocket.

net.multicastJoin(wifi.sta.getip(), "239.255.255.250")

local ssdp_notify = "NOTIFY * HTTP/1.1\r\n"..
"HOST: 239.255.255.250:1900\r\n"..
"CACHE-CONTROL: max-age=100\r\n"..
"NT: upnp:rootdevice\r\n"..
"USN: uuid:c5baf4a1-0c8e-44da-9714-ef01234"..string.format("%x",node.chipid()).."::upnp:rootdevice\r\n"..
"NTS: ssdp:alive\r\n"..
"SERVER: NodeMCU/20150415 UPnP/1.1 ovoi/0.1\r\n"..
"Location: http://"..wifi.sta.getip().."/DimmableLight.xml\r\n\r\n"


local ssdp_response = "HTTP/1.1 200 OK\r\n"..
"Cache-Control: max-age=100\r\n"..
"EXT:\r\n"..
"SERVER: NodeMCU/20150415 UPnP/1.1 ovoi/0.1\r\n"..
"ST: upnp:rootdevice\r\n"..
"USN: uuid:c5baf4a1-0c8e-44da-9714-ef01234"..string.format("%x",node.chipid()).."\r\n"..
"Location: http://"..wifi.sta.getip().."/DimmableLight.xml\r\n\r\n"

notifyCount = 0

local function notify()
    notifyCount = notifyCount + 1
    if notifyCount == 3 then
        tmr.stop(3)
        notifyCount = nil
        ssdp_notify = nil
        collectgarbage()
    else
        UPnP = net.createUDPSocket()
        --UPnP:connect(1900,"239.255.255.250")
        UPnP:send(1900,'239.255.255.250',ssdp_notify)
        print("Sending notify")
        UPnP:close()
        UPnP = nil
        notify = nil
        collectgarbage()
    end


end

local function response(connection, payLoad)
        if string.match(payLoad,"M-SEARCH") then
            connection:send(1900,'239.255.255.250',ssdp_response)
            print("sent "..node.heap())
        end
end


tmr.alarm(3, 10000, 1, notify())




UPnPd = net.createUDPSocket()

UPnPd:on("receive", response )
--[[
UPnPd:on("sent", function(connection, payLoad) 
         print("sent "..node.heap())
       
      end)]]--
UPnPd:listen(1900,"239.255.255.250")

This code runs fine, until the response function is called. The code then fails with: PANIC: unprotected error in call to Lua API (upnp2.lua:46: illegal value) Which means connection:send(1900,'239.255.255.250',ssdp_response) fails.

So far I was not able to find out what is triggering this. Any ideas/suggestions?

FrankX0 avatar Feb 20 '17 18:02 FrankX0

Seems caused by latest NodeMCU firmware: https://github.com/nodemcu/nodemcu-firmware/issues/1812

FrankX0 avatar Feb 21 '17 21:02 FrankX0

try add this line at the top of the code:

net.multicastJoin("","239.255.255.250")

and use UPnPd:listen(1900,"0.0.0.0") instead of UPnPd:listen(1900,"239.255.255.250")

it's ok here.

yangbo1979 avatar Jul 02 '18 03:07 yangbo1979