Private Sub ChangeWifiSettings (newSSID As String, newPW As String, enabled As Boolean, strCountry As String)
BuildSupplicant(strCountry, newSSID, newPW) 'build new wpa_supplicant.conf file
Wait For BuildSupplicant_Complete
RebootPi 'I think I found other ways of restarting the wifi connection unreliable. You might have better luck.
End Sub
Private Sub BuildSupplicant (strCountry As String, newSSID As String, strPW As String)
Getpsk(newSSID, strPW)
Wait For shpsk_Complete 'strPhrase now has encrypted passphrase
'need to add handling if shpsk_ProcessCompleted success=false
Dim str As String = $"ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev"$ & CRLF & _
$"update_config=1"$ & CRLF & _
$"country="$ & strCountry & CRLF & _
CRLF & _
$"network={"$ & CRLF & _
$" ssid=""$ & newSSID & $"""$ & CRLF & _
$" "$ & strPhrase
File.WriteString("/etc/wpa_supplicant", "wpa_supplicant.conf", str)
CallSubDelayed(Me, "BuildSupplicant_Complete")
End Sub
'get encrypted psk for use in wpa_supplicant.conf file
Private Sub Getpsk (strSSID As String, strPW As String)
Dim str1, str2 As String
str1 = $"""$ & strSSID & $"""$
str2 = $"""$ & strPW & $"""$
sh.Initialize("shpsk", "wpa_passphrase", Array As String(str1, str2))
sh.WorkingDirectory = "/home/pi"
sh.Run(-1)
End Sub
Private Sub shpsk_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
Log("shpsk success" & CRLF & StdOut)
Dim idxPSK As Int = StdOut.LastIndexOf($"psk="$)
strPhrase = StdOut.SubString(idxPSK) 'strPhrase is Global Variable
Else
Log("shpsk shell Error: " & CRLF & StdErr)
End If
CallSubDelayed(Me, "shpsk_Complete")
End Sub
Private Sub RebootPi
sh.Initialize("shReboot", "reboot", Array As String("now"))
sh.WorkingDirectory = "/home/pi"
sh.Run(-1)
End Sub