Skip to content

Instantly share code, notes, and snippets.

@suda
Created March 27, 2020 19:19
Show Gist options
  • Save suda/ef42cfdc0e3e5b196b7efab83de364e5 to your computer and use it in GitHub Desktop.
Save suda/ef42cfdc0e3e5b196b7efab83de364e5 to your computer and use it in GitHub Desktop.
// This is a console to a ESP8266/ESP32 running on the device UART1.
// Allows you to type AT commands from your computer via the microcontroller.
//
// In other words:
// Your computer <--> UART0 <--> MCU <--> UART1 <--> ESP8266 <--> INTERNET
//
// More information on the Espressif AT command set at:
// https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf
//
package main
import (
"machine"
"time"
"tinygo.org/x/drivers/espat"
)
// change actAsAP to true to act as an access point instead of connecting to one.
const actAsAP = false
// access point info
const ssid = "suda"
const pass = "11335577"
// these are the default pins for the Arduino Nano33 IoT.
// change these to connect to a different UART or pins for the ESP8266/ESP32
var (
uart = machine.UART0
tx = machine.ESP32_TXD_PIN
rx = machine.ESP32_RXD_PIN
espBoot = machine.ESP32_BOOT_MODE_PIN
espEn = machine.ESP32_WIFI_EN_PIN
espCts = machine.ESP32_CTS_PIN
espRts = machine.ESP32_RTS_PIN
console = machine.Serial
adaptor *espat.Device
)
func espReset() {
espBoot.High()
time.Sleep(100 * time.Millisecond)
espEn.Low()
time.Sleep(100 * time.Millisecond)
espEn.High()
time.Sleep(100 * time.Millisecond)
}
func espOff() {
espEn.Low()
}
func setup() {
espBoot.Configure(machine.PinConfig{Mode: machine.PinOutput})
espEn.Configure(machine.PinConfig{Mode: machine.PinOutput})
espCts.Configure(machine.PinConfig{Mode: machine.PinInput})
espRts.Configure(machine.PinConfig{Mode: machine.PinOutput})
espRts.High()
espOff()
console.Configure(machine.UARTConfig{})
uart.Configure(machine.UARTConfig{TX: tx, RX: rx, BaudRate: 921600})
time.Sleep(100 * time.Millisecond)
espReset()
}
func main() {
setup()
// Init esp8266
adaptor = espat.New(uart)
adaptor.Configure()
// first check if connected
if connectToESP() {
println("Connected to wifi adaptor.")
adaptor.Echo(false)
connectToAP()
} else {
println("")
failMessage("Unable to connect to wifi adaptor.")
return
}
println("Type an AT command then press enter:")
prompt()
input := make([]byte, 64)
i := 0
for {
if console.Buffered() > 0 {
data, _ := console.ReadByte()
switch data {
case 13:
// return key
console.Write([]byte("\r\n"))
// send command to ESP8266
input[i] = byte('\r')
input[i+1] = byte('\n')
adaptor.Write(input[:i+2])
// display response
r, _ := adaptor.Response(500)
console.Write(r)
// prompt
prompt()
i = 0
continue
default:
// just echo the character
console.WriteByte(data)
input[i] = data
i++
}
}
time.Sleep(10 * time.Millisecond)
}
}
func prompt() {
print("ESPAT>")
}
// connect to ESP8266/ESP32
func connectToESP() bool {
for i := 0; i < 5; i++ {
println("Connecting to wifi adaptor...")
if adaptor.Connected() {
return true
}
time.Sleep(1 * time.Second)
}
return false
}
// connect to access point
func connectToAP() {
println("Connecting to wifi network '" + ssid + "'")
adaptor.SetWifiMode(espat.WifiModeClient)
adaptor.ConnectToAP(ssid, pass, 10)
println("Connected.")
ip, err := adaptor.GetClientIP()
if err != nil {
failMessage(err.Error())
}
println(ip)
}
// provide access point
func provideAP() {
println("Starting wifi network as access point '" + ssid + "'...")
adaptor.SetWifiMode(espat.WifiModeAP)
adaptor.SetAPConfig(ssid, pass, 7, espat.WifiAPSecurityWPA2_PSK)
println("Ready.")
ip, _ := adaptor.GetAPIP()
println(ip)
}
func failMessage(msg string) {
for {
println(msg)
time.Sleep(1 * time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment