-->
Page 1 of 2

NodeMCU noob question

PostPosted: Fri Apr 17, 2015 5:03 pm
by salomonsk8r2003
Asked this before, think the post was too long... Here is the original: viewtopic.php?f=24&t=2468

Ill try to say it in less words now...

How does the NodeMCU structure work as far as files/code goes? What I mean is, I only know Arduino really (noob) and I am putting everything I write in Lua into the init.lua. Is this wrong? Is "Init.lua" similar to Arduino's "Setup()"? Should I put my "loop()" code somewhere else? How do you do that? Can someone explain to me how this works or point me to a good tutorial that will help someone like me that doesn't know much.a

I ask because my ESP-01 works for a few min then stops. Its extremely inconsistent. My first inclination is that I dont know what the hell I'm doing.

Re: NodeMCU noob question

PostPosted: Fri Apr 17, 2015 5:31 pm
by j0hncc
The short answer is:
1) Yes init.lua is like setup(). It runs once when it "boots".
2) You shouldn't have loop() or long (1 second or more) tmr.delay() in your code. Remember "blink without delay()"? If not you should look it up in the Arduino world. It's kinda like that.

The structure of the code you posted does not loop nor expect to be "loop"ed, it's setting up functions to be called when things happen.
AND, you shouldn't have any of those tmr.delay(18000000) in there. It grinds things to a halt. Instead you should set up an alarm( 1, 18000000, 0, someFunction). If you don't understand then see #2 above.

One more nuance is that if your code is messed up in "init.lua", you may not be able to proceed without re-flashing because whenever it boots it immediately runs your errant code. To get around this the recommended structure is to have two files-- one is "init.lua" and the other is say called "yourcode.lua". The code in "init.lua" pauses 10 seconds and then calls "yourcode.lua". that gives you 10 seconds to intervene if necessary by *removing* "init.lua" before the 10 seconds is up.
file "init.lua":
Code: Select all-- wait 10000 msec then call yourfile.lua
tmr.alarm(1, 10000, 0, function() dofile("yourfile.lua") end )
file "yourcode.lua":
Code: Select all-- this is the code that you had before in init.lua
wifi.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)
etc, etc, etc

Hope this helps!
John

Re: NodeMCU noob question

PostPosted: Sat Apr 18, 2015 12:07 pm
by Toshi Bass
Here's the init.lua I tend to use:

tmr.alarm(1,2000, 1, function()
if wifi.sta.getip()==nil then
print(" Wait for IP --> "..wifi.sta.status())
else
print("IP address is "..wifi.sta.getip())
tmr.stop(1)
print('main.lua')
dofile('main.lua')
end
end)

It just ensures the wifi connection is connected then starts my "main" code.

Hope its helpful

Re: NodeMCU noob question

PostPosted: Sat Apr 18, 2015 1:49 pm
by salomonsk8r2003
VERY usefull information for me! Thank you both so much!!!

I am really glad that you were able to find some stuff I was doing wrong. I was afraid that it was my device but am glad that it's just me (that means I can fix the problem!)

I am at work right now but when I get home tonight I am going to read into the timer/alarm thing so I can better understand it and then make the changes you recommended.

Very excited, thanks again!! :D :D :D :D :D