Example Code for Arduino-WIFI control LED
ESP32C3 has WIFI function, the following example uses ESP32C3 to create a wifi server, use the client to connect to the server, and control the on and off of LED.
Hardware Preparation
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Add ESP32 board to Arduino IDE
Steps
- Connect to WIFI "Beetle ESP32 C3", WIFI password has been set: 12345678
- Visit the website http://192.168.4.1/ON to turn on the light. Visit http://192.168.4.1/OFF to turn off the light
- After the visit, click the up and down to conveniently control the on and off of the light without entering the URL.
Sample Code
/*
Steps:
1. Connect to WIFI "Beetle ESP32 C3", WIFI password has been set: 12345678
2. Visit the website http://192.168.4.1/ON to turn on the light; Visit http://192.168.4.1/OFF to turn off the light
3. After the visit, click the up and down to conveniently control the on and off of the light without entering the URL.
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#define myLED 10 //Set pin 10 as LED pin
// Set WIFI name and password
const char *ssid = "Beetle ESP32 C3";//WIFI name
const char *password = "12345678";//password
WiFiServer server(80);//The default web service port is 80
void setup() {
pinMode(myLED, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
//If you want to open the network without a password, please delete the password
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) { // check if connected
Serial.println("New Client.");
String currentLine = ""; // create String variable to save the data
while (client.connected()) { // keep looping while keeping the connection
if (client.available()) { // check if the connection has data
char c = client.read(); // read received data
//Serial.write(c); // print on serial monitor
if (c == '\n') { // if newline is read
//end with a newline reminder
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
//connect characters 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>");
// HTTP response is blank
client.println();
// Break out of the loop
break;
} else { // Clear variable cached data if there is a newline
currentLine = "";
}
} else if (c != '\r') { // If you get characters other than carriage return
currentLine += c; // The obtained character is added to the end of the variable
}
// Check to see if /ON or /OFF
if (currentLine.endsWith("/ON")) {
digitalWrite(myLED, HIGH); //Turn on the light when getting /ON
}
if (currentLine.endsWith("/OFF")) {
digitalWrite(myLED, LOW); //Turn off the light when getting /OFF
}
}
}
// 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, the ip address is 192.168.4.1, and the service is enabled.

Use a browser to access the ip address and get it as shown below

Try clicking the links separately to control the LEDs
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 the service port is connected (whether WIFI is connected) -
client.connected()
Description: Check connection status
Return value: true/false -
client.available()
Description: Detect whether there is data input connected to WIFI -
client.read()
Description: Read WIFI received data -
currentLine.endsWith()
Description: Check whether the bracket content is obtained -
client.stop()
Description: Disconnect
Was this article helpful?
