Introduction: Raspberry Pi Temperature Logger Via Radio
This project makes use of a raspberry pi, to read the temperature of several radio sensors, and store them on a Sqlite database.
The same raspberry pi is used to serve webpages with the graph of the acquired temperatures.
See live demo here.
Step 1: Install Hardware on the Pi
The first thing to do is to install the radio receiver (Ciseco Slice of Radio) on the Raspberry Pi.
See the Photos.
Step 2: Confirm That You Have the Pi Configured Correctly to Free the Serial Port
Next, confirm that you have the Pi configured correctly to free the serial port, so that your monitoring software will be abble to use it. To do so, follow the instructions on Ciseco website here.
You only have to follow the instructions on "Access to the serial port". "Installing and running Minicom" and "Setting up for remote access (SSH)" are optional, and the latter, you probably have already done by this time.
Minicom is a usefull terminal program that allows you to see the radio messages being received by the Pi, and allows you to send messages - usefull for debuging
Step 3: Install Monitoring Software on the Pi
First you need to create the Database to store the temperature readings.
You will be using an Sqlite database, so you have first to install Sqlite:
a) ssh to your Pi. Example:> ssh firstname.lastname@example.org
b) Install sqlite> apt-get install sqlite3
c) Create the database. You can place it in your home folder, or on any other place where you have write access. I used /var/www. I provide a sql database definition schema.sql here.
Press the "Raw button" to download it; copy it to the Pi; and use it to create the database, like this:
> cd /var/www
> sqlite3 -init schema.sql templog.db
Check that two tables have been created, named sensors and temps:
You should see:
Now exit sqlite:
You should have a file named templog.db with your database, in the directory where you issued the sqlite command.
Step 4: Install Monitoring Software on the Pi (continued)
Now you have to install the monitoring script written in Python (called monitor.py), that you can find here.
Copy it to your home folder on the Pi.
You can edit the line dbname='/var/www/templog.db' near the top of the file to adapt it to the place where you created your database.
You can also adapt the name of the serial port to use - normally not necessary - by changing the line
DEVICE = '/dev/ttyAMA0'.
And you can change the time interval between readings of the Weather Underground website, by changing the line SAMPLE = 10*60. Time to use here is in seconds, so the default period is 10 minutes.
Step 5: Install Monitoring Software on the Pi (continued)
Now, you need to install the Ciseco Sensors that you will be using, and you have to register yourself on Weather Underground site, to get a key to be able to access their API.
You do not need to register yourself on Weather Underground, unless you are interested in storing temperature readings from meteorological stations that exist in your region.
Register is free, but you can only query their website 500 times per day, and 10 times per minute... Should be enough...Register and you get a key (a 16 character hexadecimal sequence) that will be used later. After you have registered with Weather Undergroud, you can add locations of places that you want to periodically acquire the temperature, to our database.
You do that by adding a record to the sensors table of you database:
sqlite> insert into sensors(name,id,baudrate,porta,active) ... values ("Porto (WU)","WA", "pws:IPORTOPO4", "yourAPIkeyhere", 1);
Replace yourAPIkeyhere, by your Weather Undergound key.Verify that the record has been correctly inserted:
sqlite> select * from sensors;
And exit sqlite:
Replace the "pws:IPORTOPO4" by the code of the weather station that you want to acquire. The example above acquires the temperature in Oporto city in Portugal.
You have to find the code of the Weather Station that is nearer to you. Start here.
Weather underground stations should always have an id of two letter, starting with W. Like "WA" on the example above. You can add several Weather Underground stations to the database, by repeating the steps above.
If you want to alter one of the locations that you inserted into the database you can type on a sqlite3 prompt, for example:
sqlite> UPDATE sensors SET active=0 where (id='WA');
The above command would disable the periodical acquisition of temperatures from the location with id WA on the database (the city of Oporto).
Step 6: Install Monitoring Software on the Pi (continued)
You are now ready to start your monitor script. You can either run it only once, to see if worked ok, or make it run forever in the background.
To run it once, just type ./monitor.py.
I am assuming that you have Python installed on your Pi, which is true by default on most installations. Type which python to check the path to your python interpreter, if it exists. If you need to install it, Google about how to do it.
monitoy.py should be executable. If it isn't, type chmod +x monitor.py.
To stop monitor.py from executing, press ctrl+C.
You should have acquired once the temperature of whichever locations you programmed in your database (Oporto, in the example above).
To check it, run sqlite, and query the temps table:
> sqlite3 templog.db
sqlite> select * from temps;
If you did not see a line with the timestamp, id and temperature reading, please check for a file named debug.txt on the same location of monitor.py, and edit it to see if you got any error.
When everything is ok, you can now put monitor.py to run forever in the background:
This will put the monitor.py process running as a task in the background. To check if it is running, type ps -aux. If you want to stop it, type kill 14581, replacing 14581 by the task number that you got for your case, after having checked for it with ps -aux.
Step 7: Install Apache Webserver
Ssh to your Pi, and type:
> sudo apt-get install apache2 php5 libapache2-mod-php5
If you get an error, type:
> sudo groupadd www-data
> sudo usermod -g www-data www-data
Now to restart the server:sudo service apache2 restart.
To see if it installed correctly, on a web browser, type the ip address of your Pi (example 192.168.1.87), and you should see a page saying "It works!".
Now edit the file /etc/apache2/sites-available/default.
Inside the file you should have a "<VirtualHost>" directive.
Inside that directive write:
WSGIDaemonProcess temperature user=www-data group=www-data threads=5
WSGIScriptAlias /temperature /var/www/html/temperature/temperature.wsgi
Allow from all
This tells apache to serve the pages found in /var/www/html/temperature/.
Create that folder, and copy there all the files and folders that you find here.
Press the button "Download zip" on the bottom right, to download all the files, and copy them to the folder /var/www/html/temperature, after unzipping them.
You have to edit the file temperature.wsgi if you have placed your files in a folder other than /var/www/html/temperature.
The file should look like:
from index import app as application
And you should alter the line sys.path.insert ... to suit your needs.
If you have any problems, this instructions come from here. Try to see if that link helps.
Step 8: Configure the Program Settings
Edit the file settings_pi.cfg with a text editor:
DATABASE = "/var/www/templog.db"
SUBFOLDER = "/temperature"
SERVER_NAME = "lourenco.no-ip.biz"
ONDISK = "/var/www/html"
DATABASE should point to the location where you stored your database;
SUBFOLDER is the folder name inside /var/www/html where you placed your files (usually /temperature, and you shouldn't need to change this).
SERVER_NAME is the location where your server is accessible. This can either be a local ip address (e.g. 192.168.1.87) or an external dns (like lourenco.no-ip.biz).
ONDISK is the root folder from where apache serves it's files (usually /var/www/html).
After editing this file, copy it over settings.cfg.
Restart apache: service apache2 restart
Go to a web browser, and type the address of your Pi.
You should see your Pi serving the Graph of the acquired temperatures!
We have a be nice policy.
Please be positive and constructive.