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

User avatar
By BenFH
#18482 Thanks MicKeyCZ this code looks a lot better!

I am now using the following to achieve my task and so far it worked very well.

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

ssid = 'TestAP'
found = nil
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("Network not Found! Creating..")
             cfg={}
             cfg.ssid="TestAP"
             cfg.pwd="87654321"
             wifi.ap.config(cfg)
             print(wifi.ap.getip())
             dofile("tcpserver.lua")
             print("Opening File tcpserver.lua")

        else
            print('found', ssid, found)
    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("tcpclient.lua")
                           print("Opening File tcpclient.lua")
                             end
                         end)

        end
    end
end)



Thanks for all your help! :D
User avatar
By danbicks
#18866 Hi Guys,

Don't know if this helps, this is what my code actually does. You specify AP name to find, if found in scan it will then try to attempt connection. Uses the good old SSD1306 OLED to indicate operation and once connected to AP displays IP address.

Written in Arduino no nodemcu!

Post: WIFI SCAN AND ACQUIRE OLED

Enjoy
User avatar
By dnc40085
#18918
danbicks wrote:Don't know if this helps, this is what my code actually does. You specify AP name to find, if found in scan it will then try to attempt connection. Uses the good old SSD1306 OLED to indicate operation and once connected to AP displays IP address.

NodeMCU is an interpreted language(Lua) where as Arduino(C/C++) is a compiled language, meaning that in order to write Lua scripts(similar to sketches), the firmware must be first compiled then burned , then use the serial terminal to write your Lua scripts.

The problem here is that a feature in the SDK function wifi_station_scan, the ability to set a scan configuration, was not exposed in the wifi module(similar to the ESP8266WiFi library). In order to check for a specific AP, one had to call wifi.sta.getap(), then wait for it to return a list of the available APs, then find the relevant AP by processing the list. Which is doable, but takes much longer than just setting a scan configuration.