With it clear that an Arduino would not support my project and the recommendation that I should upgrade to a RPI covered in the thread rMQTT HostName with query parameters and despite my previous less than satisfactory experience with RPi projects, I'm pleasantly surprised with progress, however I'm going to need the support of the B4X community with a couple of roadhumps.
The script from the thread Raspberry PI - setup as WiFi Access Point and Client has enabled me to set up my RPi as an access point and run a server on it to allow configuration of the device through a web app. First I want to configure and connect to a preferred/user selectable WiFi network.
I have successfully collected the SSIDs available to the RPI by running a script containing the command sudo iwlist wlan0 scan:
so the user can select the preferred WiFi network they want to log into from a dropdown list and enter the password via a simple browser interface:
However while I have the SSID and password in FormHandler, I am unsure how I can "input" those to the RPi so it logs into the preferred SSID.
There are several issues I'm grappling with:
It may be that the above approach is wrong and won't work, and so I'd appreciate comments as to whether the approach would work and if so what is the correct command, or what a more appropriate approach would be, for instance would effectively editing wpa_supplicant.conf and then restarting wlan0 work.
The script from the thread Raspberry PI - setup as WiFi Access Point and Client has enabled me to set up my RPi as an access point and run a server on it to allow configuration of the device through a web app. First I want to configure and connect to a preferred/user selectable WiFi network.
I have successfully collected the SSIDs available to the RPI by running a script containing the command sudo iwlist wlan0 scan:
B4X:
Sub ScanForWiFiNetworks
Log("Scanning networks . . ")
Try
shl.Initialize("shl", "./scanwifi.sh", Null)
shl.WorkingDirectory = "/home/pi"
shl.Run(-1)
Catch
Log(LastException.Message)
End Try
End Sub
Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
Dim index As Int = 10
Dim start As Int = 0
Dim finish As Int
WiFiList.Clear
Do While index < StdOut.LastIndexOf("ESSID:")
start = StdOut.IndexOf2("ESSID:", index) + 7
finish = StdOut.IndexOf2("""", start + 2)
Log(StdOut.SubString2(start, finish))
WiFiList.Add(StdOut.SubString2(start, finish))
index = finish + 2
Loop
Else
Log("Shell Command Error: " & StdErr)
End If
End Sub
so the user can select the preferred WiFi network they want to log into from a dropdown list and enter the password via a simple browser interface:
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
resp.Write("<!DOCTYPE html>")
resp.Write("<html>")
resp.Write("<body>")
resp.Write("<h2>Select the preferred WiFI Network</h2>")
resp.Write("<form action=""/FormHandler"">")
resp.Write("<label For=""ssid"">Choose a network: </label>")
resp.Write("<Select name=""ssid"" id=""ssid"">")
For Each network As String In Main.WiFiList
resp.Write("<option value=" & network & ">" & network & "</option>")
Next
resp.Write("</Select>")
resp.Write("<br><br><label For=""pwd"">Password:</label><br>")
resp.Write("<input Type=""password"" id=""pwd"" name=""pwd""><br><br>")
resp.Write("<input Type=""submit"" value=""Submit""><br><br>")
resp.Write("</form>")
resp.Write("</body>")
resp.Write("</html>")
End Sub
However while I have the SSID and password in FormHandler, I am unsure how I can "input" those to the RPi so it logs into the preferred SSID.
There are several issues I'm grappling with:
- Despite my research I'm uncertain what command I need to execute to cause the RPi to log into the selected SSID or if indeed there is one.
- It seems the only way to execute the command would be to put it in a Bash script file and run it, similar to the method used in the ScanForWiFiNetworks sub above
- However the script file would need to be updated each time the user selects the SSID and inputs their password, and I wonder if it would retain its executable status each time it is updated.
It may be that the above approach is wrong and won't work, and so I'd appreciate comments as to whether the approach would work and if so what is the correct command, or what a more appropriate approach would be, for instance would effectively editing wpa_supplicant.conf and then restarting wlan0 work.