Version 2.0 // Python temperature logging script Arduino + Raspberry Pi


Above is a temperature probe jig, it captures and logs the temperature from inside of the fermentor.


Plotting the data using a web API:

The plot above was generated using sample data and the service – ( once you capture the data, you can upload your data_file.dat there and with a few clicks, make the graph ) – until you learn how to manually write the plot code in python.

Plotting the data using python code:

In the python code for the log – if you use this line to capture the data, then you can manually run the python code and it should generate a plot if you installed python right with all the necessary modules – this is considered machine readable the way that time.time is written to the file.  If you use the DATE/TIME instead, then I haven’t figured this out yep, so we use the web API (above) and upload the data and generate the graph like that.


a quick python plot example below:


sample code to generate a plot from the sample data.

from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np

# select the style

# unpack values into array when using numpy // python uses lists
x,y = np.loadtxt('test3.dat', unpack=True, delimiter='\t')


# configure the labels
plt.title('temperature plot')
plt.ylabel('Y Axis')
plt.xlabel('X Axis')


Dynamic Temperature Python script…

Version 2.0 // logging of temperature code with appended date.  This version checks to see if the previous recorded sample is the same, if it is, it is skipped and not recorded.  This is useful to have when you are wanting to monitor and record temperatures over a long time, like months or a year and not waste data space on repeating values.

This code can be pretty much used with any temperature sensor, we use it with the Dallas DS18S20.

Let me briefly explain what the code is doing ( from top to bottom ), but I strongly recommend that you take some basic classes on Python, we are still learning it too :- ) that is the only way to take full advantage of it and learn a new skill – Python is a good thing to have on your resume these days too :- )

Special thanks go to “Ofnuts” (the person who helped up with the syntax to get this done from the python help forum)!

  1. we are importing some modules
  2. we are setting up some variables like LOGFILE_FORMAT & TIMESTAMP
  3. def – means defining a function, so we are defining a function called: logTemperature() – it does the logging of the temperature with an appending timestamp to a text file
  4. setup the serial connection from the Arduino over the USB cable
  5. last_temp_reading = – 273 (we are setting up a unrealistic condition for comparison temperature)
  6. the rest of the code checks for a clean string coming from the Arduino and converting it into a float so that an inequality check can be done
  7. a lot of the extra Printing was also done to test, you can take those out if you are not going to be looking at the screen and running the command in the background using nohup.
  8. If you know Python remove and add whatever you need and share with us if you end up doing something cool
import serial, datetime
from datetime import datetime
import time

LOGFILE_FORMAT = '%Y-%m-%d.dat' # could also contain a path: '/home/brewery/temperatures/%Y-%m-%d.dat'
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S' # better make them human readable

LOGFILE_FORMAT_FILE_NAME = '/home/pi/python/datalog_onefile_test2_dynamic.dat'

def logTemperature(temperature):
    if not temperature:
        return; # no need to go further # get it only once to insure consistency
# if you want the decimal part of the time to be dropped use below line instead
# un-comment it and replace the .write line above
    # logFile.write('{:10.0f}\t{:3.2f}\n'.format(time.time(),temperature))

#define for the USB serial connection between the Raspberry Pi/Arduino Uno
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)

last_temp_reading = -273

while True:
    temp = ser.readline().strip() # get something clean from the start
    if temp:   # a non-empty string is "True", an empty string (or None...) is "False"
        temperature = float(temp)
        print "printing variable", temperature
        print "printing temperature variable", last_temp_reading

# dynamic sampling, ignoring repeating temperature samples
        if last_temp_reading != temperature: # assumes you can't get a reading of -273, so first execution will always see this as true.
            print "temp is different, recording it!", temperature
            last_temp_reading = temperature
            print "temp is the same, ignoring it!!!", last_temp_reading



This entry was posted in Arduino-RaspberryPi-Make-Beer. Bookmark the permalink.