#include "DHT22.h"
#include <Wire.h>
#include <WiFi.h>
#include <ThingSpeak.h>
#include <Stepper.h>
// Pin Definitions
#define DHTPIN 15 // ESP32 pin for DHT22
#define BUZZER_PIN 14 // ESP32 pin for Buzzer
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Wi-Fi Credentials
const char* ssid = "Wokwi_guest";
const char* password = "Password";
// ThingSpeak Channel and API Key
unsigned long channelID = 2779003; // Replace with your ThingSpeak Channel ID
const char* writeAPIKey = "OOALV7SB2XCCLYHD"; // Replace with your Write API Key
WiFiClient client;
// Define the stepper motor pins for the two motors
#define STEPPER1_PIN1 16
#define STEPPER1_PIN2 17
#define STEPPER1_PIN3 18
#define STEPPER1_PIN4 19
#define STEPPER2_PIN1 21
#define STEPPER2_PIN2 22
#define STEPPER2_PIN3 23
#define STEPPER2_PIN4 25
// Stepper motor steps per revolution
#define STEPS_PER_REVOLUTION 2048 // Adjust this based on your motor specifications
// Create Stepper objects for the two motors
Stepper motor1(STEPS_PER_REVOLUTION, STEPPER1_PIN1, STEPPER1_PIN2, STEPPER1_PIN3, STEPPER1_PIN4);
Stepper motor2(STEPS_PER_REVOLUTION, STEPPER2_PIN1, STEPPER2_PIN2, STEPPER2_PIN3, STEPPER2_PIN4);
void setup() {
Serial.begin(115200);
dht.begin();
// Set the motor speed (rpm)
motor1.setSpeed(15); // 15 RPM for motor 1
motor2.setSpeed(15); // 15 RPM for motor 2
pinMode(BUZZER_PIN, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi!");
// Connect to ThingSpeak
ThingSpeak.begin(client);
}
void loop() {
// Read temperature and humidity from DHT22
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Check if the temperature reading is valid
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print Temperature and Humidity to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
// Define motor status (1 = ON, 0 = OFF)
int motorStatus = 0;
// If the temperature exceeds 40°C, rotate motors and activate the buzzer
if (temperature >= 40) {
// Rotate both motors (for biaxial movement)
Serial.println("Temperature is high! Rotating motors...");
// Rotate motor 1 (X-axis)
motor1.step(2048); // Rotate 1 full revolution (adjust as needed)
// Rotate motor 2 (Y-axis)
motor2.step(2048); // Rotate 1 full revolution (adjust as needed)
// Activate Buzzer to warn user
digitalWrite(BUZZER_PIN, HIGH);
delay(1000); // Buzzer sound duration
digitalWrite(BUZZER_PIN, LOW);
motorStatus = 1; // Motors are rotating
} else {
motor1.step(0); // Stop motor 1
motor2.step(0); // Stop motor 2
motorStatus = 0; // Motors are off
}
// Send data to ThingSpeak
ThingSpeak.setField(1, temperature); // Field 1: Temperature
ThingSpeak.setField(2, humidity); // Field 2: Humidity
ThingSpeak.setField(3, motorStatus); // Field 3: Motor Status (1 = ON, 0 = OFF)
// Update the ThingSpeak channel with the data
int httpCode = ThingSpeak.writeFields(channelID, writeAPIKey);
if (httpCode == 200) {
Serial.println("Data sent to ThingSpeak successfully.");
} else {
Serial.println("Error sending data to ThingSpeak.");
}
// Wait for 20 seconds before the next reading
delay(20000);
}