Post your best Lua script examples here

User avatar
By JeremiahLandi
#34033 I am making a website that controls LEDs. I would like to style it and have it hosted on the ESP8266.

I am afraid of running out of room. I was wondering if anyone had an opinion on how to approach this. I was thinking of installing an SD card slot and storing the website on there and just calling it when someone hit the site.

What would everyone's opinion be on this matter? I do not want to go with an IoT hosted service.
User avatar
By forlotto
#34035 first off it is LUA...

Second it is not quite as easy as hosting a website padawon.

You have much reading to do the task will be longer than you figure there are things you must learn before you can start walking running etc...

The NodeMCU is a great device but requires a decent amount of patience and a steeper learning curve than one originally figures.

Some good things to learn from is example code that already exists yes you need tight code! Not so much for storage space as for memory though it is memory which will get you on the esp8266 long before storage.

It isn't quite as simple as hosting a webpage btw you basically have to buffer out a webpage.

Now on to the nuts and bolts some learning material for you.

https://github.com/nodemcu/nodemcu-firm ... mcu_api_en
wiki/doku.php?id=start
http://www.lua.org/start.html

study examples look at what has been done.

Search for videos on youtube.

Search for code examples.

Look at interesting things that are already out there and learn from them.

Follow the references I have given you.

Try something simple to begin with light turning the LED on and OFF on your nodemcu...

And when you do figure something out share it and help others pay it forward.

I am by far and large still very new and learning only a couple of months into this maybe two hours a day avg just hammering away at reading and trying different things don't expect it will happen over night or you will get it instantly because you will defeat yourself set small goals in learning for the day.

And most of all enjoy what you are doing!

All of the work, testing, reading will pay off.

Enjoy and good luck!
User avatar
By JeremiahLandi
#34280 Thank you!

I have been reading up and working on this project:

viewtopic.php?f=19&t=990

I'm just not a 100% sure how conn:send works. I just didn't know if I could treat the code like HTML or not?

I have read up on Arduino and seen people approach projects that way. I just wasn't able to find it on Lua. :/
User avatar
By forlotto
#34335 Here I got this basic one for you to mess with this should give you an good grasp on things.

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("YOUR_NETWORK_NAME","YOUR_NETWORK_PASSWORD")
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 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=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
        buf = buf.."<p>GPIO2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
        local _on,_off = "",""
        if(_GET.pin == "ON1")then
              gpio.write(led1, gpio.HIGH);
        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.LOW);
        end
        client:send(buf);
        client:close();
        collectgarbage();
    end)
end)


Enjoy ;) try it out first then study the code and figure out just what it is the code is doing from there build on in this makes a great framework for anyone starting off.
Hope this will get you going this is what helped me start to see the light and get motivated a bit.

Anyhow wish you the best like I said it won't happen over night most likely don't get discouraged or frustrated to much leave it and come back to it if need be remember this is supposed to be fun and relaxing.