espat

package
v0.27.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2024 License: BSD-3-Clause Imports: 11 Imported by: 100

README

ESP-AT Driver

This package provides a driver to use a separate connected WiFi processor either the ESP8266 or the ESP32 from Espressif.

The way this driver works is by using the UART interface to communicate with the WiFi chip using the Espressif AT command set.

ESP-AT Firmware Installation

In order to use this driver, you must have the ESP-AT firmware installed on the ESP8266/ESP32 chip.

Installing on Arduino Nano33 IoT

In order to install the needed firmware on the Arduino Nano33 IoT board's built-in NINA W102 chip, you will need to use the arduino-nano33-iot branch of this fork of the firmware:

https://github.com/hybridgroup/esp32-at

To flash this firmware on the Arduino Nano33 IoT you will need to follow the following procedure:

  • Install Arduino SAMD Boards from the Boards Manager.
  • Install WiFiNANO from the Library Manager.
  • Using the normal Arduino software, load the SerialNINAPassthrough sketch on to the board (in File -> Examples -> WiFiNINA-> Tools).
  • Flash the NINA 102 firmware using the make flash command in the https://github.com/hybridgroup/esp32-at repo.

You only need to do this one time, and then the correct ESP-AT firmware will be on the NINA chip, and you can just flash the Arduino Nano33 IoT board using TinyGo. We should be able to remove some of these step in a future release of this software.

Installing on ESP32

The official repository for the ESP-AT for the ESP32 processor is located here:

https://github.com/espressif/esp32-at

Your best option is to follow the instructions in the official repo.

Installing on ESP8266

The official repository for the AT command set firmware for the ESP8266 processor is located here:

https://github.com/espressif/ESP8266_NONOS_SDK

First clone the repo:

git clone https://github.com/espressif/ESP8266_NONOS_SDK.git

You will also need to install the Espressif esptool to flash this firmware on your ESP8266:

https://github.com/espressif/esptool

Once you have obtained the binary code, and installed esptool, you can flash the ESP8266.

Here is an example shell script that flashes a Wemos D1 Mini board:

#!/bin/sh
SPToolDir="$HOME/.local/lib/python2.7/site-packages"
FirmwareDir="$HOME/Development/ESP8266_NONOS_SDK"
cd "$SPToolDir"
port=/dev/ttyUSB0
if [ ! -c $port ]; then
   port=/dev/ttyUSB0
fi
if [ ! -c $port ]; then
   echo "No device appears to be plugged in.  Stopping."
fi
printf "Writing AT firmware to the Wemos D1 Mini in 3..."
sleep 1; printf "2..."
sleep 1; printf "1..."
sleep 1; echo "done."
echo "Erasing the flash first"
esptool.py --port $port erase_flash
esptool.py --port /dev/ttyUSB0 --baud 115200 \
   write_flash -fm dio -ff 20m -fs detect \
   0x0000 "$FirmwareDir/bin/boot_v1.7.bin" \
   0x01000 "$FirmwareDir/bin/at/512+512/user1.1024.new.2.bin" \
   0x3fc000 "$FirmwareDir/bin/esp_init_data_default_v05.bin"  \
   0x7e000 "$FirmwareDir/bin/blank.bin" \
   0x3fe000 "$FirmwareDir/bin/blank.bin"

echo "Check the boot by typing: miniterm $port 74800"
echo " and then resetting.  Use Ctrl-] to quit miniterm."

Documentation

Index

Constants

View Source
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

View Source
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.
	// On the ESP8266 the settings will not be saved in flash memory, so they will be forgotten on next reset.
	// On the ESP32 the settings WILL be saved in flash memory, so they will be used on next reset.
	SoftAPConfigCurrent = "+CWSAP"

	// Set softAP configuration. This also activates the ESP8266/ESP32 to act as an access point.
	// On the ESP8266 the settings will not be saved in flash memory, so they will be forgotten on next reset.
	// On the ESP32 the settings WILL be saved in flash memory, so they will be used on next reset.
	SoftAPConfigFlash = "+CWSAP"

	// 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.
	// On the ESP8266 the IP address will not be saved in flash memory, so it will be forgotten on next reset.
	// On the ESP32 the IP address WILL be saved in flash memory, so it will be used on next reset.
	SetSoftAPIPCurrent = "+CIPAP"

	// Set IP address of ESP8266/ESP32 when acting as access point.
	// On the ESP8266 the IP address will not be saved in flash memory, so it will be forgotten on next reset.
	// On the ESP32 the IP address WILL be saved in flash memory, so it will be used on next reset.
	SetSoftAPIPFlash = "+CIPAP"
)

