Programming The ESP32 Using Arduino - A Web Client
Written by Harry Fairhead   
Monday, 16 December 2024
Article Index
Programming The ESP32 Using Arduino - A Web Client
WiFi Scan
The Complete Program

The ESP32 S3 has WiFi, but getting from a simple connection to a web site is a matter of using the supplied client class. This is an extract from Harry Fairhead's latest book on programming the ESP32 using C and the Arduino library.

Programming The ESP32 In C
Using The Arduino Library

By Harry Fairhead

ESP32CArduino360

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
    Extract:PWM ***NEW!!!
  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
  14. The Serial Port
  15. Using WiFi
    Extract: A Web Client
  16. Flash Files
  17. Direct To The Hardware
  18. Free RTOS 

<ASIN:B0DHKT342W>

<ASIN:187196282X>

<ASIN:1871962919>

The ESP32 comes complete with a radio capable of 2.4GHz WiFi and Bluetooth. Most of the time you can ignore the technical details as there are easy-to-use functions which enable you to connect to a WiFi network and exchange data. In this chapter we look at the basics and how to create and use a WiFi connection. The libraries involved are many and extensive due to the need to cover a wide range of different protocols. A consequence is that there is no way to cover all of them in a reasonable space. This chapter is about getting started and understanding the basic structure of the WiFi and IP infrastructures. When you understand this the rest of the API becomes much easier to understand. The topic of Bluetooth is omitted as it is so varied that it deserves a book to itself.

In Chapter but not in this extract

  • ESP32 Architecture
  • The WiFi Stack
  • IPAddress
  • ESP WiFi
  • ESP32 WiFi Events

A Practical Connect Function

Connecting to WiFi is a standard operation and it makes sense to package it in a function.

#include <WiFi.h>
int wifiConnect(char* ssid, char* password) {
  int status = WiFi.begin(ssid, password);
  while (status != WL_CONNECTED) {
    switch (status) {
      case WL_NO_SSID_AVAIL:
        Serial.printf("No AP with name %s can be found",
ssid); return status; case WL_CONNECT_FAILED: Serial.printf("Connection failed"); return status; case WL_CONNECTION_LOST: Serial.printf("Connection lost possible
security problem"); return status; } delay(100); status = WiFi.status(); } return status; }

Using the functions connection becomes easy:

void setup() {
  Serial.begin(9600);
  int status=wifiConnect("ssid", "password");
  Serial.println(status);
};

You can clearly customize the connection to include parameters to control the IP address, host name and authentication type. The delay in the while loop allows other tasks to run while waiting.

Alternatively you could use the ESP32 event system to make the connection asynchronous and to handle status changes after the connection:

#include <WiFi.h>
int status;
void WiFiEvent(WiFiEvent_t event) {
  status = event;
  Serial.println();
  switch (event) {
    case ARDUINO_EVENT_WIFI_READY:
      Serial.println("WiFi interface ready");
      break;
    case ARDUINO_EVENT_WIFI_SCAN_DONE:
      Serial.println("Completed scan for access points");
      break;
    case ARDUINO_EVENT_WIFI_STA_START:
      Serial.println("WiFi client started");
      break;
    case ARDUINO_EVENT_WIFI_STA_STOP:
      Serial.println("WiFi clients stopped");
      break;
    case ARDUINO_EVENT_WIFI_STA_CONNECTED:
      Serial.println("Connected to access point");
      break;
    case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
      Serial.println("Disconnected from
WiFi access point"); break; case ARDUINO_EVENT_WIFI_STA_GOT_IP: Serial.print("Obtained IP address: "); Serial.println(WiFi.localIP()); break; case ARDUINO_EVENT_WIFI_STA_LOST_IP: Serial.println("Lost IP address"); break; default: break; } } void wifiConnect(char* ssid, char* password) { WiFi.onEvent(WiFiEvent); status = WiFi.begin("ssid", "password"); }

Notice that the variable, status, is used to communicate with the rest of the program:

void setup() {
  Serial.begin(9600);
  wifiConnect("ssid", "password");
};
void loop() {
  while (status != ARDUINO_EVENT_WIFI_STA_GOT_IP) {
    delay(1000);
  };
  Serial.println(status);
  delay(1000);
}

The event approach is more sophisticated and can detect additional states, but it is highly ESP-specific.



Last Updated ( Monday, 16 December 2024 )