Thursday, February 18, 2021

Connect ESP-01S (ESP8266) to Raspberry Pi Pico/MicroPython, using AT Command



Just first connect ESP-01S to Raspberry Pi Pico/MicroPython UART, send AT-Command, set in Station Mode, connect to AP.

Connection:


Exercise code (MicroPython):

mpyPico_ESP-01S_20210219a.py
import uos
import machine
import utime

"""
ESPRESSIF AT Command Set
https://docs.espressif.com/projects/esp-at/en/latest/AT_Command_Set/
"""

print()
print("Machine: \t" + uos.uname()[4])
print("MicroPython: \t" + uos.uname()[3])

#indicate program started visually
led_onboard = machine.Pin(25, machine.Pin.OUT)
led_onboard.value(0)     # onboard LED OFF/ON for 0.5/1.0 sec
utime.sleep(0.5)
led_onboard.value(1)
utime.sleep(1.0)
led_onboard.value(0)

uart0 = machine.UART(0, baudrate=115200)
print(uart0)

def sendCMD_waitResp(cmd, uart=uart0, timeout=2000):
    print("CMD: " + cmd)
    uart.write(cmd)
    waitResp(uart, timeout)
    print()
    
def waitResp(uart=uart0, timeout=2000):
    prvMills = utime.ticks_ms()
    resp = b""
    while (utime.ticks_ms()-prvMills)<timeout:
        if uart.any():
            resp = b"".join([resp, uart.read(1)])
    print("resp:")
    try:
        print(resp.decode())
    except UnicodeError:
        print(resp)
    
sendCMD_waitResp('AT\r\n')          #Test AT startup
sendCMD_waitResp('AT+GMR\r\n')      #Check version information
#sendCMD_waitResp('AT+RESTORE\r\n')  #Restore Factory Default Settings
sendCMD_waitResp('AT+CWMODE?\r\n')  #Query the Wi-Fi mode
sendCMD_waitResp('AT+CWMODE=1\r\n') #Set the Wi-Fi mode = Station mode
sendCMD_waitResp('AT+CWMODE?\r\n')  #Query the Wi-Fi mode again
#sendCMD_waitResp('AT+CWLAP\r\n', timeout=10000) #List available APs
sendCMD_waitResp('AT+CWJAP="ssid","password"\r\n', timeout=5000) #Connect to AP
sendCMD_waitResp('AT+CIFSR\r\n')    #Obtain the Local IP Address

output in REPL:
MicroPython v1.14 on 2021-02-02; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT

Machine: 	Raspberry Pi Pico with RP2040
MicroPython: 	v1.14 on 2021-02-02 (GNU 9.3.0 MinSizeRel)
UART(0, baudrate=115200, bits=8, parity=None, stop=1, tx=0, rx=1)
CMD: AT

resp:
AT

OK


CMD: AT+GMR

resp:
AT+GMR
AT version:1.7.4.0(May 11 2020 19:13:04)
SDK version:3.0.4(9532ceb)
compile time:May 27 2020 10:12:17
Bin version(Wroom 02):1.7.4
OK


CMD: AT+CWMODE?

resp:
AT+CWMODE?
+CWMODE:1

OK


CMD: AT+CWMODE=1

resp:
AT+CWMODE=1

OK


CMD: AT+CWMODE?

resp:
AT+CWMODE?
+CWMODE:1

OK


CMD: AT+CWJAP="ssid","password"

resp:
AT+CWJAP="ssid","password"
WIFI DISCONNECT
WIFI CONNECTED
WIFI GOT IP

OK


CMD: AT+CIFSR

resp:
AT+CIFSR
+CIFSR:STAIP,"192.168.221.249"
+CIFSR:STAMAC,"e0:98:06:24:b3:a4"

OK


>>> 

The version of my ESP-01S unit is:
AT version:1.7.4.0(May 11 2020 19:13:04)


Next:

Remark:

No comments: