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
+25 -10
View File
@@ -20,8 +20,8 @@ var (
pass string
)
// IP address of the server aka "hub". Replace with your own info.
const server = "tinygo.org"
// IP address of the "example.com" server. Replace with your own info.
const server = "93.184.216.34"
// these are the default pins for the Arduino Nano33 IoT.
// change these to connect to a different UART or pins for the ESP8266/ESP32
@@ -63,6 +63,7 @@ func main() {
waitSerial()
connectToAP()
displayIP()
for {
readConnection()
@@ -123,28 +124,42 @@ func makeHTTPRequest() {
lastRequestTime = time.Now()
}
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() {
message(err.Error())
time.Sleep(1 * time.Second)
}
message(ip.String())
message("IP address: " + ip.String())
}
func message(msg string) {
println(msg, "\r")
}
func failMessage(msg string) {
for {
println(msg)
time.Sleep(1 * time.Second)
}
}