Programming
Programming the ESP32 with Arduino IDE
The Arduino IDE provides a simple environment for programming the ESP32. After installing ESP32 board support in the Arduino IDE, the appropriate ESP32 development board and USB port can be selected. The program can then be written, compiled, and uploaded directly to the ESP32.
The first part of the program connects the ESP32 to a selected Wi-Fi network. The network name (SSID) and password are entered in the program. Once powered on, the ESP32 attempts to establish the connection and can display its assigned IP address through the Serial Monitor.
Next, the program configures the selected LED GPIO pin as an output. For example, GPIO 2 can be configured to control the LED. The digitalWrite() function is then used to produce a HIGH or LOW signal, which determines whether the LED is switched on or off.
The ESP32 program also creates a simple web interface. A small web page containing ON and OFF buttons is served by the ESP32’s built-in web server. When a smartphone or computer opens the ESP32’s IP address, this control page allows the user to operate the LED through Wi-Fi.
Finally, the ESP32 receives the ON/OFF commands from the web browser and changes the LED state accordingly. An ON command sets the GPIO HIGH, illuminating the LED, while an OFF command sets it LOW, turning the LED off. This demonstrates the basic programming structure of a Wi-Fi-controlled IoT device.
#include <WiFi.h>
#include <WebServer.h>
// Wi-Fi credentials
const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";
// LED connected to GPIO 2
const int LED_PIN = 2;
// Create web server on port 80
WebServer server(80);
// LED control page
void handleRoot() {
String html = "<!DOCTYPE html>";
html += "<html>";
html += "<head>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1'>";
html += "<title>ESP32 LED Controller</title>";
html += "</head>";
html += "<body style='text-align:center; font-family:Arial;'>";
html += "<h1>Wi-Fi LED Controller</h1>";
html += "<p><a href='/on'>";
html += "<button style='font-size:25px; padding:15px 30px;'>ON</button>";
html += "</a></p>";
html += "<p><a href='/off'>";
html += "<button style='font-size:25px; padding:15px 30px;'>OFF</button>";
html += "</a></p>";
html += "</body>";
html += "</html>";
server.send(200, "text/html", html);
}
// Turn LED ON
void handleLEDOn() {
digitalWrite(LED_PIN, HIGH);
server.sendHeader("Location", "/");
server.send(303);
}
// Turn LED OFF
void handleLEDOff() {
digitalWrite(LED_PIN, LOW);
server.sendHeader("Location", "/");
server.send(303);
}
void setup() {
Serial.begin(115200);
// Configure LED pin as output
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Wi-Fi connected!");
Serial.print("ESP32 IP Address: ");
Serial.println(WiFi.localIP());
// Web server routes
server.on("/", handleRoot);
server.on("/on", handleLEDOn);
server.on("/off", handleLEDOff);
// Start web server
server.begin();
Serial.println("Web server started!");
}
void loop() {
// Handle incoming web requests
server.handleClient();
}