Page 268 - Hacking Roomba
P. 268
Chapter 12 — Going Wireless with Wi-Fi 249
Writing a PHP Web Page
On a LAMP system, writing a dynamic web page with PHP is as simple as creating a text file
that ends with .php. A very simple PHP program is:
<html>
<?php
print “<h1> hello world </h1>”;
?>
</html>
Save this file as helloworld.php in the document root directory of your LAMP server. You can
then test the file by loading it to the corresponding URL in your browser.
For MAMP, the document root is /Applications/MAMP/htdocs and the URL is
http://localhost:8888/helloworld.php. For WAMP, the document root
is C:\WAMP\www and the URL is http://localhost/helloworld.php.
Listing 12-2 shows roombacmd.php, which is the initial work of a PHP web page to control a
network-connected Roomba. It is divided into four main parts:
Configuration: Setting $roomba_host and $roomba_port to point to your Roomba.
Roomba functions: A set of functions to replicate RoombaComm functionality.
HTML interface: The dynamic HTML that makes up the actual user interface.
Command action: Acting on the commands issued by the user through the user
interface.
Listing 12-2: PHP Roomba Control with roombacmd.php
<?php
// config: edit these two lines to point to your Roomba
$roomba_host = “192.168.0.90”;
$roomba_port = 10001;
$cmd = isset($_GET[‘cmd’]) ? $_GET[‘cmd’] : ‘’;
$vel = isset($_GET[‘vel’]) ? $_GET[‘vel’] : 200;
function roomba_stop() {
roomba_drive(0,0);
}
function roomba_go_forward($velocity) {
roomba_drive($velocity, 0x8000);
}
function roomba_go_backward($velocity) {
roomba_drive(-$velocity, 0x8000);
}
Continued