🔧 Project Overview
In this tutorial, we’ll build a Wi-Fi-based fan controller using the NodeMCU (ESP8266) and Sinric Pro, allowing you to control the fan via your smartphone or Alexa — from anywhere!
🧰 Required Components
Component | Quantity | Notes |
---|---|---|
NodeMCU ESP8266 | 1 | Wi-Fi microcontroller |
1-Channel Relay Module | 1 | Active HIGH relay |
Fan / Light / Appliance | 1 | Up to 250V AC |
Jumper Wires | As needed | Male-to-female for relay |
USB Cable (Micro USB) | 1 | For powering NodeMCU |
Optional: Breadboard | 1 | For easy wiring |
🔌 Wiring Diagram
Here’s how to connect the components:
Relay Module NodeMCU (ESP8266)
=========== ===================
VCC 3V3
GND GND
IN D1 (GPIO5)
AC Fan Relay
====== ======
Live (L) --------> COM
Neutral (N) -----> Direct to Fan
NO (Normally Open) -> Fan's Live wire
Note: The onboard LED of NodeMCU is on GPIO2 and is also controlled to reflect the fan’s status.
📟 Code – ESP8266 + Sinric Pro
Replace the
ssid
,password
,APP_KEY
,APP_SECRET
, andDEVICE_ID
with your actual Sinric Pro values.
#include <ESP8266WiFi.h>
#include <SinricPro.h>
#include <SinricProSwitch.h>
// WiFi credentials
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
// Sinric Pro credentials
#define APP_KEY "Your_SinricPro_App_Key"
#define APP_SECRET "Your_SinricPro_App_Secret"
#define DEVICE_ID "Your_Device_ID"
// Pin Definitions
#define RELAY_PIN 5 // GPIO5 (D1)
#define LED_BUILTIN 2 // Onboard LED (GPIO2 - active LOW)
// Callback to handle state changes
bool onPowerState(const String &deviceId, bool &state) {
digitalWrite(RELAY_PIN, state ? HIGH : LOW); // Relay is active LOW
digitalWrite(LED_BUILTIN, state ? LOW : HIGH); // LED ON when fan ON
Serial.printf("Fan turned %s\n", state ? "ON" : "OFF");
return true;
}
// WiFi setup
void setupWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println(" connected!");
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Relay OFF initially (active LOW)
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // LED OFF initially
setupWiFi();
SinricProSwitch &fanSwitch = SinricPro[DEVICE_ID];
fanSwitch.onPowerState(onPowerState);
SinricPro.onConnected([]() { Serial.println("Connected to Sinric Pro!"); });
SinricPro.onDisconnected([]() { Serial.println("Disconnected from Sinric Pro."); });
SinricPro.begin(APP_KEY, APP_SECRET);
}
void loop() {
static unsigned long lastWiFiCheck = 0;
if (WiFi.status() != WL_CONNECTED && millis() - lastWiFiCheck > 5000) {
Serial.println("WiFi disconnected! Reconnecting...");
setupWiFi();
lastWiFiCheck = millis();
}
SinricPro.handle();
if (!SinricPro.isConnected()) {
Serial.println("Reconnecting to Sinric Pro...");
SinricPro.begin(APP_KEY, APP_SECRET);
}
}
📱 Control from Mobile or Alexa
- Download the Sinric Pro app from Play Store or App Store.
- Add a Switch device and copy the Device ID.
- Upload this code to your NodeMCU.
- You can now:
- Toggle the fan from the app
- Use Alexa voice commands like: “Alexa, turn on the fan.”
🔐 Safety Tip
If you’re switching AC appliances, make sure:
- You use proper insulation.
- Never touch the relay’s AC terminals when powered on.
- Consider using a fuse or protective box.
bro so hard