Webserver and database combination on Raspberry Pi

My normal combination on the big-server side would be Apache + MySQL (or PostgreSQL), but on the RPi this seems to be absolute overkill. For data-logging operations I would not use the local system anyway (looking at MQTT as well as Remote MongoDB datastore via REST Webservices).

After some poking around and reading up on the options I decided to go for the following combo: LightHTTPD + SQLite. Both are lightweight replacement of their fully-featured big-server counterparts (Apache HTTP & MySQL) and have very familiar configurations. There would be other options that have even less resource usage, but I really don’t have the time to start from scratch somewhere.

Another reason to go for this combination is that these are very well supported systems with regular security audits. Even though I am not planning to use my RPi’s for anything mission-critical this is always worth a consideration as you don’t need to unnecessarily introduce vulnerabilities to your network.

Install & configure LightHTTPD

sudo apt-get install lighttpd php5 php5-cgi php5-sqlite
sudo lighty-enable-mod fastcgi
sudo lighty-enable-mod fastcgi-php

Further config changes can be also made via the config file.
sudo vim /etc/lighttpd/lighttpd.conf
sudo service lighttpd force-reload

Install & configure SQLite

sudo apt-get install sqlite3
sqlite3 /home/username/database_name.db

All other commands are standard SQL from the ‘sqlite>’ command prompt or via SQL scripts like
sqlite3 /home/username/database_name.db < sql_script.sql

Access Databases from the webserver (using PHP)

< ?php $db = new SQLite3('mysqlitedb.db'); $results = $db->query('SELECT bar FROM foo');
while ($row = $results->fetchArray()) {
var_dump($row);
}
?>

See PHP: SQLite3::query – Manual for more details.

Accessing 1-wire devices on Raspberry Pi using OWFS

To connect 1-wire serial devices to the RPi I am using a DS9490R USB 1-wire adapter (rather than wiring I2C 1-Wire master components to GPIO I2C – which I might look at sometime down the track)

Install packages

sudo apt-get install owfs ow-shell

Edit config file

vim /etc/owfs.conf

! server: server = localhost:4304
# USB device: DS9490
server: usb = all
######################### OWFS ##########################
mountpoint = /mnt/1wire
allow_other
####################### OWHTTPD #########################
http: port = 2121
####################### OWFTPD ##########################
ftp: port = 2120
####################### OWSERVER ########################
server: port = localhost:4304

Create Startup Script

I created a startup script for owfs modelled on the owserver script (not sure why this one is actually missing)

vim /etc/init.d/owfs

#!/bin/sh
### BEGIN INIT INFO
# Provides: owfs
# Required-Start: $remote_fs $syslog $network $named
# Required-Stop: $remote_fs $syslog $network $named
# Should-Start: owfs
# Should-Stop: owfs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: 1-wire file system mount & update daemon
# Description: Start and stop 1-wire file system mount & update daemon.
### END INIT INFO
CONFFILE=/etc/owfs.conf
DESC="1-Wire file system mount"
NAME="owfs"
DAEMON=/usr/bin/$NAME
case "$1" in
start)
echo "Starting $NAME"
$DAEMON -c $CONFFILE
;;
stop)
echo "Stopping $NAME"
killall $NAME
;;
*)
echo "Usage: $N {start|stop}" >&2
exit 1
;;
esac
exit 0

Starting daemons

/etc/init.d/owserver start
/etc/init.d/owfs start

Checking output

#ls /mnt/1wire/

Should show output similar to:

10.575349000000 12.95DD17000000 alarm simultaneous uncached
10.575349000000 12.95DD17000000 bus.0 statistics
12.57DD16000000 81.B2EA2E000000 bus.1 structure
12.57DD16000000 81.B2EA2E000000 settings system

cat /mnt/1wire/10.575349000000/temperature

Will then show the temperature of the particular DS18S20 temperature sensor.

Mobile sensors and the “Internet of Things” in learning

With the Internet of Things slowly becoming mainstream the potential uses of this technology can also be seen in the Education sector. This blogpost is the first installment of a series of posts that highlights practical examples that can be used in teaching and training.

Part 1 – Environmental Noise Monitoring

WideNoise by leeander, on Flickr
Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 Generic License  by  leeander 

Noise pollution has been a serious problem in many large cities all over the world and with the help of common mobile devices (smartphones) this can be easily measured, monitored and compared with a large quantity of samples from other cities/regions.

Some of the skills taught in these projects are:

  • Environmental science
  • Citizen science (collaborative data gathering)
  • Measurement / sensing
  • Data visualisation
  • Data comparison
Here are two very useful pieces of software to undertake this type of project:

WideNoise

With WideNoise users can monitor the noise levels around them using an App downloadable from Android Market or Apple AppStore. It has geo-location capabilities allowing users to also check the online map to see the average sound level of the area around them.

The project has made it’s source code available via an Open Source license allowing further customisation.

http://www.widetag.com/widenoise/

NoiseTube

A project developed by Sony Computer Science Laboratory Paris & VUB BrusSense group allows a user to measure the level of noise in dB(A) (with a precision a bit lower than a sound level meter), and contribute to collective noise mapping effort by annotating it (tagging, e.g. subjective level of annoyance). This information can be automatically published on this website (3G/GPRS or manual upload on any PC).

http://www.noisetube.net/

These two projects provide an excellent starting point for educators to become involved on the ‘Internet of Things. A copy of this can be seen on my work blog as well.