As the title says... Chat on...

User avatar
By BenFH
#16867 Hi guys

I'm completely new and just ditched the AT Firmware and flashed NodeMCU on my ESPs, so please bare with me ;)
Im using NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4

My plan is to let the ESP scan for available networks when powering up, then check if a specific network already exists.
-If it does, it should join this network
-If it didn't find it, create it itself


Here is the (not working) code I am using:
Code: Select allprint("Set up wifi mode..")
wifi.setmode(wifi.STATIONAP)
wifi.sta.config("MyTest","87654321")


     
--Scan for available networks
print("Scanning for networks..")
  function listap(t)
      for k,v in pairs(t) do
        print(k.." : "..v)
      end
    end
    wifi.sta.getap(listap)
    tmr.delay(10000)
--Connect if network available, create if not found   
    if listap == "MyTest" then
        print("Found Network MyTest")
        wifi.sta.connect()

                   tmr.alarm(1, 1000, 1, function()
                     if wifi.sta.getip()== nil then
                       print("IP unavaiable, Waiting...")
                          else
                          tmr.stop(1)
                        print("Config done, IP is "..wifi.sta.getip())
                           --dofile("yourfile.lua")
                             end
                         end)
 
    else  print("Network not Found! Creating..")
          cfg={}
          cfg.ssid="MyTest"
          cfg.pwd="87654321"
          wifi.ap.config(cfg)
          print(wifi.ap.getip())
 end


I see multiple issues, but since I'm completely new to lua i don't know how to fix them and so far didn't have any luck with google. :?

1. Because the wifi.sta.getap command needs a couple of seconds, I added a delay. But I'm not sure if this is the right way and works as it should? I simply wanted the following code to execute when the scanning for networks is finished.
2. I am then trying to see if the desired network is in this list (with: if listap == "MyTest" then). But this is surely the wrong way. How do I search the created table the right way so that if the network MyTest is there, it connects to this, rather than creating a new network with the same name?

This is what I get from the ESPlorer:
Code: Select all> dofile("init.lua");
Set up wifi mode..
Scanning for networks..
Network not Found! Creating..
192.168.4.1   255.255.255.0   192.168.4.1
> randomAP: 3,-64,00:26:f2:f7:cf:1c,3
>randomAP2:3,-64,00:26:f2:f7:cf:1c,3


Any Help would be greatly appreciated! Thank you for any tips! :)
User avatar
By TerryE
#16969 Don't use tmr.delay in this use case. It disables interrupts in order to do an accurate delay. Use a timer.alarm with a callback.
User avatar
By BenFH
#17226 Thanks for the reply!

I tried with a modified version and added the tmr.alarm as you suggested:

Code: Select allprint("Set up wifi mode..")
wifi.setmode(wifi.STATIONAP)
wifi.sta.config("TestAP","87654321")


     
--Scan for available networks
print("Scanning for networks..")
  function listap(t)
      for k,v in pairs(t) do
        print(k.." : "..v)
      end
    end
    wifi.sta.getap(listap)
   -- tmr.delay(10000) --> use tmr.alarm instead

   tmr.alarm(1,10000, 1, function()
   print("Test")
 
 function table.contains(listap, TestAP)
  for _, value in pairs(listap) do
    if value == TestAP then
      print("success")
    end
  end
  print("Not Found")
end
end)


I get the following output:
Code: Select allSet up wifi mode..
Scanning for networks..
> AP1: 0,-40,88:75:56:87:5a:54,11
AP2 : 0,-39,88:75:56:87:5a:53,11
AP3 : 0,-40,88:75:56:87:5a:52,11
Test
Test
Test
Test


The tmr.alarm is a good start as i get the table before any of the following lines are executed.

However..the function 'table.contains' was supposed to read the values in the table and decide if the specific AP is in there. It seems however that this function doesn't execute as i get neither 'success' nor 'not found'?!

Why is that and do you see any solution?

EDIT:
I also tried the Example function (from http://wiki.mudlet.org/w/Manual:Lua_Fun ... _Functions)
Code: Select alllocal test_table = { "value1", "value2", "value3", "value4" }
if table.contains(test_table, "value1") then
   echo("Got value 1!")
else
   echo("Don't have it. Sorry!")
end


which returns: "PANIC: unprotected error in call to Lua API (init.lua:21: attempt to call field 'contains' (a nil value))"

Is .contains not implemented in the NodeMCU?


Thanks for your help
Ben