control the speed of the ac fan by pic with PWM

Hello
I would like to know how to control the speed of the ac fan by pic with PWM
I used optoisolator to protect the pic
but the circuit does not work well
is the optoisolator doing his duty ?
is this circut working properly ?
please help me
 

Picture of control the speed of the ac fan by pic with PWM