Programming The ESP32 In C - Socket Web Client
Written by Harry Fairhead   
Tuesday, 22 October 2024
Article Index
Programming The ESP32 In C - Socket Web Client
A Web Client
HTTP

The web uses the HTTP protocol and this is essentially a set of text headers that tell the server what to do, and a set of headers that the server sends back to tell you what it has done.

The most basic transaction the client can have with the server is to send a GET request for the server to send a particular file.

Thus the simplest header is:

char header[] = "GET /index.html HTTP/1.1\r\n\r\n";

which is a request for the server to send index.html.

However, in most cases we need one more header, HOST, which gives the domain name of the server. Why do we need to do this? Simply because HTTP says you should and many websites are hosted by a single server at the same IP address. Which website the server retrieves the file from is governed by the domain name you specify in the HOST header.

This means that the simplest set of headers we can send the server is:

char header[] = "GET /index.htm HTTP/1.1\r\n
                            HOST:example.com\r\n\r\n";

which corresponds to the headers:

GET /index.html HTTP/1.1
HOST:example.com

An HTTP request always ends with a blank line. If you don't send the blank line then you will get no response from most servers. In addition the HOST header has to have the domain name with no additional syntax - no slashes and no http: or similar.

With the headers defined we can send our first HTTP request using write as if the socket was just another file to write data to:

int n = write(sockfd, header, strlen(header)); 

and, of course, to use the strlen function we need to add:

#include <string.h>

The server receives the HTTP request and should respond by sending the data corresponding to the file specified, i.e. index.html. We can read the response just as if the socket was a file:

char buffer[2048];
n = read(sockfd, buffer, 2048);
printf("%s", buffer);

You can make this more complicated by checking the number of bytes read and reading more if the buffer is full, but this is a simple and direct way to get the HTML.

In fact, you get more than the HTML as you get the entire HTTP response including the response headers:

HTTP/1.1 200 OK
Accept-Ranges: bytes
Age: 483961
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Wed, 14 Aug 2024 08:49:49 GMT
Etag: "3147526947+gzip"
Expires: Wed, 21 Aug 2024 08:49:49 GMT
Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT
Server: ECAcc (nyd/D169)
Vary: Accept-Encoding
X-Cache: HIT
Content-Length: 1256
<!doctype html>
<html>

and so on...

Notice the blank line marking the end of the header and signaling that the data payload follows.

The complete program is:

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "string.h"
#include "socket.h"
#include "wificonnect.h"
void app_main(void)
{  
    wifiConnect("CO", "ssid", "password");
    while (wifiStatus != 1010) {
        vTaskDelay(10 / portTICK_PERIOD_MS);
    };
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in addr;addr.sin_family = AF_INET;
    addr.sin_port = htons(80);
    addr.sin_addr.s_addr = 0x0Ed7b85d;
    if (connect(sockfd, (struct sockaddr*)&addr,
                             sizeof(addr)) < 0)return;
    char header[] = "GET /index.html HTTP/1.1\r\n
                              Host:example.com\r\n\r\n";
    int n = write(sockfd, header, strlen(header));
    char buffer[2048];
    n = read(sockfd, buffer, 2048);
    buffer[n] = 0;
    printf("%s\n", buffer);
}


To make this work you may have to increase the size of the main task stack to 6000 bytes using the configuration editor:

Of course, we can do much better than this simple example. For one thing, each socket operation needs to be checked for errors. Here we only check for the most likely error, that the server refused the connection.

In book but not in this extract:

  • The HTTP Client Component
  • HTTPS Client
  • Request Methods
  • A Sensor Client
  • The HTTP Server Component
  • HTTPS Server Component

Summary

  • Connecting to a WiFi network is a matter of using esp_wifi configured as a station to connect to an access point

  • Once you have a connection and an IP address you can use esp_netif to send and receive packets using the standard sockets interface.

  • Implementing error handling for a WiFi connection can be challenging.

  • An HTTP client can both send and receive data to the server depending on the request it makes.

  • You can create a client or a server using sockets or the supplied client and server components

  • The most common request is GET which accepts data from the server.

  • Both POST and PUT can be used to send data to the server.

  • The only difference between a client and server is that a client can only make a connection, a server can accept a connection.

  • It is possible to avoid having to implement a server on the ESP32 by allowing a client to connect to a server running on another machine and send its data using a PUT or POST request.

  • The HTTP client component is easy use but in most cases you are going to want to use the HTTPS component which doesn’t need a custom certificate to work.

  • The HTTPS server works in roughly the same way as the HTTP server but you need to supply suitable certificates.

Programming The ESP32 In C
Using The Espressif IDF

By Harry Fairhead

espC360

Available as a softback, hardback and kindle from Amazon

Contents

       Preface

  1. The ESP32 – Before We Begin
  2. Getting Started
  3. Getting Started With The GPIO 
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. Advanced Input – Interrupts
  8. Pulse Width Modulation
  9. Controlling Motors And Servos
  10. Getting Started With The SPI Bus
  11. Using Analog Sensors
  12. Using The I2C Bus
  13. One-Wire Protocols
         
    Extract: The S3's RGB LED 
  14. The Serial Port
  15. Using WiFi
          Extract:Socket Web Client ***NEW!
  16. Direct To The Hardware
  17. Free RTOS 

<ASIN:1871962919>

<ASIN:187196282X>

espbook

 

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.

Banner



Last Updated ( Friday, 08 November 2024 )