|
Page 3 of 3
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

Buy from Amazon.
Contents
Preface
- The Pico WiFi Stack
- Introduction To TCP
Extract: Simplest HTTP Client *
- More Advanced TCP
- SSL/TLS and HTTPS
Extract: Simplest HTTPS Client *
- Details of Cryptography
Extract: Random Numbers*
- Servers
Extract: HTTP Server *
- UDP For Speed
Extract: Basic UDP *
- SNTP For Time-Keeping
- SMTP For Email
- MQTT For The IoT
- FreeRTOS
Extract: Installing FreeRTOS
- Client Sockets
Extract: Client Sockets NEW!!
- Socket Server
- Secure Sockets
Appendix 1 Getting Started In C
* Extracts from the first edition not yet updated.
<ASIN:B0FDYDPQ54>
|