-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples/httpclient: update to use new netdev interface
Signed-off-by: deadprogram <[email protected]>
- Loading branch information
1 parent
a2bd6d4
commit 6842651
Showing
2 changed files
with
58 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,196 +1,95 @@ | ||
// 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: | ||
// This example requires a network adaptor. It gets an URL using http.Get() and | ||
// displays the output on the terminal screen. | ||
// | ||
// https://github.com/arduino-libraries/WiFiNINA/blob/master/examples/WiFiWebClientRepeating/ | ||
// Note: It may be necessary to increase the stack size when using "net/http". | ||
// Use the -stack-size=4KB command line option. | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"image/color" | ||
"io" | ||
"machine" | ||
"strconv" | ||
"log" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"tinygo.org/x/drivers/netlink" | ||
"tinygo.org/x/drivers/netlink/probe" | ||
"tinygo.org/x/tinyfont/proggy" | ||
|
||
"tinygo.org/x/drivers/ili9341" | ||
"tinygo.org/x/drivers/net" | ||
"tinygo.org/x/drivers/wifinina" | ||
|
||
"github.com/valyala/fastjson" | ||
|
||
"github.com/bgould/http/client" | ||
"tinygo.org/x/tinyterm" | ||
"tinygo.org/x/tinyterm/examples/initdisplay" | ||
) | ||
|
||
// access point info | ||
var ssid string | ||
var pass string | ||
|
||
// IP address of the server aka "tinygo flash -target pyportal -ldflags="-X main.ssid=MYSSID -X main.pass=MYPASS" ./examples/httpclienthub". Replace with your own info. | ||
const server = "numbersapi.com" | ||
|
||
var ( | ||
display = ili9341.NewParallel( | ||
machine.LCD_DATA0, | ||
machine.TFT_WR, | ||
machine.TFT_DC, | ||
machine.TFT_CS, | ||
machine.TFT_RESET, | ||
machine.TFT_RD, | ||
) | ||
console = tinyterm.NewTerminal(display) | ||
black = color.RGBA{0, 0, 0, 255} | ||
font = &proggy.TinySZ8pt7b | ||
|
||
// this is the ESP chip that has the WIFININA firmware flashed on it | ||
adaptor = &wifinina.Device{ | ||
SPI: machine.NINA_SPI, | ||
CS: machine.NINA_CS, | ||
ACK: machine.NINA_ACK, | ||
GPIO0: machine.NINA_GPIO0, | ||
RESET: machine.NINA_RESETN, | ||
} | ||
|
||
buf = make([]byte, 256) | ||
last time.Time | ||
ssid string | ||
pass string | ||
) | ||
|
||
buffer = bytes.NewBuffer(make([]byte, 1024)) | ||
conn net.Conn | ||
var ( | ||
black = color.RGBA{0, 0, 0, 255} | ||
white = color.RGBA{255, 255, 255, 255} | ||
red = color.RGBA{255, 0, 0, 255} | ||
blue = color.RGBA{0, 0, 255, 255} | ||
green = color.RGBA{0, 255, 0, 255} | ||
|
||
parser = &fastjson.Parser{} | ||
font = &proggy.TinySZ8pt7b | ||
) | ||
|
||
func main() { | ||
display := initdisplay.InitDisplay() | ||
terminal := tinyterm.NewTerminal(display) | ||
|
||
machine.TFT_BACKLIGHT.Configure(machine.PinConfig{machine.PinOutput}) | ||
|
||
display.Configure(ili9341.Config{}) | ||
display.FillScreen(black) | ||
machine.TFT_BACKLIGHT.High() | ||
|
||
console.Configure(&tinyterm.Config{ | ||
terminal.Configure(&tinyterm.Config{ | ||
Font: font, | ||
FontHeight: 10, | ||
FontOffset: 6, | ||
}) | ||
|
||
// Configure spi for 8Mhz, Mode 0, MSB First | ||
machine.NINA_SPI.Configure(machine.SPIConfig{ | ||
Frequency: 8 * 1e6, | ||
SDO: machine.NINA_SDO, | ||
SDI: machine.NINA_SDI, | ||
SCK: machine.NINA_SCK, | ||
}) | ||
|
||
adaptor.Configure() | ||
|
||
connectToAP() | ||
|
||
for { | ||
if time.Now().Sub(last).Milliseconds() >= 10000 { | ||
makeHTTPRequest() | ||
} | ||
} | ||
|
||
} | ||
fmt.Fprintf(terminal, "Connecting to %s...\r\n", ssid) | ||
|
||
var i = -1 | ||
link, _ := probe.Probe() | ||
|
||
func makeHTTPRequest() { | ||
|
||
i++ | ||
|
||
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("\n---------------\nDialing TCP connection\n") | ||
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) | ||
} | ||
//defer conn.Close() | ||
|
||
message("Connected!") | ||
|
||
cl := client.NewClient(conn) | ||
|
||
cl.WriteRequest(&client.Request{ | ||
Method: "GET", | ||
Path: fmt.Sprintf("/%d", i), | ||
Query: nil, | ||
Version: client.HTTP_1_1, | ||
Headers: []client.Header{ | ||
{"Host", server}, | ||
{"User-Agent", "TinyGo"}, | ||
{"Content-Type", "application/json"}, | ||
{"Connection", "close"}, | ||
}, | ||
Body: nil, | ||
err := link.NetConnect(&netlink.ConnectParams{ | ||
Ssid: ssid, | ||
Passphrase: pass, | ||
}) | ||
|
||
last = time.Now() | ||
for time.Now().Sub(last).Milliseconds() < 1500 { | ||
} | ||
|
||
rsp, err := cl.ReadResponse() | ||
if err != nil { | ||
fmt.Fprintf(console, "error response: %v\n", err) | ||
return | ||
log.Fatal(err) | ||
} | ||
|
||
buffer.Reset() | ||
contentLength := int(rsp.ContentLength()) | ||
message(fmt.Sprintf("HTTP response status: %d\n", rsp.Code)) | ||
message(fmt.Sprintf("Content length: %d\n", contentLength)) | ||
name := "John Doe" | ||
occupation := "gardener" | ||
|
||
if _, err := io.CopyBuffer(buffer, rsp.Body, buf); err != nil { | ||
message("error reading response body: " + err.Error()) | ||
return | ||
} | ||
params := "name=" + url.QueryEscape(name) + "&" + | ||
"occupation=" + url.QueryEscape(occupation) | ||
|
||
message(string(buffer.Bytes())) | ||
path := fmt.Sprintf("https://httpbin.org/get?%s", params) | ||
|
||
v, err := parser.ParseBytes(buffer.Bytes()) | ||
if err != nil { | ||
message("error parsing JSON: " + err.Error() + "\n") | ||
return | ||
} | ||
cnt := 0 | ||
for { | ||
fmt.Fprintf(terminal, "Getting %s\r\n\r\n", path) | ||
resp, err := http.Get(path) | ||
if err != nil { | ||
fmt.Fprintf(terminal, "%s\r\n", err.Error()) | ||
time.Sleep(10 * time.Second) | ||
continue | ||
} | ||
|
||
message(v.Get("text").String() + "\n") | ||
} | ||
fmt.Fprintf(terminal, "%s %s\r\n", resp.Proto, resp.Status) | ||
for k, v := range resp.Header { | ||
fmt.Fprintf(terminal, "%s: %s\r\n", k, strings.Join(v, " ")) | ||
} | ||
fmt.Fprintf(terminal, "\r\n") | ||
|
||
// 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: " + strconv.Itoa(int(st))) | ||
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()) | ||
} | ||
body, err := io.ReadAll(resp.Body) | ||
fmt.Fprintf(terminal, string(body)) | ||
resp.Body.Close() | ||
|
||
func message(msg string) { | ||
fmt.Fprintln(console, msg) | ||
cnt++ | ||
fmt.Fprintf(terminal, "-------- %d --------\r\n", cnt) | ||
time.Sleep(10 * time.Second) | ||
} | ||
} |