This tutorial is an extension to Matthew Kirks tutorial on setting up and logging the data captured from a one-wire digital temperature sensor.
Click here for a working demonstration direct from my Pi.
Click here to download the code.
The information here assumes that you have followed the guidelines of the tutorial up to and including Step 3(A). I will not explain the set-up process as Matthews’s tutorial is clear and easy to understand.
Here is a re-cap on the materials needed:
- Raspberry Pi
- Debian Wheezy Operating System
- AdaFruit Ribon Cable Connector (Optional. I did not use this)
- A One Wire Temperature Sensor
- A 4.7k resistor, a bread board and some wires
- Fritzing diagram of the setup?
So, lets get a script going… This first one will read the sensor information and put it into a text (.txt) file in an understandable format.
I then ftp the file to my website where I can process it and display a graph of the results. But more on that later.
Here is the code for temp.py:
#!/usr/bin/python -O import time # Create a unique timestamp for inclusion in the log... timestamp = time.strftime("%d-%m-%Y %H:%M:%S") # append the filename with the date (this will start a new log daily)... logname = time.strftime("%d-%m-%Y") filename = "".join(["temp_log_", logname, ".txt"]) # If already created, this will open the current log file, if not it will create a new one... datafile = open(filename, "a") # open the sensor log file as seen in the tutuorial (replacing 28-000003f1cede with the number of your sensor)... tfile = open("/sys/bus/w1/devices/28-000003f1cede/w1_slave") # read the data and close the file... text = tfile.read() tfile.close() # Split the data and extract the temperature value... temperature_data = text.split()[-1] temperature = float(temperature_data[2:]) temperature = temperature / 1000 # write our newly formatted date, time and sensor value to our log file... datafile.write(timestamp + "," + str(temperature) + ",\n") datafile.close()
I have this code in a file called “temp.py” and have made it executable (right click on the file > permissions > make file executable) You can run it in a terminal window by typing the following:
The generated log file will be saved to the same location and called “temp_log_22-03-2013.txt” (with the date being the current date).
So! We have a log file with a load of temperature readings. Lets get it onto a webserver so that we can show the world.
First we need to ensure that the sensor is available from boot. To do this type the following into the command line:
sudo nano /etc/modules
This opens the MODULES file for editing. Navigate to the end of the file and add the following two lines:
Save the file [ctrl+x] and don’t forget to agree the changes.
Now that is done it is time to learn how to use FTP to send the file to our website.
Open up a command terminal and type:
sudo apt-get install ftp
Once installed we can write a short bash script to send our log-file to the internet.
HOST='ftp_of_your_webserver' USER='Your_username' PASSWD='Your_password' FOLDER='/webspace/httpdocs/pathtofolder' # Be sure to leave the following as it is. This will ensure that the filename of the log-file is correct. logname=$(date +"%d-%m-%Y") FILE="temp_log_$logname.txt" ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD cd $FOLDER put $FILE quit END_SCRIPT exit 0
I named this file ftp.sh and gave it full permissions and made it executable.
You can now run temp.py in the terminal and then ftp.sh to send the log-file to your site.
But why don’t we automate the whole process and make our lives a bit easier?
This is the easy part. Setting up a cronjob to run the task in the background. Slap the following into a terminal:
DO NOT DELETE ANYTHING IN THIS FILE.
All we want to do is add the following at the bottom (replacing folder names etc…):
0,30 * * * * /usr/bin/python /path_to/temp.py 01,31 * * * * /path_to/ftp.sh
The first line tells the cron to run at 00mins and 30mins past the hour. The second cron is run one (1) minute later.
If, after saving, the crontab doesn’t work please ensure there is a carriage return at the end of each line. I leave two breaks to be safe even after the last one. Save the file [ctrl+x] and don’t forget to agree the changes.
There we have it. Once the log-file is on my site it is then processed using PHP and displayed in a graph.