-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_bme280.py
More file actions
89 lines (63 loc) · 2.1 KB
/
Copy pathlog_bme280.py
File metadata and controls
89 lines (63 loc) · 2.1 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
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
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
import bme280, smbus2, time, logging
Logger = logging.getLogger(__name__)
#retrieve and return single observation from BME280 (function called by WxStation Logger)
def retrieve_bme280_ob():
bus, address = set_bme280_i2c()
port = 1
address = 0x77
with smbus2.SMBus(port) as bus:
bme280.load_calibration_params(bus,address)
obs = bme280.sample(bus,address)
q = obs.humidity
P = obs.pressure
T = obs.temperature
return T,q,P
#log BME280 T/q/P output to command line (1 second sampling frequency)
def log_bme280_to_cmd():
port = 1
address = 0x77
with smbus2.SMBus(port) as bus:
bme280.load_calibration_params(bus,address)
while True:
obs = bme280.sample(bus,address)
q = obs.humidity
P = obs.pressure
T = obs.temperature
Logger.debug(f"\r [+] Temperature: {T:5.2f} degC, Humidity: {q:5.2f} %, Pressure: {P:6.1f} mb", end=" ")
time.sleep(1)
#retrieve mean value from # of obs over interval
def get_mean_bme280_obs(num,dt):
if num < 1:
num = 1
if dt <= 0.1:
dt = 0.1
no = 0
temp = 0.
rh = 0.
pres = 0.
Logger.debug("[+] Starting BME280 obs")
port = 1
address = 0x77
with smbus2.SMBus(port) as bus:
bme280.load_calibration_params(bus,address)
while no < num:
obs = bme280.sample(bus,address)
q = obs.humidity
P = obs.pressure
T = obs.temperature
Logger.debug(f"[-] Ob T={T}, q={q}, P={P}")
no += 1
temp += T
rh += q
pres += P
#Logger.debug(f"[+] Got observation {no} of {num}")
time.sleep(dt)
temp = temp/no
rh = rh/no
pres = pres/no
Logger.debug(f"[+] Finished BME280 observation cycle ({no} obs)")
return temp, rh, pres
#if function is called, run log to command line
if __name__ == "__main__":
log_bme280_to_cmd()