Post your best Lua script examples here

User avatar
By phatjoe
#31222 Ahhhhh heeeelllp, haha.. I'm trying to build a init.lua script that will serve a simple webpage which just has a button which will activate and then deactivate the relay on a ESP8366-EVB board but I don't seem to be having much luck. I've tried various different scripts that I have found online but non seem to be doing the job. I can get the web server working easy enough but to get it the button on the page to call a function which toggles the correct GPIO seems to be rather challenging, haha.

Below is the code I am currently running at the bottom of my init.lua. The code above this just does the standard stuff to setup an AP and join my Wifi Network.

My question is how I code the web site to have a button which calls a function which then toggles a GPIO High and then Low after a certain timeout. Also on the ESP8266-EVB Board which GPIO do I need to trigger to open the relay?

Code: Select allsrv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive",function(conn,payload) print(payload)
    conn:send("HTTP/1.1 200 OK\n\n")
    conn:send("<html><body>")
    conn:send("<h1>Roy Garage</h1><BR>")
    conn:send("</html></body>")
    conn:on("sent",function(conn) conn:close() end)
  end)
User avatar
By forlotto
#31375 I am following your post will try to give you a little help here:

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("SSID","password")
wifi.sta.setip({ip="192.168.1.110",netmask="255.255.255.0",gateway="192.168.1.1"})
gpio.mode(1, gpio.OUTPUT)

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
    conn:on("receive", function(client,request)
        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>Garage Door Opener -forlotto</h1>";
        buf = buf.."<p>1 Pin5  <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              gpio.write(1, gpio.LOW);
end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


As far as what pin to use you need to use the pin that is triggering it on your relay or relay board.

You cannot run this without a relay board as well as the NodeMcu!

Refer to GPIO Table here > https://github.com/nodemcu/nodemcu-firmware

It is good practice not to use GPIO0,2,15 from my understanding!

This should help a bit

Be sure to read up on relay boards they require 5v dc most of them do not expect your NodeMcu to safely trigger the on off you may need a relay board or more hardware to make it work without it you may burn your nodemcu out.
User avatar
By Lawrence_jeff
#31472 I use a webpage with several buttons to send IR codes to my stereo, you can use this as a model. Once nice thing is it somewhat scales well for phone or PC use. So you could just cleanup the buttons and change the irsend call to one that flips the GPIO of your relay.

I don't have much in the way of error handling and how I read the button presses is not as nice as it could be but it seems to work reliably.

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("SID","PWD")
ip = wifi.sta.getip()
print(ip)
pin=12
--Sometimes the GPIO pin starts high and could burn out the LED
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.LOW)

--This is the default page, split up for ease of editing
reply = "HTTP/1.1 200/OK\r\nServer: NodeLuau\r\nContent-Type: text/html\r\n\r\n"
reply = reply.."<html><head><link rel='shortcut icon' href='about:blank'><meta name=viewport content='width=device-width, user-scalable=no'>"
reply = reply.."<title>ESP8266 Webserver</title></head><body><h1 style align='center'>Stereo&nbsp;Control</h1>"
reply = reply.."<img src='http://mlb-s2-p.mlstatic.com/receiver-pionner-vsx-454-11284-MLB20041925940_022014-O.jpg' width='99%'><br>"
reply = reply.."<button style='width: 100%; height: 12%; font-size:24px;' onclick=window.location.href='/ON'>Power Toggle</button><br>"
reply = reply.."<button style='width: 100%; height: 12%; font-size:24px;' onclick=window.location.href='/MUTE'>Mute</button><br>"
reply = reply.."<button style='width: 50%; height: 12%; font-size:24px;' onclick=window.location.href='/VU'>Volume Up</button>"
reply = reply.."<button style='width: 50%; height: 12%; font-size:24px;' onclick=window.location.href='/VD'>Volume&nbsp;Down</button><br>"
reply = reply.."<button style='width: 50%; height: 12%; font-size:24px;' onclick=window.location.href='/CD'>CD Input</button>"
reply = reply.."<button style='width: 50%; height: 12%; font-size:24px;' onclick=window.location.href='/TUN'>Tuner Input</button><br>"
reply = reply.."<button style='width: 50%; height: 12%; font-size:24px;' onclick=window.location.href='/PREV'>Prev Tuner Preset</button>"                                   
reply = reply.."<button style='width: 50%; height: 12%; font-size:24px;' onclick=window.location.href='/NEXT'>Next Tuner Preset</button><br>"
reply = reply.."</body></html>"


srv=net.createServer(net.TCP)
    srv:listen(80,function(conn)
      conn:on("receive",function(conn,payload)
        payload = string.sub(payload, 0, 20)
   
        if string.find(payload,"GET /ON") then
            dofile("irsend.lua").nec(pin, 0xA55A38C7)
       
        elseif string.find(payload,"GET /MUTE") then
            dofile("irsend.lua").nec(pin, 0xA55A48B7)
       
        elseif string.find(payload,"GET /NEXT") then
            dofile("irsend.lua").nec(pin, 0x25DA08F7)
           
        elseif string.find(payload,"GET /PREV") then
            dofile("irsend.lua").nec(pin, 0x25DA8877)
           
        elseif string.find(payload,"GET /VU") then
            dofile("irsend.lua").nec(pin, 0xA55A50AF)
           
        elseif string.find(payload,"GET /VD") then
            dofile("irsend.lua").nec(pin, 0xA55AD02F)
           
        elseif string.find(payload,"GET /TUN") then
            dofile("irsend.lua").nec(pin, 0xA55AE21D)
           
        elseif string.find(payload,"GET /CD") then
            dofile("irsend.lua").nec(pin, 0xA55A32CD)

        elseif string.find(payload,"GET /SND") then
            dofile("Sound.lua")
           
        else
            print("Home or unknown page requested")
        end
       
        conn:send(reply)
      end)
      conn:on("sent",function(conn) conn:close() end)
    end)
Attachments
LUAStereo.png
Screenshot of the webpage
User avatar
By forlotto
#31488 ahhh nice so you call lua files and store multiple files on your nodemcu and just call them as needed this would be the way to go for my project as well just have the init call other LUA files excellent.

I will look for a password using string.find searching the payload got to really start looking into more specifics shouldn't be long now I am a bit hung up on how one goes about making a function of your own that would execute a lot of checks.

I was declaring it wrong.

myfunction=
{
bunch of sub functions to make the my function
}

but every time the compiler gives me errors as soon as it hits the braces

I still don't know how to make a custom function that houses a lot of other functions...


Maybe another file being loaded would be the better idea.

check payload for worda or wordb
if word a
then load worda.lua
if word b
then load wordb.lua
else end

hrmmm still got a lot of learning to do myself I'll keep trucking.