forked from tdamdouni/Raspberry-Pi-DIY-Projects
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsensor.py
executable file
·78 lines (67 loc) · 1.79 KB
/
sensor.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import time
import os
from influxdb import InfluxDBClient
host = os.getenv("INFLUX_HOST")
port = 8086
dbname = os.getenv("DB")
user = os.getenv("USER")
password = os.getenv("PASSWORD")
host_pi = os.getenv("HOSTNAME") # change this on each Pi
sample_duration = 30 # seconds
client = InfluxDBClient(host, port, user, password, dbname)
client.create_database(dbname)
from Adafruit_BME280 import *
sensor = BME280(mode=BME280_OSAMPLE_8)
def get_cpu_temp():
path="/sys/class/thermal/thermal_zone0/temp"
f = open(path, "r")
temp_raw = int(f.read().strip())
temp_cpu = float(temp_raw / 1000.0)
return temp_cpu
try:
while True:
temp_cpu = get_cpu_temp()
degrees = float(sensor.read_temperature())
pascals = float(sensor.read_pressure())
hectopascals = pascals / 100
humidity = sensor.read_humidity()
iso = time.ctime()
json_body = [
{
"measurement": "ambient_celcius",
"tags": {"host": host_pi},
"time": iso,
"fields": {
"value": degrees,
"val": float(degrees)
}
},
{
"measurement": "cpu_celcius",
"tags": {"host": host_pi},
"time": iso,
"fields": {
"value": temp_cpu,
}
},
{
"measurement": "relative_humidity",
"tags": {"host": host_pi},
"time": iso,
"fields": {
"value": humidity,
}
},
{
"measurement": "ambient_pressure",
"tags": {"host": host_pi},
"time": iso,
"fields": {
"value": hectopascals,
}
}
]
client.write_points(json_body)
time.sleep(sample_duration)
except KeyboardInterrupt:
pass