Master The Pico WiFi: Client Sockets
Written by Harry Fairhead and Mike James   
Tuesday, 18 November 2025
Article Index
Master The Pico WiFi: Client Sockets
Setting Up Sockets
The Complete Program

The complete client.c program is:

#include <stdio.h>
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include <FreeRTOS.h>
#include <task.h>
#include "pico/lwip_freertos.h"
#include "lwip/sockets.h"
#include "lwip/inet.h"
#include "setupWifi.h"
void mainTask(void *arg) {
if (cyw43_arch_init()) {
printf("failed to initialise\n"); } connectWiFi(); int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP); struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(80); addr.sin_addr.s_addr = inet_addr("23.192.228.80");
connect(sockfd, (struct sockaddr *)&addr, sizeof(addr));
char header[] = "GET /index.html HTTP/1.1\r\n
Host: example.com\r\n\r\n ";
int n = write(sockfd, header, strlen(header));
printf("data sent \n");
int len = 1024 * 2;
char buffer[len];
int m = 0;
do
{
int n = read(sockfd, buffer + m, len - m -1);
if (n < 0)
break;
m = m + n;
buffer[m] = 0;
printf("\ndata received %d\n\n", n);
// printf("%s\n", buffer);
} while (true);
printf("Final buffer\n\n%s\n", buffer);
while (true)
{
};
}
int main()
{
TaskHandle_t maintask;
stdio_init_all();
sleep_ms(10);
xTaskCreate(mainTask, "maintask", 2048, NULL, 1,
&maintask);
vTaskStartScheduler();
return 0;
}

Notice that we now wait for the HTTP server to timeout and disconnect from the socket connection – this can take tens of seconds.

Of course, we can do much better than this simple example. For one thing each socket operation needs to be checked for errors.

In chapter but not in this extract

  • Using Tasks - DNS
  • Timeout Select
  • A Select Client Function


Summary

  • Using client sockets is just a matter of opening the socket, reading and writing the socket and then closing it.

  • Setting the address that a client socket connects to is slightly more tricky than specifying an IP address because it is designed to cope with a range of different types of address.

  • Socket functions are blocking, but this is not a problem in a system that has tasks.

  • You can convert the DNS lookup functions into a blocking call and this doesn’t stop other tasks from running.

  • The select function will wait for a file descriptor to be ready to read or write. While it waits it allows other tasks to run.

  • Select is the preferred way of adding a timeout to a socket.

  • Using sockets and select, it is easy enough to implement a standard HTTP request function that can work asynchronously from the rest of the program.

Master the Raspberry Pi Pico in C:
WiFiwith lwIP, mbedTLS & FreeRTOS
Second Edition

By Harry Fairhead & Mike James

masterPicoE2360

Buy from Amazon.

Contents

       Preface

  1. The Pico WiFi Stack
  2. Introduction To TCP
          Extract:
    Simplest HTTP Client *
  3. More Advanced TCP
  4. SSL/TLS and HTTPS
          Extract:
    Simplest HTTPS Client *
  5. Details of Cryptography
          Extract:
    Random Numbers*
  6. Servers
          Extract: HTTP Server *    
  7. UDP For Speed
          Extract: 
    Basic UDP *
  8. SNTP For Time-Keeping
  9. SMTP For Email
  10. MQTT For The IoT
  11. FreeRTOS
         Extract: 
    Installing FreeRTOS 
  12. Client Sockets
       Extract:
    Client Sockets  NEW!!
  13. Socket Server
  14. Secure Sockets

    Appendix 1 Getting Started In C

* Extracts from the first edition not yet updated.

<ASIN:B0FDYDPQ54> 

pico book

 

Comments




or email your comment to: comments@i-programmer.info

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.



Last Updated ( Tuesday, 18 November 2025 )