IoT-Based Smart Fan Control Using ESP8266, DHT11 and KiwisIoT

by ananthi rk in Circuits > Electronics

18 Views, 0 Favorites, 0 Comments

IoT-Based Smart Fan Control Using ESP8266, DHT11 and KiwisIoT

IoT Smart Fan Control Project.png
  1. In this project, we will build an IoT-based Smart Fan Control System using an ESP8266 NodeMCU, a DHT11 temperature sensor, a relay module, a 5V DC fan, and the KiwisIoT platform.
  2. The system measures the surrounding temperature using the DHT11 sensor and sends the temperature readings to the KiwisIoT dashboard through Wi-Fi. The user can also turn the fan ON or OFF remotely using the dashboard.
  3. The ESP8266 acts as the main controller. It reads temperature data, communicates with the IoT platform, and controls the fan through a relay module.
  4. This project is suitable for beginners who want to learn about IoT, sensor interfacing, Wi-Fi communication, and remote device control.

Supplies

Components Required

  1. ESP8266 NodeMCU development board – 1
  2. DHT11 temperature sensor – 1
  3. 2-channel relay module – 1
  4. 5V DC fan – 1
  5. Suitable 5V DC power supply for the fan – 1
  6. Jumper wires – as required
  7. USB cable for programming the ESP8266 – 1
  8. Computer with Arduino IDE installed
  9. Wi-Fi connection
  10. KiwisIoT account and dashboard

How Does the Project Work?

The DHT11 sensor measures the temperature of the surrounding environment and sends the readings to the ESP8266 NodeMCU.

The ESP8266 connects to a Wi-Fi network and communicates with the KiwisIoT platform. Temperature readings are sent to Channel 0, where they are displayed on the dashboard.

Channel 1 is used to control the fan. When the user selects ON, the ESP8266 activates the relay, and the fan starts running. When the user selects OFF, the relay switches off and the fan stops.

Project Workflow

Temperature Monitoring:

DHT11 Sensor → ESP8266 NodeMCU → Wi-Fi → KiwisIoT Dashboard

Remote Fan Control:

KiwisIoT Dashboard → Wi-Fi → ESP8266 NodeMCU → Relay Module → 5V DC Fan

Understand the Components

ESP8266 NodeMCU

The ESP8266 NodeMCU is the main controller of this project. It reads temperature data from the DHT11 sensor, connects to Wi-Fi, communicates with KiwisIoT, and controls the relay module.

DHT11 Temperature Sensor

The DHT11 measures the surrounding temperature. The sensor's data pin is connected to the D5 pin of the ESP8266.

Relay Module

The relay works as an electrically controlled switch. It allows the low-voltage ESP8266 controller to switch the separate power supply to the fan.

This project uses Relay Channel 1, connected to the D1 pin of the ESP8266. The second relay channel is not used.

5V DC Fan

The fan provides airflow and is controlled by the relay. It receives power from a separate suitable 5V DC supply rather than directly from an ESP8266 GPIO pin.

KiwisIoT Platform

KiwisIoT provides the dashboard for displaying temperature readings and sending fan-control commands to the ESP8266.

Circuit Connections

IoT Smart Fan Control Wiring Diagram.png

Before connecting the components, disconnect the power supply.

A. DHT11 Sensor Connections

Connect the DHT11 sensor to the ESP8266 NodeMCU as follows:

  1. DHT11 VCC → ESP8266 3.3V
  2. DHT11 DATA → ESP8266 D5
  3. DHT11 GND → ESP8266 GND

If your DHT11 is a bare sensor rather than a breakout module, check its pinout and required pull-up resistor before connecting it.

B. Relay Module Connections

Connect the relay module as follows:

  1. Relay IN1 → ESP8266 D1
  2. Relay GND → ESP8266 GND
  3. Relay VCC → Suitable supply specified for the relay module

Use a relay module whose input is compatible with the ESP8266's 3.3V logic. Connect the controller ground and relay ground together when required by the module.

Only Relay Channel 1 is used.

C. 5V DC Fan Connections

Connect the fan through the relay contacts:

  1. 5V supply positive (+) → Relay COM
  2. Relay NO → Fan positive (+)
  3. Fan negative (−) → 5V supply negative (−)

