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

🐶 Smart Pet Feeder (Serial Bluetooth Terminal App)

  • 📱 Control gate from mobile

  • 🔵 Use Bluetooth (no WiFi required)

  • 📟 See gate status in app

  • 🎮 Send OPEN / CLOSE commands

📦 Components Required

  • ESP32

  • SG90 Servo Motor

  • External 5V supply (recommended)

  • Android phone

  • Serial Bluetooth Terminal 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


📲 App Setup (Serial Bluetooth Terminal)

Use:

  • Serial Bluetooth Terminal by Kai Morich (Android)

Steps:

  1. Install app from Play Store

  2. Turn ON Bluetooth

  3. Upload code to ESP32

  4. Open app

  5. Search devices

  6. Connect to ESP32 (Default name we set in code)


💻 Complete ESP32 Bluetooth Code

Copy & paste into Arduino IDE:

#include <ESP32Servo.h>
#include "BluetoothSerial.h"

BluetoothSerial SerialBT;
Servo feederServo;

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

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

SerialBT.begin("Smart_Pet_Feeder");
feederServo.attach(servoPin);

feederServo.write(0); // Start closed

Serial.println("Bluetooth Pet Feeder Started");
Serial.println("Device Name: Smart_Pet_Feeder");
}
void loop() {

if (SerialBT.available()) {

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

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

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

SerialBT.println("Food Gate is OPEN");
}

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

SerialBT.println("Food Gate is CLOSED");
}

🧠 Code Explanation (Line by Line Concept)


1️⃣ Include Required Libraries

 
 
#include <ESP32Servo.h>
#include “BluetoothSerial.h”
 
  • ESP32Servo → Controls servo motor

  • BluetoothSerial → Enables Bluetooth communication


2️⃣ Create Objects

BluetoothSerial SerialBT;
Servo feederServo;
 
  • SerialBT → Used for Bluetooth communication

  • feederServo → Used to control servo


3️⃣ Start Bluetooth

SerialBT.begin(“Smart_Pet_Feeder”);
 

This sets Bluetooth device name.

When you scan in mobile, you will see:
👉 Smart_Pet_Feeder


4️⃣ Attach Servo

feederServo.attach(servoPin);
 

Connects servo to GPIO 18.


5️⃣ Check for Bluetooth Data

if (SerialBT.available())
 

Checks if mobile sent any data.


6️⃣ Read Command

String command = SerialBT.readStringUntil(\n);
 

Reads message until Enter key.

Example:
OPEN
CLOSE


7️⃣ Compare Commands

command.equalsIgnoreCase(“OPEN”)
 

Case insensitive:

  • open

  • OPEN

  • Open

All work.


8️⃣ Open Gate Function

feederServo.write(90);
 

Rotates servo to 90°
Gate opens.


9️⃣ Close Gate Function

feederServo.write(0);
 

Rotates servo to 0°
Gate closes.


📱 How to Use in App

After connecting:

Type:

OPEN
→ Gate opens

→ App shows: Food Gate is OPEN

Type:

CLOSE
→ Gate closes

→ App shows: Food Gate is CLOSED


🎯 What Students Learn From This Version

  • Bluetooth communication

  • Serial data handling

  • Mobile app control

  • Real-time feedback

  • Embedded system wireless control


🚀 Next Level Upgrade Options

Shiv 🔥 You can now upgrade to:

  • 🕒 Automatic close after 5 seconds

  • 📊 Send gate status continuously

  • 🔐 Password protected Bluetooth

  • 📱 Custom Android app

  • 🌐 Web server control

  • 📦 Add food level sensor

  • 🧠 Add feeding schedule timer

Scroll to Top