IoT-Smart Fan Control Using ESP8266, DHT11 and KiwisIoT

by Keerthana Dass in Circuits > Electronics

33 Views, 0 Favorites, 0 Comments

IoT-Smart Fan Control Using ESP8266, DHT11 and KiwisIoT

introduction image.png

In this project, we are going to build an IoT-based Smart Fan Control System using an ESP8266 NodeMCU, DHT11 temperature sensor, relay module, 5V DC fan, and KiwisIoT.

The ESP8266 reads the temperature from the DHT11 sensor and sends the temperature value to the KiwisIoT dashboard. The fan is connected through a relay and can be turned ON or OFF remotely using the KiwisIoT dashboard.

This project demonstrates how an ESP8266 can be used to monitor temperature and remotely control a fan through an IoT platform.

Supplies

Supplies image.png

ESP8266 NodeMCU — 1

DHT11 Temperature Sensor — 1

2-Channel Relay Module — 1

5V DC Fan — 1

5V DC Power Supply — 1

Breadboard — 1

Jumper Wires — As required

USB Cable — 1

How the Project Works

step-1.png

The DHT11 temperature sensor measures the surrounding temperature and sends the temperature value to the ESP8266 NodeMCU.

The ESP8266 processes the temperature reading and sends it to the KiwisIoT dashboard through Wi-Fi.

The fan is connected to the ESP8266 through a relay module. The relay allows the fan to be turned ON or OFF remotely using the KiwisIoT dashboard.

KiwisIoT Channels

  1. Channel 0 → Displays the temperature
  2. Channel 1 → Controls the fan

When Channel 1 is set to ON, the ESP8266 activates the relay and the fan turns ON.

When Channel 1 is set to OFF, the relay switches OFF and the fan stops.

Project Flow

Temperature Monitoring:

DHT11 → ESP8266 → Wi-Fi → KiwisIoT

Fan Control:

KiwisIoT → ESP8266 → Relay → 5V Fan

Circuit Connections

step-2.png

The following connections are used to connect the DHT11 sensor, relay module, and 5V fan to the ESP8266 NodeMCU.

DHT11 Temperature Sensor

Connect the DHT11 sensor to the ESP8266 as follows:

DHT11 VCC → ESP8266 3.3V

DHT11 DATA → ESP8266 D5

DHT11 GND → ESP8266 GND

Relay Module

Connect the relay module to the ESP8266 as follows:

Relay VCC → Relay power supply

Relay GND → ESP8266 GND

Relay IN1 → ESP8266 D1

Only IN1 (Relay Channel 1) is used in this project. The second relay channel is not used.

5V Fan

The fan is powered using a separate 5V DC power supply.

Connect the fan through the relay:

5V Power Supply (+) → Relay COM

Relay NO → Fan (+)

Fan (−) → 5V Power Supply (−)

Important

Do not connect the 5V fan directly to the ESP8266. The fan is powered from the separate 5V supply and is switched ON/OFF using the relay.

Installing Arduino IDE and Libraries

Before programming the ESP8266 NodeMCU, we need to install the Arduino IDE and the required libraries.

1. Install Arduino IDE

Download and install the Arduino IDE on your computer.

2. Install ESP8266 Board Support

Open Arduino IDE and add the ESP8266 board package through the Boards Manager.

After installation, select:

Tools → Board → ESP8266 → NodeMCU 1.0 (ESP-12E Module)

3. Install Required Libraries

Open:

Sketch → Include Library → Manage Libraries

Search for and install:

  1. DHT sensor library
  2. Adafruit Unified Sensor
  3. KiwisIoT Library

These libraries are required to read the DHT11 sensor and communicate with the KiwisIoT platform.

Arduino Code

The following Arduino code reads the temperature from the DHT11 sensor and sends the temperature value to KiwisIoT Channel 0.

It also receives the fan control command from KiwisIoT Channel 1 and switches the relay ON or OFF accordingly.

Channel 1 = 1 → Fan ON

Channel 1 = 0 → Fan OFF

#include <ESP8266WiFi.h>
#include <KiwisIoT.h>
#include <DHT.h>

// -------- WiFi ----------
const char* ssid = "YOUR_WIFI_NAME";
const char* pass = "YOUR_WIFI_PASSWORD";

// -------- KiwisIoT Dashboard ----------
const char* topic = "YOUR_TOPIC_ID";

