0% found this document useful (0 votes)
3 views

message

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)
3 views

message

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
You are on page 1/ 4

#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
yawServo.attach(YAW_SERVO_PIN);
pitchServo.attach(PITCH_SERVO_PIN);
rollServo.attach(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


dht.begin();

// Set servos to home position


homeServos();
}

// Read sensors (smoke and temperature)


void checkSensors() {
int smokeValue = analogRead(SMOKE_SENSOR_PIN);
temperature = dht.readTemperature();

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;
pitchServo.write(pitchServoVal);
delay(100);
}
}
}

void downMove(int moves) {


for (int i = 0; i < moves; i++) {
if (pitchServoVal < 170) {
pitchServoVal += 10;
pitchServo.write(pitchServoVal);
delay(100);
}
}
}

void leftMove(int moves) {


for (int i = 0; i < moves; i++) {
yawServo.write(yawServoVal + 15); // Move left
delay(100);
}
}

void rightMove(int moves) {


for (int i = 0; i < moves; i++) {
yawServo.write(yawServoVal - 15); // Move right
delay(100);
}
}
void homeServos() {
yawServo.write(90); // Reset to neutral position
pitchServo.write(90); // Reset to neutral position
rollServo.write(90); // Reset to neutral position
}

// Web interface for control


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

void handleMove() {
String direction = webServer.arg("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();
}

webServer.send(200, "text/plain", "Command executed: " + direction);


}
};

TurretControl turret;

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

// Connect to Wi-Fi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi!");

// Set up web server routes


webServer.on("/", HTTP_GET, turret.handleRoot);
webServer.on("/move", HTTP_GET, turret.handleMove);
webServer.begin();

// Initialize turret control


turret = TurretControl();
}

void loop() {
webServer.handleClient(); // Handle web requests from the mobile app

// Check sensors (smoke and temperature)


turret.checkSensors();

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


}

You might also like