COM means Common, while NO means Normally Open. With this arrangement, the fan normally remains OFF when the relay is not activated.

Important safety note: Do not power the fan directly from an ESP8266 GPIO pin. Use the correct power supply for the fan and relay module. This wiring is intended for a low-voltage 5V DC fan, not a household AC fan or mains-voltage circuit.

Install Arduino IDE and Required Libraries

We need the Arduino IDE to write and upload the program to the ESP8266 NodeMCU.

1. Install Arduino IDE

Download and install the Arduino IDE from the official Arduino website:

https://www.arduino.cc/en/software

2. Install ESP8266 Board Support

Open Arduino IDE and configure ESP8266 board support through Boards Manager.

Then select:

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

Select the correct COM port under Tools → Port after connecting the board to the computer.

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, using the installation method provided by the platform

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

Create the KiwisIoT Dashboard

Create or open your project dashboard on the KiwisIoT platform.

Configure two channels for this project.

Channel 0 – Temperature Monitoring

Use Channel 0 to display the temperature measured by the DHT11 sensor.

The ESP8266 sends the temperature value in degrees Celsius to this channel.

Example display:

  1. Temperature: 29.00 °C
  2. The displayed value will depend on the actual temperature around the sensor.

Channel 1 – Fan Control

Use Channel 1 as the fan-control input.

Configure a control that sends the appropriate values:

  1. ON = 1
  2. OFF = 0

When Channel 1 sends the value 1, the ESP8266 activates the relay. When it sends 0, the relay is deactivated.

Keep your KiwisIoT topic ID ready because it must be entered into the Arduino program.

Upload the Arduino Code

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

// Wi-Fi Settings
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 Sensor
#define DHT_PIN D5
#define DHT_TYPE DHT11

DHT dht(DHT_PIN, DHT_TYPE);

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

// Receive fan 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 = Fan Control
if (ch == "1") {
// Active LOW relay
if (val == "1") {
digitalWrite(RELAY1, LOW);
Serial.println("Relay / Fan: ON");
} else if (val == "0") {
digitalWrite(RELAY1, HIGH);
Serial.println("Relay / Fan: OFF");
}
}
}