// -------- Relay ----------
#define RELAY1 D1

// -------- DHT11 ----------
#define DHT_PIN D5
#define DHT_TYPE DHT11

DHT dht(DHT_PIN, DHT_TYPE);

// -------- Create KiwisIoT object ----------
KiwisIoT kiwisiot(ssid, pass, topic);

// Receive Control from KiwisIoT
void onControl(String ch, String val) {

Serial.print("Received from KiwisIoT -> ");
Serial.print("Channel: ");
Serial.print(ch);
Serial.print(" | Value: ");
Serial.println(val);


// Channel 1 = Relay / Fan Control
if (ch == "1") {

// Value 1 = ON
if (val == "1") {

// Active LOW relay
digitalWrite(RELAY1, LOW);

Serial.println("Relay / Fan: ON");
}

// Value 0 = OFF
else {

digitalWrite(RELAY1, HIGH);

Serial.println("Relay / Fan: OFF");
}
}
}


// SEND TEMPERATURE TO KIWISIOT
void sendTemperature() {

float temperature = dht.readTemperature();


// Check if DHT11 reading is valid
if (isnan(temperature)) {

Serial.println("Failed to read temperature from DHT11!");
return;
}


// Display temperature
Serial.print("Temperature: ");
Serial.print(temperature, 2);
Serial.println(" °C");


// Send only temperature to Channel 0
String tempData = String(temperature, 2);

kiwisiot.send("0", tempData);

Serial.print("Sent to KiwisIoT Channel 0: ");
Serial.println(tempData);
}


// SETUP
void setup() {

// Start Serial Monitor
Serial.begin(115200);

Serial.println();
Serial.println("==============================");
Serial.println(" SMART FAN + DHT11");
Serial.println("==============================");


// Relay setup
pinMode(RELAY1, OUTPUT);

// Keep fan OFF at startup
digitalWrite(RELAY1, HIGH);

Serial.println("Relay initialized: OFF");


// Start DHT11
dht.begin();

Serial.println("DHT11 initialized");


// Start KiwisIoT
Serial.println("Connecting to KiwisIoT...");

kiwisiot.begin();

// Register receiving function
kiwisiot.onReceive(onControl);

Serial.println("KiwisIoT initialized");
Serial.println("Waiting for commands...");
}


// LOOP
void loop() {

// Keep communication with KiwisIoT running
kiwisiot.run();


// Send temperature every 2 seconds
static unsigned long lastDHT = 0;

if (millis() - lastDHT >= 2000) {

lastDHT = millis();

sendTemperature();
}


delay(100);
}

KiwisIoT Dashboard

Dashboard_image_832x551.png

KiwisIoT is used in this project to monitor the temperature and control the fan remotely.

The ESP8266 communicates with the KiwisIoT platform through Wi-Fi.

Two channels are used in this project:

Channel 0 — Temperature

The temperature measured by the DHT11 sensor is sent to Channel 0 and displayed on the KiwisIoT dashboard.

Channel 1 — Fan Control

Channel 1 is used to control the fan remotely.

ON (1) → Relay turns ON → Fan turns ON

OFF (0) → Relay turns OFF → Fan turns OFF

This allows the user to monitor the temperature and control the fan from the KiwisIoT dashboard.

Testing the Project

Testing_Project_832x551.png

After uploading the program to the ESP8266 NodeMCU, open the Serial Monitor in the Arduino IDE and set the baud rate to 115200.

The ESP8266 connects to the Wi-Fi network and then connects to KiwisIoT.

The DHT11 continuously measures the temperature and sends the temperature value to KiwisIoT Channel 0.

To test the fan control, change Channel 1 on the KiwisIoT dashboard.

Channel 1 = ON (1)

The relay switches ON and the 5V fan starts running.

Channel 1 = OFF (0)

The relay switches OFF and the fan stops.

The Serial Monitor displays the temperature readings and the received fan control commands.

Final Result

Final.png

The Smart Fan Control System is now successfully completed.

The DHT11 sensor measures the surrounding temperature and sends the temperature data to the KiwisIoT dashboard through the ESP8266.

The fan can be controlled remotely using the KiwisIoT dashboard. When the fan control is set to ON, the relay activates and the 5V fan starts running. When it is set to OFF, the relay switches off and the fan stops.

This project demonstrates how an ESP8266, sensor, relay, and IoT platform can be combined to create a simple and practical IoT-based automation system.