Cookbook – Building a Layar Augmented Reality Server on Raspberry PI

Raspberry_Pi_LogoPurpose: Augmented reality is an exciting new area of computing, with many ‘apps’ available. On this Cranfield University site, we will describe building a server for the popular ‘Layar’ app (http://www.layar.com/), builds on the earlier GeoThread project to Build a LAMP server on the Raspberry Pi computer. The Raspberry Pi is an amazing low-cost computer (see http://www.raspberrypi.org/). The Layar server uses PHP, MySQL and Apache for serving up Layar ‘POI’ (point of interest) objects as a JSON (http://www.json.org/) data stream from the MySQL database.

The mobile phone and tablet ‘layar’ app allows you to connect to an augmented reality server to look at points of interest etc in the world around you. But what does the data the app ‘consumes’ look like? In this tutorial we use the Raspberry Pi to serve up Layar data as a JSON data stream.

This assumes you have a Raspberry Pi, installed with Apache, MySQL, PHP and PHPMyAdmin, plus vsftpd as per the earlier instructions. Get filezilla and ssh terminal working (or use the tightvnc service).

These notes are really to support and extend the basic ‘Layar’ tutorial at:

http://www.layar.com/documentation/browser/tutorials-tools/create-simple-geo-location-layer/

Download the layar sample source code (held in a zip file) at:

http://static.layar.com/media/content_media/186/FirstTutorial_APIv6.zip

Unpack the files. Note that we had to undertake this process manually in File Explorer, due to the format of the zipfiles not being accepted by the Windows ‘zip extract’ tool.

There are several parts to the tutorial files that you receive – an SQL script to build and populate the database, and then a number of PHP scripts.

If you are likely to load and reload and reload the SQL script – inevitably one does, then consider editing the SQL script to add these lines above each of their respective database table definitions (thus allowing you to rebuild the tables as required).

DROP TABLE IF EXISTS `Layar`;
DROP TABLE IF EXISTS `POI`;

Building web services always leads to a few errors until you fix the problems. We find it helps to turning on Apache and MySQL logging on the Raspberry Pi until such inevitable snags are removed. To do this, edit the configuration files thus:

MySQL Logging

To turn on the MySQL logging, edit the MySQL configuration file thus:

cd /etc/mysql/my.cnf
sudo nano my.cnf

then, uncomment the lines:

#general_log_file = /var/log/mysql/mysql.log
#general_log = 1

Now restart MySQL and the logging operation is commenced.

sudo service mysql restart
tail –f /var/log/mysql/mysql.log

Reverse this to stop logging once all is working correctly.

Apache Logging

First, inspect the logfile:

sudo more /var/log/apache

or better still to see only new lines as they appear in (are being written to) the logfile

sudo tail –f /var/log/apache2

Establishing the database

The database is created and administered using the phpmyadmin tool. Create a new database called

layar_custom

Run the supplied Layar example SQL script (use the IMPORT button for the database in phpmyadmin) to create and populate the two database tables.

Create a new db user ‘layar’ (password ‘layar’ – or whatever you wish) and add privileges ‘select, insert, update, delete’ rights to the ‘layar_custom’ database on 127.0.0.1 (you can change the IP later for a public website address if you want to go that far).

Website

Create a new folder in the webserver root called ‘layar’ and copy over the php scripts – e.g. to ‘http://192.168.1.100/layar/’

Adjust file permissions for web access (chmod 755 for the folder, 554 for files, owner chown pi)

sudo chmod 775 /var/www/layar
sudo chmod 554 /var/www/layar/*

Next, you need to customise the ‘config.inc.php’ file to enable your specific database credentials. Edit the file and add in your details, thus:

define('DBHOST', '127.0.0.1');
define('DBDATA', 'layar_custom');
define('DBUSER', 'layar');
define('DBPASS', 'layar');

The files you uploaded, such as “192.168.1.100/layar/firstTutorial_simplified.php” have a call that loads in the configuration file above only once. You may find you need to disable this until the tool works. In which case, edit the require code thus:

//require_once(‘config.inc.php’);
require(‘config.inc.php’);

Changing from ‘require_once()’ to ‘require()’ means the code is read in every time you refresh the browser. Once all works, put it back as was.
Running the Application

From your home networked laptop/pc browser, run 192.168.1.100/layar/firstTutorial_simplified.php. You may well need to debug the whole configuration until all error messages are resolved – this is where using the apache and mysql logs can really help bugfix. If you encounter a ‘500’ error, this is likely to mean the MySQL user privileges are an issue. Also note the require_once() / require() issue noted above. Once it works, some ‘application programming interface’ API calls can be tried. Note the Layar name is ‘layername’ (as recorded in the MySQL table ‘Layar’).

Example Rest API calls

http://192.168.1.100/layar/firstTutorial_complete.php?lang=en&countryCode=NL&lon=4.9342500000&userId=6f85d06929d160a7c8a3cc1ab4b54b87db99f74b&developerId=4441&developerHash=26ec094e19db2c4a82ebafa200ea2a5e87a7d671&version=4.0&radius=2500&timestamp=1286357071952&lat=52.3741180000&layerName=layername&accuracy=100
http://192.168.1.100/layar/firstTutorial_simplified.php?countryCode=NL&lon=4.9342500000&userId=ed48067cda8e1b985dbb8ff3653a2da4fd490a37&radius=2500&lat=52.3741180000&layerName=layername&accuracy=100

This returns a stream of ‘JavaScript Object Notation’ JSON data, e.g.

{"layer":"layername","hotspots":[{"id":"geo_1","imageURL":"http:\/\/custom.layar.nl\/Layar_banner_icon.png","anchor":{"geolocation":{"lat":52.374118,"lon":4.93425}},"text":{"title":"The Layar Office","description":"The Location of the Layar Office","footnote":"powered by Layar"}}],"errorCode":0,"errorString":"ok"}

API Documentation

Request: see http://www.layar.com/documentation/browser/api/getpois-request/

Returns: see http://www.layar.com/documentation/browser/api/getpois-response/

Epilogue

Once this works and your setup is creating valid JSON data you can turn off all the debugging fixes. These instructions are just the start. Taking things forward, you would now need to place the server in the public domain and reveal (register) the service to your Layar account server for registered layars.