This part will describe the web server portion of the project. As I stated in a previous post, I am using bottle.py as my web serving agent.
Officially, according to http://bottlepy.org, bottle.py is known as a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library. WSGI stands for Web Server Gateway Interface. According to Wikipedia, it defines a simple and universal interface between web servers and web applications or frameworks for the Python programming language.
In other words, you don’t really need anything beyond Python to take advantage of publishing things to the web. Another thing I really like about it is that its footprint is extremely small: 47kB, yes, that’s kilo not mega.
Perhaps it isn’t quite robust enough to be able to handle hundreds of requests per second, but for my purposes, it is perfect.
The simplest way to get started is download and install bottle.py, create the smallest of Python programs:
Save it, call it whatever you like and run it. It shouldn’t do anything. Now open up a browser window and type ‘http://localhost:8080/hello/world’. That’s it. A running web application.
I did the above, got it working and then started expanding on the application. I created a method for retrieving the temperatures for the last half hour of logging. I also wrote a method that creates a graph of this data using gnuplot. Gnuplot is a somewhat quirky, but very powerful and fast graphing application. Did I mention it is free? Finally, the last method I wrote gathers some relevant system data such as CPU temperature, CPU utilization etc.
Then I created a template web page to host this data. This template contains place holders which will be replaced by the relevant data once a web request comes in. Nothing more than an old fashioned mail merge really.
This template is actually fairly advanced in that it uses jquery. Not wanting to overload my little Pi with download requests every time the page was requested, I decided to use Google’s library storage facility (ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js), which will easy the burden on the Pi. Same with the graphics: they are stored on Google Picasa, for lack of a better place.
Data retrieval is done using SQLite. I optimized the database by adding several indices, which makes it fairly snappy. Overall response time is about 2 seconds, from the time I issue the request (provided I have a fairly speedy internet connection, wherever I am in the world), to the time the screen is populated. Not bad.
Here’s a screen. Notice that this screen captured occurred shortly after a generator startup, hence the curve.
Here’s a shot of the overall web page:
Here’s the bottle code. In an earlier post, I stated that I added some LEDs as indicator lights to the Pi to indicate when it was running headless that a request from the web was coming in. Hence I added the library wiringpi to allow this to happen.