Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

🐶 Smart Pet Feeder using ESP32 + Servo + Serial WiFi Terminal

  • Connect ESP32 to WiFi

  • Create a WiFi TCP Server

  • Control gate from mobile app

  • Show real-time status in terminal


📦 Components Required

  • ESP32

  • SG90 Servo Motor

  • External 5V supply (recommended)

  • WiFi connection (Hotspot or Router)

  • Android App: Serial WiFi Terminal (TCP Client app)

Recommended app:

  • “TCP Telnet Terminal”

  • “Serial WiFi Terminal”

  • Any TCP Client app


🔌 Circuit Connections

Servo → ESP32

Servo Wire Connect To
Red (VCC) 5V External Supply
Brown/Black (GND) ESP32 GND
Orange/Yellow (Signal) GPIO 18

⚠ IMPORTANT:
Connect External GND and ESP32 GND together


🌐 Working Principle

  1. ESP32 connects to WiFi

  2. Creates TCP server (Port 23)

  3. Mobile connects using IP address

  4. User sends:

    • OPEN

    • CLOSE

  5. ESP32 controls servo

  6. Status shown in app


💻 Complete ESP32 WiFi Terminal Code

Copy and paste into Arduino IDE:

#include <WiFi.h>
#include <ESP32Servo.h>

const char* ssid = "YOUR_WIFI_NAME";
const char* password = "YOUR_WIFI_PASSWORD";

WiFiServer server(23); // Port 23 (Telnet)

Servo feederServo;
const int servoPin = 18;
int gateState = 0; // 0 = Closed, 1 = Open

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

feederServo.attach(servoPin);
feederServo.write(0); // Start closed

Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("\nWiFi Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

server.begin();
Serial.println("Server Started");
}

void loop() {

WiFiClient client = server.available();

if (client) {
Serial.println("Client Connected");

while (client.connected()) {

if (client.available()) {

String command = client.readStringUntil('\n');
command.trim();

if (command.equalsIgnoreCase("OPEN")) {
openGate(client);
}
else if (command.equalsIgnoreCase("CLOSE")) {
closeGate(client);
}
else {
client.println("Invalid Command! Send OPEN or CLOSE");
}
}
}

client.stop();
Serial.println("Client Disconnected");
}
}

void openGate(WiFiClient client) {
feederServo.write(90);
gateState = 1;

client.println("Food Gate is OPEN");
Serial.println("Gate Opened");
}

void closeGate(WiFiClient client) {
feederServo.write(0);
gateState = 0;

client.println("Food Gate is CLOSED");
Serial.println("Gate Closed");
}


🧠 Code Explanation (Concept Wise)


1️⃣ Include Libraries

 
 
#include <WiFi.h>
#include <ESP32Servo.h>
 
  • WiFi.h → Connect ESP32 to WiFi

  • ESP32Servo → Control servo motor


2️⃣ WiFi Credentials

 
 
const char* ssid = “YOUR_WIFI_NAME”;
const char* password = “YOUR_WIFI_PASSWORD”;
 

Replace with:

  • Your router name

  • Your router password


3️⃣ Create Server

 
 
WiFiServer server(23);
 

Port 23 is Telnet port.
Mobile app connects using:

IP + Port 23


4️⃣ Connect to WiFi

 
 
WiFi.begin(ssid, password);
 

Loop waits until connected.

Then prints:

 
 
IP Address: 192.168.X.X
 

You will use this IP in mobile app.


5️⃣ Accept Client

 
 
WiFiClient client = server.available();
 

Checks if someone connected from mobile.


6️⃣ Read Command

 
 
String command = client.readStringUntil(\n);
 

Reads message sent from mobile.

Example:
OPEN
CLOSE


7️⃣ Control Servo

OPEN → 90°
CLOSE → 0°


📱 How to Use Mobile App

  1. Upload code

  2. Open Serial Monitor

  3. Note IP address

  4. Install TCP Client app

  5. Enter:

    • Host: (ESP32 IP)

    • Port: 23

  6. Connect

Now type:

 
 
OPEN
 

Gate opens.

 
 
CLOSE
 

Gate closes.


📟 Example Output in App

 
 
Food Gate is OPEN
Food Gate is CLOSED
 

🎯 What Students Learn

  • WiFi networking

  • TCP Server concept

  • IP address usage

  • Remote control via local network

  • Client-Server communication

  • IoT fundamentals


🚀 Upgrade Ideas (Professional Level)

  • 🌍 Global control using Port Forwarding

  • 🌐 Web browser control (no app required)

  • 📊 Web dashboard

  • 🕒 Auto feeding timer

  • 📦 Food level sensor integration

  • 📱 Custom Android app



Scroll to Top