Skip to content
Snippets Groups Projects
Verified Commit c3a42049 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add sensor test

parents
No related branches found
No related tags found
No related merge requests found
temp.py 0 → 100755
# import necessary modules
import json
import sys
import os
import time
import datetime
import numpy
import requests
from gpiozero import Button
BASE_URL = "http://energie.intern/api/"
def temp_it():
while True:
if not os.path.exists("/sys/bus/w1/devices/"):
print("The iterator is not executed on a Raspberry Pi")
sys.exit(-1)
# open 1-wire slaves list for reading
file = open('/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves', 'r')
# read 1-wire slaves list
w1_slaves = file.readlines()
# close 1-wire slaves list
file.close()
temp_count = 0
temps = []
# repeat following steps with each 1-wire slave
for line in w1_slaves:
# extract 1-wire slave
w1_slave = line.split("\n")[0]
# open 1-wire slave file
file = open('/sys/bus/w1/devices/' + str(w1_slave) + '/w1_slave')
# read content from 1-wire slave file
file_content = file.read()
# close 1-wire slave file
file.close()
# extract temperature string
str_value = file_content.split("\n")[1].split(" ")[9].split("=")[1]
# convert temperature value
temp = float(str_value) / 1000
# temp = float("%.2f" % temp)
temps.append(temp)
++temp_count
yield ((temps, temp_count))
if __name__ == "__main__":
# Load config
# conf_file = open("config.json", "r")
#config = json.load(conf_file)
#conf_file.close()
#room = config.get("room_id")
#api_key = config.get("room_api_key")
# Log config
#log.log("Read data for room: " + room + "\n")
windows = [12, 13]
buttons = {}
for window in windows:
print(f"Window {window}")
buttons[window] = Button(window)
for temps, temp_count in temp_it():
########
# TEMP #
########
# Calculate average
avg_temp = numpy.mean(temps)
avg_temp = float("%.2f" % avg_temp)
# Log average and time to console
print("Measured temperatures: " + str(temps))
print("Logged data: " + str(datetime.datetime.now())[0:10] + ", " +
str(datetime.datetime.now())[11:-10] + ", " + str(
avg_temp) + " °C \n")
##########
# WINDOW #
##########
# Get windows status
window_data = {}
for window in windows:
# Add one window to data
window_data[window] = buttons[window].is_pressed
# log.log(window["button"].is_pressed)
# Log window data
print(str(window_data))
# Wait for next data
# time.sleep(7.675)
time.sleep(3)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment