Introduction
We are now going to look at the first step of building the controller system for our home automation system using the Raspberry Pi as the “brains”.
In order to host web applications, write data to our temperature database and host a method for serving the data stored in the DB via http we are going to need to install a number of tools.
For calculating the temperature and communicating with the thermostats we are going to use Java and setup a Java based web server. Following from this we will once again look at HTSQL and setup an HTSQL server for reading the temperature data from our database. We will also in later posts consider using Hadoop for a scalable parallel based controller capable of handling lots of data.
Having built the controller, we will then need to re-visit the post on Postgres and build out our database.
In part one of this post we will look at installing Java Apache Tomcat.
Setting up Apache Tomcat
Apache Tomcat provides us with a web server that can run Java based applications, distributed through WAR file.
Before we install the WAR file we should setup a new user on our Raspberry Pi. Tomcat will then run under this user, and we can lock down the user so it can only perform certain tasks.
To add a user run the following command from the RPi shell:
useradd controller passwd <password>
This will create a new user on the system. Under the home directory you will see a list of the user on your RPi. New directories can be added here using the mkdir command. You should create a home directory for your new user controller
mkdir controller
Ownership of the directory can be changed using the chown command, for example:
chown controller:users /home/controller
Now we have a user who will be used for running our controller code and servers under.
Our next step is to install Java on the Raspberry Pi.
Installing Java
We will start by installing the latest version of Java onto our Raspberry Pi. The first command we should run is to remove the existing version installed. Keeping your version of Java up-to-date is important as security patches are released that address underlying security flaws.
To remove your existing Java version run the following from the command line:
sudo apt-get purge openjdk*
Once this is complete we can run the following commands to install Java
sudo apt-get update sudo apt-get install openjdk-7-jdk
In the above command we have installed the Java Development Kit version 7. You should install the latest version, a list can be found at the following URL:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
Once you have installed Java run the clean up command:
sudo apt-get clean
You now have the latest version of Java running on your Raspberry Pi. If you need a basic guide to the language you can read IBM’s introduction guide:
http://www.ibm.com/developerworks/java/tutorials/j-introtojava1/index.html
Installing Tomcat
We now need to install the Tomcat web server onto the Raspberry Pi. You can read more on Tomcat at the Apache Tomcat website at the link below:
Let’s start by downloading the tar.gz for the web server.
From the command line on your RPi run the following command, make sure you are located in the controller directory under /home:
wget http://mirror.cogentco.com/pub/apache/tomcat/tomcat-7/v7.0.39/bin/apache-tomcat-7.0.39.tar.gz
As with installing Java you should replace the version number above with the latest version.
Once wget has finished running you should be find a .tar.gz file located in the controller directory.
We can untar/unzip the file as follows:
tar xzf apache-tomcat-7.0.39.tar.gz
Now change directory into the conf folder located in the new apache tomcat folder created when untar/unzipping
cd apache-tomcat-7.0.39/conf
We can now add our controller user to the configuration so they can start and stop the server.
Open the tomcat-users.xml and add the following:
<user username="controller" password="raspberry" roles="manager-gui"/>
Save the file. We can now test that Tomcat runs.
Navigate to the bin directory e.g.
cd ../bin
then run
sudo sh startup.sh
This will start your Tomcat server.
Tomcat by default runs on port 8080 of the localhost. In the next post we will take a look at the management console and explain how it works and look at further configuration of our Tomcat server.