One Wire Temperature Sensor With Cron & FTP

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

#!/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 =
 # 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")

I have this code in a file called “” 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.

# 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")
quote USER $USER
put $FILE
exit 0

I named this file and gave it full permissions and made it executable.
You can now run in the terminal and then 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:

crontab -e

All we want to do is add the following at the bottom (replacing folder names etc…):

0,30 * * * * /usr/bin/python /path_to/

01,31 * * * * /path_to/

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.

This entry was posted in GPIO, raspberry pi, Tutorial and tagged , , , , . Bookmark the permalink.

24 Responses to One Wire Temperature Sensor With Cron & FTP

  1. Pingback: Kidpi…! |

  2. Steven says:

    Hello there I have just set up my temprature sensor currently in my living room running a thermostat program that I have been playing with. I really like your temprature loging website where you can look back in its history that is really good I would be intrested doing my own similer to what you have for my garden. Thanks Steve.

    • drewibbo says:

      Hi Steve,
      I like the sound of your project. If you want any information that can help then please let me know. I am happy to share the PHP scripts involved. I currently use the data directly from the txt file, but can also input the data to a database.
      Good luck :)


      • Steven says:

        Hi Andy, Thank you for the reply. I have been playing with the script all morning and it works fantastically I have added comments to the code (To help me understand how it works) for testing I have added a time.sleep(1800) just for local testing but I will be moving to the crontab when things are set up. I am very interested in the PHP scripts that you have there for the website I can then view this from my phone when I am away from home. Thank you in advanced. By the way is your website ran from your Pi? It’s very fast :-) Many thanks Steve

        • drewibbo says:

          I’m glad it is working for you. The good thing about having it set up with the crontab is that it runs in the background leaving the terminal free to do other things. The php code for the site is my own, but the graph was open source software by Gerd Tentler. Here are the files needed to display the results on a webpage: Download

          You will need to amend graph.php to reflect your txt file naming system, or include database details (if you use one).
          Let me know if you have any problems.
          P.s. My site is hosted by My Hosting. They are very good value for money.

          • Steven says:

            Thank you for the link I have downloaded the file and now have it working on my test pi now I just need to setup the finished product :) thank you very much for your help. I will mention you on my website.

            Regards Steve

            • drewibbo says:

              Excellent! I hope it works well for you. Thanks in advance for the mention on your site. I’ll add a link to yours as well. :D

    • drewibbo says:

      I have had a look at your site. Nice work. I would love to see information on your thermostat set-up. I have also been thinking of doing the same thing myself.


      • Steven says:

        Thank you I dont have much on there yet with work in the way and now the better weather is starting to get hear the website takes a back seat. I am happy to give you the code that I have for my thermostat however its someone elses work with some changes that I have made. This is his youtube video his code is in the description. I am happy to give you the code that I have I just wanted to make sure that you knew where the code came from. Steve

  3. web design says:

    I will be interested as well as considering what you will be writing about here.

    • Steven says:

      I have been keeping an eye on your temperature chart with great interest. I have now got mine setup and working although my setup is different to yours it still gives the same results. I have not published my chart yet on the website but I thought that you may want to compare temperatures between Worthing and Flitwick so hear us the link my first full day was yesterday I. Saturday I was making the lead for my pi to the transistor like sensor. I then got the scrips running with cron before 22:30. Happy viewing. Steve

      • drewibbo says:

        Hey! Thats awesome. i love your colour scheme. Glad to see my work is being put to good use. I’ll keep an eye on it. :D

  4. “One Wire Temperature Sensor With Cron & FTP |” definitely
    got me simply addicted on ur internet page! I actuallydefinitely will
    be returning much more often. Thank you -Dani

  5. Steve says:

    Love what your doing with the temp graph. I would love to see a photo of your setup. Keep up with the good work. I am a regular visitor especially to the graph.


  6. joe90 says:

    excellent guide, took a while to get fully working though, I never got the ‘bash script to send our log-file’ to work. But, I ‘re-wrote’ it in python, and it works a treat.

    Looking forward to the next stage of your guide ‘processing and graphing using PHP’ and uploading to my website.


    • drewibbo says:

      Hi Joe90,
      I’m glad you like the tut. Im sorry that parts didn’t work for you. It works for me perfectly.

      Since I wrote the tutorial I have updated it so that the sensor data is written directly to a mysql datatbase. I then pull the readings and use Highcharts to display the data.

      I will get working on the next tutorial for you though and should have it up soon.

      :D Andy

    • drewibbo says:

      Here is the link to the graph/php tutorial:

      Hope it works for you. Let me know if you get any problems. :D


  7. Pingback: Process Temperature Data And Display A Graph |

  8. Erik Rossen says:

    Thanks for the tutorial. You might want to check your Fritzing diagram, however. The temperature sensor is shown backwards. Matthew Kirk’s tutorial shows that pin 1 should be hooked to ground, not 3v3 like you show in your diagram. I burned my fingers while figuring this out.

  9. andrew says:

    Love your tutorial, glad I found your site.
    I’ve managed to get it up and running and it generates the txt file.
    However it appears to only do a read / write to the txt file each time I run the, is this correct or is it meant to add a line every minute or two?
    If I wanted it to log an entry every two minutes how do I get it to do that?
    sorry I’m a complete linux Rpi nube here and trying to learn.

    • drewibbo says:

      Hi Andrew,

      You must tell the cron file that you want to make more than one read/write to the file:

      0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58 * * * * /usr/bin/python /path_to/

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>