// 以下で使っている バッククォート ` は、Goでは「raw string literal(生文字列)」を表す!
package main
import (
"fmt"
"machine"
"time"
)
const (
SSID = ""
PASSWORD = "090e219cbdbf5"
)
func main() {
// ==========================================
// Raspberry Pi Pico
//
// GP0 = UART0 TX -> ESP-01 RX
// GP1 = UART0 RX <- ESP-01 TX
// ==========================================
uart := machine.UART0
uart.Configure(machine.UARTConfig{
BaudRate: 115200,
TX: machine.GP0,
RX: machine.GP1,
})
fmt.Println("================================")
fmt.Println("Pico + ESP-01 WiFi test")
fmt.Println("================================")
// ESP-01起動待ち
time.Sleep(3 * time.Second)
// ------------------------------------------
// 受信バッファをクリア
// ------------------------------------------
clearUART(uart)
// ------------------------------------------
// AT
// ------------------------------------------
fmt.Println()
fmt.Println(">> AT")
sendAT(uart, "AT", 2*time.Second)
// ------------------------------------------
// WiFi Stationモード
// ------------------------------------------
fmt.Println()
fmt.Println(">> AT+CWMODE=1")
sendAT(uart, "AT+CWMODE=1", 2*time.Second)
// ------------------------------------------
// WiFiアクセスポイントへ接続
// ------------------------------------------
fmt.Println()
fmt.Println(">> AT+CWJAP")
cmd := `AT+CWJAP="` + SSID + `","` + PASSWORD + `"`
sendAT(uart, cmd, 15*time.Second)
// ------------------------------------------
// IPアドレス確認
// ------------------------------------------
fmt.Println()
fmt.Println(">> AT+CIFSR")
sendAT(uart, "AT+CIFSR", 3*time.Second)
fmt.Println()
fmt.Println("================================")
fmt.Println("WiFi connection test finished")
fmt.Println("================================")
// ------------------------------------------
// 以後、ESP-01からの受信を表示
// ------------------------------------------
for {
readUART(uart)
time.Sleep(100 * time.Millisecond)
}
}
// ==================================================
// ATコマンド送信
// ==================================================
func sendAT(uart *machine.UART, command string, wait time.Duration) {
// 受信バッファをクリア
clearUART(uart)
// ATコマンド送信
uart.Write([]byte(command + "\r\n"))
// 指定時間、ESP-01の応答を受信
start := time.Now()
for time.Since(start) < wait {
readUART(uart)
time.Sleep(10 * time.Millisecond)
}
}
// ==================================================
// ESP-01からデータを読む
// ==================================================
func readUART(uart *machine.UART) {
buf := make([]byte, 64)
for uart.Buffered() > 0 {
n, err := uart.Read(buf)
if err != nil {
return
if n > 0 {
// TinyGoではMicroPythonの
//
// print(data.decode("utf-8", "ignore"), end="")
//
// に相当する処理
fmt.Print(string(buf[:n]))
}
}
}
// ==================================================
// UART受信バッファをクリア
// ==================================================
func clearUART(uart *machine.UART) {
buf := make([]byte, 64)
for uart.Buffered() > 0 {
_, _ = uart.Read(buf)
}
}
古いtinygoだと文句いわれながらチャットくんにつくてもらった 3.3v駆動のws2812
package main
import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers/ws2812"
)
const numLEDs = 5
const ledPin = machine.GP15
func main() {
ledPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
// ビットバン駆動の初期化
ws := ws2812.New(ledPin)
leds := make([]color.RGBA, numLEDs)
for {
// 1. 赤色に点灯
fill(leds, color.RGBA{R: 0xFF, G: 0x00, B: 0x00})
// colorsToBytesで変換した []byte を Write に渡す
ws.Write(colorsToBytes(leds))
time.Sleep(time.Second)
// 2. 緑色に点灯
fill(leds, color.RGBA{R: 0x00, G: 0xFF, B: 0x00})
ws.Write(colorsToBytes(leds))
time.Sleep(time.Second)
}
}
func fill(leds []color.RGBA, c color.RGBA) {
for i := range leds {
leds[i] = c
}
}