-->
Page 1 of 3

Beginner NodeMCU/Lua question (init.lua and other .lua flow)

PostPosted: Mon Apr 13, 2015 12:11 pm
by salomonsk8r2003
BACKGROUND:
I am an average programming/electronics 'hobbyist'. I am not trained or anything, I have just been doing it for fun for the past few years, so my knowledge is limited. Most of my knowledge is Arduino based (like most noobs :) , so please think of that when you reply. Arduino references/comparisons will help me understand what's happening)

I am so frustrated with this ESP8266 ESP-01 and am about to give up on it (or more accurately, give up on myself). I get the thing working, everything is smooth, then suddenly nothing works! No change or anything, it all just STOPS. I've tried higher amperage sources and different things recommended online but its just unreliable for me (and believe me, I have tried every single thing mentioned online. Still inconsistent results).

Anyways, this question is more Lua/NodeMCU based. I am starting to think that I am programming it incorrectly, which brings me to my question about the Lua project flow.


MY REAL QUESTION:
I am currently putting ALL of my code in the init.lua because I dont know what else to do with it (remember, I'm a noob). I thought this was the right way to do it until I just saw something saying "the init.lua can not be compiled".... what???? What does that mean? I thought this init.lua booted up and just looped over and over like an Arduino "loop()", but now I am getting the hint that it is more like the Arduino's "Setup()" that runs once, then stops. Is this why I'm having terrible results? Am I only supposed to put a small amount of bootup 'one-time-code' in the init.lua and then put all my looping code somewhere else?


EXAMPLE CODE WITH ISSUES:
Ok, I got this example of a simple server that lets you turn on and off two LEDS online. I took that code that worked great and modified it for my use. The end goal is to open and close my garage. This is why the code sends a signal to GPIO-0 for 18 seconds, then if the door is in the wrong spot it does it again. This is because my door takes about 15 seconds for a full cycle.

The code works fine for like 2 executions, then the entire ESP stops serving the website and I have to completely RELOAD the code for it to work again. Resetting it doesn't work, I have to completely reload the code in ESPlorer. Also, sometimes I have to completely REFLASH IT!

Code: Select allwifi.setmode(wifi.STATION)
wifi.sta.config("ARDUINO123","ARDUINO123")
exec = 3
status = 4
current = ""
gpio.mode(exec, gpio.OUTPUT)
gpio.mode(status, gpio.OUTPUT)
gpio.write(exec, gpio.LOW)
srv=net.createServer(net.TCP)
srv:listen(9999,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
        if(gpio.read(status) == 1)then
            current = "OPEN"
        else
            current = "CLOSED"
        end
        local _open,_closed = "",""
        if(_GET.pin == "OPEN")then
              gpio.write(exec, gpio.HIGH);
              tmr.delay(1000000)
              gpio.write(exec, gpio.LOW);
              tmr.delay(18000000)
              if(gpio.read(status) == 0)then
                  gpio.write(exec, gpio.HIGH);
                  tmr.delay(1000000)
                  gpio.write(exec, gpio.LOW);
                  tmr.delay(18000000)
              end
        elseif(_GET.pin == "CLOSE")then
              gpio.write(exec, gpio.HIGH);
              tmr.delay(1000000)
              gpio.write(exec, gpio.LOW);
              tmr.delay(18000000)
              if(gpio.read(status) == 1)then
                  gpio.write(exec, gpio.HIGH);
                  tmr.delay(1000000)
                  gpio.write(exec, gpio.LOW);
                  tmr.delay(18000000)
              end
        end
        if(gpio.read(status) == 1)then
            current = "OPEN"
        else
            current = "CLOSED"
        end
        buf = buf.."<h1>Garage Door</h1>";
        buf = buf.."<h3>CURRENT STATUS : ";
        buf = buf..current;
        buf = buf.."</h3>";
        buf = buf.."<p><a href=\"?pin=OPEN\"><button>OPEN</button></a>&nbsp;<a href=\"?pin=CLOSE\"><button>CLOSE</button></a></p>";
        buf = buf.."<p>Note: Actions may take up to 40 seconds to complete.</p>"
        buf = buf.."<p>Do not press button again until page has refreshed</p>"
        client:send(buf)
        client:close()
        collectgarbage();
    end)
end)


Thank you for any help or advice. I know this was a pretty winded post, but I was about to throw my ESP in the trash after being so excited about it's potential. I figured since there is such a strong community behind this beautiful little thing that someone else with similar frustrations would be able to point me in the right direction. Otherwise I will be going back to the nrf24l01+ that the rest of my security system is built on (and which has been beautifully reliable for me).

Re: Beginner NodeMCU/Lua question (init.lua and other .lua f

PostPosted: Mon Apr 13, 2015 1:28 pm
by cal
Moin,

you didn't wrote what lua firmware you are using. I suggest not to use the latest march version because of memory
problems they will lead to reboots like you had.
Builds from dev branch:
https://github.com/nodemcu/nodemcu-firmware/releases

Carsten

Re: Beginner NodeMCU/Lua question (init.lua and other .lua f

PostPosted: Mon Apr 13, 2015 2:01 pm
by salomonsk8r2003
Thanks for the reply!

This is the .bin that I used to flash it

nodemcu_integer_0.9.5_20150318

That is the firmware right? It is the 18 March one. Is this bad?

Re: Beginner NodeMCU/Lua question (init.lua and other .lua f

PostPosted: Mon Apr 13, 2015 2:02 pm
by salomonsk8r2003
So when you say it leads to needing reboots, do you mean that the memory fills up and you have to power-cycle the device to get it back? Because mine is so bad that power-cycle doesnt work, I have to actually plug the FTDI back in and reload the exact same code and hit the restart button two or three times. Then it works again (sometimes).