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

To send data over a socket you use CIPSEND, which will send any data you specify to the server. As already made clear, what you send is a matter of what protocol you are using over the socket. In this case it is HTTP and we are going to send headers corresponding to a GET request for index.html

    char http[150];
    sprintf(http, 
"GET %s HTTP/1.1\r\nHost:%s\r\n\r\n", page, URL);

This time we can't just send the string using uart_write_blocking because we need to include the length of the string in the header we send to the server.

The headers we are sending are:

GET /index.html HTTP/1.1 
Host:192.168.253.23

Remember, an HTTP request always ends with a blank line.

To send this request we use the CIPSEND command which specifies the number of characters that are to follow:

    count = snprintf(command, 128, 
"AT+CIPSEND=%d\r\n", strlen(http)); uart_write_blocking(uart1, command, count);

Now we have to send the number of bytes/ characters that we specified in the CIPSEND. but first we wait for a ">" to indicate that the device is ready to receive the data:

    if (getBlocks(buf, len, 20, ">") < 0)
        return -1;
    uart_write_blocking(uart1, http, strlen(http));
    return getBlocks(buf, len, 20, "</html>");

What happens next depends on the server. As a result of the HTTP GET the server will now send data over the WiFi link and the device will send this over the serial connection as soon as it gets it. Notice that this data is not a direct response to a command and so the device prefixes it with: 

+IPD,len:

The +IPD makes it clear to the client that is a packet of data sent from the server. The len value gives the number of characters sent after the colon. You could use this to work out when to stop reading data, but for simplicity we scan for "</html>" which is usually, but not always, the final tag at the end of a web page. 

In principle what your program should do next is sit in a polling loop looking for +IPD. It should then read the digits between the comma and the colon and convert this to an integer. Finally it should then read exactly that number of characters from the serial port:

+IPD,1440:HTTP/1.1 200 OK 
Accept-Ranges: bytes 
Age: 223181
Cache-Control: max-age=604800 
Content-Type: text/html; charset=UTF-8 
Date: Sun, 11 Apr 2021 06:40:22 GMT
Etag: "3147526947+gzip" 
Expires: Sun, 18 Apr 2021 06:40:22 GMT 
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT 
Server: ECS (dcb/7F83)
Vary: Accept-Encoding 
X-Cache: HIT 
Content-Length: 1256 
+IPD,127:<html><head><title>Temperature</title></head>
<body><p>{"humidity":0,"airtemperature":0}
</p></body></html>CLOSED

You can see that there are three "packets" of data – 1440 characters and 127 characters. In principle you could process the +IPD characters as they come in and work out how many characters to read. However, you still wouldn't know how many packets to expect. 

The complete function and a more general main program is:

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;
    char http[150];
    sprintf(http, "GET %s HTTP/1.1\r\nHost:%s\r\n\r\n", 
page, URL); count = snprintf(command, 128,
"AT+CIPSEND=%d\r\n", strlen(http)); uart_write_blocking(uart1, command, count); if (getBlocks(buf, len, 20, ">") < 0) return -1; uart_write_blocking(uart1, http, strlen(http)); return getBlocks(buf, len, 20, "</html>"); } int main() { stdio_init_all(); initWiFi(); uint8_t buf[512]; modeWiFi(buf, 512, 1); connectWiFi(buf, 512, "ssid", "password"); getWebPageWiFi(buf, 512, "example.com",
"/index.html"); sleep_ms(1000); }

This program makes use of some of the functions given earlier: initWiFi, modeWiFi, getBlocks and getBlock. It starts with:

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h" #include <string.h> #define Debug true


Last Updated ( Saturday, 24 August 2024 )