Skip to content

Commit

Permalink
wifi/espat, rtl8720dn, wifinina: move towards standard common interfa…
Browse files Browse the repository at this point in the history
…ce for wifi adaptors

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jan 24, 2022
1 parent 905fc6f commit d8c813d
Show file tree
Hide file tree
Showing 28 changed files with 187 additions and 102 deletions.
19 changes: 19 additions & 0 deletions espat/wifidriver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package espat

import (
"time"

"tinygo.org/x/drivers"
)

func (d *Device) ConnectToAccessPoint(ssid, pass string, timeout time.Duration) error {
if len(ssid) == 0 {
return drivers.ErrWiFiMissingSSID
}

return d.ConnectToAP(ssid, pass, 10)
}

func (d *Device) Disconnect() error {
return d.DisconnectFromAP()
}
4 changes: 3 additions & 1 deletion examples/espat/espconsole/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ func connectToAP() {
println("Connecting to wifi network '" + ssid + "'")

adaptor.SetWifiMode(espat.WifiModeClient)
adaptor.ConnectToAP(ssid, pass, 10)
if err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second); err != nil {
failMessage(err.Error())
}

println("Connected.")
ip, err := adaptor.GetClientIP()
Expand Down
4 changes: 3 additions & 1 deletion examples/espat/esphub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ func connectToAP() {
println("Connecting to wifi network '" + ssid + "'")

adaptor.SetWifiMode(espat.WifiModeClient)
adaptor.ConnectToAP(ssid, pass, 10)
if err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second); err != nil {
failMessage(err.Error())
}

println("Connected.")
ip, err := adaptor.GetClientIP()
Expand Down
4 changes: 3 additions & 1 deletion examples/espat/espstation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func connectToAP() {
println("Connecting to wifi network '" + ssid + "'")

adaptor.SetWifiMode(espat.WifiModeClient)
adaptor.ConnectToAP(ssid, pass, 10)
if err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second); err != nil {
failMessage(err.Error())
}

println("Connected.")
ip, err := adaptor.GetClientIP()
Expand Down
4 changes: 3 additions & 1 deletion examples/espat/mqttclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ func connectToAP() {
println("Connecting to wifi network '" + ssid + "'")

adaptor.SetWifiMode(espat.WifiModeClient)
adaptor.ConnectToAP(ssid, pass, 10)
if err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second); err != nil {
failMessage(err.Error())
}

println("Connected.")
ip, err := adaptor.GetClientIP()
Expand Down
4 changes: 3 additions & 1 deletion examples/espat/mqttsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func connectToAP() {
println("Connecting to wifi network '" + ssid + "'")

adaptor.SetWifiMode(espat.WifiModeClient)
adaptor.ConnectToAP(ssid, pass, 10)
if err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second); err != nil {
failMessage(err.Error())
}

println("Connected.")
ip, err := adaptor.GetClientIP()
Expand Down
4 changes: 3 additions & 1 deletion examples/espat/tcpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ func connectToAP() {
println("Connecting to wifi network '" + ssid + "'")

adaptor.SetWifiMode(espat.WifiModeClient)
adaptor.ConnectToAP(ssid, pass, 10)
if err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second); err != nil {
failMessage(err.Error())
}

