Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wifinina: connect improvements #561

Merged
merged 3 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions examples/wifinina/connect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,29 @@ func waitSerial() {
}
}

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 failMessage(msg string) {
for {
println(msg)
time.Sleep(1 * time.Second)
}
}
31 changes: 23 additions & 8 deletions examples/wifinina/http-get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func main() {
waitSerial()

connectToAP()
displayIP()

// You can send and receive cookies in the following way
// import "tinygo.org/x/drivers/net/http/cookiejar"
Expand Down Expand Up @@ -131,28 +132,42 @@ func waitSerial() {
}
}

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)
}
}
24 changes: 16 additions & 8 deletions examples/wifinina/mqttclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func main() {
adaptor.Configure()

connectToAP()
displayIP()

opts := mqtt.NewClientOptions()
opts.AddBroker(server).SetClientID("tinygo-client-" + randomString(10))
Expand Down Expand Up @@ -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
Expand Down
25 changes: 16 additions & 9 deletions examples/wifinina/mqttsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func main() {
adaptor.Configure()

connectToAP()
displayIP()

opts := mqtt.NewClientOptions()
opts.AddBroker(server).SetClientID("tinygo-client-" + randomString(10))
Expand Down Expand Up @@ -113,27 +114,33 @@ func publishing() {
}
}

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() {
println(err.Error())
time.Sleep(1 * time.Second)
}
println(ip.String())
println("IP address: " + ip.String())
}

// Returns an int >= min, < max
Expand Down
32 changes: 23 additions & 9 deletions examples/wifinina/ntpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func main() {
waitSerial()

connectToAP()
displayIP()

// now make UDP connection
ip := net.ParseIP(ntpHost)
Expand Down Expand Up @@ -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)
}
}
32 changes: 23 additions & 9 deletions examples/wifinina/tcpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func main() {
adaptor.Configure()

connectToAP()
displayIP()

for {
sendBatch()
Expand Down Expand Up @@ -111,29 +112,42 @@ func sendBatch() {
conn.Close()
}

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(msg string) {
println(msg, "\r")
}

func failMessage(msg string) {
for {
println(msg)
time.Sleep(1 * time.Second)
}
}
32 changes: 23 additions & 9 deletions examples/wifinina/tlsclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func main() {
waitSerial()

connectToAP()
displayIP()

for {
readConnection()
Expand Down Expand Up @@ -121,29 +122,42 @@ func makeHTTPSRequest() {
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())
}

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(msg string) {
println(msg, "\r")
}

func failMessage(msg string) {
for {
println(msg)
time.Sleep(1 * time.Second)
}
}
Loading