WiFi commands.

View Source
const (
	// Get connection status
	TCPStatus = "+CIPSTATUS"

	// Establish TCP connection or register UDP port
	TCPConnect = "+CIPSTART"

	// DNS Lookup
	TCPDNSLookup = "+CIPDOMAIN"

	// 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

View Source
const (
	TCPMuxSingle   = 0
	TCPMuxMultiple = 1

	TCPTransferModeNormal      = 0
	TCPTransferModeUnvarnished = 1
)
View Source
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 Config added in v0.27.0

type Config struct {
	// UART config
	Uart *machine.UART
	Tx   machine.Pin
	Rx   machine.Pin
}

type Device

type Device struct {
	// contains filtered or unexported fields
}

func NewDevice added in v0.27.0

func NewDevice(cfg *Config) *Device

func (*Device) Accept added in v0.27.0

func (d *Device) Accept(sockfd int) (int, netip.AddrPort, error)

func (*Device) Addr added in v0.27.0

func (d *Device) Addr() (netip.Addr, error)

func (*Device) Bind added in v0.27.0

func (d *Device) Bind(sockfd int, ip netip.AddrPort) error

func (*Device) Close added in v0.27.0

func (d *Device) Close(sockfd int) error

func (*Device) Connect added in v0.27.0

func (d *Device) Connect(sockfd int, host string, ip netip.AddrPort) error

func (*Device) ConnectSSLSocket added in v0.3.0

func (d *Device) ConnectSSLSocket(addr, port string) error

ConnectSSLSocket creates a new SSL socket connection for the ESP8266/ESP32. Currently only supports single connection mode.

func (*Device) ConnectTCPSocket

func (d *Device) ConnectTCPSocket(addr, port string) error

ConnectTCPSocket creates a new TCP socket connection for the ESP8266/ESP32. Currently only supports single connection mode.

func (*Device) ConnectToAP

func (d *Device) ConnectToAP(ssid, pwd string, ws int) error

ConnectToAP connects the ESP8266/ESP32 to an access point. ws is the number of seconds to wait for connection.

func (*Device) ConnectUDPSocket

func (d *Device) ConnectUDPSocket(addr, sendport, listenport string) error

ConnectUDPSocket creates a new UDP connection for the ESP8266/ESP32.

func (*Device) Connected

func (d *Device) Connected() bool

Connected checks if there is communication with the ESP8266/ESP32.

func (*Device) DisconnectFromAP

func (d *Device) DisconnectFromAP() error

DisconnectFromAP disconnects the ESP8266/ESP32 from the current access point.

func (*Device) DisconnectSocket

func (d *Device) DisconnectSocket() error

DisconnectSocket disconnects the ESP8266/ESP32 from the current TCP/UDP connection.

func (Device) Echo

func (d Device) Echo(set bool)

Echo sets the ESP8266/ESP32 echo setting.

func (*Device) EndSocketSend

func (d *Device) EndSocketSend() error

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) Execute

func (d Device) Execute(cmd string) error

Execute sends an AT command to the ESP8266/ESP32.

func (*Device) GetAPClients

func (d *Device) GetAPClients() (string, error)

GetAPClients returns the ESP8266/ESP32 current clients when acting as an Access Point.

func (*Device) GetAPConfig

func (d *Device) GetAPConfig() (string, error)

GetAPConfig returns the ESP8266/ESP32 current configuration when acting as an Access Point.

func (*Device) GetAPConfigFlash

func (d *Device) GetAPConfigFlash() (string, error)

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

func (d *Device) GetAPIP() (string, error)

GetAPIP returns the ESP8266/ESP32 current IP addess when configured as an Access Point.

func (*Device) GetAPIPFlash

func (d *Device) GetAPIPFlash() (string, error)

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

func (d *Device) GetClientIP() (string, error)

GetClientIP returns the ESP8266/ESP32 current client IP addess when connected to an Access Point.

func (*Device) GetConnectedAP

func (d *Device) GetConnectedAP() ([]byte, error)

GetConnectedAP returns the ESP8266/ESP32 is currently connected to as a client.

func (*Device) GetDNS added in v0.3.0

func (d *Device) GetDNS(domain string) (string, error)

GetDNS returns the IP address for a domain name.

func (*Device) GetHardwareAddr added in v0.27.0

