Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Last active January 24, 2019 12:55
Show Gist options
  • Save deadprogram/c2ced43736ae537c3739f78780e956da to your computer and use it in GitHub Desktop.
Save deadprogram/c2ced43736ae537c3739f78780e956da to your computer and use it in GitHub Desktop.
// This is a console to a ESP8266 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
//
// More information on the Espressif AT command set at:
// https://github.com/espressif/ESP8266_AT/wiki
//
package main
import (
"machine"
"time"
"github.com/tinygo-org/drivers/esp8266"
)
// change these to connectc to a different UART or pins for the ESP8266
var (
uart = machine.UART1
tx uint8 = machine.D10
rx uint8 = machine.D11
console = machine.UART0
)
func main() {
uart.Configure(machine.UARTConfig{TX: tx, RX: rx})
// Init esp8266
wifi := esp8266.New(uart)
wifi.Configure()
// first check if connected
if wifi.Connected() {
console.Write([]byte("ESP8266 console enabled. Type a command then press enter:\r\n"))
} else {
console.Write([]byte("Unable to connect to esp8266.\r\n"))
}
input := make([]byte, 64)
i := 0
for {
if console.Buffered() > 0 {
data, _ := console.ReadByte()
switch data {
case 13:
// return key
console.Write([]byte{'\r'})
// send command to ESP8266
input[i] = byte('\r')
input[i+1] = byte('\n')
wifi.Write(input[:i+2])
// give the ESP8266 a chance to respond.
time.Sleep(10 * time.Millisecond)
// display response
console.Write(wifi.Response())
i = 0
continue
default:
// just echo the character
console.WriteByte(data)
input[i] = data
i++
}
}
time.Sleep(10 * time.Millisecond)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment