Pi Pico Wireless Part 2

Firstly pressed on jalanjalantok.wordpress.com

In this post, I’d like to share how to connect Pi Pico to WiFi network. Pi Pico is entirely an offline microcontroller, therefore an additional module needs to be attached to get wireless capability. Part 1 of this post talked about bluetooth communication using HC-05 module, and this part 2 is about WiFi with ESP01 module. Maker Pi Pico has a breakout pin to slot in the 1US$ ESP01 module neatly. As literally printed on the PCB, it is traced to GPIO 16 (TX0) and GPIO 17 (RX0) or UART0 of the Pico.

ESP01 listen and respond to AT Command (a set of instructions to control modem). To get started with ESP01 we need to do wiring. ESP01 module is not packed with USB serial controller (see ESP8266 dev kit), so we need the help of FTDI USB to TTL device that bridges the ESP01 module to our computer.

You need a Serial Monitor such as Arduino IDE to send AT command to ESP01. Set the serial monitor to end line with CR and NL. This will make all sent command suffixed with \r\n in order to be accepted. Type AT, click Send then wait for OK respond. When ESP01 returns OK, that means it works.

Next step we need to update the baudrate of ESP01 to 9600 (default 115200). Type AT+UART_DEF=9600,8,1,0,0 and click Send. If ESP01 returns OK, then the baudrate has been adjusted to 9600. We need baud rate at 9600 as Pi Pico is slow in processing incoming UART bytes and no RX buffer. So we might lost some bytes while receiving bulk message from ESP-01. To fix this, we need to set ESP01 to use 9600.

Now we are ready to send AT command to ESP01 from Micropython in Pico. If you are not using Maker Pi Pico, you need to setup the circuit between ESP01 and Pi Pico. As mentioned earlier, I used Maker Pi Pico which has a breakout header to sit ESP01 nicely, no wiring is required. However if you use Pi Pico, you need to connect both modules with jumper cable as illustrated below.

From this point, we are ready to write the python script to send AT command and to receive feedback from the ESP01.

from machine import Pin, UART
import time

uart=UART(0,9600, tx=Pin(16), rx=Pin(17))

ssid="YourAP"
psk="YouPreSharedKey"
  
def send(cmd, timeout=2000):
    print("CMD: " + cmd)
    uart.write(cmd.encode('utf-8'))
    wait(timeout)
    
def wait(timeout=2000):
    t0 = time.ticks_ms()
    resp = b""
    while (time.ticks_ms()-t0) < timeout:
        if uart.any():
            resp = b"". join([resp, uart.read(1)])
    print(resp)

def initiate():
    send("AT\r\n")
    send("AT+GMR\r\n")
    send("AT+RST\r\n")
    send("AT+CWMODE=3\r\n")
    
def connect(ssid,psk):
    send('AT+CWJAP="{}","{}"\r\n'.format(ssid,psk), 5000)

def ifconfig():
    send("AT+CIFSR\r\n", 5000)

if __name__ == '__main__':
    initiate()
    connect(ssid,psk)
    ifconfig()

The output will be like the following:

&gt;&gt;&gt; %Run -c $EDITOR_CONTENT
UART(0, baudrate=9600, bits=8, parity=None, stop=1, tx=16, rx=17, timeout=0, timeout_char=2, invert=None)
CMD: AT

b'AT\r\r\n\r\nOK\r\n'
CMD: AT+GMR

b'AT+GMR\r\r\nAT version:1.2.0.0(Jul  1 2016 20:04:45)\r\nSDK version:1.5.4.1(39cb9a32)\r\nv1.0.0\r\nMar 11 2018 18:27:31\r\nOK\r\n'
CMD: AT+RST

b'AT+RST\r\r\n\r\nOK\r\nWIFI DISCONNECT\r\nF\xe5\xdc\xc2\x08\xae\xf8\x18\xd5!\x83\x88\x00!\x10D\xdef\xe6\x84\x10\x08\n(\x00\x88\xe6\x18\xf1\xe1\xd9kii\xa2\xd8FT@I\xf8\xd8I\xfc\r\nready\r\n'
CMD: AT+CWMODE=3

b'AT+CWMODE=3\r\r\n\r\nOK\r\nWIFI CONNECTED\r\n'
CMD: AT+CWJAP="YourAP","YourPreSharedKey"

b'AT+CWJAP="YourAP","YourPreSharedKey"\r\r\nWIFI DISCONNECT\r\nWIFI CONNECTED\r\nWIFI GOT IP\r\n'
CMD: AT+CIFSR

b'AT+CIFSR\r\r\n+CIFSR:APIP,"192.168.4.1"\r\n+CIFSR:APMAC,"ea:db:84:96:d5:c9"\r\n+CIFSR:STAIP,"192.168.43.123"\r\n+CIFSR:STAMAC,"e8:db:84:96:d5:c9"\r\n\r\nOK\r\n'

Congratulation! You have established two-way communication between Pi Pico and ESP-01 connected to WiFi Network. I am new to AT COMMAND, so lot to learn still. Luckily there are lot of resources out there to help. These are resources i used:

Thanks for reading, until next article.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s