Documentation ¶
Overview ¶
Package espat implements TCP/UDP wireless communication over serial with a separate ESP8266 or ESP32 board using the Espressif AT command set across a UART interface.
In order to use this driver, the ESP8266/ESP32 must be flashed with firmware supporting the AT command set. Many ESP8266/ESP32 chips already have this firmware installed by default. You will need to install this firmware if you have an ESP8266 that has been flashed with NodeMCU (Lua) or Arduino firmware.
AT Command Core repository: https://github.com/espressif/esp32-at
Datasheet: https://www.espressif.com/sites/default/files/documentation/0a-esp8266ex_datasheet_en.pdf
AT command set: https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf
Index ¶
- Constants
- type Device
- func (d Device) Configure()
- func (d *Device) ConnectTCPSocket(addr, port string) error
- func (d *Device) ConnectToAP(ssid, pwd string, ws int) error
- func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error
- func (d *Device) Connected() bool
- func (d Device) DialUDP(network string, laddr, raddr *UDPAddr) (*SerialConn, error)
- func (d *Device) DisconnectFromAP() error
- func (d *Device) DisconnectSocket() error
- func (d Device) Echo(set bool)
- func (d *Device) EndSocketSend() error
- func (d Device) Execute(cmd string) error
- func (d *Device) GetAPClients() string
- func (d *Device) GetAPConfig() string
- func (d *Device) GetAPConfigFlash() string
- func (d *Device) GetAPIP() string
- func (d *Device) GetAPIPFlash() string
- func (d *Device) GetClientIP() string
- func (d *Device) GetConnectedAP() []byte
- func (d *Device) GetMux() ([]byte, error)
- func (d *Device) GetTCPTransferMode() []byte
- func (d *Device) GetWifiMode() []byte
- func (d Device) ListenUDP(network string, laddr *UDPAddr) (*SerialConn, error)
- func (d Device) Query(cmd string) (string, error)
- func (d *Device) Read(b []byte) (n int, err error)
- func (d *Device) ReadSocket(b []byte) (n int, err error)
- func (d Device) Reset()
- func (d *Device) Response() []byte
- func (d Device) Set(cmd, params string) error
- func (d *Device) SetAPConfig(ssid, pwd string, ch, security int) error
- func (d *Device) SetAPConfigFlash(ssid, pwd string, ch, security int) error
- func (d *Device) SetAPIP(ipaddr string) error
- func (d *Device) SetAPIPFlash(ipaddr string) error
- func (d *Device) SetClientIP(ipaddr string) []byte
- func (d *Device) SetMux(mode int) error
- func (d *Device) SetTCPTransferMode(mode int) error
- func (d *Device) SetWifiMode(mode int) error
- func (d *Device) StartSocketSend(size int) error
- func (d Device) Version() []byte
- func (d *Device) Write(b []byte) (n int, err error)
- type IP
- type SerialConn
- func (c *SerialConn) Close() error
- func (c *SerialConn) LocalAddr() UDPAddr
- func (c *SerialConn) Read(b []byte) (n int, err error)
- func (c *SerialConn) RemoteAddr() UDPAddr
- func (c *SerialConn) SetDeadline(t time.Time) error
- func (c *SerialConn) SetReadDeadline(t time.Time) error
- func (c *SerialConn) SetWriteDeadline(t time.Time) error
- func (c *SerialConn) Write(b []byte) (n int, err error)
- type UDPAddr
Constants ¶
const ( // Test that the device is working. Test = "" // Restart module Restart = "+RST" // Version show info about the current software version. Version = "+GMR" // Enter deep-sleep mode Sleep = "+GSLP" // Configure echo. EchoConfig = "E" // EchoConfigOn EchoConfigOn = EchoConfig + "1" // EchoConfigOff EchoConfigOff = EchoConfig + "0" // Configure UART UARTConfig = "+UART" )
Basic AT commands
const ( // WiFi mode (sta/AP/sta+AP) WifiMode = "+CWMODE" // Connect to an access point. ConnectAP = "+CWJAP" // List available AP's ListAP = "+CWLAP" // Disconnect from the current AP Disconnect = "+CWQAP" // Set softAP configuration. This also activates the ESP8266/ESP32 to act as an access point. // The settings will not be saved in flash memory, so they will be forgotten on next reset. SoftAPConfigCurrent = "+CWSAP_CUR" // Set softAP configuration as saved in flash. This also activates the ESP8266/ESP32 to act as an // access point. The settings will be saved in flash memory, so they will be used on next reset. SoftAPConfigFlash = "+CWSAP_DEF" // List station IP's connected to softAP ListConnectedIP = "+CWLIF" // Enable/disable DHCP DHCPConfig = "+CWDHCP" // Set MAC address of station SetStationMACAddress = "+CIPSTAMAC" // Set MAC address of softAP SetAPMACAddress = "+CIPAPMAC" // Set IP address of ESP8266/ESP32 station SetStationIP = "+CIPSTA" // Set IP address of ESP8266/ESP32 when acting as access point. // The IP address will not be saved in flash memory, so it will be forgotten on next reset. SetSoftAPIPCurrent = "+CIPAP_CUR" // Set IP address of ESP8266/ESP32 when acting as access point. // The IP address will be saved in flash memory, so they will be used on next reset. SetSoftAPIPFlash = "+CIPAP_DEF" )
WiFi commands.
const ( // Get connection status TCPStatus = "+CIPSTATUS" // Establish TCP connection or register UDP port TCPConnect = "+CIPSTART" // Send Data TCPSend = "+CIPSEND" // Close TCP/UDP connection TCPClose = "+CIPCLOSE" // Get local IP address GetLocalIP = "+CIFSR" // Set multiple connections mode TCPMultiple = "+CIPMUX" // Configure as server ServerConfig = "+CIPSERVER" // Set transmission mode TransmissionMode = "+CIPMODE" // Set timeout when ESP8266/ESP32 runs as TCP server SetServerTimeout = "+CIPSTO" )
TCP/IP commands
const ( TCPMuxSingle = 0 TCPMuxMultiple = 1 TCPTransferModeNormal = 0 TCPTransferModeUnvarnished = 1 )
const ( WifiModeClient = 1 WifiModeAP = 2 WifiModeDual = 3 WifiAPSecurityOpen = 1 WifiAPSecurityWPA_PSK = 2 WifiAPSecurityWPA2_PSK = 3 WifiAPSecurityWPA_WPA2_PSK = 4 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
Device wraps UART connection to the ESP8266/ESP32.
func (Device) Configure ¶
func (d Device) Configure()
Configure sets up the device for communication.
func (*Device) ConnectTCPSocket ¶
ConnectTCPSocket creates a new TCP socket connection for the ESP8266/ESP32. Currently only supports single connection mode.
func (*Device) ConnectToAP ¶
ConnectToAP connects the ESP8266/ESP32 to an access point. ws is the number of seconds to wait for connection.
func (*Device) ConnectUDPSocket ¶
ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32.
func (Device) DialUDP ¶
func (d Device) DialUDP(network string, laddr, raddr *UDPAddr) (*SerialConn, error)
DialUDP makes a UDP network connection. raadr is the port that the messages will be sent to, and laddr is the port that will be listened to in order to receive incoming messages.
func (*Device) DisconnectFromAP ¶
DisconnectFromAP disconnects the ESP8266/ESP32 from the current access point.
func (*Device) DisconnectSocket ¶
DisconnectSocket disconnects the ESP8266/ESP32 from the current TCP/UDP connection.
func (*Device) EndSocketSend ¶
EndSocketSend tell the ESP8266/ESP32 the TCP/UDP socket data sending is complete, and to return to command mode. This is only used in "unvarnished" raw mode.
func (*Device) GetAPClients ¶
GetAPClients returns the ESP8266/ESP32 current clients when acting as an Access Point.
func (*Device) GetAPConfig ¶
GetAPConfig returns the ESP8266/ESP32 current configuration when acting as an Access Point.
func (*Device) GetAPConfigFlash ¶
GetAPConfigFlash returns the ESP8266/ESP32 current configuration acting as an Access Point from flash storage. These settings are those used after a reset.
func (*Device) GetAPIP ¶
GetAPIP returns the ESP8266/ESP32 current IP addess when configured as an Access Point.
func (*Device) GetAPIPFlash ¶
GetAPIPFlash returns the ESP8266/ESP32 IP address as saved to flash storage. This is the IP address that will be used after a reset.
func (*Device) GetClientIP ¶
GetClientIP returns the ESP8266/ESP32 current client IP addess when connected to an Access Point.
func (*Device) GetConnectedAP ¶
GetConnectedAP returns the ESP8266/ESP32 is currently connected to as a client.
func (*Device) GetMux ¶
GetMux returns the ESP8266/ESP32 current client TCP/UDP configuration for concurrent connections.
func (*Device) GetTCPTransferMode ¶
GetTCPTransferMode returns the ESP8266/ESP32 current client TCP/UDP transfer mode.
func (*Device) GetWifiMode ¶
GetWifiMode returns the ESP8266/ESP32 wifi mode.
func (Device) ListenUDP ¶
func (d Device) ListenUDP(network string, laddr *UDPAddr) (*SerialConn, error)
ListenUDP listens for UDP connections on the port listed in laddr.
func (Device) Query ¶
Query sends an AT command to the ESP8266/ESP32 that returns the current value for some configuration parameter.
func (*Device) ReadSocket ¶
ReadSocket returns the data that has already been read in from the responses.
func (Device) Reset ¶
func (d Device) Reset()
Reset restarts the ESP8266/ESP32 firmware. Due to how the baud rate changes, this messes up communication with the ESP8266/ESP32 module. So make sure you know what you are doing when you call this.
func (Device) Set ¶
Set sends an AT command with params to the ESP8266/ESP32 for a configuration value to be set.
func (*Device) SetAPConfig ¶
SetAPConfig sets the ESP8266/ESP32 current configuration when acting as an Access Point. ch indicates which radiochannel to use. security should be one of the const values such as WifiAPSecurityOpen etc.
func (*Device) SetAPConfigFlash ¶
SetAPConfigFlash sets the ESP8266/ESP32 current configuration acting as an Access Point, and saves them to flash storage. These settings will be used after a reset. ch indicates which radiochannel to use. security should be one of the const values such as WifiAPSecurityOpen etc.
func (*Device) SetAPIP ¶
SetAPIP sets the ESP8266/ESP32 current IP addess when configured as an Access Point.
func (*Device) SetAPIPFlash ¶
SetAPIPFlash sets the ESP8266/ESP32 current IP addess when configured as an Access Point. The IP will be saved to flash storage, and will be used after a reset.
func (*Device) SetClientIP ¶
SetClientIP sets the ESP8266/ESP32 current client IP addess when connected to an Access Point.
func (*Device) SetMux ¶
SetMux sets the ESP8266/ESP32 current client TCP/UDP configuration for concurrent connections either single TCPMuxSingle or multiple TCPMuxMultiple (up to 4).
func (*Device) SetTCPTransferMode ¶
SetTCPTransferMode sets the ESP8266/ESP32 current client TCP/UDP transfer mode. Either TCPTransferModeNormal or TCPTransferModeUnvarnished.
func (*Device) SetWifiMode ¶
SetWifiMode sets the ESP8266/ESP32 wifi mode.
func (*Device) StartSocketSend ¶
StartSocketSend gets the ESP8266/ESP32 ready to receive TCP/UDP socket data.
type IP ¶
type IP []byte
IP is an IP address. Unlike the standard implementation, it is only a buffer of bytes that contains the string form of the IP address, not the full byte format used by the Go standard .
type SerialConn ¶
type SerialConn struct { Adaptor *Device // contains filtered or unexported fields }
SerialConn is a loosely net.Conn compatible intended to support TCP/UDP over serial.
func (*SerialConn) Close ¶
func (c *SerialConn) Close() error
Close closes the connection. Currently only supports a single Read or Write operations without blocking.
func (*SerialConn) LocalAddr ¶
func (c *SerialConn) LocalAddr() UDPAddr
LocalAddr returns the local network address.
func (*SerialConn) Read ¶
func (c *SerialConn) Read(b []byte) (n int, err error)
Read reads data from the connection. TODO: implement the full method functionality: Read can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetReadDeadline.
func (*SerialConn) RemoteAddr ¶
func (c *SerialConn) RemoteAddr() UDPAddr
RemoteAddr returns the remote network address.
func (*SerialConn) SetDeadline ¶
func (c *SerialConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines associated with the connection. It is equivalent to calling both SetReadDeadline and SetWriteDeadline.
A deadline is an absolute time after which I/O operations fail with a timeout (see type Error) instead of blocking. The deadline applies to all future and pending I/O, not just the immediately following call to Read or Write. After a deadline has been exceeded, the connection can be refreshed by setting a deadline in the future.
An idle timeout can be implemented by repeatedly extending the deadline after successful Read or Write calls.
A zero value for t means I/O operations will not time out.
func (*SerialConn) SetReadDeadline ¶
func (c *SerialConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the deadline for future Read calls and any currently-blocked Read call. A zero value for t means Read will not time out.
func (*SerialConn) SetWriteDeadline ¶
func (c *SerialConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the deadline for future Write calls and any currently-blocked Write call. Even if write times out, it may return n > 0, indicating that some of the data was successfully written. A zero value for t means Write will not time out.
func (*SerialConn) Write ¶
func (c *SerialConn) Write(b []byte) (n int, err error)
Write writes data to the connection. TODO: implement the full method functionality for timeouts. Write can be made to time out and return an Error with Timeout() == true after a fixed time limit; see SetDeadline and SetWriteDeadline.