#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <PubSubClient.h>
// Pin definitions
#define DHTPIN 4 // DHT11 data pin
#define FAN_PIN 5 // GPIO pin controlling the fan via transistor
#define POT_PIN 34 // Potentiometer for manual speed control
#define DHTTYPE DHT11 // DHT11 sensor
// Set up DHT11
DHT dht(DHTPIN, DHTTYPE);
// Set up LCD (I2C)
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change address if needed
// WiFi and MQTT setup
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";
WiFiClient espClient;
PubSubClient client(espClient);
// Variables
float temperature = 0.0;
int fanSpeed = 0;
bool autoMode = true;
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("Scanning...");
for (byte i = 8; i < 120; i++) {
Wire.beginTransmission(i);
if (Wire.endTransmission() == 0) {
Serial.print("I2C device found at address 0x");
if (i < 16) {
Serial.print("0");
}
Serial.println(i, HEX);
}
}
Serial.println("Scan complete");
// Start DHT sensor
dht.begin();
// Set up the fan control pin
pinMode(FAN_PIN, OUTPUT);
digitalWrite(FAN_PIN, LOW);
// Set up the LCD
lcd.begin(16, 2);
lcd.backlight();
// Connect to WiFi
connectToWiFi();
// Set up MQTT client
client.setServer(mqtt_server, 1883);
client.setCallback(mqttCallback);
// Subscribe to MQTT topics
client.subscribe("fan/mode");
client.subscribe("fan/speed");
}
void loop() {
// Check WiFi connection
if (!WiFi.isConnected()) {
connectToWiFi();
}
// Read temperature from DHT11 sensor
temperature = dht.readTemperature();
// Display temperature and fan speed on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print(" C");
lcd.setCursor(0, 1);
lcd.print("Fan Speed: ");
lcd.print(fanSpeed);
// Publish temperature to MQTT
String tempStr = String(temperature);
client.publish("home/temperature", tempStr.c_str());
// MQTT client loop
if (!client.connected()) {
reconnectMQTT();
}
client.loop();
// Control fan speed based on temperature
if (autoMode) {
if (temperature < 20) {
fanSpeed = 0;
} else if (temperature >= 20 && temperature <= 50) {
fanSpeed = map(temperature, 20, 50, 0, 255); // Mapping temperature to PWM range
} else {
fanSpeed = 255; // Max speed if temp > 50°C
}
} else {
// Read the potentiometer for manual control
fanSpeed = analogRead(POT_PIN) / 4; // Map 0-4095 to 0-255
}
// Set fan speed using PWM
analogWrite(FAN_PIN, fanSpeed);
// Delay before next loop
delay(2000);
}
void connectToWiFi() {
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Connecting to WiFi...");
WiFi.begin(ssid, password);
delay(5000);
}
Serial.println("Connected to WiFi");
}
void reconnectMQTT() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32Client")) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed, retrying in 5 seconds...");
delay(5000);
}
}
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
String message = "";
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
// Control mode: Auto or Manual
if (String(topic) == "fan/mode") {
if (message == "auto") {
autoMode = true;
} else if (message == "manual") {
autoMode = false;
}
}
// Set fan speed manually
if (String(topic) == "fan/speed" && !autoMode) {
fanSpeed = message.toInt();
}
}