Example Code for Arduino - Control LED via WiFi
The ESP32-S3 is equipped with WiFi function. The example below creates a WiFi server using ESP32-S3, and then connects a client to this server to control the LED's ON/OFF.
Hardware Preparation
FireBeetle 2 ESP32-S3 AI Board with OV2640 Camera x 1
Software Preparation
When you use the ESP32 for the first time, you need to know the following steps:
-
Add the ESP32 development board in Arduino IDE (How to add the ESP32 board to Arduino IDE?)
-
Select the development board and serial port
-
Burn the program
Other Preparation Work
-
Connect WiFi to “ESP32 S3”, WiFi password: 12345678
-
Access the website: http://192.168.4.1/ON to turn on the LED. Enter http://192.168.4.1/OFF to turn off the LED.
-
Click here to control the brightness of LED.
Sample Code
/*
**Steps:**
1. Connect WiFit to “ESP32 S3”, WiFi password: 12345678
2. Access the website: http://192.168.4.1/ON to turn on the LED. Enter http://192.168.4.1/OFF to turn off the LED.
3. Click **here** to control the brightness of LED.
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define myLED 21 //Set pin 21 to the LED pin
// Set WIFI name and password
const char *ssid = "ESP32 S3";//WIFI name
const char *password = "12345678";//password
WiFiServer server(80);//Port 80 is the default web server port
void setup() {
pinMode(myLED, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
//Delete password if you want an open network.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.begin();
Serial.println("Server started");
}
void loop() {
WiFiClient client = server.available(); // Detect waiting for connection...
if (client) { // Connection detecting
Serial.println("New Client.");
String currentLine = ""; // Create a String variable to store data
while (client.connected()) { // loop when keep connecting
if (client.available()) { // Detect is there is data on the connection
char c = client.read(); // Read the received data
//Serial.write(c); // Print on serial monitor
if (c == '\n') { // If an newline character is read
//Use newline character to indicate the end
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
//Connect character with here
client.print("Click <a href=\"/ON\">here</a> to turn ON the LED.<br>");
client.print("Click <a href=\"/OFF\">here</a> to turn OFF the LED.<br>");
// empty line in a HTTP response
client.println();
// Break out of a loop
break;
} else { // Clear cached data in variable if there is one newline character
currentLine = "";
}
} else if (c != '\r') { // If characters except Carriage Return is obtained
currentLine += c; // Add the obtained character to end of the variable
}
// Check if /ON or /OFF is obtained
if (currentLine.endsWith("/ON")) {
digitalWrite(myLED, HIGH); //Turn on LED when /ON is obtained
}
if (currentLine.endsWith("/OFF")) {
digitalWrite(myLED, LOW); //Turn on LED when /OFF is obtained
}
}
}
// disconnect
client.stop();
Serial.println("Client Disconnected.");
}
}
Result
Use the mobile phone to connect to this wifi, access 192.168.4.1 through the browser. As shown in the figure below, the IP address is 192.168.4.1, and the service is enabled.

Access the IP address on a broswer and the fllowing will appear.

Other Supplementary Information
Member function
-
WiFiServer server()
- Description: Set the server port
-
softAP(ssid,password)
-
Description: Configure WiFi as AP mode, and set the name and password
-
Parameter:
-
ssid: wifi name in AP mode
-
password: wifi password in ap mode
-
-
-
server.available()
- Description: Check whether there is connection on service port(whether WIFI is connected)
-
client.connected()
-
Description: Check connection status
-
Return value: true/false
-
-
client.available()
- Description: Detect whether there is data input from the WIFI it is connected to
-
client.read()
- Description: Read the data received by WiFi
-
currentLine.endsWith()
- Description: if it ends with the given suffix in the bracket
-
client.stop()
- Description: Disconnect
Was this article helpful?
