package main import ( "fmt" "machine" "strconv" "time" "tinygo.org/x/drivers/net/http" ) // You can override the setting with the init() in another source code. // func init() { // ssid = "your-ssid" // password = "your-password" // debug = true // } var ( ssid string password string debug = false ) var led = machine.LED var backlight = machine.LCD_BACKLIGHT func main() { led.Configure(machine.PinConfig{Mode: machine.PinOutput}) backlight.Configure(machine.PinConfig{Mode: machine.PinOutput}) err := run() for err != nil { fmt.Printf("error: %s\r\n", err.Error()) time.Sleep(5 * time.Second) } } func run() error { rtl, err := setupRTL8720DN() if err != nil { return err } http.UseDriver(rtl) err = rtl.ConnectToAccessPoint(ssid, password, 10*time.Second) if err != nil { return err } ip, subnet, gateway, err := rtl.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.HandleFunc("/", root) http.HandleFunc("/hello", hello) http.HandleFunc("/cnt", cnt) http.HandleFunc("/6", sixlines) http.HandleFunc("/off", LED_OFF) http.HandleFunc("/on", LED_ON) if err := http.ListenAndServe(":80", nil); err != nil { message(err.Error()) } return nil } func root(w http.ResponseWriter, r *http.Request) { access := 1 cookie, err := r.Cookie("access") if err != nil { if err == http.ErrNoCookie { cookie = &http.Cookie{ Name: "access", Value: "1", } } else { http.Error(w, fmt.Sprintf("%s", err.Error()), http.StatusBadRequest) return } } else { v, err := strconv.ParseInt(cookie.Value, 10, 0) if err != nil { http.Error(w, fmt.Sprintf("invalid cookie.Value : %s", cookie.Value), http.StatusBadRequest) return } cookie.Value = fmt.Sprintf("%d", v+1) access = int(v) + 1 } http.SetCookie(w, cookie) w.WriteHeader(http.StatusOK) fmt.Fprintf(w, `
access: %d
/hello