Login
Register
Forum
Kontakt
Bitmania
2013-02-23 19:50
by bitman
Eine WebGUI für das Relais am PortC des Ethersex AVR-NET-IO
AVR-NET-IO

Ich hab mal etwas gecodet, rausgekommen ist dabei ist eine WebGUI für das AVR-NET-IO-Relais, welches ich neulich gebastelt habe.

Die PHP-Seite funktioniert für sich alleine, kann aber auch in andere Seiten eingebettet werden.







Angesprochen werden kann sie über HTTP-GET oder POST.
Das PHP-Script benötigt einen Webserver, die GUI verbindet sich dann mit dem AVR-NET-IO per Ip session und sendet die entsprechenden Commands und stellt das Ergebnis dar.

Damit kann ich nun meinen Backup-Server ein- und ausschalten und habe somit auch eine Rückmeldung über den Status.







netio.php:

<?php
$ts_start=time();
$scriptname = $_SERVER["SCRIPT_NAME"] ;
$host = '192.168.0.91';
$port = '2701';
$col_0 = '#FFB043'; // background color if bit is zero
$col_1 = '#B5C2FD'; // background color if bit is one

//echo "[$ts_start]<br>";

//echo "<pre>";print_r($_POST);echo "</pre>";
if($_GET){
$cmd = $_GET["cmd"];
}
if($_POST){
$cmd = $_POST["cmd"];
}


function send($fp, $cmd){
$tmp='';
fputs($fp, $cmd."
");
while ($line = fgets($fp)) {
//echo $line;
$tmp = $tmp . $line;
}
return($tmp);
}



// Open a socket
if (!($fp = fsockopen($host, $port, $errno, $errstr, 15))) die("Could not connect to host");

// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");

// communicate with avr-net-io running ethersex
if($fp){
// Command reference:
// http://old.ethersex.de/index.php/Ecmd_Reference

// Check DIR Register of Port C and initialize if necessary
$tmp = send($fp, "io get ddr 2");
if( hexdec(substr($tmp,10,2))==00 ){
// Set PortC high
$tmp = send($fp, "io set port 2 ff");
// Set entire PortC as output
$tmp = send($fp, "io set ddr 2 ff");
}

// decide on which command to react
if( strpos($cmd,"portc_get" ) !== false ){
// pass
}
if( strpos($cmd,"portc_clr_all" ) !== false ){
send($fp, "io set port 2 00");
}
if( strpos($cmd,"portc_set_all" ) !== false ){
send($fp, "io set port 2 ff");
}
if( strpos($cmd,"portc_set_bit" ) !== false ){
$bit = pow(2, (substr($cmd, 13, 1)) );
send($fp, "io set port 2 ff ".dechex($bit));
}
if( strpos($cmd,"portc_clr_bit" ) !== false ){
$bit = pow(2, (substr($cmd, 13, 1)) );
send($fp, "io set port 2 00 ".dechex($bit));
}

// get latest status of portc
$tmp = send($fp, "io get port 2");
$portc = hexdec(substr($tmp, 10, 12)); // port 2: 0x81
if($portc & 1) { $portc0=1; }else{ $portc0=0; };
if($portc & 2) { $portc1=1; }else{ $portc1=0; };
if($portc & 4) { $portc2=1; }else{ $portc2=0; };
if($portc & 8) { $portc3=1; }else{ $portc3=0; };
if($portc & 16) { $portc4=1; }else{ $portc4=0; };
if($portc & 32) { $portc5=1; }else{ $portc5=0; };
if($portc & 64) { $portc6=1; }else{ $portc6=0; };
if($portc & 128) { $portc7=1; }else{ $portc7=0; };
fclose($fp);
}



//------------------------------------------------------------------------------
// Show the GUI
echo "<table border='0' bgcolor='#CECECE'>";

echo "<tr>";
echo " <td colspan='11'>PORTC";
echo "</tr>";

echo "<tr>";
echo " <td align='center'>7";
echo " <td align='center'>6";
echo " <td align='center'>5";
echo " <td align='center'>4";
echo " <td align='center'>3";
echo " <td align='center'>2";
echo " <td align='center'>1";
echo " <td align='center'>0";
if($cmd){
echo " <td align='center' colspan='3'><font size=2 face=Arial,Helvetica color=000000>Command was:<br>$cmd</font>";
}
echo "</tr>";

echo "<tr>";
if($portc7==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc7";
if($portc6==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc6";
if($portc5==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc5";
if($portc4==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc4";
if($portc3==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc3";
if($portc2==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc2";
if($portc1==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc1";
if($portc0==1) { $col=$col_1;}else{$col=$col_0;}
echo " <td align='center' bgcolor='$col'>$portc0";
echo " <td align='center'>0x".dechex($portc);
echo " <td align='center'>$portc";
echo "</tr>";

echo "<tr>";
if($portc7==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit7'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit7'></form>";
}
if($portc6==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit6'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit6'></form>";
}
if($portc5==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit5'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit5'></form>";
}
if($portc4==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit4'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit4'></form>";
}
if($portc3==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit3'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit3'></form>";
}
if($portc2==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit2'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit2'></form>";
}
if($portc1==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit1'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit1'></form>";
}
if($portc0==1){
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr'><input type='hidden' name='cmd' value='portc_clr_bit0'></form>";
}else{
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set'><input type='hidden' name='cmd' value='portc_set_bit0'></form>";
}
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='clr *'><input type='hidden' name='cmd' value='portc_clr_all' ></form>";
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='set *'><input type='hidden' name='cmd' value='portc_set_all' ></form>";
echo "<td align='center'><form style='display:inline;' method='post' action='$scriptname'><input type='submit' name='' value='get'><input type='hidden' name='cmd' value='portc_get' ></form>";
echo "</tr>";

echo "<tr>";
if($portc7==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit7' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit7' style=''>set</a>";
}
if($portc6==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit6' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit6' style=''>set</a>";
}
if($portc5==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit5' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit5' style=''>set</a>";
}
if($portc4==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit4' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit4' style=''>set</a>";
}
if($portc3==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit3' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit3' style=''>set</a>";
}
if($portc2==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit2' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit2' style=''>set</a>";
}
if($portc1==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit1' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit1' style=''>set</a>";
}
if($portc0==1){
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_bit0' style=''>clr</a>";
}else{
echo " <td align='center'><a href='$scriptname?cmd=portc_set_bit0' style=''>set</a>";
}
echo " <td align='center'><a href='$scriptname?cmd=portc_clr_all' style=''>clr*</a>";
echo " <td align='center'><a href='$scriptname?cmd=portc_set_all' style=''>set*</a>";
echo " <td align='center'><a href='$scriptname?cmd=portc_get' style=''>get</a>";
echo "</tr>";
echo "</table>";

echo "<br>";
$ts_end=time();
$duration = $ts_end-$ts_start;
//echo "[$ts_end]<br>";
echo "$duration secs<br>";

?>