Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. ssid = "DSPLAB"
  2. pass = "feriiot123!"
  3. wifi.setmode(wifi.STATION)
  4. print('set mode=STATION (mode=' .. wifi.getmode()..')\n')
  5. print('MAC Address: ',wifi.sta.getmac())
  6. print('Chip ID: ',node.chipid())
  7. wifi.sta.config(ssid,pass)
  8. wifi.sta.connect()
  9. relay2 = 3
  10. relay1 = 2
  11. tmr.alarm(1, 1000, 1, function()
  12. if wifi.sta.getip() == nil then
  13. print("IP unavaiable, Waiting...")
  14. else
  15. tmr.stop(1)
  16. print("ESP8266 mode is: " .. wifi.getmode())
  17. print("The module MAC address is: " .. wifi.ap.getmac())
  18. print("Config done, IP is "..wifi.sta.getip())
  19.  
  20. end
  21. end)
  22. print(wifi.sta.getip())
  23.  
  24. srv=net.createServer(net.TCP)
  25. srv:listen(80,function(conn)
  26. conn:on("receive",function(conn,payload)
  27. print("PAYLOAD " .. payload)
  28.  
  29. if(string.match(payload,"/temp")) then
  30.  
  31. t = require("ds18b20")
  32. t.setup(1)
  33. temperatura = t.read()
  34. print(t.read())
  35. conn:send("HTTP/1.1 200 OK\r\n\r\n"..temperatura)
  36. end
  37. if(string.match(payload,"/hum")) then
  38. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  39. end
  40. if(string.match(payload,"/rgb/off"))then
  41. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  42.  
  43. end
  44. if(string.match(payload,"/rgb/color?"))then
  45. r,g,b= payload:match("R=(%d+)&G=(%d+)&B=(%d+)")
  46. print (r,g,b)
  47. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  48. --NASTAVI BARVO, GLEDE NA PARAMETRE R G B
  49. --IF SUCCESSFUL
  50. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  51. end
  52. if(string.match(payload,"/relay1/on"))then
  53. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  54. gpio.write(relay1, gpio.HIGH)
  55. end
  56. if(string.match(payload,"/relay1/off"))then
  57. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  58.  
  59. if(pcall(gpio.write(relay1, gpio.LOW))then
  60. conn:send("OK");
  61. end
  62. end
  63. if(string.match(payload,"/relay2/on"))then
  64. gpio.write(relay2, gpio.HIGH)
  65. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  66. end
  67. if(string.match(payload,"/relay2/off"))then
  68. gpio.write(relay2, gpio.LOW)
  69. conn:send("HTTP/1.1 200 OK\r\n\r\n")
  70. end
  71.  
  72.  
  73. end)
  74. conn:on("sent",function(conn) conn:close() end)
  75. t = nil
  76. ds18b20 = nil
  77. package.loaded["ds18b20"]=nil
  78. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement