#include <WiFi.h>
#include <DHT.h>
#include <Stepper.h>
// DHT11 settings
#define DHTPIN 4 // Pin where the DHT11 is connected
#define DHTTYPE DHT22 // DHT 22 type
DHT dht(DHTPIN, DHTTYPE);
// Wi-Fi settings
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// Stepper Motor settings
const int stepsPerRevolution = 200; // Change according to your motor
Stepper stepper(stepsPerRevolution, 12, 13, 14, 15); // Motor connected to GPIO 12, 13, 14, 15
// Temperature threshold
const float temperatureThreshold = 30.0;
void setup() {
Serial.begin(115200);y
// Initialize DHT sensor
dht.begin();
// Initialize stepper motor speed
stepper.setSpeed(60); // 60 RPM
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to Wi-Fi");
}
void loop() {
// Read temperature from DHT11 sensor
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Display temperature on Serial Monitor
Serial.print("Temperature: ");
Serial.println(temperature);
// Send data to web server (for simulation you can just print it)
sendDataToServer(temperature);
// Rotate stepper motor if temperature exceeds threshold
if (temperature > temperatureThreshold) {
Serial.println("Temperature threshold exceeded! Rotating motor.");
stepper.step(stepsPerRevolution); // Rotate the stepper motor
}
delay(2000); // Wait for 2 seconds before reading again
}
// Simulate sending data to a web server
void sendDataToServer(float temperature) {
Serial.print("Sending temperature data to server: ");
Serial.println(temperature);
// Here you can implement the WebSocket or HTTP request logic
}