Skip to content

Commit 476a391

Browse files
committed
Merge branch 'cipy-improved-logging'
2 parents 998bb2e + 23cf044 commit 476a391

File tree

10 files changed

+117
-56
lines changed

10 files changed

+117
-56
lines changed

examples/adc.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import time
44
from enviroplus import gas
5+
import logging
56

6-
print("""adc.py - Print readings from the MICS6814 Gas sensor.
7+
logging.basicConfig(
8+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
9+
level=logging.INFO,
10+
datefmt='%Y-%m-%d %H:%M:%S')
11+
12+
logging.info("""adc.py - Print readings from the MICS6814 Gas sensor.
713
814
Press Ctrl+C to exit!
915
@@ -15,7 +21,7 @@
1521
try:
1622
while True:
1723
readings = gas.read_all()
18-
print(readings)
24+
logging.info(readings)
1925
time.sleep(1.0)
2026
except KeyboardInterrupt:
2127
pass

examples/all-in-one-no-pm.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@
1313
from PIL import Image
1414
from PIL import ImageDraw
1515
from PIL import ImageFont
16+
import logging
1617

17-
print("""all-in-one.py - Displays readings from all of Enviro plus' sensors
18+
logging.basicConfig(
19+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
20+
level=logging.INFO,
21+
datefmt='%Y-%m-%d %H:%M:%S')
22+
23+
logging.info("""all-in-one.py - Displays readings from all of Enviro plus' sensors
1824
Press Ctrl+C to exit!
1925
""")
2026

@@ -58,7 +64,7 @@ def display_text(variable, data, unit):
5864
- min(values[variable]) + 1) for v in values[variable]]
5965
# Format the variable name and value
6066
message = "{}: {:.1f} {}".format(variable[:4], data, unit)
61-
print(message)
67+
logging.info(message)
6268
draw.rectangle((0, 0, WIDTH, HEIGHT), (255, 255, 255))
6369
for i in range(len(colours)):
6470
# Convert the values to colours from red to blue
@@ -87,7 +93,7 @@ def get_cpu_temperature():
8793
# temperature down, and increase to adjust up
8894
factor = 0.8
8995

90-
cpu_temps = [0] * 5
96+
cpu_temps = [get_cpu_temperature()] * 5
9197

9298
delay = 0.5 # Debounce the proximity tap
9399
mode = 0 # The starting mode
@@ -121,57 +127,57 @@ def get_cpu_temperature():
121127

122128
# One mode for each variable
123129
if mode == 0:
124-
variable = "temperature"
130+
# variable = "temperature"
125131
unit = "C"
126132
cpu_temp = get_cpu_temperature()
127133
# Smooth out with some averaging to decrease jitter
128134
cpu_temps = cpu_temps[1:] + [cpu_temp]
129135
avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
130136
raw_temp = bme280.get_temperature()
131137
data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
132-
display_text(variable, data, unit)
138+
display_text(variables[mode], data, unit)
133139

134140
if mode == 1:
135-
variable = "pressure"
141+
# variable = "pressure"
136142
unit = "hPa"
137143
data = bme280.get_pressure()
138-
display_text(variable, data, unit)
144+
display_text(variables[mode], data, unit)
139145

140146
if mode == 2:
141-
variable = "humidity"
147+
# variable = "humidity"
142148
unit = "%"
143149
data = bme280.get_humidity()
144-
display_text(variable, data, unit)
150+
display_text(variables[mode], data, unit)
145151

146152
if mode == 3:
147-
variable = "light"
153+
# variable = "light"
148154
unit = "Lux"
149155
if proximity < 10:
150156
data = ltr559.get_lux()
151157
else:
152158
data = 1
153-
display_text(variable, data, unit)
159+
display_text(variables[mode], data, unit)
154160

155161
if mode == 4:
156-
variable = "oxidised"
162+
# variable = "oxidised"
157163
unit = "kO"
158164
data = gas.read_all()
159165
data = data.oxidising / 1000
160-
display_text(variable, data, unit)
166+
display_text(variables[mode], data, unit)
161167

162168
if mode == 5:
163-
variable = "reduced"
169+
# variable = "reduced"
164170
unit = "kO"
165171
data = gas.read_all()
166172
data = data.reducing / 1000
167-
display_text(variable, data, unit)
173+
display_text(variables[mode], data, unit)
168174

169175
if mode == 6:
170-
variable = "nh3"
176+
# variable = "nh3"
171177
unit = "kO"
172178
data = gas.read_all()
173179
data = data.nh3 / 1000
174-
display_text(variable, data, unit)
180+
display_text(variables[mode], data, unit)
175181

176182
# Exit cleanly
177183
except KeyboardInterrupt:

examples/all-in-one.py

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@
1414
from PIL import Image
1515
from PIL import ImageDraw
1616
from PIL import ImageFont
17+
import logging
1718

18-
print("""all-in-one.py - Displays readings from all of Enviro plus' sensors
19+
logging.basicConfig(
20+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
21+
level=logging.INFO,
22+
datefmt='%Y-%m-%d %H:%M:%S')
23+
24+
logging.info("""all-in-one.py - Displays readings from all of Enviro plus' sensors
1925
2026
Press Ctrl+C to exit!
2127
@@ -64,7 +70,7 @@ def display_text(variable, data, unit):
6470
- min(values[variable]) + 1) for v in values[variable]]
6571
# Format the variable name and value
6672
message = "{}: {:.1f} {}".format(variable[:4], data, unit)
67-
print(message)
73+
logging.info(message)
6874
draw.rectangle((0, 0, WIDTH, HEIGHT), (255, 255, 255))
6975
for i in range(len(colours)):
7076
# Convert the values to colours from red to blue
@@ -93,7 +99,7 @@ def get_cpu_temperature():
9399
# temperature down, and increase to adjust up
94100
factor = 0.8
95101

96-
cpu_temps = [0] * 5
102+
cpu_temps = [get_cpu_temperature()] * 5
97103

98104
delay = 0.5 # Debounce the proximity tap
99105
mode = 0 # The starting mode
@@ -130,78 +136,82 @@ def get_cpu_temperature():
130136

131137
# One mode for each variable
132138
if mode == 0:
133-
variable = "temperature"
139+
# variable = "temperature"
134140
unit = "C"
135141
cpu_temp = get_cpu_temperature()
136142
# Smooth out with some averaging to decrease jitter
137143
cpu_temps = cpu_temps[1:] + [cpu_temp]
138144
avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
139145
raw_temp = bme280.get_temperature()
140146
data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
141-
display_text(variable, data, unit)
147+
display_text(variables[mode], data, unit)
142148

143149
if mode == 1:
144-
variable = "pressure"
150+
# variable = "pressure"
145151
unit = "hPa"
146152
data = bme280.get_pressure()
147-
display_text(variable, data, unit)
153+
display_text(variables[mode], data, unit)
148154

149155
if mode == 2:
150-
variable = "humidity"
156+
# variable = "humidity"
151157
unit = "%"
152158
data = bme280.get_humidity()
153-
display_text(variable, data, unit)
159+
display_text(variables[mode], data, unit)
154160

155161
if mode == 3:
156-
variable = "light"
162+
# variable = "light"
157163
unit = "Lux"
158164
if proximity < 10:
159165
data = ltr559.get_lux()
160166
else:
161167
data = 1
162-
display_text(variable, data, unit)
168+
display_text(variables[mode], data, unit)
163169

164170
if mode == 4:
165-
variable = "oxidised"
171+
# variable = "oxidised"
166172
unit = "kO"
167173
data = gas.read_all()
168174
data = data.oxidising / 1000
169-
display_text(variable, data, unit)
175+
display_text(variables[mode], data, unit)
170176

171177
if mode == 5:
172-
variable = "reduced"
178+
# variable = "reduced"
173179
unit = "kO"
174180
data = gas.read_all()
175181
data = data.reducing / 1000
176-
display_text(variable, data, unit)
182+
display_text(variables[mode], data, unit)
177183

178184
if mode == 6:
179-
variable = "nh3"
185+
# variable = "nh3"
180186
unit = "kO"
181187
data = gas.read_all()
182188
data = data.nh3 / 1000
183-
display_text(variable, data, unit)
189+
display_text(variables[mode], data, unit)
184190

185191
if mode == 7:
186-
variable = "pm1"
192+
# variable = "pm1"
187193
unit = "ug/m3"
188-
data = pms5003.read()
189-
data = data.pm_ug_per_m3(1.0)
190-
display_text(variable, data, unit)
194+
try:
195+
data = pms5003.read()
196+
except pms5003.ReadTimeoutError:
197+
pass
198+
else:
199+
data = data.pm_ug_per_m3(1.0)
200+
display_text(variables[mode], data, unit)
191201

192202
if mode == 8:
193-
variable = "pm25"
203+
# variable = "pm25"
194204
unit = "ug/m3"
195205
data = pms5003.read()
196206
data = data.pm_ug_per_m3(2.5)
197-
display_text(variable, data, unit)
207+
display_text(variables[mode], data, unit)
198208

199209
if mode == 9:
200-
variable = "pm10"
210+
# variable = "pm10"
201211
unit = "ug/m3"
202212
data = pms5003.read()
203213
data = data.pm_ug_per_m3(10)
204-
display_text(variable, data, unit)
214+
display_text(variables[mode], data, unit)
205215

206216
# Exit cleanly
207217
except KeyboardInterrupt:

examples/compensated-temperature.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
except ImportError:
99
from smbus import SMBus
1010

11-
print("""compensated-temperature.py - Use the CPU temperature
11+
import logging
12+
13+
logging.basicConfig(
14+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
15+
level=logging.INFO,
16+
datefmt='%Y-%m-%d %H:%M:%S')
17+
18+
logging.info("""compensated-temperature.py - Use the CPU temperature
1219
to compensate temperature readings from the BME280 sensor.
1320
Method adapted from Initial State's Enviro pHAT review:
1421
https://medium.com/@InitialState/tutorial-review-enviro-phat-for-raspberry-pi-4cd6d8c63441
@@ -33,7 +40,7 @@ def get_cpu_temperature():
3340
# temperature down, and increase to adjust up
3441
factor = 0.8
3542

36-
cpu_temps = [0] * 5
43+
cpu_temps = [get_cpu_temperature()] * 5
3744

3845
while True:
3946
cpu_temp = get_cpu_temperature()
@@ -42,5 +49,5 @@ def get_cpu_temperature():
4249
avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
4350
raw_temp = bme280.get_temperature()
4451
comp_temp = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
45-
print("Compensated temperature: {:05.2f} *C".format(comp_temp))
52+
logging.info("Compensated temperature: {:05.2f} *C".format(comp_temp))
4653
time.sleep(1.0)

examples/gas.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import time
44
from enviroplus import gas
5+
import logging
56

6-
print("""gas.py - Print readings from the MICS6814 Gas sensor.
7+
logging.basicConfig(
8+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
9+
level=logging.INFO,
10+
datefmt='%Y-%m-%d %H:%M:%S')
11+
12+
logging.info("""gas.py - Print readings from the MICS6814 Gas sensor.
713
814
Press Ctrl+C to exit!
915
@@ -12,7 +18,7 @@
1218
try:
1319
while True:
1420
readings = gas.read_all()
15-
print(readings)
21+
logging.info(readings)
1622
time.sleep(1.0)
1723
except KeyboardInterrupt:
1824
pass

examples/lcd.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import ST7735
44
from PIL import Image, ImageDraw, ImageFont
5+
import logging
56

6-
print("""lcd.py - Hello, World! example on the 0.96" LCD.
7+
logging.basicConfig(
8+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
9+
level=logging.INFO,
10+
datefmt='%Y-%m-%d %H:%M:%S')
11+
12+
logging.info("""lcd.py - Hello, World! example on the 0.96" LCD.
713
814
Press Ctrl+C to exit!
915

examples/light.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import time
44
import ltr559
5+
import logging
56

6-
print("""light.py - Print readings from the LTR559 Light & Proximity sensor.
7+
logging.basicConfig(
8+
format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
9+
level=logging.INFO,
10+
datefmt='%Y-%m-%d %H:%M:%S')
11+
12+
logging.info("""light.py - Print readings from the LTR559 Light & Proximity sensor.
713
814
Press Ctrl+C to exit!
915
@@ -13,7 +19,7 @@
1319
while True:
1420
lux = ltr559.get_lux()
1521
prox = ltr559.get_proximity()
16-
print("""Light: {:05.02f} Lux
22+
logging.info("""Light: {:05.02f} Lux
1723
Proximity: {:05.02f}
1824
""".format(lux, prox))
1925
time.sleep(1.0)

examples/luftdaten.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def read_values():
7474
def get_cpu_temperature():
7575
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE, universal_newlines=True)
7676
output, _error = process.communicate()
77+
output = output.decode()
7778
return float(output[output.index('=') + 1:output.rindex("'")])
7879

7980

0 commit comments

Comments
 (0)