// Send temperature to KiwisIoT
void sendTemperature() {
float temperature = dht.readTemperature();

// Check sensor reading
if (isnan(temperature)) {
Serial.println("Failed to read temperature from DHT11!");
return;
}

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

// Send temperature to Channel 0
String tempData = String(temperature, 2);
kiwisiot.send("0", tempData);

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

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

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

// Initialize relay and keep fan OFF
pinMode(RELAY1, OUTPUT);
digitalWrite(RELAY1, HIGH);

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

// Initialize DHT11
dht.begin();
Serial.println("DHT11 initialized");

// Initialize KiwisIoT
Serial.println("Connecting to KiwisIoT...");
kiwisiot.begin();

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

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

void loop() {
// Maintain KiwisIoT communication
kiwisiot.run();

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

if (millis() - lastDHT >= 2000) {
lastDHT = millis();
sendTemperature();
}

delay(100);
}

Code Explanation

1. Library declarations

The ESP8266 Wi-Fi, KiwisIoT, and DHT libraries are included at the beginning of the program.

2. Wi-Fi and dashboard settings

The Wi-Fi credentials allow the ESP8266 to connect to the network. The topic ID identifies the KiwisIoT dashboard or device topic used by the project.

3. Sensor initialization

The DHT11 data pin is configured as D5. The program reads the temperature using dht.readTemperature().

4. Temperature transmission

The sendTemperature() function reads the sensor, checks whether the reading is valid, and sends the value to Channel 0.

5. Fan control

The onControl() function checks incoming channel commands. Channel 1 controls the relay: the value 1 turns the fan ON, and the value 0 turns it OFF.

6. Main loop

The loop() function repeatedly runs KiwisIoT communication and sends a new temperature reading every two seconds.

Note: This program assumes an active-LOW relay module. Relay behavior varies between modules, so verify the ON/OFF logic with your specific relay before connecting the fan. The exact channel callback and dashboard configuration must also match your installed KiwisIoT library version.

Test the Smart Fan Control System

After uploading the program, follow these steps to test the project.

1. Check the Serial Monitor

Open Arduino IDE and select:

Tools → Serial Monitor

Set the baud rate to 115200.

Check whether the program starts and whether temperature readings appear.

Example output:

==============================
SMART FAN + DHT11
==============================
Relay initialized: OFF
DHT11 initialized
Connecting to KiwisIoT...
KiwisIoT initialized
Waiting for commands...
Temperature: 29.00 C
Sent to KiwisIoT Channel 0: 29.00

The values shown above are examples. Your output may differ depending on the sensor reading and library behavior.

2. Test Temperature Monitoring

Open the KiwisIoT dashboard and observe Channel 0.

The temperature displayed on the dashboard should correspond to the reading reported by the DHT11 sensor.

Allow a little time for the sensor to respond when the surrounding temperature changes.

3. Test Fan ON

Set Channel 1 to ON or send the value 1.

Expected behavior:

  1. The ESP8266 receives the control command.
  2. The relay activates.
  3. The 5V DC fan starts running.
  4. The Serial Monitor reports the received command.

Example:

Received from KiwisIoT -> Channel: 1 | Value: 1
Relay / Fan: ON

4. Test Fan OFF

Set Channel 1 to OFF or send the value 0.

Expected behavior:

  1. The ESP8266 receives the OFF command.
  2. The relay deactivates.
  3. The fan stops running.

Example:

Received from KiwisIoT -> Channel: 1 | Value: 0
Relay / Fan: OFF

5. Verify Both Functions

Confirm that Channel 0 displays temperature and Channel 1 controls the fan independently.

Take photographs of the assembled circuit, Arduino Serial Monitor, and KiwisIoT dashboard for your project documentation.

Troubleshooting

Problem 1: Temperature Is Not Displayed

Possible causes:

  1. Incorrect DHT11 wiring.
  2. Incorrect sensor data pin.
  3. Missing DHT library.
  4. Loose jumper wires.

Solution: Check the VCC, DATA, and GND connections. Confirm that the code uses D5 for the sensor and verify the sensor's power requirements.

Problem 2: ESP8266 Does Not Connect to Wi-Fi

Possible causes:

  1. Incorrect Wi-Fi name or password.
  2. Unsupported Wi-Fi configuration.
  3. Weak signal.

Solution: Check the credentials and ensure the network provides compatible 2.4 GHz Wi-Fi access for the ESP8266.

Problem 3: Temperature Is Not Appearing on KiwisIoT

Possible causes:

  1. Incorrect topic ID.
  2. Dashboard channel mismatch.
  3. Network connection problems.
  4. Incorrect KiwisIoT library setup.

Solution: Verify the topic ID and Channel 0 configuration. Confirm that the ESP8266 can communicate with the platform.

Problem 4: Fan Does Not Turn ON or OFF

Possible causes:

  1. Incorrect relay wiring.
  2. Incorrect relay input pin.
  3. Unsupported relay logic.
  4. Insufficient or unsuitable power supply.

Solution: Verify the D1 connection, relay supply, common ground requirements, and relay contact wiring. Test the relay without the fan first and confirm whether it is active-LOW or active-HIGH.

Problem 5: ESP8266 Restarts When the Fan Starts

Possible causes:

  1. Unstable power supply.
  2. Electrical noise from the fan or relay.
  3. Incorrect grounding or wiring.

Solution: Use a suitable, stable power supply for the fan and relay. Check the wiring and keep the fan's current path separate from the ESP8266's GPIO power path.

Final Result

The IoT-based Smart Fan Control System demonstrates how a temperature sensor, ESP8266 NodeMCU, relay module, and IoT dashboard can work together.

The DHT11 measures the surrounding temperature and sends the readings to KiwisIoT through the ESP8266. The user can then operate the fan remotely through the dashboard.

The project combines temperature monitoring with remote switching and provides a foundation for learning more advanced IoT automation.

Conclusion

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

The system demonstrates temperature monitoring, Wi-Fi communication, dashboard integration, and remote fan control. It is a practical beginner-level project for understanding how IoT hardware and software work together.

By extending this design with automatic control and additional monitoring features, it can serve as a foundation for more advanced IoT automation systems.