I'm measuring the CPU temperature in Raspberry Pi 3 and getting very noisy values.
The temperature is measured every second, should be put through moving average (aka Sliding Window). Each 5 seconds a new Temp.value should be put into SQLite Database.
Till now I've found solutions only for static arrays of Data. Can anyone help with the problem? I need a function that takes a value each second and returns averaged value every 5 seconds.
The algorithm could be then applied to values like humidity, voltage and so on.
Temp sensor in Raspberry Pi 3 have a Precision of 0.538°C.
Here's what I have at the moment:
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Das Programm speist Temperaturen von CPU ins SQLite Datenbank für spätere
# Darstellung auf einer Webseite
from subprocess import check_output
import sqlite3
import time
import numpy
# Pfad zur Datenbank
dbname = "/var/www/cpuTemp_Database.db"
temp_array = []
def log_temperature(currentTemp):
""" Speichern in die Datenbank"""
myConnection = sqlite3.connect(dbname)
myCursor = myConnection.cursor()
myCursor.execute("INSERT INTO temps VALUES (datetime('now', 'localtime'), (?))", (currentTemp,))
myConnection.commit()
myConnection.close()
def abfrage():
""" Macht CPU-Temperaturabfrage und gibt das Wert zurück wie 53.692 """
output = check_output(['cat', '/sys/class/thermal/thermal_zone0/temp'])
stripedVal = output.strip()
tempVal = float(stripedVal)/1000
return tempVal
def main():
""" Main Function"""
while True:
for i in range(5):
tempValue = abfrage()
temp_array.append(tempValue)
time.sleep(1)
meanTemp = numpy.median(temp_array)
log_temperature(meanTemp)
temp_array [:] = []
if __name__ == "__main__":
main()