This is the code
The relay module that I used is active low
All inputs pins are pull up manually by using 4.7k resistor
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Follow us: http://www.fb.com/blynkapp
http://twitter.com/blynk_app
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
This example runs directly on ESP8266 chip.
Note: This requires ESP8266 support package:
https://github.com/esp8266/Arduino
Please be sure to select the right ESP8266 module
in the Tools -> Board menu!
Change WiFi ssid, pass, and Blynk auth token to run :)
Feel free to apply it to any other example. It's simple!
*************************************************************/
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "13123123";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*******";
char pass[] = "*******";
#define speed_1_out D1 //Drive the relay one which is controlling the speed level one of the Fan
#define speed_2_out D2 //Drive the relay two which is controlling the speed level 2 of the Fan
#define speed_3_out D4 //Drive the relay three which is controlling the speed level 3 of the Fan
#define mainSwitch D3 //Drive the relay four which is turning of power to the fan
#define off_switch D5 //Read the momentary wsitch for off the fan
#define speed_1_switch D6 //Read the momentary wsitch for speed 1
#define speed_2_switch D7 //Read the momentary wsitch for speed 2
#define speed_3_switch D8 //Read the momentary wsitch for speed 3
unsigned char offSwitch; //veriable to store status read from the " off_switch "
unsigned char speed_1_switch_read; //veriable to store status read from the " speed_1_switch "
unsigned char speed_2_switch_read; //veriable to store status read from the " speed_2_switch "
unsigned char speed_3_switch_read; //veriable to store status read from the " speed_3_switch "
bool x = 1;
long offTime;
int previous = LOW; // the previous reading from the input pin
long debounce = 200; // the debounce time, increase if the output flickers
long Time = 0; // the last time the output pin was toggled//
long const OnOff_Duration = 5000;
long OnOff_Duration_offset;
void setup() {
Blynk.begin(auth, ssid, pass);
// Serial.begin(115200);
pinMode(speed_1_out , OUTPUT);
pinMode(speed_2_out , OUTPUT);
pinMode(speed_3_out , OUTPUT);
pinMode(mainSwitch , OUTPUT);
pinMode(off_switch , INPUT);
pinMode(speed_1_switch , INPUT);
pinMode(speed_2_switch , INPUT);
pinMode(speed_3_switch , INPUT);
digitalWrite(speed_1_switch, HIGH);
digitalWrite(speed_2_switch, HIGH);
digitalWrite(speed_3_switch, HIGH);
digitalWrite(speed_1_out, HIGH);
digitalWrite(speed_2_out , HIGH);
digitalWrite(speed_3_out, HIGH);
digitalWrite(mainSwitch, HIGH);
//digitalWrite(mainSwitch, HIGH);
OnOff_Duration_offset = OnOff_Duration;
}
void loop() {
if ((millis() - offTime) > OnOff_Duration_offset)//150000
{
offTime = millis();
digitalWrite(mainSwitch, x = !x);
if (digitalRead(speed_1_out) == HIGH && digitalRead(speed_2_out) == HIGH && digitalRead(speed_3_out) == HIGH ) {
digitalWrite(mainSwitch, HIGH);
}
}
speed_1_switch_read = digitalRead(speed_1_switch);
if (speed_1_switch_read == HIGH && previous == LOW && millis() - Time > debounce) {
digitalWrite(speed_1_out, LOW);
digitalWrite(speed_2_out, HIGH);
digitalWrite(speed_3_out, HIGH);
digitalWrite(mainSwitch, LOW);
offTime = millis();
Time = millis();
x = 1;
}
speed_2_switch_read = digitalRead(speed_2_switch);
if (speed_2_switch_read == HIGH && previous == LOW && millis() - Time > debounce) {
digitalWrite(speed_1_out, HIGH);
digitalWrite(speed_2_out, LOW);
digitalWrite(speed_3_out, HIGH);
digitalWrite(mainSwitch, LOW);
offTime = millis();
Time = millis();
x = 1;
}
speed_3_switch_read = digitalRead(speed_3_switch);
if (speed_3_switch_read == HIGH && previous == LOW && millis() - Time > debounce) {
digitalWrite(speed_1_out, HIGH);
digitalWrite(speed_2_out, HIGH);
digitalWrite(speed_3_out, LOW);
digitalWrite(mainSwitch, LOW);
offTime = millis();
Time = millis();
x = 1;
}
offSwitch = digitalRead(off_switch);
if (offSwitch == HIGH && previous == LOW && millis() - Time > debounce) {
digitalWrite(speed_1_out, HIGH);
digitalWrite(speed_2_out, HIGH);
digitalWrite(speed_3_out, HIGH);
digitalWrite(mainSwitch, HIGH);
Time = millis();
}
Blynk.run();
}
BLYNK_WRITE(V1) { // Widget for interval timing
//Serial.println(param.asInt());
switch (param.asInt())//param.asInt()
{
case 1: { // Item 1
digitalWrite(speed_1_out, HIGH);
digitalWrite(speed_2_out, HIGH);
digitalWrite(speed_3_out, HIGH);
digitalWrite(mainSwitch, HIGH);
break;
}
case 2: { // Item 2
digitalWrite(speed_1_out, LOW);
digitalWrite(speed_2_out, HIGH);
digitalWrite(speed_3_out, HIGH);
digitalWrite(mainSwitch, LOW);
offTime = millis();
x = 1;
break;
}
case 3: { // Item 3
digitalWrite(speed_1_out, HIGH);
digitalWrite(speed_2_out, LOW);
digitalWrite(speed_3_out, HIGH);
digitalWrite(mainSwitch, LOW);
offTime = millis();
x = 1;
break;
}
case 4: { // Item 3
digitalWrite(speed_1_out, HIGH);
digitalWrite(speed_2_out, HIGH);
digitalWrite(speed_3_out, LOW);
digitalWrite(mainSwitch, LOW);
offTime = millis();
x = 1;
break;
}
}
}
BLYNK_WRITE(V4) {
int value = param.asInt(); // This gets the 'value' of the Widget as an integer
OnOff_Duration_offset = OnOff_Duration * value;
// Serial.println(OnOff_Duration_offset);
}
following code are used to turn OFF and ON the fan as per the value set to the " OnOff_Duration_offset " veritable . This is used to for the purpose of power saving . the value of variable called " OnOff_Duration_offset " control by the slider module
if ((millis() - offTime) > OnOff_Duration_offset)//150000
{
offTime = millis();
digitalWrite(mainSwitch, x = !x);
if (digitalRead(speed_1_out) == HIGH && digitalRead(speed_2_out) == HIGH && digitalRead(speed_3_out) == HIGH ) {
digitalWrite(mainSwitch, HIGH);
}
}
I am using Node MCU ESP8266
Without connecting motor wires everything is working Perfect, Once the motor wires connected to the relay and once AC powered it operate in unpredictably