Control a Fan Using Sinric Pro and NodeMCU (ESP8266)

Control a Fan Using Sinric Pro and NodeMCU (ESP8266)

🔧 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

ComponentQuantityNotes
NodeMCU ESP82661Wi-Fi microcontroller
1-Channel Relay Module1Active HIGH relay
Fan / Light / Appliance1Up to 250V AC
Jumper WiresAs neededMale-to-female for relay
USB Cable (Micro USB)1For powering NodeMCU
Optional: Breadboard1For 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, and DEVICE_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

  1. Download the Sinric Pro app from Play Store or App Store.
  2. Add a Switch device and copy the Device ID.
  3. Upload this code to your NodeMCU.
  4. 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.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *