Now that we have a programmable robotic platform, built with a Raspberry Pi, a motor controller and a chassis, we’d like to control it remotely, from a PC and later maybe from a mobile device. Let’s try to achieve this over HTTP.
Requirements and wrap-up
First of all we need to have our stuff in order. The robotic platform is up and running with a fixed wireless IP address:
- A robotic platform with a Raspberry Pi and a motor controller.
- A wireless interface with a static IP address.
- Streaming video to our PC.
- Optionally: an obstacle detector.
Since we are going to use the Pi as a webserver, we’ll set up an FTP server. Of course we can create and edit files in situ as well, but FTP is a convenient way to get them into place from wherever we edit them.
sudo apt-get update sudo apt-get upgrade sudo apt-get install vsftpd
In my configuration I have the following lines uncommented:
listen=YES anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES use_localtime=YES xferlog_enable=YES connect_from_port_20=YES secure_chroot_dir=/var/run/vsftpd/empty pam_service_name=vsftpd rsa_cert_file=/etc/ssl/private/vsftpd.pem force_dot_files=YES
This will take the FTP user to his home directory. Restart the FTP daemon after a configuration change:
sudo service vsftpd restart
We’ll use the Apache web server here.
sudo apt-get install apache2
The web root is set to /var/www . We want the FTP user to have access to this path, for example ‘pi’ in group ‘pi’:
sudo chown pi:pi /var/www
Create a link in the home directory of this user (e.g. /home/pi):
ln -s /var/www www
So the FTP user can cd to www to reach the web root.
sudo apt-get install php5 libapache2-mod-php5
We want the server to access the motor controller over a serial interface, so add user www-data to group dialout:
useradd -G dialout www-data
Now we can easily upload scripts to the www root. Here’s an example of a PHP run configuration in NetBeans IDE:
The are several approaches to control the Pi Rover from a client application. Here we choose to move the command interpretation logic to a PHP script, whereas we pass the command and data bytes to the motor controller via a Python Script. Later we’ll write a client script in Python that sends a POST request to this PHP script.
The Python send script takes one or to command line arguments (command and data) and passes them to the controller over the serial interface.
(Note that I use Python 2.x here. I had some issues, like unacceptably long serial response times, with Python 3.x. I haven’t found to cause of this yet.)
The PHP script takes a POST request and sends the appropriate command and data to the motor controller.
We’ll use a Python script with Tkinter to control the Pi Rover from a PC. A slider sets the overall value for speed (more like engine thrust, actually). The UI will look like this:
We use Requests to make sending the request as easy as it gets.
- In Python 3 use ‘tkinter’ instead of ‘Tkinter’.
- Tweak numerical values to get optimal results for your case.
You should now be able to navigate the Pi Rover around within your Wi-Fi range.
When sending and receiving more data than just simple control commands you may want to consider implementing a REST API. I use PHP REST API by Srinivas Tamada as a starting point in serveral projects, like the Hangman mobile app, the Hangman web app, the AptiMob distributed enterprise solution and of course this Pi Rover robot.
2 thoughts on “Controlling the Pi Rover over HTTP”
Nice Post Thanks for Sharing
my name is jay, We have best technozu blog if you want to more information about tech please visit us.
Comments are closed.