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

Attention!

To start with the easiest, but possibly least useful, command let's implement some functions that test that we have a connection to the device - always a good idea at the beginning of a project.

In this section we develop the first of a number of functions that send a command to the device and get back a response. To do this we need a few other functions to help and these are reused in other AT functions. Once you have one AT function, the rest are very similar, so it is worth spending some time following how this most simple AT function works.

There isn't much point in moving on until you have tested that the connection works. To do this send the string AT\r\n and the ESP8266 should reply with a single "OK", which proves that the serial interface is working.

The most basic AT function is simply:

int ATWiFi()
{   
    uint8_t SendData[] = "AT\r\n";
    uart_write_blocking(uart1, SendData, 4);
    return 9;
}

This is the most basic form of function that will do the job, but it isn't really practical. We need to check that it works and get some feedback by reading the data from the device. 

The following function attempts to read a block of data from the device:

int getBlock(uint8_t buf[], int len)
{
    int count = 0;
    while (count < len - 1)
    {
if (uart_is_readable_within_us(uart1, 10000)) {
buf[count++] = uart_getc(uart1); if (Debug) uart_putc(uart0,buf[count-1]); } else { break; } }
buf[count] = 0; return count; }

The uart_is_readable_within_us function is used to set an inter-character timeout of 0.1s. That is, if no data is received after 0.1s it is assumed that this is the end of the transaction. The timeout is enough for more than 500 characters at 115200 baud and 100 at 9600 baud. Typical responses are a few hundred bytes, so using a buffer 512 bytes in size is reasonable.

Next the read function attempts to read a full buffer of data. Notice that there might not be a full buffer when the call is made, but as long as the data is coming in with gaps shorter than 0.1s between characters it will continue reading until the buffer is full. Notice that a zero byte is added to the end of the buffer so that it can be treated like a valid C string and count gives the length of the string, not including the zero. 

The return value is the number of bytes read which could be zero. If the constant Debug is a 1 then the string is printed to UART0 and you can examine it. Notice that the function can come to an end for two different reasons – the time between characters is long enough to suggest that the transmission is finished or the buffer is full. If the buffer is full there may well be more data to be received and, as it stands, this data is lost. 

If the AT command has been successful it returns a string containing "OK". One of the problems of working with AT commands is that they don't have an exact syntax and there is no formal "end of message" signal. By observation, any successful command does end with "OK" and if you can't find "OK" in the response then you either haven't read it all or there has been an error. 

With all of this we can now create an ATWiFi function that returns its response:

int ATWiFi(uint8_t buf[], int len)
{   
    uint8_t SendData[] = "AT\r\n";
    uart_write_blocking(uart1, SendData, 4);
    return getBlock(buf,len);
}

A complete program is:

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#define Debug true
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);
    sleep_ms(100);
    return 0;
} int getBlock(uint8_t buf[], int len) { int count = 0; while (count < len - 1) {
if (uart_is_readable_within_us(uart1, 10000))
{
buf[count++] = uart_getc(uart1);
if (Debug)
uart_putc(uart0,buf[count-1]);
}
else
{
break;
}
}
buf[count] = 0;
return count;
}
int ATWiFi(uint8_t buf[], int len)
{
uint8_t SendData[] = "AT\r\n";
uart_write_blocking(uart1, SendData, 4);
return getBlock(buf, len);
}
int main()
{
stdio_init_all();
initWiFi();
uint8_t buf[512];
ATWiFi(buf, 512);
sleep_ms(1000);
}

When you run this program you should see: 

AT
OK

printed on the serial console. If you don't there are five possible reasons:

  1. You have connected the wrong pins – check

  2. You haven’t connected enable to +V

  3. The power supply you are using is inadequate - check/replace

  4. The serial console isn't working - check you can see any message

  5. The baud rate is wrong - try 9600

  6. The ESP8266 is broken - try another



Last Updated ( Saturday, 24 August 2024 )