Skip to content
Snippets Groups Projects
Commit 6723183b authored by Jarkko Hanhela's avatar Jarkko Hanhela
Browse files

Raspi data logger, version 1.2020.02.06

parent b080981c
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3
#/home/pi/bmp280_tupla.py
# Author: Jarkko Hanhela
# Date: 4.10.2019
import os
os.chdir('/home/pi/')
import sys
import time
from datetime import datetime
import digitalio
import busio
import board
import adafruit_bmp280
from tendo import singleton
# Next line will stop this instance if there already is a running instance of the program
me = singleton.SingleInstance()
# This is the time between two measurements in seconds.
# 600 = 10 min
timeperiod = 120
# Define the name of the file that shall contain the measurement data.
# The fileversion numbers (to prevent overwriting data by mistake) and filetype '.csv' will be
# automatically appended to the variable $filename determined here.
# The file will be saved to a folder named "p-T_data".
#filename = 'both_sensors_in_closed_DUT_indoors'
filename = '3-measure_after_startup_test'
fileversion = 1
# Check if current version for the filename already exists, and make new version.
while os.path.isfile('p-T_data/' + filename + '-' + str(fileversion) + '.csv'):
fileversion += 1
# Compile the full filename.
filename = 'p-T_data/' + filename + '-' + str(fileversion) + '.csv'
print(filename)
# Initialize the SPI connection for the two bmp280 sensors.
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
bmp_inside_cs = digitalio.DigitalInOut(board.D5) #CS inside = pin 5
bmp_outside_cs = digitalio.DigitalInOut(board.D6) #CS outside = pin 6
# Let's make a connection to the sensor inside
try:
bmp280_inside = adafruit_bmp280.Adafruit_BMP280_SPI(spi, bmp_inside_cs)
except Exception as ex:
file = open('p-T_data/error_log.txt', 'w')
file.write(datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")+"; "+str(ex) +"\n")
file.close()
print(ex)
# Same for sensor outside
try:
bmp280_outside = adafruit_bmp280.Adafruit_BMP280_SPI(spi, bmp_outside_cs)
except Exception as ex:
file = open('p-T_data/error_log.txt', 'w')
file.write(datetime.strftime(datetime.now(), "%Y-%m-%d %H:%M:%S")+"; "+str(ex)+"\n")
file.close()
print(ex)
# Print a header for the new datafile named by $filename.
file = open(filename, 'w')
file.write("Timestamp, p_inside[Pa], T_inside[degC], p_outside[Pa], T_outside[degC]\n")
file.close()
# Make the data format for pressure and temperature to have just 4 digits
dataformat = "{:0.4f}"
# Start the measurement and loop until the program is stopped.
while True:
# Print the temperature and pressure data from the sensors
# and the current time from Raspberry Pi before reading data from the sensors.
time_before_reading = datetime.now()
print(str(time_before_reading))
#print(datetime.strftime(time_before_reading, "%Y-%m-%d %H:%M:%S"))
#print("\n\nPressure inside: %0.1f Pa" % (bmp280_inside.pressure*100))
#print("Temperature inside: %0.1f C" % bmp280_inside.temperature)
#print("\nPressure outside: %0.1f Pa" % (bmp280_outside.pressure*100))
#print("Temperature outside: %0.1f C" % bmp280_outside.temperature)
# Write the data from the sensors to the datafile.
file = open(filename, 'a+')
try:
file.write(datetime.strftime(time_before_reading, "%Y-%m-%d %H:%M:%S") + ','
+ dataformat.format(bmp280_inside.pressure*100) + ','
+ dataformat.format(bmp280_inside.temperature) + ','
+ dataformat.format(bmp280_outside.pressure*100) + ','
+ dataformat.format(bmp280_outside.temperature) + '\n')
# The connection to the datafile is closed after each write,
# so the file should not corrupt even due to an unexpected power-off
finally:
file.close()
# Sleep for the defined time period minus the time taken by reading and writing data
sleeptime = timeperiod - (datetime.now().timestamp() - time_before_reading.timestamp())
if sleeptime < 0:
sleeptime = 0.0
time.sleep(sleeptime)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment