🐶 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
-
ESP32 connects to WiFi
-
Creates TCP server (Port 23)
-
Mobile connects using IP address
-
User sends:
-
OPEN
-
CLOSE
-
-
ESP32 controls servo
-
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 <ESP32Servo.h>
-
WiFi.h → Connect ESP32 to WiFi
-
ESP32Servo → Control servo motor
2️⃣ WiFi Credentials
const char* password = “YOUR_WIFI_PASSWORD”;
Replace with:
-
Your router name
-
Your router password
3️⃣ Create Server
Port 23 is Telnet port.
Mobile app connects using:
IP + Port 23
4️⃣ Connect to WiFi
Loop waits until connected.
Then prints:
You will use this IP in mobile app.
5️⃣ Accept Client
Checks if someone connected from mobile.
6️⃣ Read Command
Reads message sent from mobile.
Example:
OPEN
CLOSE
7️⃣ Control Servo
OPEN → 90°
CLOSE → 0°
📱 How to Use Mobile App
-
Upload code
-
Open Serial Monitor
-
Note IP address
-
Install TCP Client app
-
Enter:
-
Host: (ESP32 IP)
-
Port: 23
-
-
Connect
Now type:
Gate opens.
Gate closes.
📟 Example Output in App
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