Course Content
IoT Engineering Course using ESP32 Wifi Robots

📘Real-Time Control (Detailed Explanation)


🎯 Lesson Objective

In this lesson, students will learn:

  • How complete system integration works
  • How ESP32 reads commands from Blynk
  • How functions control robot movement
  • How real-time control is achieved

By the end of this lesson, students will understand both working AND internal logic of the system.


🤖 What We Are Doing in This Lesson

We are combining:

  • Motor control logic
  • WiFi connectivity
  • Blynk communication

👉 Into one complete working system


🔄 Complete System Flow

Button Press → Blynk Cloud → ESP32 → Function Call → Motor Movement


💻 Complete Code (Reference)

 
#define BLYNK_TEMPLATE_ID “YourTemplateID”
#define BLYNK_TEMPLATE_NAME “WiFi Robot”
#define BLYNK_AUTH_TOKEN “YourAuthToken”

#include <WiFi.h>
#include <BlynkSimpleEsp32.h>

char ssid[] = “YourWiFiName”;
char pass[] = “YourPassword”;

// Motor Pins
int IN1 = 26;
int IN2 = 27;
int IN3 = 14;
int IN4 = 12;

 

🔍 SECTION 1: Movement Functions (CORE LOGIC)

These functions control the actual movement of the robot.


🔹 1. stopRobot()

 
void stopRobot() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
 

👉 Explanation:

  • All pins LOW → No current → Motors stop
  • This is a safety function

🔹 2. forward()

 
void forward() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
 

👉 Explanation:

  • Motor A → Forward
  • Motor B → Forward
  • Robot moves straight

🔹 3. backward()

 
void backward() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
 

👉 Explanation:

  • Both motors reverse direction
  • Robot moves backward

🔹 4. left()

 
void left() {
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
 

👉 Explanation:

  • Left motor → backward
  • Right motor → forward
  • Robot turns left

🔹 5. right()

 
void right() {
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
 

👉 Explanation:

  • Left motor → forward
  • Right motor → backward
  • Robot turns right

🔍 SECTION 2: BLYNK_WRITE() (MOST IMPORTANT PART)

This is where ESP32 receives data from mobile app


🧠 How Button Works Internally

In Blynk:

  • Button ON → sends 1
  • Button OFF → sends 0

👉 This value is received in ESP32


🔹 Forward Button (V0)

 
BLYNK_WRITE(V0) {
int value = param.asInt();

if (value == 1) {
forward();
} else {
stopRobot();
}
}

 

🔍 Explanation:

  • BLYNK_WRITE(V0) → triggers when V0 button changes
  • param.asInt() → reads value from app

Case 1:

  • Button pressed → value = 1
  • Calls → forward()

Case 2:

  • Button released → value = 0
  • Calls → stopRobot()

🔹 Backward Button (V1)

 
BLYNK_WRITE(V1) {
int value = param.asInt();

if (value == 1) {
backward();
} else {
stopRobot();
}
}

 

🔹 Left Button (V2)

 
BLYNK_WRITE(V2) {
int value = param.asInt();

if (value == 1) {
left();
} else {
stopRobot();
}
}

 

🔹 Right Button (V3)

 
BLYNK_WRITE(V3) {
int value = param.asInt();

if (value == 1) {
right();
} else {
stopRobot();
}
}

 

🧠 IMPORTANT CONCEPT: EVENT-BASED PROGRAMMING

👉 Unlike loop-based control:

  • Code runs only when button is pressed
  • This is called event-driven system

🔍 SECTION 3: setup() FUNCTION

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

pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);

stopRobot();

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}

 

🔍 Explanation:

  • Serial.begin() → debugging
  • pinMode() → set pins as output
  • stopRobot() → safety start
  • Blynk.begin() → connects ESP32 to:
    • WiFi
    • Blynk cloud

🔍 SECTION 4: loop() FUNCTION

 
void loop() {
Blynk.run();
}
 

🔍 Explanation:

  • Keeps connection alive
  • Listens for incoming commands
  • Without this → nothing works

🧪 REAL-TIME FLOW (VERY IMPORTANT)

  1. User presses button in app
  2. App sends value (1) to Blynk Cloud
  3. ESP32 receives it
  4. Corresponding BLYNK_WRITE() runs
  5. Function is called
  6. Motors move

⚠️ IMPORTANT BEHAVIOR

👉 If two buttons are pressed together:

  • Commands may conflict
  • Robot may behave unexpectedly

👉 Solution:

  • Use only one button at a time

🧠 Key Learning Concepts

  • Event-driven programming
  • Function-based design
  • Real-time communication
  • Input → Processing → Output system

📝 Lesson Summary

In this lesson, students deeply understood how a WiFi-controlled robot works internally. They learned how mobile app commands are received, processed, and converted into physical movement using structured programming.


📌 Practice Task

  • Add Serial print inside each function
  • Display which button is pressed
  • Try pressing multiple buttons and observe behavior
Scroll to Top