Adding WiFi To The Pico 2
Written by Harry Fairhead   
Monday, 19 August 2024
Article Index
Adding WiFi To The Pico 2
Connecting the ESP8266 ESP-01
Attention!
Some Utility Functions
Connecting to WiFi
Sending Data
A Web Server
Complete Listing Of Web Server

Connecting the ESP8266 ESP-01

There a number of minor problems in using the ESP8266. The first is that it comes with an 8-pin male connector which is not prototype board friendly. The best solution to this is to use some female-to-male jumper cables to connect it to the prototype board or use female-to-female cables to connect directly to the Pico. Note that for high baud rates cables should be kept as short as possible.

You can power the ESP8266 directly from the Pico's 3.3V supply pin, but if possible it is better to use an alternative as, when transmitting, the ESP8266 takes a lot of current, 300mA or so, and this means there isn't a lot left over to power other things. The maximum current for the Pico’s 3.3V isn’t given, but the chip used claims to be happy at 1200mA and it worked to run all of the examples in this chapter.

The pinout of the ESP-01 is usually shown from the component side, but in fact the pins that you want to connect to are on the other side. To make things easier, the two views are given in the diagram:
The pin functions are:

1 Ground connect to ground
2 TXO the serial tx pin
3 GPIO2 ignore
4 CHPD chip enable connect to 3.3V
5 GPIO0 ignore
6 RST reset leave unconnected
7 RXI the serial rx pin
8 VDD supply voltage connect to 3.3V

esp1From the pinouts you should be able to work out the way the ESP8266 has to be connected. If we use GP4 as Tx from the Pico and GP5 as Rx to the Pico we have to connect ESP-01 pin 7 to GP4, pin 2 to GP5 and pins 8 and 4 to the power supply. To make it all work we also have to connect pin 1 to the ground. 

AT Commands

The key idea in using the ESP8266 is that the Pico has to send an AT command, literally the characters AT, followed by other command strings. The command has to end with \r\n for the ESP8266 to take notice of it. 

You can find a fill list of commands at the Espressif web site, but the most important are:

AT Attention
AT+RST Reset the board
AT+GMR Firmware version
AT+CWMODE= Operating Mode
1 - Client, 2 -Access Point, 3 - Client and Access Point
AT+CWJAP= Join network
AT+CWLAP View available networks
AT+CWQAP Disconnect from network
AT+CIPSTATUS Show current status as socket client or server
AT+CIPSTART= Connect to socket server
AT+CIPCLOSE Close socket connection
AT+CIFSR Show assigned IP address when connected to network
AT+CIPMUX= Set connection
0 - Single Connection, 1 - Multi-Channel Connection
AT+CIPSERVER= Open the Socket Server
AT+CIPMODE= Set transparent mode
AT+CIPSTO= Set auto socket client disconnect timeout from 1s to 28800s

+IPD Data

This is just a very general overview and omits the commands that allow the device to work as an access point. It is assumed that client mode is the more common application, but it isn't difficult to extend this example to access point operation.

Setup

We need a function to set up the hardware ready to communicate with the device:

int initWiFi()
{
    uart_init(uart1, 115200);
    gpio_set_function(4, GPIO_FUNC_UART);
    gpio_set_function(5, GPIO_FUNC_UART);
    uart_set_format(uart1, 8, 1, UART_PARITY_NONE);
    uart_set_translate_crlf(uart1,true);
    sleep_ms(100);
    return 0;
}

The model of ESP8266 used worked at 115200 baud by default. Newer models and updated firmware are reported to work at 9600 baud. If this is the case you need to modify the data rate and/or change the ESP8266's baud rate. 

The entire program is hard coded to use UART1 as this leaves UART0 free for use as a debug port. The 100ms delay at the end is needed to allow the ESP8266 time to initialize. The uart_set_translate_crlf call is only needed to make web pages display correctly as HTML tends to use lf in place of cr.



Last Updated ( Saturday, 24 August 2024 )