-
Notifications
You must be signed in to change notification settings - Fork 2
/
bme280.py
111 lines (93 loc) · 2.61 KB
/
bme280.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from import time
from datetime import datetime
counterfile = open("bme280counter.txt","r")
cur = counterfile.read()
newcur = int(cur)+1
newcounterfile = open("bme280counter.txt","w")
newcounterfile.write(str(newcur))
#Initialization
for x in range(0,3):
try:
from Adafruit_BME280 import *
str_error = None
except Exception as str_error:
print 'BME280: Fail to import Adafruit_BME280 Library'
pass
if str_error:
sleep(1)
else:
break
for x in range(0,3):
try:
sensor = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
str_error = None
except Exception as str_error:
print 'Sensor initialization failed'
pass
if str_error:
sleep(1)
else:
break
loopcounter = 0
DataName = "BME280"+str(cur)+".csv"
DataFile = open(DataName, "a")
DataFile.write("Time,Iteration,Temperature,Pressure,Humidity,Altitude")
#Loop Receive Data
while True:
for x in range(0, 3):
try:
sensor = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
str_error = None
except Exception as str_error:
print 'Sensor Initialization Failed'
pass
if str_error:
sleep(1)
else:
break
for x in range(0, 3):
try:
degrees = sensor.read_temperature()
str_error = None
except Exception as str_error:
print 'BME280 Read Temperature Failed'
degrees = 0000
pass
if str_error:
sleep(1)
else:
break
for x in range(0, 3):
try:
pascals = sensor.read_pressure()
hectopascals = pascals / 100
str_error = None
except Exception as str_error:
print 'BME280 Read Pressure Failed'
pascals = 0000
degrees = 0000
pass
if str_error:
sleep(1)
else:
break
for x in range(0, 3):
try:
humidity = sensor.read_humidity()
str_error = None
except Exception as str_error:
print 'BME280 Read Humidity Failed'
humidity = 0000
pass
if str_error:
sleep(1)
else:
break
loopcounter = loopcounter+1;
timestr = str(datetime.now())
loopcounterstr = str(loopcounter)
tempstr = str(degrees)
presstr = str(hectopascals)
humstr = str(humidity)
DataFile.write(','.join([timestr,loopcounterstr,tempstr,presstr,humstr]))
sleep(2)