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- Channel 0 β Displays the temperature
- 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 FlowTemperature Monitoring:
DHT11 β ESP8266 β Wi-Fi β KiwisIoT
Fan Control:
KiwisIoT β ESP8266 β Relay β 5V FanStep 2: Circuit Connections
The following connections are used to connect the DHT11 sensor, relay module, and 5V fan to the ESP8266 NodeMCU.
DHT11 Temperature SensorConnect the DHT11 sensor to the ESP8266 as follows:
DHT11 VCC β ESP8266 3.3V
DHT11 DATA β ESP8266 D5
DHT11 GND β ESP8266 GND
Relay ModuleConnect 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 FanThe 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 (β)
ImportantDo 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.
Step 3: Installing Arduino IDE and LibrariesBefore programming the ESP8266 NodeMCU, we need to install the Arduino IDE and the required libraries.
1. Install Arduino IDEDownload and install the Arduino IDE on your computer.
2. Install ESP8266 Board SupportOpen 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 LibrariesOpen:
Sketch β Include Library β Manage Libraries
Search for and install:
- DHT sensor library
- Adafruit Unified Sensor
- KiwisIoT Library
These libraries are required to read the DHT11 sensor and communicate with the KiwisIoT platform.
Step 4: Arduino CodeThe 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);
}
Step 5: KiwisIoT DashboardKiwisIoT 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.
Step 6: Testing the ProjectAfter 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.
Step 7: Final ResultThe 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.






_JnuTDtq8iW.jpeg?auto=compress%2Cformat&w=48&h=48&fit=fill&bg=ffffff)







Comments