This program simply logs the time, temperature, humidity and air pressure every six seconds. The data is saved into a text file for later inspection.
| Is it getting hot in here? Actually this is the temperature of the air just near the CPU (I am actually really cold right now in my hovel - please send warm jumpers). |
I have read the output file over my home server and produced this graph in Windows 10.
# Data Logging App
# Logs the time, temperature, humidity and air pressure
# Tim Street
# 2015-01-14
#
# version history
# ---------------
# 1.0.0.1
# logger class with time, temp, humidity and pressure
from datetime import datetime
import time
from sense_hat import SenseHat
VERSION = "1.0.0.1"
FILE_PATH = "log.csv" # file path used to log data
class logger:
""" A logger provides methods for reading the sensor values and saving to a file """
def __init__(self, filepath):
self.__time = 0
self.__temp = 0
self.__humidity = 0
self.__pressure = 0
self.__sense = SenseHat()
self.__savepath = filepath
def __str__(self):
""" String representation of logger """
return "Time:\t\t"+str(self.getTime())+"\nTemp:\t\t"+str(self.getTemp())+"C\nHumidity:\t"+str(self.getHumidity())+"%\nPressure:\t"+str(self.getPressure())+"mb"
def __toCSV(self):
""" Retuns a CSV file line """
return str(self.getTime())+","+str(self.getTemp())+","+str(self.getHumidity())+","+str(self.getPressure())+",\n"
def __setTime( self ):
""" sets log to current date and time """
self.__time = datetime.today()
def getTime(self):
""" returns the date and time from the log """
return self.__time
def __setTemp(self):
""" logs the current temperature """
self.__temp = self.__sense.get_temperature()
def getTemp(self):
""" returns the temperature """
return self.__temp
def __setHumidity(self):
""" logs the current humidity """
self.__humidity = self.__sense.get_humidity()
def getHumidity(self):
""" returns the humidity """
return self.__humidity
def __setPressure(self):
""" logs the current air pressure """
self.__pressure = self.__sense.get_pressure()
def getPressure(self):
""" returns the pressure """
return self.__pressure
def update(self):
""" updates all sensors """
self.__setTime()
self.__setTemp()
self.__setHumidity()
self.__setPressure()
def save(self):
file = open(self.__savepath, 'a')
file.write( self.__toCSV() )
file.close()
#main
log = logger( FILE_PATH )
while True:
log.update()
log.save()
print(log)
time.sleep(6)
The program needs to append a comma separated file "log.csv" which you can create yourself, or run the script below to create it automatically.
# creates a new output filefile = open("log.csv", "w")
file.write("time,temp /C,humidity /%,pressure /mb\n")
file.close()
Features I intend to implement next:
- Command line interface
- Multiple logs by name
- Option to toggle sensors on off
- Variable log sleep time
- LED matrix icons for each state - 'ready', 'sensing', 'writing', 'sleeping'
Well that's it for now, but do check back soon!
#raspberrypi #pi #sensehat #python #datalogging
No comments:
Post a Comment