No edit summary |
|||
Line 33: | Line 33: | ||
== Quelltext == | == Quelltext == | ||
<pre> | |||
<syntaxhighlight lang="php"> | <syntaxhighlight lang="php"> | ||
<? | <? | ||
/*********************************************** | /*********************************************** | ||
Line 347: | Line 347: | ||
} | } | ||
?> | ?> | ||
</syntaxhighlight> | |||
</pre> | </pre> | ||
== Download == | == Download == | ||
[http://defcon-cc.dyndns.org/files/netio_wrapper.zip netio_wrapper.zip] | [http://defcon-cc.dyndns.org/files/netio_wrapper.zip netio_wrapper.zip] |
Revision as of 08:55, 14 March 2013
Pollin AVR-NetIO PHP Wrapper
Bei mir bestand irgendwann einmal das Bedürfnis mein Pollin AVR-NetIO Board über einen Webserver mit Befehlen zu füttern. Daraus entstanden einige PHP-Funktionen die eine Kommunikation mit dem NetIO-Board bewerkstelligen.
Benutzung
Die Benutzung gestaltet sich relativ einfach. Dazu einfach die
PHP-Datei "wrapper.php", mittels PHP-Befehl include("wrapper.php");
einbinden. Nachdem dann über die Funktion function wrapper_open($server, $port)
unter
angabe der IP-Adresse und des Ports, auf dem das NetIO-Board lauscht, eine Verbindung aufgebaut werden.
Als Rückgabewert der funktion erhält man einen Zeiger auf die aufgebaute Verbindung, diesen muss man
immer bei aufrufen einer Funktion mit angeben.
Die Verbindung kann dann mittels function wrapper_close($fp)
unter angabe des Zeigers
beendet werden.
Beispiel
Ein einfaches Beispiel, zum auslesen der Ausgänge. Dafür muss sich die Datei "wrapper.php" im selben Ordner wie die Beispiel PHP-Datei befinden.
<?
include("wrapper.php");
$fp = wrapper_open("192.168.0.90", 50290); // NetIO Standard IP & Port
$data = get_dig_output_status($fp);
echo $data;
?>
Quelltext
<syntaxhighlight lang="php"> <? /*********************************************** *** *** eeeeee eeeee eeeee eeeee eeeeee eeeeee *** 8 8 8 8 8 8 88 8 8 *** 8e 8 8 8 8 8 8 8 8 *** 88 8 8eee 8eee 8e 8 8 8e 8 *** 88 8 8e 8e 88 8 8 88 8 *** 88 e 88 88 88 8 e 88 8 *** 88eee8 88eee 88 88eee 88eee8 88 8 *** *** Coded by Crashdemon.... *** HAVE A LOT OF PHUN! *** *** AVR-NetIO Wrapper *** ver 0.1 *** ***********************************************/ $BUFFER_SIZE = 16384; // Establishes a connection to the Pollin AVR-NetIO // Parameters: $server -> IP of AVR-NetIO // $port -> Port running the AVR-NetIO // Return: The file description pointer of the established connection function wrapper_open($server, $port) { $errno = $errstr = ""; $fp = fsockopen($server, $port, $errno, $errstr, 30); if(!$fp) { printf("Error: %s (%s)", $errstr, $errno); $data = 0; } return($fp); } // Shows the current Info about NetIO // Parameters: $fp -> file description pointer of the established connection // Return: the Info as a String function get_version($fp) { fputs($fp, "VERSION\r\n"); $data = fread($fp, $BUFFER_SIZE); return($data); } // Status of given digital Pin // Parameters: $fp -> file description pointer of the established connection // $number -> number of the pin // Return: status auf the pin HIGH = 1 ; LOW = 0, false if fails function get_dig_input($fp, $number) { if($number > 0 and $number <=4) { fputs($fp, "GETPORT ".$number."\r\n"); $data = fread($fp, $BUFFER_SIZE); return($data); } else { return(false); } } // Value of the ADC Input Pin // Parameters: $fp -> file description pointer of the established connection // $number -> number of the pin // Return: decimal value from the AD-Conversion, false if fails function get_adc_input($fp, $number) { if($number > 0 and $number <=4) { fputs($fp, "GETADC ".$number."\r\n"); $data = fread($fp, $BUFFER_SIZE); //printf("%s", $data); return($data); } else { return(false); } } // Set output pin // Parameters: $fp -> file description pointer of the established connection // $number -> number of the pin // $set -> HIGH or LOW level set on output pin // Return: true if everthing went well, false if fails function set_dig_output($fp, $number, $set) { if($number > 0 and $number <=8 and ($set == 0 || $set == 1)) { fputs($fp, "SETPORT ".$number.".".$set."\r\n"); $data = fread($fp, $BUFFER_SIZE); if(strcmp($data, "ACK")) { return(true); } else { return(false); } } else { return(false); } } // Get the current status of the digital output // Parameters: $fp -> file description pointer of the established connection // Return: status auf the output pins HIGH = 1 ; LOW = 0 function get_dig_output_status($fp) { fputs($fp, "GETSTATUS\r\n"); $data = fread($fp, $BUFFER_SIZE); return($data); } // Get the IP of AVR NetIO // Parameters: $fp -> file description pointer of the established connection // Return: the IP as a string function get_ip($fp) { fputs($fp, "GETIP\r\n"); $data = fread($fp, $BUFFER_SIZE); return($data); } // Sets a new IP on AVR NetIO // Parameters: $fp -> file description pointer of the established connection // $new_ip -> The new IP as a string // Return: true if everthing went well, false if fails function set_ip($fp, $new_ip) { $ip = explode('.', $new_ip); if($ip[0] <= 255 and $ip[1] <= 255 and $ip[2] <= 255 and $ip[3] <= 255 and preg_match("!^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$!", $new_ip)) { fputs($fp, "SETIP ".$new_ip."\r\n"); $data = fread($fp, $BUFFER_SIZE); if(strcmp($data, "ACK")) { return($data); } else { return(false); } } else { return(false); } } // Gets the current Subnetmask of AVR NetIO // Parameters: $fp -> file description pointer of the established connection // Return: subnetmask as a string function get_mask($fp) { fputs($fp, "GETMASK\r\n"); $data = fread($fp, $BUFFER_SIZE); return($data); } // Sets new subnetmask on AVR NetIO // Parameters: $fp -> file description pointer of the established connection // $new_mask -> the new mask as a string // Return: true if everthing went well, false if fails function set_mask($fp, $new_mask) { $mask = explode('.', $new_mask); if($mask[0] <= 255 and $mask[1] <= 255 and $mask[2] <= 255 and $mask[3] <= 255 and preg_match("!^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$!", $new_mask)) { fputs($fp, "SETMASK ".$new_mask."\r\n"); $data = fread($fp, $BUFFER_SIZE); if(strcmp($data, "ACK")) { return($data); } else { return(false); } } } // Get the Gateway of AVR-NetIO // Parameters: $fp -> file description pointer of the established connection // Return: the gateway as a string function get_gateway($fp) { fputs($fp, "GETGW\r\n"); $data = fread($fp, $BUFFER_SIZE); return($data); } // Set the Gateway on AVR-NetIO // Parameters: $fp -> file description pointer of the established connection // $new_gw -> The new Gateway adress as a string // Return: true if everthing went well, false if fails function set_gateway($fp, $new_gw) { $gw = explode('.', $new_mask); if($gw[0] <= 255 and $gw[1] <= 255 and $gw[2] <= 255 and $gw[3] <= 255 and preg_match("!^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$!", $new_gw)) { fputs($fp, "SETGW ".$new_gw."\r\n"); $data = fread($fp, $BUFFER_SIZE); if(strcmp($data, "ACK")) { return($data); } else { return(false); } } } // Initializes the LCD, do this first before write to it // Parameters: $fp -> file description pointer of the established connection // Return: true if everthing went well, false if fails function init_lcd($fp) { fputs($fp, "INITLCD\r\n"); $data = fread($fp, $BUFFER_SIZE); if(strcmp($data, "ACK")) { return($data); } else { return(false); } } // Print text on LCD // Parameters: $fp -> file description pointer of the established connection // $row -> number of the row // $text -> the text which we would show // Return: true if everthing went well, false if fails function write_lcd($fp, $row, $text) { if($row > 0 and $row <=2) { fputs($fp, "WRITELCD ".$row.".".$text."\r\n"); $data = fread($fp, $BUFFER_SIZE); if(strcmp($data, "ACK")) { return($data); } else { return(false); } } } // Clear the given row on the LCD // Parameters: $fp -> file description pointer of the established connection // $row -> number of the row // Return: non function clear_lcd($fp, $row) { if($row > 0 and $row <=2) { fputs($fp, "CLEARLCD ".$row."\r\n"); } } // Closes the network connection to AVR-NetIO // Parameters: $fp -> file description pointer of the established connection // Return: non function wrapper_close($fp) { fclose($fp); } ?> </syntaxhighlight>