Internet Switch using PHPoC Board and 4-Port Di/Do Board

Internet Switch using PHPoC Board and 4-Port Di/Do Board

Postby Roy » Tue Mar 14, 2017 6:16 pm

[Subject]

This project is an implementation of the Internet switch by connecting a 4-port relay output board (PES-2401) and a 4-port digital input board (PES-2402) to the PHPoC board type product.


[Video - PHPoC Blue]
phpBB [video]


[Video - PHPoC Black]
phpBB [video]



[Description - Input Part]

The PHPoC board monitors the swtich input through the 4 port digital input port (PES-2402), and when the switch input state is changed, 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 lamp using the connected 4 port relay output board (PES-2401).
At this time, on / off of the lamp is synchronized with the input ports of the opposite PHPoC board.


[Components - Input Part]

1. PHPoC Blue(P4S-342) or PHPoC Black(P4S-341) x 1
p4s342.jpg
p4s342.jpg (23.19 KiB) Viewed 589 times
or
p4s341.jpg
p4s341.jpg (20.91 KiB) Viewed 589 times


2. 4 Port Digital Input Board (PES-2402) x 1
pes2402.jpg
pes2402.jpg (18.75 KiB) Viewed 589 times


3. PHPoC Bread Board (PES-020-002) 1
pes020002.jpg
pes020002.jpg (18.37 KiB) Viewed 589 times


4. Push Buttons x 2 and Jumper Wires


[Components - Output Part]

1. PHPoC Blue(P4S-342) or PHPoC Black(P4S-341) x 1

2. 4 Port Relay Output Board (PES-2401) x 1
pes2401.jpg
pes2401.jpg (20.61 KiB) Viewed 589 times


3. A House Model x 2


[Connection - Input Part]
connection_input.jpg
connection_input.jpg (36.99 KiB) Viewed 589 times


[Connection - Output Part]
connection_output.jpg
connection_output.jpg (40 KiB) Viewed 510 times


[Souce Code - Input Part]
Code: Select all

<?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 = array(0, 0, 0, 0);
$in = array(0, 0, 0, 0);
$server_addr = "10.6.0.241";
$server_port = 5020;
 
function read_in
($sid, $pin)
{
 
    $response 
= spc_request($sid, 4, "get $pin input");
 
    $response_code 
= substr($response, 0, 3);
    if($response_code != "200")
        return -1;
 
    $response_data 
= substr($response, 4, 1);
    if($response_data == "1")
        return 1;
    elseif($response_data == "0")
        return 0;
    else
        return 
-1;
}
 
spc_request
($sid, 4, "set 0 delay 100");
 
echo 
"connecting...";
 
tcp_client
(0, $server_addr, $server_port);
while(
tcp_state(0) != TCP_CONNECTED)
    ;
 
echo 
"connected!\r\n";
 
while(1)
{
    $state_in = "";
    $event = false;
 
    for
($pin_id = 0; $pin_id < 4; $pin_id++)
    {
        if(($in[$pin_id] = read_in($sid, $pin_id)) != $last_in[$pin_id])
        {
            $last_in[$pin_id] = $in[$pin_id];
            $event = true;
        }
        usleep(5000);
    }
 
    if
($event)
    {
        for($pin_id = 0; $pin_id < 4; $pin_id++)
        {
            $state_in .= (string)$last_in[$pin_id];
        }
        echo $state_in, "\r\n";
        tcp_write(0, $state_in);
    }
}
 
?>



[Souce Code - Output Part]
Code: Select all

<?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;
$new_out = array(0, 0, 0, 0);
$local_port = 5020;
 
function get_out
($sid, $pin)
{
    $response = spc_request($sid, 4, "get $pin output");
 
    $response_code 
= substr($response, 0, 3);
    if($response_code != "200")
        return -1;
 
    $response_data 
= substr($response, 4, 1);
    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($sid, 4, "set $pin output $level");
}
 
echo 
"listen...";
 
tcp_server
(0, $local_port);
while(
tcp_state(0) != TCP_CONNECTED)
    ;
 
echo 
"connected!\r\n";
 
while(1)
{
    $state_in = "";
    if(tcp_read(0, $state_in) == 4)
    {
        for($pin_id = 0; $pin_id < 4; $pin_id++)
        {
            $new_out[$pin_id] = (int)substr($state_in, $pin_id, 1);
            set_out($sid, $pin_id, $new_out[$pin_id]);
            usleep(5000);
        }
        echo $state_in, "\r\n";
    }
    usleep(200000);
}
 
?>
Roy
 
Posts: 8
Joined: Fri Mar 14, 2014 10:49 am

Return to Project

Who is online

Users browsing this forum: No registered users and 1 guest

cron