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
+16 -8
View File
@@ -65,6 +65,7 @@ func main() {
adaptor.Configure()
connectToAP()
displayIP()
opts := mqtt.NewClientOptions()
opts.AddBroker(server).SetClientID("tinygo-client-" + randomString(10))
@@ -101,26 +102,33 @@ func main() {
println("Done.")
}
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())
}
func displayIP() {
ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
println(err.Error())
time.Sleep(1 * time.Second)
}
println(ip.String())
println("IP address: " + ip.String())
}
// Returns an int >= min, < max