http_server.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. --
  2. -- Simple NodeMCU web server (done is a not so nodeie fashion :-)
  3. --
  4. -- Highly modified by Bruce Meacham, based on work by Scott Beasley 2015
  5. -- Open and free to change and use. Enjoy. [Beasley/Meacham 2015]
  6. --
  7. -- Meacham Update: I streamlined/improved the parsing to focus on simple HTTP GET request and their simple parameters
  8. -- Also added the code to drive a servo/light. Comment out as you see fit.
  9. --
  10. -- Usage:
  11. -- Change SSID and SSID_PASSPHRASE for your wifi network
  12. -- Download to NodeMCU
  13. -- node.compile("http_server.lua")
  14. -- dofile("http_server.lc")
  15. -- When the server is esablished it will output the IP address.
  16. -- http://{ip address}/?s0=1200&light=1
  17. -- s0 is the servo position (actually the PWM hertz), 500 - 2000 are all good values
  18. -- light chanel high(1)/low(0), some evaluation boards have LEDs pre-wired in a "pulled high" confguration, so '0' ground the emitter and turns it on backwards.
  19. --
  20. -- Add to init.lua if you want it to autoboot.
  21. --
  22. -- Your Wifi connection data
  23. local SSID = "YOUR WIFI SSID"
  24. local SSID_PASSWORD = "YOUR SSID PASSPHRASE"
  25. -- General setup
  26. local pinLight = 2 -- this is GPIO4
  27. gpio.mode(pinLight,gpio.OUTPUT)
  28. gpio.write(pinLight,gpio.HIGH)
  29. servo = {}
  30. servo.pin = 4 --this is GPIO2
  31. servo.value = 1500
  32. servo.id = "servo"
  33. gpio.mode(servo.pin, gpio.OUTPUT)
  34. gpio.write(servo.pin, gpio.LOW)
  35. -- This alarm drives the servo
  36. tmr.alarm(0,10,1,function() -- 50Hz
  37. if servo.value then -- generate pulse
  38. gpio.write(servo.pin, gpio.HIGH)
  39. tmr.delay(servo.value)
  40. gpio.write(servo.pin, gpio.LOW)
  41. end
  42. end)
  43. local function connect (conn, data)
  44. local query_data
  45. conn:on ("receive",
  46. function (cn, req_data)
  47. params = get_http_req (req_data)
  48. cn:send("HTTP/1.1 200/OK\r\nServer: NodeLuau\r\nContent-Type: text/html\r\n\r\n")
  49. cn:send ("<h1>ESP8266 Servo &amp; Light Server</h1>\r\n")
  50. if (params["light"] ~= nil) then
  51. if ("0" == params["light"]) then
  52. gpio.write(pinLight, gpio.LOW)
  53. else
  54. gpio.write(pinLight, gpio.HIGH)
  55. end
  56. end
  57. if (params["s0"] ~= nil) then
  58. servo.value = tonumber(params["s0"]);
  59. end
  60. -- Close the connection for the request
  61. cn:close ( )
  62. end)
  63. end
  64. -- Build and return a table of the http request data
  65. function get_http_req (instr)
  66. local t = {}
  67. local str = string.sub(instr, 0, 200)
  68. local v = string.gsub(split(str, ' ')[2], '+', ' ')
  69. parts = split(v, '?')
  70. local params = {}
  71. if (table.maxn(parts) > 1) then
  72. for idx,part in ipairs(split(parts[2], '&')) do
  73. parmPart = split(part, '=')
  74. params[parmPart[1]] = parmPart[2]
  75. end
  76. end
  77. return params
  78. end
  79. -- Source: http://lua-users.org/wiki/MakingLuaLikePhp
  80. -- Credit: http://richard.warburton.it/
  81. function split(str, splitOn)
  82. if (splitOn=='') then return false end
  83. local pos,arr = 0,{}
  84. for st,sp in function() return string.find(str,splitOn,pos,true) end do
  85. table.insert(arr,string.sub(str,pos,st-1))
  86. pos = sp + 1
  87. end
  88. table.insert(arr,string.sub(str,pos))
  89. return arr
  90. end
  91. -- Configure the ESP as a station (client)
  92. wifi.setmode (wifi.STATION)
  93. wifi.sta.config (SSID, SSID_PASSWORD)
  94. wifi.sta.autoconnect (1)
  95. -- Hang out until we get a wifi connection before the httpd server is started.
  96. tmr.alarm (1, 800, 1, function ( )
  97. if wifi.sta.getip ( ) == nil then
  98. print ("Waiting for Wifi connection")
  99. else
  100. tmr.stop (1)
  101. print ("Config done, IP is " .. wifi.sta.getip ( ))
  102. end
  103. end)
  104. -- Create the httpd server
  105. svr = net.createServer (net.TCP, 30)
  106. -- Server listening on port 80, call connect function if a request is received
  107. svr:listen (80, connect)