[Subject]

This project is an implementation of controlling 16 street lights by a PHPoC board with 4 relay output boards(pes-2401).
Note that maximum 14 smart expansion boards are available on one PHPoC board.

This project is designed based on the project "an implementation of the Internet switch by connecting a 4-port relay output."
* https://forum.phpoc.com/blogs/roy/33...rt-di-do-board


[video]




[Description - Input Part]

The PHPoC board monitors the swtich input through the 4 port digital input port (PES-2402), and when the switch is pressed, it passes the state to the opposite PHPoC board via TCP/IP.



[Description - Output Part]

The PHPoC board which receives the status turns on / off the lights using the connected four 4-port relay output board (PES-2401) in 4 steps.
The 4 steps are as follows:
- step 1: turn all lights on
- step 2: turn half of them off
- step 3: invert the result of step 2
- step 4: turn all lights off



[Components - Input Part]

1. PHPoC Blue(P4S-342) x 1
Click image for larger version

Name:	p4s342.jpg
Views:	8
Size:	21.4 KB
ID:	200

2. 4-Port Digital Input Board (PES-2402) x 1
Click image for larger version

Name:	pes2402.jpg
Views:	3
Size:	17.5 KB
ID:	201

3. PHPoC Bread Board (PES-020-002) 1
Click image for larger version

Name:	pes020002.jpg
Views:	2
Size:	17.1 KB
ID:	202

4. Push Buttons x 1 and Jumper Wires


[Components - Output Part]

1. PHPoC Blue(P4S-342) x 1

2. 4-Port Relay Output Board (PES-2401) x 4
Click image for larger version

Name:	pes2401.jpg
Views:	4
Size:	19.2 KB
ID:	203

3. street lights x 16


[Connection - Input Part]
Click image for larger version

Name:	connection_in.jpg
Views:	2
Size:	28.0 KB
ID:	204


[Connection - Output Part]
Click image for larger version

Name:	connection_out.jpg
Views:	2
Size:	129.9 KB
ID:	205


[Souce Code - Input Part]

PHP Code:
<?php

include_once "/lib/sd_spc.php";
include_once 
"/lib/sn_tcp_ac.php";

spc_reset();
spc_sync_baud(115200);

$sid 1;
$pin_id 0;
$last_in 0;
$in 0;
$server_addr "10.6.0.241";
$server_port 5020;
$control_int 0;

function 
read_in($sid$pin)
{

    
$response spc_request($sid4"get $pin input");

    
$response_code substr($response03);
    if(
$response_code != "200")
        return -
1;

    
$response_data substr($response41);
    if(
$response_data == "1")
        return 
1;
    elseif(
$response_data == "0")
        return 
0;
    else
        return -
1;
}

spc_request($sid4"set 0 delay 100");

sleep(2);

echo 
"connecting...";

tcp_client(0$server_addr$server_port);
while(
tcp_state(0) != TCP_CONNECTED)
    ;

echo 
"connected!\r\n";

while(
1)
{
    
//$control = "";
    
$event false;

    if((
$in read_in($sid$pin_id)) != $last_in)
    {
        
$last_in $in;
        if(
$in != 0)
            
$event true;
    }

    if(
$event)
    {
        
$control_int++;
        if(
$control_int 3)
            
$control_int -= 4;

        
$control = (string)$control_int;
        echo 
$control"\r\n";
        
tcp_write(0$control);
    }
}

?>
[/code]


[Souce Code - Output Part]

[code=php]
<?php

include_once "/lib/sd_spc.php";
include_once 
"/lib/sn_tcp_ac.php";
include_once 
"/lib/sd_340.php";

define("TURN_OFF",    0x0000);
define("TURN_ON",     0xffff);
define("TURN_HALF_A"0x5555);
define("TURN_HALF_B"0xaaaa);

//define("MODE_MANUAL",     0x00);
//define("MODE_TIME",       0x00);
//define("MODE_BRIGHTNESS", 0x00);

spc_reset();
spc_sync_baud(115200);

$sid 1;
$pin_id 0;
$local_port 5020;
$control 0;

function 
get_out($sid$pin)
{
    
$response spc_request($sid4"get $pin output");

    
$response_code substr($response03);
    if(
$response_code != "200")
        return -
1;

    
$response_data substr($response41);
    if(
$response_data == "1")
        return 
1;
    elseif(
$response_data == "0")
        return 
0;
    else
        return -
1;
}

function 
set_out($sid$pin$sig)
{
    if(
$sig == 1)
        
$level "high";
    elseif(
$sig == 0)
        
$level "low";
    else
        exit(
"undefined state \r\n");
    
spc_request($sid4"set $pin output $level");
}

sleep(2);

echo 
"listen...";

tcp_server(0$local_port);
while(
tcp_state(0) != TCP_CONNECTED)
    ;

echo 
"connected\r\n";

while(
1)
{
    
$rbuf "";
    if(
tcp_read(0$rbuf) > 0)
    {
        
$control = (int)$rbuf;
        echo 
"control changed to $rbuf ($control) \r\n";
        switch(
$control)
        {
            case 
0:
                
$output TURN_OFF;
                break;
            case 
1:
                
$output TURN_ON;
                break;
            case 
2:
                
$output TURN_HALF_A;
                break;
            case 
3:
                
$output TURN_HALF_B;
                break;
            default:
                echo 
"out of range\r\n";
        }

        for(
$i 0$i 16$i++)
        {
            
$sid = ($i 4) + 1;
            
$pin $i 4;
            
$sig = ($output>> $i) & 1;
            
set_out($sid$pin$sig);
            
//usleep(10000);
        
}
    }
    else
        ;
    
usleep(10000);
}

?>