Uncategorized

ESP8266 WiFi Dimmer | Control the Speed of Fan using Smartphone

ESP8266 Wi-Fi AC Dimmer for controlling the speed of Fan

ESP8266 Wi-Fi AC Dimmer can be used to control the speed of Fan or the brightness of Bulb using any smart devices like Google Home, Amazon Alexa or your smartphone (Using App that works on a Local network made using MIT App Inventor or Over the Internet using Google’s Firebase.)

In this tutorial, you will learn to design and make a working prototype of Wi-Fi Dimmer using NodeMCU as the Wi-Fi Unit ( this is going to the Main MCU) along with the dimmer module designed using Triac on EasyEda ( an Online PCB Prototyping too ). And, we are going to use Arduino IDE for coding our NodeMCU.

Want to make a Bluetooth Controlled Dimmer? Know How

If, you are interested in making a Bluetooth based AC Dimmer than it can also be realized with the same dimmer module but you need to replace the NodeMCU with Arduino and Bluetooth Module (HC-05)

Components Required

  • NodeMCU
  • BTA12A ( Triac)
  • MOC3021 (Opto-Isolator Triac Driver IC )
  • MCT2E IC (Optocoupler)
  • MB1S ( Single Phase Bridge Rectifier)

Schematics:

 

 

Basic Code on – How 16 AC Dimming Levels works?

const int d3 =D3;
const int d2 =D2;
const int d1 =D1;
const int d0 =D0;
void setup() 
{
 pinMode(d3, OUTPUT);
 pinMode(d2, OUTPUT);
 pinMode(d1, OUTPUT);
 pinMode(d0, OUTPUT); 
}

void loop()
{
 digitalWrite(d3, LOW);  // In ON state -- 100%
 digitalWrite(d2, LOW);
 digitalWrite(d1, LOW);
 digitalWrite(d0, LOW);
 delay(1000);
 
 digitalWrite(d3, LOW);  // 86% Brightness
 digitalWrite(d2, LOW);
 digitalWrite(d1, LOW);
 digitalWrite(d0, HIGH);
 delay(1000);
 
 digitalWrite(d3, LOW);  //80% Brightness
 digitalWrite(d2, LOW);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, LOW);
 delay(1000);
 
 digitalWrite(d3, LOW);  //75% Brightness
 digitalWrite(d2, LOW);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, HIGH);
 delay(1000);

 digitalWrite(d3, LOW);  //70% Brightness
 digitalWrite(d2, HIGH);
 digitalWrite(d1, LOW);
 digitalWrite(d0, LOW);
 delay(1000);
 
 digitalWrite(d3, LOW); //65% Brightness
 digitalWrite(d2, HIGH);
 digitalWrite(d1, LOW);
 digitalWrite(d0, HIGH);
 delay(1000);

 digitalWrite(d3,LOW);   //60% Brightness
 digitalWrite(d2, HIGH);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, LOW);
 delay(1000);
 
 digitalWrite(d3, LOW);   //50% Brightness
 digitalWrite(d2, HIGH);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, HIGH);
 delay(1000);
 
 digitalWrite(d3, HIGH);  //40% Brightness
 digitalWrite(d2, LOW);
 digitalWrite(d1, LOW);
 digitalWrite(d0, LOW);
 delay(1000);
  
 digitalWrite(d3, HIGH);  //30% Brightness
 digitalWrite(d2, LOW);
 digitalWrite(d1, LOW);
 digitalWrite(d0, HIGH);
 delay(1000);
 
 digitalWrite(d3, HIGH);  //25% Brightness
 digitalWrite(d2, LOW);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, LOW);
 delay(1000);
 
 digitalWrite(d3, HIGH);  //20% Brightness     
 digitalWrite(d2, LOW);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, HIGH);
 delay(1000);
 
 digitalWrite(d3, HIGH);  //15% Brightness
 digitalWrite(d2, HIGH);
 digitalWrite(d1, LOW);
 digitalWrite(d0, LOW);
 delay(1000);
 
 digitalWrite(d3, HIGH);  //10% Brightness
 digitalWrite(d2, HIGH);
 digitalWrite(d1, LOW); 
 digitalWrite(d0, HIGH);
 delay(1000);
 
 digitalWrite(d3, HIGH);  //5% Brightness
 digitalWrite(d2, HIGH);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, LOW);
 delay(1000);
 
 digitalWrite(d3, HIGH);    // In Off State
 digitalWrite(d2, HIGH);
 digitalWrite(d1, HIGH);
 digitalWrite(d0, HIGH);
 delay(1000);
 }

 

Final Code :

//Visit www.roboshala.com for more details on NodeMCU and other projects.
// This code is Written by Alok Shaw(alokom78@gmail.com)) and you are free to modify this code with giving proper credits to the owner.

