Connecting ESP32 to WiFi in 4 Steps

As our dependence on the internet deepens, it seems that people have developed a reliance on WiFi. Regardless of where they are, they are constantly searching for available WiFi hotspots to stay connected online. While connecting to WiFi is a simple task for smartphones and tablets, configuring WiFi for your ESP32 to enable remote control or IoT development requires some time. Fortunately, the ESP32 offers a convenient solution to this problem: Web Hotspot Manual Provisioning. So let’s take a look at how to implement this operation!

Step 1: Preparation

Before getting started, make sure you have the following:

  • Arduino IDE installed with support for ESP32 development boards.
  • WebServer and WiFi libraries installed to enable serial communication between ESP32 and SNR9816TTS modules.

Step 2: Example Code

Here’s a simple example code demonstrating how to use ESP32 to create an AP hotspot and provide a network configuration page for entering WiFi information.

				
					#include <WiFi.h>
#include <WebServer.h>
const char *ssid = "ESP32-AP";
const char *password = "";  // Set to an empty string to indicate no password
WebServer server(2024);
void setup() {
  Serial.begin(115200);
  // Set ESP32 to AP mode
  WiFi.softAP(ssid, password);
  // Configure server handling functions
  server.on("/", HTTP_GET, handleRoot);
  server.on("/connect", HTTP_POST, handleConnect);
  // Start server
  server.begin();
  Serial.println("WiFi hotspot started");
}
void loop() {
  // Handle client requests
  server.handleClient();
}
void handleRoot() {
  // Return a simple HTML form for entering WiFi information
  server.send(200, "text/html", "<h1>ESP32 WiFi Provisioning</h1><p>Please enter your WiFi name and password</p><form action='/connect' method='post'><label for='ssid'>WiFi Name:</label><input type='text' id='ssid' name='ssid'><br><label for='password'>WiFi Password:</label><input type='password' id='password' name='password'><br><input type='submit' value='Connect'></form>");
}
void handleConnect() {
  // Handle connection request, attempt to connect to the specified WiFi network
  String ssid = server.arg("ssid");
  String password = server.arg("password");
  if (ssid != "" && password != "") {
    WiFi.begin(ssid.c_str(), password.c_str());
    int count = 0;
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
      if (++count > 60) {
        Serial.println("WiFi connection failed");
        server.send(400, "text/html", "<h1>Connection failed</h1><p>Please check your WiFi information and try again.</p>");
        return;
      }
    }
    Serial.println("WiFi connected successfully");
    server.send(200, "text/html", "<h1>Connection successful</h1><p>Please disconnect from the network.</p>");
  } else {
    Serial.println("Incomplete WiFi information");
    server.send(400, "text/html", "<h1>Error</h1><p>Please enter WiFi name and password.</p>");
  }
}

				
			

Step 3: Upload Program

In the Arduino IDE, select the correct ESP32 development board model and port, then click the “Upload” button to burn the program into the ESP32.

Step 4: Perform Manual Provisioning

  1. Connect to the ESP32 hotspot: Open the WiFi settings on your smartphone or computer, find and connect to the hotspot named “ESP32-AP.”
  2. Access the provisioning page: Enter http://192.168.33.33:2024 in your browser to access the provisioning page provided by the ESP32.

Visit the network configuration page

  1. Enter WiFi information: On the page, enter the SSID and password for your home WiFi network, then click the “Connect” button.
  2. Confirm provisioning result: If provisioning is successful, the browser will display “Connection successful, please disconnect the network.” Then, disconnect from the ESP32 hotspot to allow the ESP32 to automatically connect to your home WiFi.

You Might Be Interested

what is esp32
What is an ESP32

Dive into the world of microcontrollers with TechSparks’ comprehensive guide to the ESP32! Discover the power of this versatile chip, renowned for its integration, low power consumption, and robust security features, perfect for IoT enthusiasts and DIY projects. Explore its architecture, development boards, and key features, and unleash the full potential of your next project!

esp32 ledc
ESP32 LEDC: Precise Control for Diverse Applications

Dive into the world of precise LED control with ESP32’s LEDC module, revolutionizing microcontroller-based PWM technology for versatile applications. Explore the enhanced capabilities, flexible programming methods, and real-world examples to unlock endless possibilities in lighting, motor control, and more!

esp32 connect to wifi
Connecting ESP32 to WiFi in 4 Steps

Unlock the potential of your ESP32 with these 4 simple steps to WiFi connectivity! Dive into the world of IoT and remote control effortlessly, as we guide you through setting up a Web Hotspot Manual Provisioning system. Say goodbye to complex configurations and hello to seamless connectivity.

Getting Started with ESP-IDF in VSCode
Getting Started with ESP-IDF in VSCode

This guide introduces an efficient method, “ESP-IDF + VSCode,” for ESP32 development, simplifying environment setup and allowing seamless switching between ESP-IDF versions. It covers downloading development tools, configuring ESP-IDF, addressing VSCode configuration issues, managing components, and resolving dependencies.

How to Enable Multi-Core on ESP32 Microcontroller
How to Enable Multi-Core on ESP32 Microcontroller

This comprehensive guide elucidates the process of enabling multi-core functionality on the ESP32 microcontroller, leveraging its dual-core architecture for enhanced parallel processing capabilities. Through detailed steps and code examples, developers can effectively harness the power of both cores to optimize resource utilization and achieve versatile application scenarios.

A Step-by-Step Guide to Using LVGL with ESP32
A Step-by-Step Guide to Using LVGL with ESP32

This guide illustrates how to integrate LVGL with the ESP32, enabling the creation of engaging graphical user interfaces for embedded systems. By configuring libraries and writing code, developers can enhance user experiences on ESP32 devices with dynamic interfaces.

Scroll to Top