家のネットがあれなんでスマホのテザリングをつかった
package main
import (
"machine"
"strconv"
"time"
)
const (
ssid = "HUAWEI@nova@lite@3+"
password = "99991111"
)
// UARTからデータが返ってくるのを待って読み込む関数
func sendATCommand(uart *machine.UART, cmd string, timeout time.Duration) string {
println("送信:", cmd)
uart.Write([]byte(cmd + "\r\n"))
// 応答を待つ
start := time.Now()
var response []byte
buf := make([]byte, 64)
for time.Since(start) < timeout {
if uart.Buffered() > 0 {
n, _ := uart.Read(buf)
response = append(response, buf[:n]...) //
resStr := string(response)
if hasSubstring(resStr, "OK\r\n") || hasSubstring(resStr, "ERROR\r\n") {
break
}
}
time.Sleep(10 * time.Millisecond)
}
return string(response)
}
// 簡易文字列含まれるか判定
func hasSubstring(s, sub string) bool {
if len(sub) == 0 {
return true
}
for i := 0; i <= len(s)-len(sub); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}
func main() {
time.Sleep(3 * time.Second)
println("\n--- TinyGo ESPAT Direct AT Command Control ---")
// 1. UART1の初期化 (配線: GP4=TX, GP5=RX)
uart := machine.UART1
err := uart.Configure(machine.UARTConfig{
BaudRate: 115200,
TX: machine.GP4,
RX: machine.GP5,
})
if err != nil {
println("UART初期化失敗")
return
}
// 2. モジュールの生存確認
println("\n[1/4] 生存確認中...")
res := sendATCommand(uart, "AT", 1*time.Second)
println("応答:\n", res)
// 3. Wi-Fiモードを「Station」に設定
println("\n[2/4] Wi-Fiモード設定中...")
res = sendATCommand(uart, "AT+CWMODE=1", 1*time.Second)
println("応答:\n", res)
// 4. アクセスポイントに接続
println("\n[3/4] Wi-Fiアクセスポイントに接続中...")
joinCmd := `AT+CWJAP="` + ssid + `","` + password + `"`
res = sendATCommand(uart, joinCmd, 10*time.Second)
println("応答:\n", res)
// 5. TCP接続を確立 (example.com)
println("\n[4/4] TCP接続テスト (example.com)...")
res = sendATCommand(uart, `AT+CIPSTART="TCP","example.com",80`, 3*time.Second)
println("応答:\n", res)
// 🚨 修正:Hostヘッダーを含んだ正しいHTTP/1.1リクエストを作成
req := "GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"
sendCmd := "AT+CIPSEND=" + strconv.Itoa(len(req))
res = sendATCommand(uart, sendCmd, 1*time.Second)
println("応答:\n", res)
// 実際のHTTPリクエストを送信
uart.Write([]byte(req))
// 🚨 修正:サーバーからの返信データを漏らさず溜める受信ループ
println("\n--- 受信データ (HTTP Response) ---")
var finalResponse []byte
readBuf := make([]byte, 128)
// データが流れ終わるまで、または3秒タイムアウトまでループ
readStart := time.Now()
for time.Since(readStart) < 3*time.Second {
if uart.Buffered() > 0 {
n, _ := uart.Read(readBuf)
finalResponse = append(finalResponse, readBuf[:n]...)
// データを受信したらタイムアウトタイマーを少し延長して追従する
readStart = time.Now()
}
time.Sleep(20 * time.Millisecond)
}
// 取得した生データを一括表示
print(string(finalResponse))
println("\n---------------------------------")
}
tinygo monitor
Connected to /dev/ttyACM0. Press Ctrl-C to exit.
--- TinyGo ESPAT Direct AT Command Control ---
[1/4] 生存確認中...
送信: AT
応答:
AT
OK
[2/4] Wi-Fiモード設定中...
送信: AT+CWMODE=1
応答:
AT+CWMODE=1
OK
[3/4] Wi-Fiアクセスポイントに接続中...
送信: AT+CWJAP="HUAWEI@nova@lite@3+","99991111"
応答:
AT+CWJAP="HUAWEI@nova@lite@3+","99991111"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP
OK
[4/4] TCP接続テスト (example.com)...
送信: AT+CIPSTART="TCP","example.com",80
応答:
AT+CIPSTART="TCP","example.com",80
CONNECT
OK
送信: AT+CIPSEND=56
応答:
AT+CIPSEND=56
OK
>
--- 受信データ (HTTP Response) ---
Recv 56 bytes
SEND OK
+IPD,927:HTTP/1.1 200 OK
Date: Sun, 01 Nov 2026 04:00:21 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
S
Age: 14269
cf-cache-status: HIT
CF-RAY: a537fdf6794fd3f5-KIX
alt-svc: h3=":443"; ma=86400
241
<!doctype html><html langh,initial-scale=1"><title>Example Domain</title><style>html{color-scheme:light dark;background:light-dark(#eee,#222)}body{font:1</head><body><p>This domain is for use in documentation examples without needing permission. This is not a service; avoid relyin
CLOSED
----------ssd1306 gpio4,gpio5でないと出ん,gpio0,gpio1はuart専用みたい!---------------------
package main
import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers/ssd1306"
"tinygo.org/x/tinyfont"
"tinygo.org/x/tinyfont/freesans"
)
func main() {
println("Starting SSD1306")
// I2C0 初期化
err := machine.I2C0.Configure(machine.I2CConfig{
SDA: machine.GP4,
SCL: machine.GP5,
Frequency: 100 * machine.KHz,
})
if err != nil {
println("I2C initialization failed:", err.Error())
return
}
println("I2C initialized")
// SSD1306 初期化
display := ssd1306.NewI2C(machine.I2C0)
display.Configure(ssd1306.Config{
Address: 0x3C,
Width: 128,
Height: 64,
})
println("SSD1306 initialized")
time.Sleep(time.Millisecond * 200)
// 画面クリア
display.ClearDisplay()
white := color.RGBA{
R: 255,
G: 255,
B: 255,
A: 255,
}
// 文字表示
tinyfont.WriteLine(
display,
&freesans.Regular12pt7b,
10,
25,
"Hello",
white,
)
tinyfont.WriteLine(
display,
&freesans.Regular12pt7b,
10,
50,
"TinyGo",
white,
)
// OLED更新
err = display.Display()
if err != nil {
println("Display error:", err.Error())
return
}
println("Display completed")
for {
time.Sleep(time.Second)
}
}
---------------------------------------------------------------------------------
package main
import (
"fmt"
"machine"
"time"
"tinygo.org/x/drivers/dht"
)
func main() {
// DHT11 DATA → Pico GP0
pin := machine.GP0
sensor := dht.New(pin, dht.DHT11)
for {
// 温度と湿度を同時に取得
temp, hum, err := sensor.Measurements()
if err != nil {
fmt.Printf("DHT11 Error: %v\n", err)
} else {
fmt.Printf(
"Temperature: %d.%d C, Humidity: %d.%d %%\n",
temp/10,
temp%10,
hum/10,
hum%10,
)
} // DHT11は最低でも約1秒空ける
time.Sleep(2 * time.Second)
}
}