Post your best Lua script examples here

User avatar
By Camelot
#82172 Hi there,
I currently try to run my first MQTT code, but I got a problem concerning the timer module. I get the following result when I run my script:

NodeMCU custom build by frightanic.com
branch: master
commit: 4905381c004bdeaf744a60bb940c1906c2e052d4
SSL: false
modules: dht,file,gpio,mqtt,net,node,tmr,uart,wifi
build created on 2019-05-04 10:28
powered by Lua 5.1.4 on SDK 2.2.1(6ab97e9)
lua: mqtt_connect.lua:19: attempt to call field ‚alarm‘ (a nil value)
stack traceback:
mqtt_connect.lua:19: in function ‚wait_for_wifi_conn‘
mqtt_connect.lua:85: in main chunk
[C]: in function ‚dofile‘
init.lua:1: in main chunk
[C]: ?

Obviously "alarm" is not known, but I flashed the newest Firmware release with the tmr module as you can see.
The function which causes this error is:
function wait_for_wifi_conn ( callback )
tmr.alarm (1, 1000, 1, function ( )
if wifi.sta.getip ( ) == nil then
print ("Waiting for Wifi connection")
else
tmr.stop (1)
print("\n====================================")
print ("ESP8266 mode is: " .. wifi.getmode ( ))
print ("The module MAC address is: " .. wifi.ap.getmac ( ))
print ("Config done, IP is " .. wifi.sta.getip ( ))
print("====================================")

callback()
end
end)
end

Have you got an idea?
Thanks!
User avatar
By paneton
#86409 Camelot: is a sintax error

your code :

tmr.alarm (1, 1000, 1, function ( )
if wifi.sta.getip ( ) == nil then
print ("Waiting for Wifi connection")
else
tmr.stop (1)

I add the curly brackets and the end in the else clausule

tmr.alarm (1, 1000, 1, function ( ) {
if wifi.sta.getip ( ) == nil then
print ("Waiting for Wifi connection")
else
tmr.stop (1)
end
})