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

User avatar
By TerryE
#17555 The version of Lua is optimized for embedded use so ROMable objects like table are kept in ROM and so can't be extended. table.contains function . . . is the same as table["contains"] = function . . . and you can't do this.

Anyway the simplest way to test if table X contains element Y is just to say
Code: Select allif X[Y] then
  -- . . .
end
User avatar
By BenFH
#17841 I'm sorry but I can't get this to work :| :oops:

I tried this code:
Code: Select allprint("Scanning for networks..")
  function listap(tbl)
      for k,v in pairs(tbl) do
        print(k.." : "..v)
      end
    end
    wifi.sta.getap(listap)
   -- tmr.delay(10000) --> use tmr.alarm instead
    tmr.alarm(0, 10000, 0, function()
--Connect if network available, create if not found 

          if tbl["TestAP"] then
          tmr.stop(0)
                print("Found Network TestAP")
               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 
           tmr.stop(0)
              print("Network not Found! Creating..")
              cfg={}
             cfg.ssid="TestAP"
             cfg.pwd="87654321"
             wifi.ap.config(cfg)
             print(wifi.ap.getip())
 end
 end )


But it still doesnt 'find' my TestAP and returns "Network not Found! Creating.." :cry:

And just to check i used the following bit:
Code: Select alllocal tbl = {"bacon","apples","TestAP"}     
print("Concatenated string ",table.concat(tbl,", "))
   if tbl["TestAP"] then
print("it worked!")
  else
print("Not found")
end


Output is:
Concatenated string bacon, apples, TestAP
Not found
User avatar
By BenFH
#17844 Okay so with my very limited knowledge I figured out that the issue might be the tbl table is not defined outside of the function? So I included the code inside the listap function and it seems to work!! :D

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

local tbl = {}

--Scan for available networks
print("Scanning for networks..")
  function listap(tbl)
      for k,v in pairs(tbl) do
        print(k.." : "..v)
      end
       if tbl["TestAP"] then
                print("Found Network TestAP")
               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="TestAP"
             cfg.pwd="87654321"
             wifi.ap.config(cfg)
             print(wifi.ap.getip())
     end
    end
    wifi.sta.getap(listap)


So I need to define the table 'tbl' outside of the function listap for it to work if I don't want everyting in thr listap function?
User avatar
By MicKeyCZ
#17850 Hi,
try something like this
Code: Select allssid = 'MySSID'
found = nil
wifi.setmode(wifi.STATIONAP)
wifi.sta.getap(function(t)
    --for k,v in pairs(t) do print(k.." : "..v) end 
    found = t[ssid] or false
end)
tmr.alarm(0, 500, 1, function()
    if (found == nil) then
        print('scanning...')
    else
        tmr.stop(0)
        if (found == false) then
            print('not found')
        else
            print('found', ssid, found)
        end
    end
end)


M