Chat about current Lua tools and IDEs

User avatar
By forlotto
#31700 Try this finally got a page running ;)

Still technically a server by all rights but just hosted a single page to do some on off with GPIO technically not a hijack but it is a good base to start with when messing around and trying to figure stuff out.

Code: Select all-- figure out how to read and display output
-- when you run gpio.write(3,gpio.high) It is actually turning gpio 0 on!!!


wifi.setmode(wifi.STATION)
wifi.sta.config("ssid","pass")
print(wifi.sta.getip())
led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
    local buf3 = "<p>on</p>";
    local buf2 = "<p>off</p>";
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        end
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
            end
        end
       
        buf = buf.."<h1> ESP8266 Web Server</h1>";
        buf = buf.."<p>GPIO0 <a href=\"?pin=OFF1\"><button>OFF</button></a>&nbsp;<a href=\"?pin=ON1\"><button>ON</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=OFF2\"><button>OFF</button></a>&nbsp;<a href=\"?pin=ON2\"><button>ON</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.LOW)  do client:send(buf3) ; end
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.HIGH)  do client:send(buf2) ; end
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.LOW)  do client:send(buf3) ; end
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.HIGH)  do client:send(buf2) ; end
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)
Last edited by forlotto on Wed Oct 21, 2015 9:08 pm, edited 3 times in total.
User avatar
By xtal
#31908 I found the following code, looks like it will save memory. Tried inserting the Wpage code I want, but could nat make it work. I'm using POST this uses GET ..
Have not figuered it out yet. NOTE the local vars/ functions are 1st then code at bottom.
node.heap results execellent.
Wish the code was commented for clarity.....




Code: Select all-- Simple NodeMCU web server (done is a not so nodeie fashion :-)
--
-- Written by Scott Beasley 2015
-- Open and free to change and use. Enjoy.
--

-- Your Wifi connection data
local SSID = "xxxxxxxx"
local SSID_PASSWORD = "xxxxxxxx"

local function connect (conn, data)
   local query_data

   conn:on ("receive",
      function (cn, req_data)
         query_data = get_http_req (req_data)
         print (query_data["METHOD"] .. " " .. " " .. query_data["User-Agent"])
         cn:send ("Hello World from ESP8266 and NodeMCU!!")
         -- Close the connection for the request
         cn:close ( )
      end)
end

function wait_for_wifi_conn ( )
   tmr.alarm (1, 1000, 1, function ( )
      if wifi.sta.getip ( ) == nil then
         print ("Waiting for Wifi connection")
      else
         tmr.stop (1)
         print ("ESP8266 mode is: " .. wifi.getmode ( ))
         print ("The module MAC address is: " .. wifi.ap.getmac ( ))
         print ("Config done, IP is " .. wifi.sta.getip ( ))
      end
   end)
end

-- Build and return a table of the http request data
function get_http_req (instr)
   local t = {}
   local first = nil
   local key, v, strt_ndx, end_ndx

   for str in string.gmatch (instr, "([^\n]+)") do
      -- First line in the method and path
      if (first == nil) then
         first = 1
         strt_ndx, end_ndx = string.find (str, "([^ ]+)")
         v = trim (string.sub (str, end_ndx + 2))
         key = trim (string.sub (str, strt_ndx, end_ndx))
         t["METHOD"] = key
         t["REQUEST"] = v
      else -- Process and reamaining ":" fields
         strt_ndx, end_ndx = string.find (str, "([^:]+)")
         if (end_ndx ~= nil) then
            v = trim (string.sub (str, end_ndx + 2))
            key = trim (string.sub (str, strt_ndx, end_ndx))
            t[key] = v
         end
      end
   end

   return t
end

-- String trim left and right
function trim (s)
  return (s:gsub ("^%s*(.-)%s*$", "%1"))
end

-- Configure the ESP as a station (client)
wifi.setmode (wifi.STATION)
wifi.sta.config (SSID, SSID_PASSWORD)
wifi.sta.autoconnect (1)

-- Hang out until we get a wifi connection before the httpd server is started.
wait_for_wifi_conn ( )

-- Create the httpd server
svr = net.createServer (net.TCP, 30)

-- Server listening on port 80, call connect function if a request is received
svr:listen (80, connect)

User avatar
By forlotto
#31977 http server project looks interesting.
Last edited by forlotto on Wed Oct 21, 2015 9:02 pm, edited 1 time in total.