Monitoring electrical devices ( power line interface) with arduino

Hello there !
I am working on controlling devices with arduino/esp8166 from web page

An example by using arduino pin 2 to switch ON the relay through web page, in the web page it shows the relay ON but in real world it is not, so I found this link in which the relay out put is further
connected with the opto-coupler circuit. When the opto-coupler gives 5 V output, I must get the
message relay ON and when 0V relay is OFF.

Can it be done with only one pin or I have to use separate pin for the opto-coupler also ?
What logic I have to use to make this working ?

That link shows a circuit which acts as an input. It will let you know if there is 240V at the input pins on the left.

How is the relay wired? Do you have a relay shield? Note that most relay boards available for the Arduino are not safe to be connected to mains power, even if it says "250V" on the relays on the board.

Store the relay state in a file on the server so that when it changes its updated and then the web page can check this file or state and always show its real or current state.