0

I am trying to build this project: https://www.instructables.com/id/Motorized-WiFi-IKEA-Roller-Blind/

having had some problems and still do... this is my wifi_setup.lau

-- file: setup.lua
local module = {}

config = {}
config.SSID = {}
config.SSID["ssid"] = "password"

function wifi_wait_ip()
  if wifi.sta.getip() == nil then
    print("IP unavailable, Waiting...")
  else
    tmr.stop(1)
    print("\n====================================")
    print("ESP8266 mode is: " .. wifi.getmode())
    print("MAC address is: " .. wifi.ap.getmac())
    print("IP is " .. wifi.sta.getip())
    print("====================================")
  end
end

function wifi_start(list_aps)
  if list_aps then
    for key, value in pairs(list_aps) do
      if config.SSID and config.SSID[key] then
        wifi.setmode(wifi.STATION);
        wifi.sta.config{ssid=key, pwd=config.SSID[key]}
        --wifi.sta.connect()
        print("Connecting to " .. key .. " ...")
        tmr.alarm(1, 2500, 1, wifi_wait_ip)
      end
    end
  else
    print("Error getting AP list")
  end
end

function module.start()  
  print("Configuring Wifi ...")
  wifi.setmode(wifi.STATION);
  wifi.sta.getap(wifi_start)
end

return module

But this is the output:

Configuring Wifi ...
> Connecting to ssid ...
PANIC: unprotected error in call to Lua API (wifi_setup.lua:29: attempt to call field 'alarm' (a nil value))

So anything is wrong with the -String

What can I do?

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
rasae
  • 1
  • Check that this isn't a duplicate of https://stackoverflow.com/questions/59147393/lua-init-lua15-attempt-to-call-method-alarm-a-nil-value – Ben T Apr 22 '20 at 12:14
  • Does this answer your question? [lua: init.lua:15: attempt to call method 'alarm' (a nil value)](https://stackoverflow.com/questions/59147393/lua-init-lua15-attempt-to-call-method-alarm-a-nil-value) – Ben T Apr 23 '20 at 06:57

1 Answers1

0

Thank You Ben for the Hint. I think I managed to correct the problem. For anyone else looking for it - and I hope I coded correctly - here the solution:

`{    -- file: setup.lua
local module = {}
config = {}
config.SSID = {}
config.SSID["ssid"] = "password"

-- create a timer object
local tObj = tmr.create()
-- register an alarm


function wifi_wait_ip()
  if wifi.sta.getip() == nil then
    print("IP unavailable, Waiting...")
  else
    --tmr.stop(1)
    tObj:unregister()
    print("\n====================================")
    print("ESP8266 mode is: " .. wifi.getmode())
    print("MAC address is: " .. wifi.ap.getmac())
    print("IP is " .. wifi.sta.getip())
    print("====================================")
  end
end

function wifi_start(list_aps)
  if list_aps then
    for key, value in pairs(list_aps) do
      if config.SSID and config.SSID[key] then
        wifi.setmode(wifi.STATION);
        wifi.sta.config{ssid=key, pwd=config.SSID[key]}
       --wifi.sta.connect()
        print("Connecting to " .. key .. " ...")
       --tmr.create(1, 2500, 1, wifi_wait_ip)
        tObj:alarm(2000, tmr.ALARM_AUTO, wifi_wait_ip)
      end
    end
  else
    print("Error getting AP list")
  end
end

function module.start()  
  print("Configuring Wifi ...")
  wifi.setmode(wifi.STATION);
  wifi.sta.getap(wifi_start)
end

return module`
rasae
  • 1