5.4.21 Project 12.2 WiFi Control LED and Fan
1. Description
In this project, we will learn how to realize different functions of the smart home through accessing different strings under the address. There is a LCD screen that can print out the IP address, which is much more convenient.
2. Test Code
⚠️ \ ATTENTION:\ After opening the code file, you need to modify
the WiFi name and passwords that the ESP32 development board needs to
connect to. Replace ChinaNet-2.4G-0DF0 and ChinaNet@233 with
your own WiFi name and password respectively. You must do this before
uploading the code; otherwise, the ESP32 board will not be able to
connect to the network.
const char* ssid = "ChinaNet-2.4G-0DF0"; // Enter your own WiFi name
const char* password = "ChinaNet@233"; // Enter your own WiFi passwords
⚠️ NOTE: Please ensure that the WiFi name and passwords in the code are the same as the network connected to your computer, mobile phone/tablet, ESP32 development board and router. They must be within the same local area network (WiFi).
⚠️ NOTE: The WiFi must be on a 2.4Ghz frequency; otherwise, the ESP32 cannot connect to WiFi.
#include <Arduino.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
String item = "0";
const char* ssid = "ChinaNet-2.4G-0DF0";
const char* password = "ChinaNet@233";
WiFiServer server(80);
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C mylcd(0x27,16,2);
//#include <analogWrite.h>
#define fanPin1 19
#define fanPin2 18
#define led_y 12 //Define yellow LED pin as 12
void setup() {
Serial.begin(115200);
mylcd.init();
mylcd.backlight();
pinMode(led_y, OUTPUT);
pinMode(fanPin1, OUTPUT);
pinMode(fanPin2, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial.println("TCP server started");
MDNS.addService("http", "tcp", 80);
mylcd.setCursor(0, 0);
mylcd.print("ip:");
mylcd.setCursor(0, 1);
mylcd.print(WiFi.localIP()); //LCD displays IP address
}
void loop() {
WiFiClient client = server.available();
if (!client) {
return;
}
while(client.connected() && !client.available()){
delay(1);
}
String req = client.readStringUntil('\r');
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
item=req;
Serial.println(item);
String s;
if (req == "/") //Browser can read the information sent by client.println(s) when accessing the address
{
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
s += ipStr;
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
client.println(s); //Send the content of string S. When accessing the E-smart home address using a browser, the information can be read.
}
if(req == "/led/on") //Browser accesses IP address/led/on
{
client.println("turn on the LED");
digitalWrite(led_y, HIGH);
}
if(req == "/led/off") //Browser accesses IP address/led/off
{
client.println("turn off the LED");
digitalWrite(led_y, LOW);
}
if(req == "/fan/on") //Browser accesses IP address/fan/on
{
client.println("turn on the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 180);
}
if(req == "/fan/off") //Browser accesses IP address/fan/on
{
client.println("turn off the fan");
digitalWrite(fanPin1, LOW); //pwm = 0
analogWrite(fanPin2, 0);
}
//client.print(s);
client.stop();
}
3. Test Result
⚠️ Note: The mobile phone or tablet must be connected to the ESP32 development board via the same WiFi. Otherwise, it will not be able to access the control page. Also, when the ESP32 development board uses the WiFi function, it consumes a lot of power. An external DC power supply is required to meet its power demand for operation. If the power demand is not met, the ESP32 board will keep resetting, resulting in the code not running normally.
If the smart home is successfully connected to WiFi, the LCD screen will display the assigned address.

Accessing address must add / led/on when using the browser, such as my address is 192.168.0.129/ led/on. Then the smart home LED lights will be turned on, if accessing 192.168.0.129/ led /off, then the LED lights will be off.

When the browser accesses 192.168.0.129/fan/ on, the fan of the smart home will be turned on and at 192.168.0.129/fan/ off will be turned off.
