Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. wifi.sta.config({ssid = "SSID", pwd = "PASS"})
  2. wifi.setmode(wifi.STATION)
  3. wifi.sta.connect()
  4. i2c.setup(0, 1, 2, i2c.SLOW)
  5. oled = u8g2.ssd1306_i2c_128x64_noname(0, 0x3c)
  6.  
  7. tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
  8.     if wifi.sta.getip() then
  9.         tmr.stop(1)
  10.     end
  11. end)
  12.  
  13. function work_time(seconds)
  14.     days = math.floor(seconds / 86400)
  15.     hours = math.floor(seconds / 3600 - days * 24)
  16.     mins = math.floor(seconds / 60 - days * 1440 - hours * 60)
  17.     secs = math.floor(seconds - days * 86400 - hours * 3600 - mins * 60)
  18.     return days .. "д, " .. hours .. "ч, " .. mins .. "мин, " .. secs .. "с"
  19. end
  20.  
  21. function receiver(socket, data)
  22.     print(data)
  23.     info = data:match("GET /(%a+)")
  24.     socket:on("sent", function(socket) socket:close() end)
  25.     socket:send("HTTP/1.1 200 OK\r\n")
  26.     if info then
  27.         if info == "display" then
  28.             oled:drawCircle(60, 35, 28, U8G2_DRAW_ALL)
  29.             oled:sendBuffer()
  30.             oled:clearBuffer()
  31.             oled:sendBuffer()
  32.         end
  33.     else
  34.         socket:send("Content-Type: text/html; charset=utf-8\r\n\r\n" ..
  35.         "<html>" ..
  36.         "<head>" ..
  37.         "<title>NodeMCU</title>" ..
  38.         "</head>" ..
  39.         "<body>" ..
  40.         "<button onclick='fetch(\"/display\")'>Нарисовать круг</button>" ..
  41.         "</body>" ..
  42.         "</html>")
  43.     end
  44. end
  45.  
  46. tmr.alarm(2, 1000, 1, function()
  47.     if not wifi.sta.getip() and not tmr.state(1) then
  48.         wifi.sta.connect()
  49.         tmr.start(1)
  50.     end
  51. end)
  52.  
  53. web_server = net.createServer(net.TCP)
  54.  
  55. if web_server then
  56.     web_server:listen(80, function(conn)
  57.         conn:on("receive", receiver)
  58.     end)
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement