Course Content
📘 MODULE 6 – Smart Pet Feeder
0/1
📘 MODULE 7 – Smart Water Management System
0/2
📘 MODULE 8 – Water Quality Monitoring System
0/2
📘 MODULE 10 – Gas Leakage Detection System
0/2
Mastering IoT with 11 Real-World Projects and 1 mega project

✅ ESP32 + DHT11 + WiFi Terminal (Hotspot Mode)

#include <WiFi.h>
#include <DHT.h>

// ——————– Hotspot (Access Point) Details ——————–
const char* ap_ssid = “ESP32_DHT11”;
const char* ap_password = “12345678”; // Minimum 8 characters

// ——————– DHT Setup ——————–
#define DHTPIN 4
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

// ——————– WiFi Server Setup ——————–
// Port 23 = Telnet (WiFi Terminal Apps mostly use this)
WiFiServer server(23);
WiFiClient client;

void setup() {
Serial.begin(115200);

// Start DHT sensor
dht.begin();

// ——————– Start ESP32 Hotspot ——————–
WiFi.softAP(ap_ssid, ap_password);

// Get ESP32 Hotspot IP Address (Usually 192.168.4.1)
IPAddress IP = WiFi.softAPIP();

Serial.println(“✅ ESP32 Hotspot Started!”);
Serial.print(“📶 Hotspot Name (SSID): “);
Serial.println(ap_ssid);

Serial.print(“🔑 Password: “);
Serial.println(ap_password);

Serial.print(“🌐 Hotspot IP Address: “);
Serial.println(IP);

// ——————– Start WiFi Terminal Server ——————–
server.begin();
Serial.println(“✅ WiFi Terminal Server Started on Port 23”);
}

void loop() {
// ——————– Wait for Client Connection ——————–
if (!client || !client.connected()) {
client = server.available();
return;
}

// DHT11 needs delay between readings
delay(2000);

// ——————– Read DHT11 Data ——————–
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();

// ——————– Check for Sensor Error ——————–
if (isnan(humidity) || isnan(temperature)) {
client.println(“❌ Failed to read from DHT11 sensor!”);
Serial.println(“❌ Failed to read from DHT11 sensor!”);
return;
}

// ——————– Send Data to WiFi Terminal ——————–
client.print(“🌡 Temperature: “);
client.print(temperature);
client.print(” °C | “);

client.print(“💧 Humidity: “);
client.print(humidity);
client.println(” %”);

// ——————– Also Print on Serial Monitor ——————–
Serial.print(“Temperature: “);
Serial.print(temperature);
Serial.print(” °C | Humidity: “);
Serial.print(humidity);
Serial.println(” %”);
}

🔌 Wiring (DHT11 → ESP32)

DHT11 Pin ESP32
VCC 3.3V
GND GND
DATA GPIO4

📱 How to Use WiFi Terminal App (Step-by-Step)

✅ Step 1: Upload Code

Upload to ESP32.

✅ Step 2: Open Serial Monitor (115200)

You will see something like:

 
ESP32 Hotspot Started!
Hotspot Name: ESP32_DHT11
Password: 12345678
Hotspot
IP Address: 192.168.4.1
WiFi Terminal Server Started on Port 23

✅ Step 3: Connect Phone to ESP32 Hotspot

Open WiFi settings in phone and connect to:

📶 WiFi Name: ESP32_DHT11
🔑 Password: 12345678


✅ Step 4: Open WiFi Terminal App

In the terminal app, set:

  • Host / IP: 192.168.4.1

  • Port: 23

Then press Connect


✅ Output on Phone Terminal

 
🌡 Temperature: 28.00 °C | 💧 Humidity: 64.00 %

🧠 FULL DETAILED CODE EXPLANATION (Teaching Style)


✅ 1) Include Libraries

 
#include <WiFi.h>
#include <DHT.h>

🔹 WiFi.h

Used to:

  • Create hotspot

  • Create WiFi server

  • Accept client connection

🔹 DHT.h

Used to:

  • Read temperature

  • Read humidity


✅ 2) Hotspot Name & Password

 
const char* ap_ssid = "ESP32_DHT11";
const char* ap_password = "12345678";

This sets the WiFi hotspot details.

📌 Rules:

  • Password must be minimum 8 characters

  • SSID can be any name


✅ 3) DHT Sensor Setup

 
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
  • DHT data pin connected to GPIO4

  • Sensor type is DHT11

  • Creates DHT object


✅ 4) Create WiFi Server

 
WiFiServer server(23);
WiFiClient client;

🔹 WiFiServer server(23)

This creates a TCP server on port 23.

Port 23 is standard for Telnet.

Most WiFi terminal apps use this.

🔹 WiFiClient client

This stores the phone connection.


✅ 5) setup() Function

Runs once.


🔹 Start Serial Monitor

 
Serial.begin(115200);

🔹 Start DHT sensor

 
dht.begin();

🔹 Start Hotspot

 
WiFi.softAP(ap_ssid, ap_password);

This makes ESP32 act like a WiFi router.

Now your phone can connect to ESP32.


🔹 Get Hotspot IP

 
IPAddress IP = WiFi.softAPIP();

ESP32 hotspot IP is usually:

 
192.168.4.1

🔹 Start server

 
server.begin();

This starts the WiFi terminal server.


✅ 6) loop() Function

Runs forever.


🔹 Check if phone connected

 
if (!client || !client.connected())
{
client = server.available();
return;
}

This means:

  • If no phone is connected → wait

  • When phone connects → store that connection


🔹 Delay for DHT11

 
delay(2000);

DHT11 needs 2 seconds gap.


🔹 Read sensor

 
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();

🔹 Check error

 
if (isnan(humidity) || isnan(temperature))

If wiring wrong or sensor fails, values become NAN.


🔹 Send output to phone terminal

 
client.print(...)

This prints on WiFi terminal app.


🔹 Print on Serial Monitor also

 
Serial.print(...)

So you can debug on laptop also.


⚠ Common Problems & Fix

❌ Phone not connecting

✅ Connect phone WiFi first: ESP32_DHT11

❌ Terminal not connecting

Use:

  • IP: 192.168.4.1

  • Port: 23

❌ Port blocked

Change port 23 to 8080:

 
WiFiServer server(8080);

Then in terminal app:

  • Port = 8080

 

Scroll to Top