Plotting Your Raspberry Pi’s Temperature

Preface!

Ever wondered what kind of temperature changes your IoT (Internet of Things) device goes through?

I recently installed an IoT Information Panel in my home. I programmed it using Python and HTML.
If you also want to know how to write temperature values to a file, you’ll find that here

At the bottom of the display I take current temperature readings of my RPi (Raspberry Pi).

I decided to graph those temperature’s to see when the max and min temps happened.

Because my InfoPane is in the middle of my house, it graphed my home temperature (Within reason – it’s the CPU temp after all)

Since the InfoPane’s inception I saw the following Data:

THE LAST 1596 HOURS (Current MAX) 672 HOURS 8 HOURS 24 HOURS
Max: 68.8C 9May2018 1600 Max: 67.7C Max: 64.5C Max: 65.5C
Min: 49.4C 31May2018 0431 Min: 49.4C Min: 58.5C Min: 56.4C
Mean: 60.36C Mean: 59.07C Mean: 61.27C Mean: 62.42C

Something to note, the larger the scale (Hours of data) the wider the temperature range from Min to Max.

And get this! 1596 Hours makes my InfoPane 66.5 Days old!

I also took timestamps of these. From the last ~2 months of data the coldest time was around the end of May and the warmest was the start of May. Neat.

 

Here is the code if anyone is interesting in creating their own plots!

#<A File named plot.py>

#To Run it type python plot.py

#This script saves the image to the directory you place the plot file in

from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
try:
import matplotlib
except:
print “You’ll need to install matplotlib type: sudo apt-get install python-matplotlib”

matplotlib.use(‘Agg’) # Enables plotting over ssh, otherwise you’ll need TKinter and XMing

with open(‘pane.log’) as log:
lines = log.readlines()

#NOTE: My InfoPane log files are written in this format. 
You'll need to change this script if you don't follow the format:
# <date> <time> <anything in here_ with spaces or whatnot>< | Temp: >< | Run: >
# 2018-07-10 03:31:11,814 INFO main(119) #Photos: 260 | Temp: 56.9 | Run: 0:00:07.498138
# Be sure to check the spacing, it changes where the start/stop values end up

complete = []
for line in lines:
    data = []

    start = line.find("Temp:")
    if start !=-1:
        start= line.find(" | Temp: ")
        end = line.find(" | Run:")
        data.append(float(line[start+9:end]))
        data.append(line.split(' ')[0] + ' ' + line.split(' ')[1])
        complete.append(data)

#complete = complete[::-1] #Reverse complete so the times match up
temps = [val[0] for val in complete]
dates = [val[1] for val in complete]

hoursToPlot = 24

if hoursToPlot > len(temps):
    print "Error! You only have " + str(len(temps)) + "h worth of data, lower your hoursToPlot value."
    return

fig = plt.figure()
graph = fig.add_subplot(111)
graph.set_title("InfoPane Temps")
graph.set_xlabel("Last " + str(hoursToPlot) + "h")
graph.set_ylabel("Temp[F]")

temps = temps[-hoursToPlot:]
dates = dates[-hoursToPlot:]
maxTemp = max(temps)
print "---THE LAST " + str(hoursToPlot) + " HOURS---"
print "Max: " + str(maxTemp) + "F Time: " + str(dates[temps.index(maxTemp)])
minTemp = min(temps)
print "Min: "  + str(minTemp)+ "F Time: " + str(dates[temps.index(minTemp)]).split(',')[0]
print "Mean: " + str(round((sum(temps) / len(temps)),2))
totalTemps = len(temps)



timeInterval = 1 
#Currently my data gets written to my logs every hour on the 31min mark - so hours=i*1 
#Below range(totalTemps) will create i values from 0 to the length of temps
xA = [datetime.now() - timedelta(hours=i * timeInterval) for i in range(totalTemps)]

yA = [int(row) for row in temps]  # Using ::-1 to reverse the order of the data to coincide with the order of the dates

plt.plot(xA, yA, c='r', label='CPU')
leg = graph.legend()

# Create some unique save data
day = datetime.now().date().day
month = datetime.now().date().month

# plt.scatter()

plt.gcf().autofmt_xdate()  # Keeps data from being smashed together
myFmt = mdates.DateFormatter('%m/%d %I:%M%p')  # Prints labels for the x axis in a "Month/Day 12hr:##PM" format
plt.gca().xaxis.set_major_formatter(myFmt)

# Usethe day and month to prevent overwriting files
imagePath = str(totalTemps) + "h_" + str(month) + "_" + str(day) + "temps.png"
plt.savefig(imagePath)


Some example graphs for your hypergraphia pleasures!

The units are Celsius, not Fahrenheit… Doh!

 

Comments

comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.