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]
[Video - PHPoC Black]
[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
2. 4 Port Digital Input Board (PES-2402) x 1
3. PHPoC Bread Board (PES-020-002) 1
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
3. A House Model x 2
[Connection - Input Part]
[Connection - Output Part]
[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 = 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]
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;
$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);
}
?>