Latest update: August 2016
In this tutorial, we’re going to cover the basics using the WiFi device from Lua. This includes scanning nearby networks and connecting to one, as well as uploading files once connected.
First, lets try listing the local networks and logging them to a file. This script is probably best run onBoot, and note that fa.Scan and fa.GetScanInfo may be unavailable if the FlashAir is already connected to a network.
--[[ This script will scan for available WiFi networks, and output their SSIDs to a file. ]]-- local logfile = "ssidList.txt" count = fa.Scan() -- Open the log file local outfile = io.open(logfile, "w") -- Write a header outfile:write("SSID list: \n") --Note that the ssids start at 0, and go to (count-1) for i=0, (count-1), 1 do ssid, other = fa.GetScanInfo(i) outfile:write(i..": "..ssid) end
Next, lets try scanning for a specific SSID, connecting to it when we see it, and triggering an upload script. In this case, we’ll trigger the dropbox tutorial from earlier.
--[[ This script will scan for a specific network, and if it sees it will connect... then trigger a dropbox uploading script! It logs to a file. ]]-- local logfile = "wifiConnect.txt" local SSID = "VITP_Open" -- SSID to connect to local PASSWORD = "" --A blank password should be used for an open network local uploader = "dropbox_tut.lua" -- Open the log file local outfile = io.open(logfile, "w") while true do -- Initiate a scan count = fa.Scan(SSID) --count = fa.Scan() found=0 --See if we found it if count > 0 then --ssid, other = fa.GetScanInfo(0) --Should return SSID provided for i=0, (count-1), 1 do found_ssid, other = fa.GetScanInfo(i) print(SSID.."|"..found_ssid) if SSID == found_ssid then --We did! print("Found: "..SSID.."... attempting to connect.") outfile:write("Found: "..SSID.."... attempting to connect.") found=1 break end end end if found == 1 then break end --If we found it, stop looping end --We should only get here if the above loop found the network --fa.Connect has no return, but if the SSID and password are correct it should work. fa.Connect(SSID, PASSWORD) --Execute the upload script dofile(uploader) --Close our log file outfile:close()
All sample code on this page is licensed under BSD 2-Clause License