Why is wifi.sta nil even after wifi.setmode(wifi.STATIONAP)?

EDIT: what I'm trying to do is essentially configure the station after the softap is running a TCP server.

I get a panic error for wifi.sta being nil when I call wifi.sta.config(station_cfg) even after I configured it properly before.

When I do:

function connectHib()
   wifi.setmode(wifi.STATIONAP)
   [AP config here]
   station_cfg={}
   station_cfg.ssid = ""
   station_cfg.pwd = ""
   station_cfg.save = false
   station_cfg.auto = false   
   wifi.sta.config(station_cfg)
end

It works fine, but when I call it, then start a server with srv=net.createServer(net.TCP) and then call the following function:

function validateSTA()
   station_cfg={}
   station_cfg.ssid = _G.wifi
   station_cfg.pwd = _G.senha
   station_cfg.save = false
   station_cfg.auto = false
   wifi.sta.config(station_cfg) -- this creates an error
   wifi.sta.connect()
end

It gives me a PANIC error on the commented line. I'm just trying to reconfigure the STATION module only and then tell it to connect so I can validate the conection.

I'm using:

NodeMCU custom build by frightanic.com
    branch: master
    commit: 67027c0d05f7e8d1b97104e05a3715f6ebc8d07f
    SSL: false
    modules: adc,file,gpio,net,node,pwm,sjson,tmr,uart,wifi
 build created on 2018-04-16 13:55
 powered by Lua 5.1.4 on SDK 2.2.1(cfd48f3)

What bugs me is why it seems like it has not been "declared" before, but it works on the first function... I thought wifi. was global like _G. variables.

It also works if I call the functions separately on the ESPlorer command line on an ESP without my init.lua.

I also tried the debug firmware and the only odd thing I saw poping up was wifi_event_monitor_handle_event_cb that is called every 4 secconds or so.

Thanks guys, any help is apreciated as I'm stuck about a week now.

Upvotes: 0

Views: 271

Answers (1)

Dimitry
Dimitry

Reputation: 2348

Like many other script languages, lua doesn't have declarations, only assignments. You should perform checks what is wifi's value during the first and the second calls. It seems that at first you are using it as a table, and in the second function you try to use it as a string in the station_cfg.ssid = _G.wifi.

Or the reason is that global variables are not "like _G", they're stored in _G.

Upvotes: 0

Related Questions