0% found this document useful (0 votes)
11 views4 pages

ESP32 Turret Control System Code

Uploaded by

champ4827
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

ESP32 Turret Control System Code

Uploaded by

champ4827
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#include <WiFi.

h>
#include <Servo.h>
#include <DHT.h>
#include <ESP32WebServer.h>

// WiFi credentials
#define WIFI_SSID "your_ssid"
#define WIFI_PASSWORD "your_password"

// Pin definitions
#define SMOKE_SENSOR_PIN 34 // Smoke sensor
#define TEMP_SENSOR_PIN 32 // DHT22 or DHT11 sensor for temperature
#define WATER_PUMP_PIN 33 // Relay or transistor for controlling the water
pump
#define YAW_SERVO_PIN 10
#define PITCH_SERVO_PIN 11
#define ROLL_SERVO_PIN 12

// Turret movement variables


int yawServoVal = 90;
int pitchServoVal = 90;
int rollServoVal = 90;

// Thresholds for sensors


#define SMOKE_THRESHOLD 300 // Change based on your smoke sensor's calibration
#define TEMP_THRESHOLD 30.0 // Temperature threshold to activate water pump

// Define servos
Servo yawServo;
Servo pitchServo;
Servo rollServo;

// Define DHT sensor


DHT dht(TEMP_SENSOR_PIN, DHT22); // Using DHT22 for temperature and humidity

// Define web server


ESP32WebServer webServer(80); // Web server for mobile control

// Heat and smoke detection


bool isSmokeDetected = false;
float temperature = 0.0;

class TurretControl {
public:
TurretControl() {
// Initialize servos
[Link](YAW_SERVO_PIN);
[Link](PITCH_SERVO_PIN);
[Link](ROLL_SERVO_PIN);

// Initialize water pump pin


pinMode(WATER_PUMP_PIN, OUTPUT);
digitalWrite(WATER_PUMP_PIN, LOW); // Initially turn off the water pump

// Initialize DHT sensor


[Link]();

// Set servos to home position


homeServos();
}

// Read sensors (smoke and temperature)


void checkSensors() {
int smokeValue = analogRead(SMOKE_SENSOR_PIN);
temperature = [Link]();

if (smokeValue > SMOKE_THRESHOLD) {


isSmokeDetected = true;
} else {
isSmokeDetected = false;
}

if (temperature > TEMP_THRESHOLD) {


fireWater();
}
}

// Trigger water pump to shoot water


void fireWater() {
digitalWrite(WATER_PUMP_PIN, HIGH); // Activate water pump
delay(2000); // Water shooting duration
digitalWrite(WATER_PUMP_PIN, LOW); // Turn off water pump
}

// Servo control functions


void upMove(int moves) {
for (int i = 0; i < moves; i++) {
if (pitchServoVal > 10) {
pitchServoVal -= 10;
[Link](pitchServoVal);
delay(100);
}
}
}

void downMove(int moves) {


for (int i = 0; i < moves; i++) {
if (pitchServoVal < 170) {
pitchServoVal += 10;
[Link](pitchServoVal);
delay(100);
}
}
}

void leftMove(int moves) {


for (int i = 0; i < moves; i++) {
[Link](yawServoVal + 15); // Move left
delay(100);
}
}

void rightMove(int moves) {


for (int i = 0; i < moves; i++) {
[Link](yawServoVal - 15); // Move right
delay(100);
}
}
void homeServos() {
[Link](90); // Reset to neutral position
[Link](90); // Reset to neutral position
[Link](90); // Reset to neutral position
}

// Web interface for control


void handleRoot() {
String html = "<html><body>";
html += "<h1>Turret Control</h1>";
html += "<button
onclick=\"[Link]='/move?direction=up'\">Up</button>";
html += "<button
onclick=\"[Link]='/move?direction=down'\">Down</button>";
html += "<button
onclick=\"[Link]='/move?direction=left'\">Left</button>";
html += "<button
onclick=\"[Link]='/move?direction=right'\">Right</button>";
html += "<br>";
html += "<button onclick=\"[Link]='/move?direction=fire'\">Fire
Water</button>";
html += "</body></html>";
[Link](200, "text/html", html);
}

void handleMove() {
String direction = [Link]("direction");

if (direction == "up") {
upMove(1);
} else if (direction == "down") {
downMove(1);
} else if (direction == "left") {
leftMove(1);
} else if (direction == "right") {
rightMove(1);
} else if (direction == "fire") {
fireWater();
}

[Link](200, "text/plain", "Command executed: " + direction);


}
};

TurretControl turret;

void setup() {
[Link](9600);

// Connect to Wi-Fi
[Link](WIFI_SSID, WIFI_PASSWORD);
while ([Link]() != WL_CONNECTED) {
delay(1000);
[Link]("Connecting to WiFi...");
}
[Link]("Connected to WiFi!");

// Set up web server routes


[Link]("/", HTTP_GET, [Link]);
[Link]("/move", HTTP_GET, [Link]);
[Link]();

// Initialize turret control


turret = TurretControl();
}

void loop() {
[Link](); // Handle web requests from the mobile app

// Check sensors (smoke and temperature)


[Link]();

delay(100); // Small delay to ensure smooth control and sensor readings


}

You might also like