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 to WiFi

Our final, and most useful, functions connect the device to a known WiFi network. All you have to do is supply the SSID and password. There are other versions of the command that allow you to specify the connection more precisely, but this general form is the most useful. Connection to a network takes a while and there is quite a lot of data sent back, so we need to use a long timeout in the getBlocks function introduced in the scan function:

int connectWiFi(uint8_t buf[], int len, char ssid[],
char pass[])
{
uint8_t command[128];
int count = snprintf(command, 128,
"AT+CWJAP_CUR=\"%s\",\"%s\"\r\n", ssid, pass);
uart_write_blocking(uart1, command, count);
return getBlocks(buf, len, 20, "OK");
}

Notice the way the snprintf function is used to insert the SSID and password into the command. If you have an older device you might need to change CWJAP_CUR to the deprecated CWJAP command. There is also a CWJAP_DEF command that will save the connection in the flash memory. 

The connection is made with:

connectWiFi(buf, 512,"myWiFi","myPassword");

After a few seconds you should see:

AT+CWJAP_CUR="myWiFi","myPassword"
WIFI CONNECTED
WIFI GOT IP
OK

Once you are connected and the "WIFI GOT IP" message has been received you can ask what the IP address is:

int getIPWiFi(uint8_t buf[], int len)
{
uint8_t SendData[] = "AT+CIFSR\r\n";
uart_write_blocking(uart1, SendData, 10);
return getBlocks(buf, len, 20, "OK");
}

Of course, if you really need to know the IP address within a program you need to extract it from the string. The device replies with:

AT+CIFSR
+CIFSR:STAIP,"192.168.253.4"
+CIFSR:STAMAC,"5c:cf:7f:16:97:ab"
OK

This makes it very easy to get the IP address, even without the help of a regular expression. 

Getting a Web Page

Now we have enough functions to tackle the two standard tasks in using the TCP stack - getting and sending data as a client and as a server. 

First we tackle the problem of acting as a client. This isn't as common a requirement as you might expect because most of the time devices like the Pico are used to supply data to other servers, not the other way round. However, it is worth seeing how it is done. 

It doesn't matter if you are implementing a client or a server, either way you make use of sockets which represent the basic TCP connection. What you do with this connection is up to you. For example, if you send HTTP headers on an appropriate port, you can fetch or deliver a web page, i.e. HTTP over TCP.

The first thing we have to do is set up a socket connection between the client, i.e. the Pico, and the server.

int getWebPageWiFi(uint8_t buf[], int len, char URL[],
char page[])
{
uint8_t command[128];
int count = snprintf(command, 128,
"AT+CIPSTART=\"TCP\",\"%s\",80\r\n", URL);
uart_write_blocking(uart1, command, count);
if (getBlocks(buf, len, 20, "OK") < 0)
return -1;
}

You pass the URL to the function as an IP address or as a full URL, but the device looks up domain names using a fixed set of DNS servers. It is recommended that you use an IP address, especially when testing. The CIPSTART command opens a socket to the specified IP address and port.

You can also specify a TCP or UDP connection in the AT command:

AT+CIPSTART=type, IP, port

In this case we open port 80 on the specified IP address or URL. If it works you will get back a message something like:

Connect
AT+CIPSTART="TCP","192.168.253.23",80
CONNECT
OK

Now we have a socket open we can send some data to the server and wait for some data to be sent back to us. This can be a problem as you can't anticipate the amount of data you get back from the web server. 

For this example, the web page is served by a small sensor that returns a JSON temperature and Humidity reading. The sensor is another Pico running the web server as described in the next section. 



Last Updated ( Saturday, 24 August 2024 )