WIFI CONTROLLED FAN AND LIGHT

CIRCUIT

PROGRAM
//#include <EEPROM.h>
#define TRIAC1 9 
#define TRIAC2 A2 
#define zerocrossingpin A5 

unsigned short int ZERO,triggertime=20,fan=0;
char uart_data;//SERIAL COMMUNICATION
void setup() 
Serial.begin(115200);  delay(100);messagereadinit(); 
triacinit(); 
pinMode(zerocrossingpin,INPUT_PULLUP);
digitalWrite(TRIAC1, HIGH);delay(1000);digitalWrite(TRIAC1, LOW);
//triggertime=EEPROM.read(10);   
}

void loop() 
{
if(triggertime>0 && triggertime<9 && fan==1)
{  
waitfornextzerocrossing ();
delay(triggertime);    digitalWrite(TRIAC1, HIGH);
delayMicroseconds(200); digitalWrite(TRIAC1 , LOW);            
}
else if(triggertime==0 && fan==1){digitalWrite(TRIAC1, HIGH);wifi();wifi();wifi();wifi();}
else if(triggertime>10 || fan==1 )digitalWrite(TRIAC1, LOW); 
wifi();
if(triggertime<5){wifi();wifi();}


}
void triacinit(void)
{
pinMode(TRIAC1, OUTPUT);pinMode(TRIAC2, OUTPUT);
digitalWrite(TRIAC1 , LOW);digitalWrite(TRIAC2, LOW);  
}

void wifi(void)
{
if(Serial.available()>0)
{
uart_data=Serial.read();

if(uart_data==':')
while(!(Serial.available()>0));
uart_data=Serial.read();
if(uart_data=='+' && fan==1)
fancontrol();   
else if(uart_data=='A')
digitalWrite(TRIAC2, HIGH); // Serial.println("light ON"); 
}  
else if(uart_data=='a')
digitalWrite(TRIAC2, LOW);//   Serial.println("light OFF"); 
}  
else if(uart_data=='B')
fan=1;// Serial.println("FAN ON"); 
}  
else if(uart_data=='b')
fan=0;//  Serial.println("FAN OFF"); 
}  
}
}  
}

void waitfornextzerocrossing (void)
{
 do
   { 
     ZERO =digitalRead(zerocrossingpin); 
   }
   while(!ZERO); //wait until zero reaches--HIGH PULSE
}
void fancontrol(void)
{   
while(!(Serial.available()>0));//skips 1st zero
uart_data=Serial.read();
while(!(Serial.available()>0));//skips 2nd zero
uart_data=Serial.read();

while(!(Serial.available()>0));//msb of speed
uart_data=Serial.read();
triggertime=100*(uart_data-0x30);
while(!(Serial.available()>0));//middle byte of speed
uart_data=Serial.read();
triggertime=triggertime + 10*(uart_data-0x30);
while(!(Serial.available()>0));//lsb byte of speed
uart_data=Serial.read();
triggertime=triggertime + (uart_data-0x30);  

triggertime=triggertime*10/255;     

    if(triggertime==1)
    {
    triggertime=8;
  //  EEPROM.write(10,triggertime);
    }
    else if(triggertime==2)
    {
      triggertime=7;
    //  EEPROM.write(10,triggertime);
    }
    else if(triggertime==3 || triggertime==4)
    {
    triggertime=5;
    // EEPROM.write(10,triggertime);
    }
    else if(triggertime==5 || triggertime==6)
    {
     triggertime=4;
   // EEPROM.write(10,triggertime);
    }
    else if(triggertime==7 || triggertime==8)
    {
     triggertime=2;
     // EEPROM.write(10,triggertime);
    }
     else if(triggertime>8)
    {
     triggertime=0; 
     //EEPROM.write(10,triggertime);
    }
      else if(triggertime==0)
    {
     triggertime=30; 
     //EEPROM.write(10,triggertime);
    }
//Serial.print("triggertime=");Serial.println(triggertime);

    
    
}

void messagereadinit(void)
{
Serial.print("AT+CWMODE=3");//TEXT MODE
Serial.write(0xd);//ENTER
Serial.write(0xa);//NEW LINE
delay(500);
Serial.print("AT+CIPMUX=1");//TEXT MODE
Serial.write(0xd);//ENTER
Serial.write(0xa);//NEW LINE
delay(500);
Serial.print("AT+CIPSERVER=1,23");//TEXT MODE
Serial.write(0xd);//ENTER
Serial.write(0xa);//NEW LINE
delay(500);

}