Last active
June 1, 2020 06:29
-
-
Save vieskees/27835ff90f047b8e34500c91ddd049f4 to your computer and use it in GitHub Desktop.
A little script to test the basic set-up of the Pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
import time | |
from pytz import timezone | |
from datetime import datetime | |
base_dir = '/sys/bus/w1/devices/' | |
device_folder = glob.glob(base_dir + '28*')[0] | |
device_file = device_folder + '/w1_slave' | |
amsterdam = timezone('Europe/Amsterdam') | |
def read_temp_raw(): | |
f = open(device_file, 'r') | |
lines = f.readlines() | |
f.close() | |
return lines | |
def read_temp(): | |
lines = read_temp_raw() | |
while lines[0].strip()[-3:] != 'YES': | |
time.sleep(0.2) | |
lines = read_temp_raw() | |
equals_pos = lines[1].find('t=') | |
if equals_pos != -1: | |
temp_string = lines[1][equals_pos+2:] | |
temp_c = float(temp_string) / 1000.0 | |
return temp_c | |
def get_time(): | |
# now = datetime.now() | |
now = datetime.now(amsterdam) | |
time = now.strftime("%H:%M:%S") | |
return time | |
while True: | |
with open('./temp_input.txt', 'a') as writer: | |
timestamp = get_time() | |
temp = str(read_temp()) | |
writer.write(timestamp + " : temperature: ") | |
writer.write(temp + "C") | |
writer.write("\n") | |
time.sleep(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment