Controlling a blind with PHP sockets

Controlling a blind with php sockets

One of my school projects was to control the blind of an enterprise using only a webpage and an ethernet connected relay board.
I had to use the ETH-IO32B board from EKO électronique. Which is way too expensive in my opinion.
Anyway, the webpage had to be hosted on a Raspberry Pi, this website contains some buttons to control the blind.

Prepare the Raspberry Pi.

My first goal was to install LAMP (Apache MySQL PHP bundle for Linux) on the Raspberry Pi, which permitted me to transform my Raspberry Pi into a webserver, and make a webpage available on the LAN.

Then I had to figure out how the ETH-IO board work. Hopefully I already had its local IP address.

To control the board I could have used the web interface created by the ETH-IO board (which is available by navigating to the boards IP address on port 9999, or 9998). But I am only using two relays out of the eight available.
In order to do that I had to use UDP sockets with PHP.

To create a socket I used the socket_create() function.
This function needs three parameters, which are :

  • domain
  • type
  • protocol
    And I used AF_INET for IPv4
    SOCK_DGRAM to support datagrams (for UDP)
    and udp as the protocol.

Once the socket was created, I had to send it using socket_sendto(). This function contains six parameters :

  • Socket created with socket_create()
  • Data
    • To activate the first relay, I had to send SwitchDIGOUT0 to the ETH-IO board. And SwitchDIGOUT1 to activate the second one.
  • Data Length in bytes (using simply strlen())
  • Flags ( 0 )
  • IPv4 address (192.168.1.211)
  • Port number (9998)

Once the socket was sent, it need to be closed using socket_close().

This is the PHP function I created to control the blinds.

function send_to_ethio($relay) {
    $socket = socket_create(AF_INET , SOCK_DGRAM , SOL_UDP);
        if($socket < 0)
        {
            echo 'Erreur: cannot create socket.';
            exit(1);
        }
    $dest_addr = '192.168.1.211';
    $dest_port = '9998'; 
    $donnees = $relay;
    $data_lengh = strlen($donnees); 
    $envoi = socket_sendto($socket, $donnees, $data_lengh, 0, $dest_addr, $dest_port);
    socket_close($socket);
    }

I only had to add some HTML and CSS for the buttons, and then it looked really nice (no):

Control center

We have one button to change (switch) the position of the blind, and another one to stop (and restart) it whenever we want.

I also had to create a socket to receive some information from the ETH-IO board. But I had not enough time, and I don’t have this board anymore…

If you want an idea of what the entire system looked like, here is a simple schematic :
System schematic
Of course you can have my entire source code

Here you have my HTML :

<!DOCTYPE html>
<html>
    <head>
        <title>ETHIO</title>
        <meta charset="UTF-8"/>
    </head>
    
    <body style="background-color: #2f2f2f; color:whitesmoke">
        <header>
        <h1>Control center</h1>


        </header>
        <section class="scrollable">

        <form method="post">
            <h3>Change position: <br><input class="bouton" type="submit" value="Change" name="swi_btn"></h3>
            <h3>Start/Stop position : <br><input class="bouton" type="submit" value="Start/Stop" name="arr_btn"></h3>
        </form>

        </section>
    </body>
</html>

And there is my PHP :

<?php

if(isset($_POST['swi_btn']))
{
    send_to_ethio('SwitchDIGOUT1');
}

if(isset($_POST['arr_btn']))
{
    send_to_ethio('SwitchDIGOUT0');
}

function send_to_ethio($relay) {
    $socket = socket_create(AF_INET , SOCK_DGRAM , SOL_UDP);
        if($socket < 0)
        {
            echo 'Erreur: cannot create socket.';
            exit(1);
        }
    $dest_addr = '192.168.1.211';
    $dest_port = '9998'; 
    $data = $relay;
    $data_lengh = strlen($data); 
    $envoi = socket_sendto($socket, $data, $data_lengh, 0, $dest_addr, $dest_port);
    socket_close($socket);
    }
        
?>

I hope this will help you if, one day, you have to use the ETH-IO32B relay board.

ETH-IO32B user manual - http://wifipower.fr/fr/index.php?controller=attachment&id_attachment=20

Official website - https://www.eko-fpga.com/