Files
DS18B20_raspi/temperature_reporter.py
2021-01-06 15:55:24 +01:00

57 lines
1.6 KiB
Python

#! /usr/bin/env python3
import os
import subprocess
from influxdb import InfluxDBClient
# Hardcoded path and dict of device names
w1_path = "/sys/bus/w1/devices"
device_names = {"28-000000000000":"Sensor_Name"}
device_blacklist = ["w1_bus_master1", "00-600000000000"]
# Influx server data
INFLUX_IP = ""
INFLUX_PORT = 8086
INFLUX_DB = ""
INFLUX_USER = ""
INFLUX_PASS = ""
# Iterate all i2c temperature sensors and
for device in os.listdir(w1_path):
if device not in device_blacklist:
device_name = device if not device_names[device] else device_names[device]
with open(f"{w1_path}/{device}/temperature") as t:
temp = float(t.readline()[:-1])/1000
print(f"Device = {device_name} Temperature = {temp}")
#prepare json of temperature data
temp_data = [
{
"measurement" : "temperature",
"tags" : {
"host": f"{device_names[device]}"
},
"fields" : {
"temperature": temp
}
}
]
# Report sensor data to server
client = InfluxDBClient(INFLUX_IP, INFLUX_PORT, INFLUX_DB, INFLUX_USER, INFLUX_PASS)
client.write_points(temp_data)
# Report CPU temperature data to server
temp = float(subprocess.check_output(["vcgencmd", "measure_temp"]).decode("utf-8")[:-3].split("=")[1])
temp_data = [{"measurement" : "temperature",
"tags" : {"host": "Raspberry_cpu"},
"fields" : { "temperature": temp }
}]
client = InfluxDBClient(INFLUX_IP, INFLUX_PORT, INFLUX_DB, INFLUX_USER, INFLUX_PASS)
client.write_points(temp_data)