mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-21 15:09:00 +00:00
examples/wifinina: improve connectToAP() and other needed minor corrections
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
@@ -128,17 +128,29 @@ func waitSerial() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ func main() {
|
|||||||
waitSerial()
|
waitSerial()
|
||||||
|
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
// You can send and receive cookies in the following way
|
// You can send and receive cookies in the following way
|
||||||
// import "tinygo.org/x/drivers/net/http/cookiejar"
|
// import "tinygo.org/x/drivers/net/http/cookiejar"
|
||||||
@@ -131,28 +132,42 @@ func waitSerial() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
message(err.Error())
|
message(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
message(ip.String())
|
message("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(msg string) {
|
func message(msg string) {
|
||||||
println(msg, "\r")
|
println(msg, "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ func main() {
|
|||||||
adaptor.Configure()
|
adaptor.Configure()
|
||||||
|
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
opts.AddBroker(server).SetClientID("tinygo-client-" + randomString(10))
|
opts.AddBroker(server).SetClientID("tinygo-client-" + randomString(10))
|
||||||
@@ -101,26 +102,33 @@ func main() {
|
|||||||
println("Done.")
|
println("Done.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
println(ip.String())
|
println("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an int >= min, < max
|
// Returns an int >= min, < max
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ func main() {
|
|||||||
adaptor.Configure()
|
adaptor.Configure()
|
||||||
|
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
opts := mqtt.NewClientOptions()
|
opts := mqtt.NewClientOptions()
|
||||||
opts.AddBroker(server).SetClientID("tinygo-client-" + randomString(10))
|
opts.AddBroker(server).SetClientID("tinygo-client-" + randomString(10))
|
||||||
@@ -113,27 +114,33 @@ func publishing() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
println(ip.String())
|
println("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns an int >= min, < max
|
// Returns an int >= min, < max
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ func main() {
|
|||||||
waitSerial()
|
waitSerial()
|
||||||
|
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
// now make UDP connection
|
// now make UDP connection
|
||||||
ip := net.ParseIP(ntpHost)
|
ip := net.ParseIP(ntpHost)
|
||||||
@@ -149,29 +150,42 @@ func clearBuffer() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
message(err.Error())
|
message(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
message(ip.String())
|
message("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(format string, args ...interface{}) {
|
func message(format string, args ...interface{}) {
|
||||||
println(fmt.Sprintf(format, args...), "\r")
|
println(fmt.Sprintf(format, args...), "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ func main() {
|
|||||||
adaptor.Configure()
|
adaptor.Configure()
|
||||||
|
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
sendBatch()
|
sendBatch()
|
||||||
@@ -111,29 +112,42 @@ func sendBatch() {
|
|||||||
conn.Close()
|
conn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
message(err.Error())
|
message(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
message(ip.String())
|
message("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(msg string) {
|
func message(msg string) {
|
||||||
println(msg, "\r")
|
println(msg, "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ func main() {
|
|||||||
waitSerial()
|
waitSerial()
|
||||||
|
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
readConnection()
|
readConnection()
|
||||||
@@ -121,29 +122,42 @@ func makeHTTPSRequest() {
|
|||||||
lastRequestTime = time.Now()
|
lastRequestTime = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
message(err.Error())
|
message(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
message(ip.String())
|
message("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(msg string) {
|
func message(msg string) {
|
||||||
println(msg, "\r")
|
println(msg, "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ func main() {
|
|||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
// now make UDP connection
|
// now make UDP connection
|
||||||
ip := net.ParseIP(hubIP)
|
ip := net.ParseIP(hubIP)
|
||||||
@@ -57,7 +58,10 @@ func main() {
|
|||||||
laddr := &net.UDPAddr{Port: 2222}
|
laddr := &net.UDPAddr{Port: 2222}
|
||||||
|
|
||||||
println("Dialing UDP connection...")
|
println("Dialing UDP connection...")
|
||||||
conn, _ := net.DialUDP("udp", laddr, raddr)
|
conn, err := net.DialUDP("udp", laddr, raddr)
|
||||||
|
if err != nil {
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// send data
|
// send data
|
||||||
@@ -74,28 +78,42 @@ func main() {
|
|||||||
println("Done.")
|
println("Done.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
message(err.Error())
|
message(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
message(ip.String())
|
message("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(msg string) {
|
func message(msg string) {
|
||||||
println(msg, "\r")
|
println(msg, "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ var (
|
|||||||
pass string
|
pass string
|
||||||
)
|
)
|
||||||
|
|
||||||
// IP address of the server aka "hub". Replace with your own info.
|
// IP address of the "example.com" server. Replace with your own info.
|
||||||
const server = "tinygo.org"
|
const server = "93.184.216.34"
|
||||||
|
|
||||||
// these are the default pins for the Arduino Nano33 IoT.
|
// these are the default pins for the Arduino Nano33 IoT.
|
||||||
// change these to connect to a different UART or pins for the ESP8266/ESP32
|
// change these to connect to a different UART or pins for the ESP8266/ESP32
|
||||||
@@ -63,6 +63,7 @@ func main() {
|
|||||||
waitSerial()
|
waitSerial()
|
||||||
|
|
||||||
connectToAP()
|
connectToAP()
|
||||||
|
displayIP()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
readConnection()
|
readConnection()
|
||||||
@@ -123,28 +124,42 @@ func makeHTTPRequest() {
|
|||||||
lastRequestTime = time.Now()
|
lastRequestTime = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
// connect to access point
|
// connect to access point
|
||||||
func connectToAP() {
|
func connectToAP() {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
println("Connecting to " + ssid)
|
var err error
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
for i := 0; i < retriesBeforeFailure; i++ {
|
||||||
if err != nil { // error connecting to AP
|
println("Connecting to " + ssid)
|
||||||
for {
|
err = adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
||||||
println(err)
|
if err == nil {
|
||||||
time.Sleep(1 * time.Second)
|
println("Connected.")
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Connected.")
|
// error connecting to AP
|
||||||
|
failMessage(err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayIP() {
|
||||||
ip, _, _, err := adaptor.GetIP()
|
ip, _, _, err := adaptor.GetIP()
|
||||||
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
||||||
message(err.Error())
|
message(err.Error())
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
message(ip.String())
|
message("IP address: " + ip.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(msg string) {
|
func message(msg string) {
|
||||||
println(msg, "\r")
|
println(msg, "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ var (
|
|||||||
pass string
|
pass string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// this is the ESP chip that has the WIFININA firmware flashed on it
|
||||||
|
adaptor *wifinina.Device
|
||||||
|
)
|
||||||
|
|
||||||
var led = machine.LED
|
var led = machine.LED
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -49,31 +54,15 @@ func run() error {
|
|||||||
SCK: machine.NINA_SCK,
|
SCK: machine.NINA_SCK,
|
||||||
})
|
})
|
||||||
|
|
||||||
adaptor := wifinina.New(spi,
|
adaptor = wifinina.New(spi,
|
||||||
machine.NINA_CS,
|
machine.NINA_CS,
|
||||||
machine.NINA_ACK,
|
machine.NINA_ACK,
|
||||||
machine.NINA_GPIO0,
|
machine.NINA_GPIO0,
|
||||||
machine.NINA_RESETN)
|
machine.NINA_RESETN)
|
||||||
adaptor.Configure()
|
adaptor.Configure()
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
connectToAP()
|
||||||
println("Connecting to " + ssid)
|
displayIP()
|
||||||
err := adaptor.ConnectToAccessPoint(ssid, pass, 10*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
println("Connected.")
|
|
||||||
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
ip, subnet, gateway, err := adaptor.GetIP()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("IP Address : %s\r\n", ip)
|
|
||||||
fmt.Printf("Mask : %s\r\n", subnet)
|
|
||||||
fmt.Printf("Gateway : %s\r\n", gateway)
|
|
||||||
|
|
||||||
http.UseDriver(adaptor)
|
http.UseDriver(adaptor)
|
||||||
|
|
||||||
@@ -204,6 +193,42 @@ func cnt(w http.ResponseWriter, r *http.Request) {
|
|||||||
fmt.Fprintf(w, `{"cnt": %d}`, counter)
|
fmt.Fprintf(w, `{"cnt": %d}`, counter)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const retriesBeforeFailure = 3
|
||||||
|
|
||||||
|
// connect to access point
|
||||||
|
func connectToAP() {
|
||||||
|
time.Sleep(2 * 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 address: " + ip.String())
|
||||||
|
}
|
||||||
|
|
||||||
func message(msg string) {
|
func message(msg string) {
|
||||||
println(msg, "\r")
|
println(msg, "\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func failMessage(msg string) {
|
||||||
|
for {
|
||||||
|
println(msg)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user