#define BLYNK_TEMPLATE_ID "YourTemplateID"
#define BLYNK_TEMPLATE_NAME "DeliveryBot"
#define BLYNK_AUTH_TOKEN "YourAuthToken"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// WiFi credentials
char ssid[] = "ZTE_F19AAA";
char pass[] = "6EB62JA52P";
// Stepper motor pins
const int stepPins[4] = {2, 4, 6, 8};
const int dirPins[4] = {3, 5, 7, 9};
// Constants
const int STEP_DELAY = 800;
const int CM_TO_STEPS = 1; // 1 cm = 1 rev = 1 step (as per your assumption)
// Blynk virtual pins
#define VROOM1_BTN V1
#define VROOM2_BTN V2
#define VROOM3_BTN V3
#define VRETURN_BTN V4
#define VLED1 V5
#define VLED2 V6
#define VLED3 V7
void setup() {
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
for (int i = 0; i < 4; i++) {
pinMode(stepPins[i], OUTPUT);
pinMode(dirPins[i], OUTPUT);
}
}
// Set all motors' direction
void setDirection(bool fl, bool fr, bool bl, bool br) {
digitalWrite(dirPins[0], fl);
digitalWrite(dirPins[1], fr);
digitalWrite(dirPins[2], bl);
digitalWrite(dirPins[3], br);
}
// Step all motors
void stepMotors(int steps) {
for (int i = 0; i < steps; i++) {
for (int j = 0; j < 4; j++) digitalWrite(stepPins[j], HIGH);
delayMicroseconds(STEP_DELAY);
for (int j = 0; j < 4; j++) digitalWrite(stepPins[j], LOW);
delayMicroseconds(STEP_DELAY);
}
}
// Movements
void moveForward(int cm) {
Serial.println("Forward");
setDirection(HIGH, HIGH, HIGH, HIGH);
stepMotors(cm * CM_TO_STEPS);
}
void moveBackward(int cm) {
Serial.println("Backward");
setDirection(LOW, LOW, LOW, LOW);
stepMotors(cm * CM_TO_STEPS);
}
void turnRight(int steps) {
Serial.println("Right");
setDirection(HIGH, LOW, HIGH, LOW);
stepMotors(steps);
}
void turnLeft(int steps) {
Serial.println("Left");
setDirection(LOW, HIGH, LOW, HIGH);
stepMotors(steps);
}
// Blynk Button Actions
BLYNK_WRITE(VROOM1_BTN) {
int pressed = param.asInt();
if (pressed) {
Blynk.virtualWrite(VLED1, 255);
moveForward(100);
turnRight(150);
moveForward(30);
delay(60000);
moveBackward(30);
turnRight(150);
moveForward(100);
Blynk.virtualWrite(VLED1, 0);
}
}
BLYNK_WRITE(VROOM2_BTN) {
int pressed = param.asInt();
if (pressed) {
Blynk.virtualWrite(VLED2, 255);
moveForward(120);
delay(60000);
moveBackward(120);
Blynk.virtualWrite(VLED2, 0);
}
}
BLYNK_WRITE(VROOM3_BTN) {
int pressed = param.asInt();
if (pressed) {
Blynk.virtualWrite(VLED3, 255);
moveForward(100);
turnLeft(150);
moveForward(30);
delay(60000);
moveBackward(30);
turnLeft(150);
moveForward(100);
Blynk.virtualWrite(VLED3, 0);
}
}
BLYNK_WRITE(VRETURN_BTN) {
int pressed = param.asInt();
if (pressed) {
Serial.println("Return to base triggered.");
// You can set a default return logic or home position
moveBackward(30);
turnRight(150);
moveForward(100);
}
}
void loop() {
Blynk.run();
}
"""
with open("/mnt/data/delivery_bot_blynk_esp32.ino", "w") as f:
f.write(code)
"/mnt/data/delivery_bot_blynk_esp32.ino"