I got two sensors connected to my pi and I like to log their status to a csv file.
I made the following code:
Code: Select all
#!/usr/bin/python
import csv, os, glob, time, datetime
tfile1 = open("/sys/bus/w1/devices/28-00000742260e/w1_slave")
text1 =tfile1.read()
tfile1.close()
secondline1 = text1.split("\n")[1]
temperaturedata1 = secondline1.split(" ")[9]
temperature1 = float(temperaturedata1[2:])
temperature1 = temperature1 / 1000
tfile2 = open("/sys/bus/w1/devices/28-0000074270de/w1_slave")
text2 =tfile2.read()
tfile2.close()
secondline2 = text2.split("\n")[1]
temperaturedata2 = secondline2.split(" ")[9]
temperature2 = float(temperaturedata2[2:])
temperature2 = temperature2 / 1000
with open("/home/pi/temp.csv", "a") as csvfile:
out = csv.writer(csvfile, delimiter=",",
quotechar='|',quoting=csv.QUOTE_MINIMAL)
date = datetime.datetime.now()
out.writerow([date.strftime("%Y"),date.strftime("%m"),date.strftime("%d"),date.strftime("%H"),date.strftime("%M"), temperature1, temperature2])
time.sleep(5) #wait 5seconds
using:
Code: Select all
sudo crontab -eIn the file I wanted to add
Code: Select all
***** /usr/bin/python /home/pi/temperature_csv.pyCode: Select all
***** sudo /usr/bin/python /home/pi/temperature.csv.pyin addition, how can i stop the loop to view the data using the terminal?
thank you!