func (d *Device) GetHardwareAddr() (net.HardwareAddr, error)

func (*Device) GetHostByName added in v0.27.0

func (d *Device) GetHostByName(name string) (netip.Addr, error)

func (*Device) GetMux

func (d *Device) GetMux() ([]byte, error)

GetMux returns the ESP8266/ESP32 current client TCP/UDP configuration for concurrent connections.

func (*Device) GetTCPTransferMode

func (d *Device) GetTCPTransferMode() ([]byte, error)

GetTCPTransferMode returns the ESP8266/ESP32 current client TCP/UDP transfer mode.

func (*Device) GetWifiMode

func (d *Device) GetWifiMode() ([]byte, error)

GetWifiMode returns the ESP8266/ESP32 wifi mode.

func (*Device) IsSocketDataAvailable added in v0.8.0

func (d *Device) IsSocketDataAvailable() bool

IsSocketDataAvailable returns of there is socket data available

func (*Device) Listen added in v0.27.0

func (d *Device) Listen(sockfd int, backlog int) error

func (*Device) NetConnect added in v0.27.0

func (d *Device) NetConnect(params *netlink.ConnectParams) error

func (*Device) NetDisconnect added in v0.27.0

func (d *Device) NetDisconnect()

func (*Device) NetNotify added in v0.27.0

func (d *Device) NetNotify(cb func(netlink.Event))

func (Device) Query

func (d Device) Query(cmd string) (string, error)

Query sends an AT command to the ESP8266/ESP32 that returns the current value for some configuration parameter.

func (*Device) Read

func (d *Device) Read(b []byte) (n int, err error)

Read raw bytes from the UART.

func (*Device) ReadSocket

func (d *Device) ReadSocket(b []byte) (n int, err error)

ReadSocket returns the data that has already been read in from the responses.

func (*Device) Recv added in v0.27.0

func (d *Device) Recv(sockfd int, buf []byte, flags int, deadline time.Time) (int, error)

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) Response

func (d *Device) Response(timeout int) ([]byte, error)

Response gets the next response bytes from the ESP8266/ESP32. The call will retry for up to timeout milliseconds before returning nothing.

func (*Device) Send added in v0.27.0

func (d *Device) Send(sockfd int, buf []byte, flags int, deadline time.Time) (int, error)

func (Device) Set

func (d Device) Set(cmd, params string) error

Set sends an AT command with params to the ESP8266/ESP32 for a configuration value to be set.

func (*Device) SetAPConfig

func (d *Device) SetAPConfig(ssid, pwd string, ch, security int) error

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

func (d *Device) SetAPConfigFlash(ssid, pwd string, ch, security int) error

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

func (d *Device) SetAPIP(ipaddr string) error

SetAPIP sets the ESP8266/ESP32 current IP addess when configured as an Access Point.

func (*Device) SetAPIPFlash

func (d *Device) SetAPIPFlash(ipaddr string) error

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

func (d *Device) SetClientIP(ipaddr string) error

SetClientIP sets the ESP8266/ESP32 current client IP addess when connected to an Access Point.

func (*Device) SetMux

func (d *Device) SetMux(mode int) error

SetMux sets the ESP8266/ESP32 current client TCP/UDP configuration for concurrent connections either single TCPMuxSingle or multiple TCPMuxMultiple (up to 4).

func (*Device) SetSockOpt added in v0.27.0

func (d *Device) SetSockOpt(sockfd int, level int, opt int, value interface{}) error

func (*Device) SetTCPTransferMode

func (d *Device) SetTCPTransferMode(mode int) error

SetTCPTransferMode sets the ESP8266/ESP32 current client TCP/UDP transfer mode. Either TCPTransferModeNormal or TCPTransferModeUnvarnished.

func (*Device) SetWifiMode

func (d *Device) SetWifiMode(mode int) error

SetWifiMode sets the ESP8266/ESP32 wifi mode.

func (*Device) Socket added in v0.27.0

func (d *Device) Socket(domain int, stype int, protocol int) (int, error)

func (*Device) StartSocketSend

func (d *Device) StartSocketSend(size int) error

StartSocketSend gets the ESP8266/ESP32 ready to receive TCP/UDP socket data.

func (Device) Version

func (d Device) Version() []byte

Version returns the ESP8266/ESP32 firmware version info.

func (*Device) Write

func (d *Device) Write(b []byte) (n int, err error)

Write raw bytes to the UART.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL