package main import ( "fmt" "machine" "strconv" "time" "tinygo.org/x/drivers/net/http" "tinygo.org/x/drivers/wifinina" ) // You can override the settings with the init() in another source code: // // func init() { // ssid = "your-ssid" // pass = "your-password" // } // // Or use -ldflags option on tinygo command to set at compile-time: // // tinygo flash ... -ldflags '-X "main.ssid=xxx" -X "main.pass=xxx"' ... // var ( ssid string pass string ) var ( // this is the ESP chip that has the WIFININA firmware flashed on it adaptor *wifinina.Device ) var led = machine.LED func main() { led.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 { spi := machine.NINA_SPI 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() displayIP() http.UseDriver(adaptor) 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) return http.ListenAndServe(":80", 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