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 (, 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 The Layar server uses PHP, MySQL and Apache for serving up Layar ‘POI’ (point of interest) objects as a JSON ( 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:

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

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).


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


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 (you can change the IP later for a public website address if you want to go that far).


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

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 ‘’ file to enable your specific database credentials. Edit the file and add in your details, thus:

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

The files you uploaded, such as “” 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:


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 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

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

{"layer":"layername","hotspots":[{"id":"geo_1","imageURL":"http:\/\/\/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

Returns: see


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.