println("Connected.")
ip, err := adaptor.GetClientIP()
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/mqttclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func run() error {
}
net.UseDriver(rtl)

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/mqttsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func run() error {
}
net.UseDriver(rtl)

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/ntpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func run() error {
}
net.UseDriver(rtl)

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/tcpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func run() error {
}
net.UseDriver(rtl)

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/tlsclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func run() error {
net.UseDriver(rtl)
http.SetBuf(buf[:])

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/udpstation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func run() error {
net.UseDriver(rtl)
http.SetBuf(buf[:])

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/webclient-tinyterm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func run() error {
http.SetBuf(buf[:])

fmt.Fprintf(terminal, "ConnectToAP()\r\n")
err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/webclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func run() error {
net.UseDriver(rtl)
http.SetBuf(buf[:])

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rtl8720dn/webserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func run() error {
}
http.UseDriver(rtl)

err = rtl.ConnectToAP(ssid, password)
err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second)
if err != nil {
return err
}
Expand Down
18 changes: 7 additions & 11 deletions examples/wifinina/connect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,15 @@ func waitSerial() {

// connect to access point
func connectToAP() {
if len(ssid) == 0 || len(pass) == 0 {
for {
println("Connection failed: Either ssid or password not set")
time.Sleep(10 * time.Second)
}
}
time.Sleep(2 * time.Second)
println("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
println("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
if err != nil { // error connecting to AP
for {
println(err)
time.Sleep(1 * time.Second)
}
}

println("Connected.")
}
22 changes: 9 additions & 13 deletions examples/wifinina/http-get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,18 @@ func waitSerial() {

// connect to access point
func connectToAP() {
if len(ssid) == 0 || len(pass) == 0 {
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("Connection failed: Either ssid or password not set")
time.Sleep(10 * time.Second)
println(err)
time.Sleep(1 * time.Second)
}
}
time.Sleep(2 * time.Second)
message("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
message("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
}
message("Connected.")
time.Sleep(2 * time.Second)

println("Connected.")

ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
message(err.Error())
Expand Down
14 changes: 8 additions & 6 deletions examples/wifinina/mqttclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ func main() {
func connectToAP() {
time.Sleep(2 * time.Second)
println("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
println("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
if err != nil { // error connecting to AP
for {
println(err)
time.Sleep(1 * time.Second)
}
}

println("Connected.")
time.Sleep(2 * time.Second)

ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
println(err.Error())
Expand Down
13 changes: 8 additions & 5 deletions examples/wifinina/mqttsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,16 @@ func publishing() {
func connectToAP() {
time.Sleep(2 * time.Second)
println("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
println("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
if err != nil { // error connecting to AP
for {
println(err)
time.Sleep(1 * time.Second)
}
}

println("Connected.")

time.Sleep(2 * time.Second)
ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
Expand Down
21 changes: 9 additions & 12 deletions examples/wifinina/ntpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,18 @@ func clearBuffer() {

// connect to access point
func connectToAP() {
if len(ssid) == 0 || len(pass) == 0 {
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("Connection failed: Either ssid or password not set")
time.Sleep(10 * time.Second)
println(err)
time.Sleep(1 * time.Second)
}
}
time.Sleep(2 * time.Second)
message("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
message("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
}
message("Connected.")

println("Connected.")

time.Sleep(2 * time.Second)
ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
Expand Down
17 changes: 10 additions & 7 deletions examples/wifinina/tcpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,17 @@ func sendBatch() {
// connect to access point
func connectToAP() {
time.Sleep(2 * time.Second)
message("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
message("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
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)
}
}
message("Connected.")

println("Connected.")

time.Sleep(2 * time.Second)
ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
Expand Down
21 changes: 9 additions & 12 deletions examples/wifinina/tlsclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,18 @@ func makeHTTPSRequest() {

// connect to access point
func connectToAP() {
if len(ssid) == 0 || len(pass) == 0 {
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("Connection failed: Either ssid or password not set")
time.Sleep(10 * time.Second)
println(err)
time.Sleep(1 * time.Second)
}
}
time.Sleep(2 * time.Second)
message("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
message("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
}
message("Connected.")

println("Connected.")

time.Sleep(2 * time.Second)
ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
Expand Down
18 changes: 10 additions & 8 deletions examples/wifinina/udpstation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,17 @@ func main() {
// connect to access point
func connectToAP() {
time.Sleep(2 * time.Second)
message("Connecting to " + ssid)
adaptor.SetPassphrase(ssid, pass)
for st, _ := adaptor.GetConnectionStatus(); st != wifinina.StatusConnected; {
message("Connection status: " + st.String())
time.Sleep(1 * time.Second)
st, _ = adaptor.GetConnectionStatus()
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)
}
}
message("Connected.")
time.Sleep(2 * time.Second)

println("Connected.")

ip, _, _, err := adaptor.GetIP()
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
message(err.Error())
Expand Down
Loading

0 comments on commit d8c813d

Please sign in to comment.