examples/wifinina: improve connectToAP() and other needed minor corrections

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2023-05-02 19:27:51 +02:00
committed by Ron Evans
parent 5f25a4f6be
commit 66abe9c28b
10 changed files with 239 additions and 97 deletions
+23 -9
View File
@@ -61,6 +61,7 @@ func main() {
waitSerial()
connectToAP()
displayIP()
// now make UDP connection
ip := net.ParseIP(ntpHost)
@@ -149,29 +150,42 @@ func clearBuffer() {
}
}
const retriesBeforeFailure = 3
// connect to access point
func connectToAP() {
time.Sleep(2 * time.Second)
println("Connecting to " + ssid)
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
if err != nil { // error connecting to AP
for {
println(err)
time.Sleep(1 * time.Second)
var err error
for i := 0; i < retriesBeforeFailure; i++ {
println("Connecting to " + ssid)
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
if err == nil {
println("Connected.")
return
}
}
println("Connected.")
// error connecting to AP
failMessage(err.Error())
}
time.Sleep(2 * time.Second)
func displayIP() {
ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
message(err.Error())
time.Sleep(1 * time.Second)
}
message(ip.String())
message("IP address: " + ip.String())
}
func message(format string, args ...interface{}) {
println(fmt.Sprintf(format, args...), "\r")
}
func failMessage(msg string) {
for {
println(msg)
time.Sleep(1 * time.Second)
}
}