first of all I’m not a pro. But i would like to have some feedback on the code I’m running,
Situation: 5 DS18B20 sensors conected to pi, I want them to read out temprature and safe it in some kind of log file.
Steps,
i symlinked the w1_slave files to readable names in my working directory, sensor1, sensor2, sensor3 etc
python script to read them and put it excel,
import datetime
import time
import pandas as pd
i = 1
while i < 9:
a = open ("sensor1", "r")
b = open ("sensor2", "r")
c = open ("sensor3", "r")
d = open ("sensor4", "r")
e = open ("sensor5", "r")
howLate = datetime.datetime.now()
day = datetime.date.today().year
sensor1 = int(a.read()[69:74]) / 1000
sensor2 = int(b.read()[69:74]) / 1000
sensor3 = int(c.read()[69:74]) / 1000
sensor4 = int(d.read()[69:74]) / 1000
sensor5 = int(e.read()[69:74]) / 1000
existing_file = 'temp.xlsx'
new_data = {'time': [howLate], 'sensor1': [sensor1], 'sensor2': [sensor2], 'sensor3': [sensor3], 'sensor4': [sensor4], 'sensor5': [sensor5]}
df_new = pd.DataFrame(data=new_data)
df_existing = pd.read_excel(existing_file)
df_combined = df_existing.append(df_new, ignore_index=True)
df_combined.to_excel(existing_file, index=False)
a.close
b.close
c.close
d.close
e.close
time.sleep(2)
i += 1
I made a crontab to run this script every minute, and an other one to refresh the xls file every hour, and copy the old file to xls file with time stamp.
Questions i have,
What would be best practice to solve this ?
Is it bad i used excel ? why would i use Json for example ?