#include <ESP8266WiFi.h>
 
const char* ssid = "USE ME"; // SSID i.e. Service Set Identifier is the name of your WIFI i.e. your wifi netowrks's Name
const char* password = "123443211234"; // Your Wifi password, in case you have open network comment the whole statement.
 
int d3=D3; // Initializing Data Pins of Dimmer Module
int d2=D2;
int d1=D1;
int d0=D0;
WiFiServer server(80); // Creates a server that listens for incoming connections on the specified port, here in this case port is 80.

int plus=0; // Flag for current level of Brightness 

 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  pinMode(d3, OUTPUT);  // Setting the Data pins of NodeMCU in Output Mode
  pinMode(d2, OUTPUT);
  pinMode(d1, OUTPUT);
  pinMode(d0, OUTPUT);
  
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address
  Serial.print("Use this URL to connect: ");
  Serial.print("http://");
  Serial.print(WiFi.localIP()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor
  Serial.println("/");
 
}
 
void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) 
  {
    return;
  }
 
  // Wait until the client sends some data
  Serial.println("new client");
  while(!client.available())
  {
    delay(1);
  }
 
  // Read the first line of the request
  String request = client.readStringUntil('\r');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
 
  if (request.indexOf("/Off") != -1)  // Turning OFF the Light
  {
    digitalWrite(d3,HIGH);
    digitalWrite(d2,HIGH);
    digitalWrite(d1,HIGH);
    digitalWrite(d0,HIGH);
    plus=0;
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Bulb is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/On") != -1)  // Turning ON the light
  {
    digitalWrite(d3,LOW);
    digitalWrite(d2,LOW);
    digitalWrite(d1,LOW);
    digitalWrite(d0,LOW);
    plus=5;
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Bulb is On 100%");
      client.println("</html>");
      client.stop();
      delay(1);
  }


  if (request.indexOf("/SpeedUp") != -1)  // This Loop increment the value of Plus and also the Brightness.
  {
    plus++;

    if( plus>=5)
    {
      plus=5;
       
    }
Serial.println(plus);

    if ( plus==5)                  
    {
      digitalWrite(d3,LOW);
      digitalWrite(d2,HIGH);
      digitalWrite(d1,LOW);
      digitalWrite(d0,LOW);
    }
    else if (plus==4)
    {
      digitalWrite(d3,LOW);
      digitalWrite(d2,HIGH);
      digitalWrite(d1,HIGH);
      digitalWrite(d0,LOW);
    }

    else if (plus==3)
    {
      digitalWrite(d3,HIGH);
      digitalWrite(d2,LOW);
      digitalWrite(d1,LOW);
      digitalWrite(d0,LOW);
    }

    else if (plus==2)
    {
      digitalWrite(d3,HIGH);
      digitalWrite(d2,LOW);
      digitalWrite(d1,HIGH);
      digitalWrite(d0,LOW);
    }

     else if (plus==1)
    {
      digitalWrite(d3,HIGH);
      digitalWrite(d2,HIGH);
      digitalWrite(d1,LOW);
      digitalWrite(d0,LOW);
    }

    
    

    
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Increasing Brightness");
      client.println("</html>");
      client.stop();
      delay(1);

  }
  
if (request.indexOf("/SpeedDown") != -1)  // This Loop decrement the value of Plus and also the Brightness.
  {
    plus--;

    if( plus<=1)
    {
      plus=1;
    }
    Serial.println(plus);

    if ( plus==5)
    {
      digitalWrite(d3,LOW);
      digitalWrite(d2,HIGH);
      digitalWrite(d1,LOW);
      digitalWrite(d0,LOW);
    }
    else if (plus==4)
    {
      digitalWrite(d3,LOW);
      digitalWrite(d2,HIGH);
      digitalWrite(d1,HIGH);
      digitalWrite(d0,LOW);
    }

    else if (plus==3)
    {
      digitalWrite(d3,HIGH);
      digitalWrite(d2,LOW);
      digitalWrite(d1,LOW);
      digitalWrite(d0,LOW);
    }

    else if (plus==2)
    {
      digitalWrite(d3,HIGH);
      digitalWrite(d2,LOW);
      digitalWrite(d1,HIGH);
      digitalWrite(d0,LOW);
    }

     else if (plus==1)
    {
      digitalWrite(d3,HIGH);
      digitalWrite(d2,HIGH);
      digitalWrite(d1,LOW);
      digitalWrite(d0,LOW);
    }


    

    
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Decreasing Brightness");
      client.println("</html>");
      client.stop();
      delay(1);

  }

  

}
 

 

 

MIT App Inventor Project file : https://goo.gl/iPbTVi

 

 

Show More

Leave a Reply

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

Back to top button
Close