-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposter.py
More file actions
51 lines (43 loc) · 1.48 KB
/
composter.py
File metadata and controls
51 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import adafruit_max31855
from digitalio import DigitalInOut
import board
from prometheus_client import start_http_server, Gauge, Histogram
import time
import logging
import yaml
# Read configuration from config.yaml
with open('config.yaml', 'r') as file:
config = yaml.safe_load(file)
temperature_offset = config['composterpy']['temperature-offset']
#digital_io = getattr(board, config['composterpy']['digital-io'])
digital_io = board.D5
prometheus_interval = config['composterpy']['prometheus-interval']
# Initialize sensor
spi = board.SPI()
cs = DigitalInOut(digital_io)
sensor = adafruit_max31855.MAX31855(spi, cs)
t = Gauge('temperature', 'Current temperature in Fahrenheit', ['scale'])
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def get_temp():
readings = []
for _ in range(10):
readings.append(sensor.temperature)
time.sleep(0.3) # short delay between readings
#print(readings)
average_temp = sum(readings) / len(readings)
calibrated_temp = average_temp + temperature_offset
return round(celsius_to_fahrenheit(calibrated_temp), 1)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(2323)
# Generate some requests.
while True:
try:
t.labels('C').set(sensor.temperature)
t.labels('F').set(get_temp())
print(get_temp())
time.sleep(prometheus_interval)
except Exception as E:
print(E)
pass