web analytics

Raspberry Pi Application Example

This is an example project for use with the servo widgets. It’s more or less complete but may have some rough edges. It does require that all the pieces are in place and installed to work correctly, your mileage may vary as the saying goes. There is a bit of tweaking involved but it does work quite well once you get everything set up.

All of this was designed for a specific application- move servos via wireless to control turnouts and animatronics for large scale garden railroads. I wanted to have the user interface be a standard tablet device running a web browser with my Raspberry Pi as the Web Server.

I’ve since abandoned the Wixel for this, not because it doesn’t work but because the range of the Wixel is not suitable for my application and I really need a point to multi-point instead of a master slave sort of setup. I’ve since switched to a custom board using an Atmel processor and the Xbee series 1 modules, but that’s another project entirely, so I digress.

Anyhow, here is a block diagram

system

First is the tablet. Mine is a knock off Android tablet running Firefox. Note that the default browser on Android does NOT support HTML5 so you will need to install Firefox or Chrome if you don’t already have it. I have no idea if Ipads or Iphones work but since this is just an application running on a web browser, as long as HTML5 is supported, it should be fine.

The next thing is the wifi router. This is an off the shelf box, plugged into the rest of my network. I’ve configured it so it doesn’t do DHCP but that’s all, everything else is stock out of the box. Here is the basic hookup I use, a very minimalistic network:

Image2

The Raspberry Pi needs to be configured as a web server and support PHP for this application to work. You can also put MySQL on it and turn it into a miniature LAMP server (that’s what I did) if you are so inclined but that isn’t needed in this example.

I run my RPi remotely, without a keyboard or monitor using Putty and SSH. This lets me have the RPi just sit on the network like any other server and I can log in remotely to install things and do admin. Here is a good tutorial about setting this up:

Remote Login to Raspberry Pi

Ignore the link for the IP scanner tool, HERE IS A BETTER ONE

Another really nice feature to have when developing and installing things with the RPi is Samba. Samba allows you to setup your RPi so you see it in ‘Network’ on your Windows PC. You can also adjust samba so the RPi folders appear letting you directly edit Python on the Pi from your Windows Desktop. Very Handy. Here is a tutorial for doing that:

Samba File Server for Raspberry Pi

You will also need a web server on your Pi. Apache seemed to be a bit overkill really, the Pi is not a speed demon after all, so I went with lighttpd. An excellent tutorial for installing and configuring it (and PHP too) can be found at this link.

Setup and install lighttpd and PHP

Once you have these installed, you will need to install Tornado. This is a web socket implementation that works on the RPi. A websocket lets the tablet (client) send commands back to the host. The user interface application uses it to send touch commands to the web server (the RPi). These touch commands are then converted to messages to the master Wixel and then out over the airwaves to each slave. For the RPi, I downloaded the source (it’s python) and installed it, you may try the other ways listed on their site but the source install worked for me.

Tornado Web Server

Here are some useful links for getting Tornado working and understanding what is going on in the code samples:

Web Sockets Cookbook

This next link offers some advice on the Javascript side of things. The User interface application that runs in the tablet browser is written in Javascript using the HTML5 canvas.

More Web Sockets Info

Ok, after all of that install and stuff, here is a screen shot of the latest incarnation of the User Interface. This is based on a tiles game approach but I basically reworked the entire thing to do what I wanted. For me, the tiles are pieces of track and turnouts, but as you can see from this set of tiles, you can make them be anything you want. It’s a very simple user interface, click on one of the icons on the pallet, then click on a spot on the main canvas and the icon will be deposited there. You can right click to remove an icon. To set a servo to a particular square, click the wixel button at the top and then click the square you want to assign it to. Enter the slave address followed but the servo number separated by a comma. Click the pic below, there is a minimal working copy online you can play with:

interface

My mode of operation involves building the screen using my PC, then saving it. I then load and run the saved screen on the Tablet. I couldn’t figure out the new interface for loading and saving files via HTML5 (which I personally find confusing) so I used a bit of PHP to do that. Hence the need to have PHP running on your web server. There are lots of options for expanding this and adding more features, I’ll leave that to others for now.

You can download a zip file with the HTML5 javascript, graphics and other assorted stuff HERE