mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-26 10:38:41 +00:00
46c9ba9595
Signed-off-by: deadprogram <ron@hybridgroup.com>
151 lines
3.3 KiB
Go
151 lines
3.3 KiB
Go
// This example opens a TCP connection using a device with WiFiNINA firmware
|
|
// and sends a HTTP request to retrieve a webpage, based on the following
|
|
// Arduino example:
|
|
//
|
|
// https://github.com/arduino-libraries/WiFiNINA/blob/master/examples/WiFiWebClientRepeating/
|
|
//
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"machine"
|
|
"time"
|
|
|
|
"tinygo.org/x/drivers/net"
|
|
"tinygo.org/x/drivers/wifinina"
|
|
)
|
|
|
|
// access point info
|
|
const ssid = ""
|
|
const pass = ""
|
|
|
|
// IP address of the server aka "hub". Replace with your own info.
|
|
const server = "tinygo.org"
|
|
|
|
// these are the default pins for the Arduino Nano33 IoT.
|
|
// change these to connect to a different UART or pins for the ESP8266/ESP32
|
|
var (
|
|
|
|
// these are the default pins for the Arduino Nano33 IoT.
|
|
uart = machine.UART2
|
|
tx = machine.NINA_TX
|
|
rx = machine.NINA_RX
|
|
spi = machine.NINA_SPI
|
|
|
|
// this is the ESP chip that has the WIFININA firmware flashed on it
|
|
adaptor *wifinina.Device
|
|
)
|
|
|
|
var buf [256]byte
|
|
|
|
var lastRequestTime time.Time
|
|
var conn net.Conn
|
|
|
|
func main() {
|
|
|
|
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
|
|
|
|
// Configure SPI for 8Mhz, Mode 0, MSB First
|
|
spi.Configure(machine.SPIConfig{
|
|
Frequency: 8 * 1e6,
|
|
SDO: machine.NINA_SDO,
|
|
SDI: machine.NINA_SDI,
|
|
SCK: machine.NINA_SCK,
|
|
})
|
|
|
|
adaptor = wifinina.New(spi,
|
|
machine.NINA_CS,
|
|
machine.NINA_ACK,
|
|
machine.NINA_GPIO0,
|
|
machine.NINA_RESETN)
|
|
adaptor.Configure()
|
|
|
|
connectToAP()
|
|
|
|
for {
|
|
loop()
|
|
}
|
|
println("Done.")
|
|
}
|
|
|
|
func loop() {
|
|
if conn != nil {
|
|
for n, err := conn.Read(buf[:]); n > 0; n, err = conn.Read(buf[:]) {
|
|
if err != nil {
|
|
println("Read error: " + err.Error())
|
|
} else {
|
|
print(string(buf[0:n]))
|
|
}
|
|
}
|
|
}
|
|
if time.Now().Sub(lastRequestTime).Milliseconds() >= 10000 {
|
|
makeHTTPRequest()
|
|
}
|
|
}
|
|
|
|
func makeHTTPRequest() {
|
|
|
|
var err error
|
|
if conn != nil {
|
|
conn.Close()
|
|
}
|
|
|
|
// make TCP connection
|
|
ip := net.ParseIP(server)
|
|
raddr := &net.TCPAddr{IP: ip, Port: 80}
|
|
laddr := &net.TCPAddr{Port: 8080}
|
|
|
|
message("\r\n---------------\r\nDialing TCP connection")
|
|
conn, err = net.DialTCP("tcp", laddr, raddr)
|
|
for ; err != nil; conn, err = net.DialTCP("tcp", laddr, raddr) {
|
|
message("connection failed: " + err.Error())
|
|
time.Sleep(5 * time.Second)
|
|
}
|
|
println("Connected!\r")
|
|
|
|
print("Sending HTTP request...")
|
|
fmt.Fprintln(conn, "GET / HTTP/1.1")
|
|
fmt.Fprintln(conn, "Host:", server)
|
|
fmt.Fprintln(conn, "User-Agent: TinyGo/0.10.0")
|
|
fmt.Fprintln(conn, "Connection: close")
|
|
fmt.Fprintln(conn)
|
|
println("Sent!\r\n\r")
|
|
|
|
lastRequestTime = time.Now()
|
|
}
|
|
|
|
func readLine(conn *net.TCPSerialConn) string {
|
|
println("Attempting to read...\r")
|
|
b := buf[:]
|
|
for expiry := time.Now().Unix() + 10; time.Now().Unix() > expiry; {
|
|
if n, err := conn.Read(b); n > 0 && err == nil {
|
|
return string(b[0:n])
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
message("Connected.")
|
|
time.Sleep(2 * time.Second)
|
|
ip, _, _, err := adaptor.GetIP()
|
|
for ; err != nil; ip, _, _, err = adaptor.GetIP() {
|
|
message(err.Error())
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
message(ip.String())
|
|
}
|
|
|
|
func message(msg string) {
|
|
println(msg, "\r")
|
|
}
|