Advertisement
pastukhov

Untitled

Apr 22nd, 2015
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | None | 0 0
  1. net.multicastJoin(wifi.sta.getip(), "239.255.255.250")
  2.  
  3. local ssdp_notify = "NOTIFY * HTTP/1.1\r\n"..
  4. "HOST: 239.255.255.250:1900\r\n"..
  5. "CACHE-CONTROL: max-age=100\r\n"..
  6. "NT: upnp:rootdevice\r\n"..
  7. "USN: uuid:c5baf4a1-0c8e-44da-9714-ef01234"..string.format("%x",node.chipid()).."::upnp:rootdevice\r\n"..
  8. "NTS: ssdp:alive\r\n"..
  9. "SERVER: NodeMCU/20150415 UPnP/1.1 ovoi/0.1\r\n"..
  10. "Location: http://"..wifi.sta.getip().."/DimmableLight.xml\r\n\r\n"
  11.  
  12.  
  13. local ssdp_response = "HTTP/1.1 200 OK\r\n"..
  14. "Cache-Control: max-age=100\r\n"..
  15. "EXT:\r\n"..
  16. "SERVER: NodeMCU/20150415 UPnP/1.1 ovoi/0.1\r\n"..
  17. "ST: upnp:rootdevice\r\n"..
  18. "USN: uuid:c5baf4a1-0c8e-44da-9714-ef01234"..string.format("%x",node.chipid()).."\r\n"..
  19. "Location: http://"..wifi.sta.getip().."/DimmableLight.xml\r\n\r\n"
  20.  
  21. local notifyCount = 0
  22.  
  23. tmr.alarm(3, 10000, 1, function()
  24.     notifyCount = notifyCount + 1
  25.     if notifyCount == 3 then
  26.         tmr.stop(3)
  27.         notifyCount = nil
  28.         ssdp_notify = nil
  29.         collectgarbage()
  30.     else
  31.         UPnP = net.createConnection(net.UDP)
  32.         UPnP:connect(1900,"239.255.255.250")
  33.         UPnP:send(ssdp_notify)
  34.         print("Sending notify")
  35.         UPnP:close()
  36.         UPnP = nil
  37.         collectgarbage()
  38.     end
  39. end)
  40.  
  41.  
  42.  
  43.  
  44. UPnPd=net.createServer(net.UDP)
  45.  
  46. UPnPd:on("receive", function(connection, payLoad)
  47.          if string.match(payLoad,"M-SEARCH") then
  48.             connection:send(ssdp_response)
  49.          end
  50.       end)
  51. UPnPd:on("sent", function(connection, payLoad)
  52.          print("sent "..node.heap())
  53.          --connection:close()
  54.       end)
  55. UPnPd:listen(1900,"239.255.255.250")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement