質問編集履歴

5

プログラムの修正

2021/07/31 03:30

投稿

jake0228
jake0228

スコア16

test CHANGED
File without changes
test CHANGED
@@ -4,11 +4,7 @@
4
4
 
5
5
  ```python3
6
6
 
7
- コード
8
-
9
- ```python
10
-
11
- !/usr/bin/env python3
7
+ #!/usr/bin/env python3
12
8
 
13
9
 
14
10
 

4

プログラムの修正

2021/07/31 03:30

投稿

jake0228
jake0228

スコア16

test CHANGED
File without changes
test CHANGED
@@ -5,6 +5,8 @@
5
5
  ```python3
6
6
 
7
7
  コード
8
+
9
+ ```python
8
10
 
9
11
  !/usr/bin/env python3
10
12
 
@@ -75,3 +77,5 @@
75
77
  """.format(temperature, pressure, humidity))
76
78
 
77
79
  time.sleep(1)
80
+
81
+ ```

3

プログラムの修正

2021/07/31 03:18

投稿

jake0228
jake0228

スコア16

test CHANGED
File without changes
test CHANGED
@@ -12,41 +12,19 @@
12
12
 
13
13
  import time
14
14
 
15
- import colorsys
15
+ from bme280 import BME280
16
16
 
17
- import sys
18
17
 
19
- import ST7735
20
18
 
21
19
  try:
22
20
 
23
- Transitional fix for breaking change in LTR559
24
-
25
- from ltr559 import LTR559
21
+ from smbus2 import SMBus
26
-
27
- ltr559 = LTR559()
28
22
 
29
23
  except ImportError:
30
24
 
31
- import ltr559
25
+ from smbus import SMBus
32
26
 
33
27
 
34
-
35
- from bme280 import BME280
36
-
37
- from pms5003 import PMS5003, ReadTimeoutError as pmsReadTimeoutError, SerialTimeoutError
38
-
39
- from enviroplus import gas
40
-
41
- from subprocess import PIPE, Popen
42
-
43
- from PIL import Image
44
-
45
- from PIL import ImageDraw
46
-
47
- from PIL import ImageFont
48
-
49
- from fonts.ttf import RobotoMedium as UserFont
50
28
 
51
29
  import logging
52
30
 
@@ -62,7 +40,7 @@
62
40
 
63
41
 
64
42
 
65
- logging.info("""combined.py - Displays readings from all of Enviro plus' sensors
43
+ logging.info("""weather.py - Print readings from the BME280 weather sensor.
66
44
 
67
45
 
68
46
 
@@ -72,520 +50,28 @@
72
50
 
73
51
  """)
74
52
 
75
- BME280 temperature/pressure/humidity sensor
76
53
 
54
+
55
+ bus = SMBus(1)
56
+
77
- bme280 = BME280()
57
+ bme280 = BME280(i2c_dev=bus)
78
58
 
79
59
 
80
60
 
81
- PMS5003 particulate sensor
61
+ while True:
82
62
 
83
- pms5003 = PMS5003()
63
+ temperature = bme280.get_temperature()
84
64
 
85
- time.sleep(1.0)
65
+ pressure = bme280.get_pressure()
86
66
 
67
+ humidity = bme280.get_humidity()
87
68
 
69
+ logging.info("""Temperature: {:05.2f} *C
88
70
 
89
- Create ST7735 LCD display class
71
+ Pressure: {:05.2f} hPa
90
72
 
91
- st7735 = ST7735.ST7735(
73
+ Relative humidity: {:05.2f} %
92
74
 
93
- port=0,
75
+ """.format(temperature, pressure, humidity))
94
76
 
95
- cs=1,
96
-
97
- dc=9,
98
-
99
- backlight=12,
100
-
101
- rotation=270,
102
-
103
- spi_speed_hz=10000000
104
-
105
- )
106
-
107
-
108
-
109
- Initialize display
110
-
111
- st7735.begin()
77
+ time.sleep(1)
112
-
113
-
114
-
115
- WIDTH = st7735.width
116
-
117
- HEIGHT = st7735.height
118
-
119
-
120
-
121
- Set up canvas and font
122
-
123
- img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
124
-
125
- draw = ImageDraw.Draw(img)
126
-
127
- font_size_small = 10
128
-
129
- font_size_large = 20
130
-
131
- font = ImageFont.truetype(UserFont, font_size_large)
132
-
133
- smallfont = ImageFont.truetype(UserFont, font_size_small)
134
-
135
- x_offset = 2
136
-
137
- y_offset = 2
138
-
139
-
140
-
141
- message = ""
142
-
143
-
144
-
145
- The position of the top bar
146
-
147
- top_pos = 25
148
-
149
-
150
-
151
- Create a values dict to store the data
152
-
153
- variables = ["temperature",
154
-
155
- "pressure",
156
-
157
- "humidity",
158
-
159
- "light",
160
-
161
- "oxidised",
162
-
163
- "reduced",
164
-
165
- "nh3",
166
-
167
- "pm1",
168
-
169
- "pm25",
170
-
171
- "pm10"]
172
-
173
-
174
-
175
- units = ["C",
176
-
177
- "hPa",
178
-
179
- "%",
180
-
181
- "Lux",
182
-
183
- "kO",
184
-
185
- "kO",
186
-
187
- "kO",
188
-
189
- "ug/m3",
190
-
191
- "ug/m3",
192
-
193
- "ug/m3"]
194
-
195
-
196
-
197
- Define your own warning limits
198
-
199
- The limits definition follows the order of the variables array
200
-
201
- Example limits explanation for temperature:
202
-
203
- [4,18,28,35] means
204
-
205
- [-273.15 .. 4] -> Dangerously Low
206
-
207
- (4 .. 18] -> Low
208
-
209
- (18 .. 28] -> Normal
210
-
211
- (28 .. 35] -> High
212
-
213
- (35 .. MAX] -> Dangerously High
214
-
215
- DISCLAIMER: The limits provided here are just examples and come
216
-
217
- with NO WARRANTY. The authors of this example code claim
218
-
219
- NO RESPONSIBILITY if reliance on the following values or this
220
-
221
- code in general leads to ANY DAMAGES or DEATH.
222
-
223
- limits = [[4, 18, 28, 35],
224
-
225
- [250, 650, 1013.25, 1015],
226
-
227
- [20, 30, 60, 70],
228
-
229
- [-1, -1, 30000, 100000],
230
-
231
- [-1, -1, 40, 50],
232
-
233
- [-1, -1, 450, 550],
234
-
235
- [-1, -1, 200, 300],
236
-
237
- [-1, -1, 50, 100],
238
-
239
- [-1, -1, 50, 100],
240
-
241
- [-1, -1, 50, 100]]
242
-
243
-
244
-
245
- RGB palette for values on the combined screen
246
-
247
- palette = [(0, 0, 255), # Dangerously Low
248
-
249
- (0, 255, 255), # Low
250
-
251
- (0, 255, 0), # Normal
252
-
253
- (255, 255, 0), # High
254
-
255
- (255, 0, 0)] # Dangerously High
256
-
257
-
258
-
259
- values = {}
260
-
261
-
262
-
263
-
264
-
265
-
266
-
267
-
268
-
269
- Get the temperature of the CPU for compensation
270
-
271
- def get_cpu_temperature():
272
-
273
- process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE, universal_newlines=True)
274
-
275
- output, _error = process.communicate()
276
-
277
- return float(output[output.index('=') + 1:output.rindex("'")])
278
-
279
-
280
-
281
-
282
-
283
- def main():
284
-
285
- Tuning factor for compensation. Decrease this number to adjust the
286
-
287
- temperature down, and increase to adjust up
288
-
289
- factor = 2.25
290
-
291
-
292
-
293
- cpu_temps = [get_cpu_temperature()] * 5
294
-
295
-
296
-
297
- delay = 0.5 # Debounce the proximity tap
298
-
299
- mode = 10 # The starting mode
300
-
301
- last_page = 0
302
-
303
-
304
-
305
- for v in variables:
306
-
307
- values[v] = [1] * WIDTH
308
-
309
-
310
-
311
- The main loop
312
-
313
- try:
314
-
315
- while True:
316
-
317
- proximity = ltr559.get_proximity()
318
-
319
-
320
-
321
- If the proximity crosses the threshold, toggle the mode
322
-
323
- if proximity > 1500 and time.time() - last_page > delay:
324
-
325
- mode += 1
326
-
327
- mode %= (len(variables) + 1)
328
-
329
- last_page = time.time()
330
-
331
-
332
-
333
- One mode for each variable
334
-
335
- if mode == 0:
336
-
337
- variable = "temperature"
338
-
339
- unit = "C"
340
-
341
- cpu_temp = get_cpu_temperature()
342
-
343
- Smooth out with some averaging to decrease jitter
344
-
345
- cpu_temps = cpu_temps[1:] + [cpu_temp]
346
-
347
- avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
348
-
349
- raw_temp = bme280.get_temperature()
350
-
351
- data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
352
-
353
- display_text(variables[mode], data, unit)
354
-
355
-
356
-
357
- if mode == 1:
358
-
359
- variable = "pressure"
360
-
361
- unit = "hPa"
362
-
363
- data = bme280.get_pressure()
364
-
365
- display_text(variables[mode], data, unit)
366
-
367
-
368
-
369
- if mode == 2:
370
-
371
- variable = "humidity"
372
-
373
- unit = "%"
374
-
375
- data = bme280.get_humidity()
376
-
377
- display_text(variables[mode], data, unit)
378
-
379
-
380
-
381
- if mode == 3:
382
-
383
- variable = "light"
384
-
385
- unit = "Lux"
386
-
387
- if proximity < 10:
388
-
389
- data = ltr559.get_lux()
390
-
391
- else:
392
-
393
- data = 1
394
-
395
- display_text(variables[mode], data, unit)
396
-
397
-
398
-
399
- if mode == 4:
400
-
401
- variable = "oxidised"
402
-
403
- unit = "kO"
404
-
405
- data = gas.read_all()
406
-
407
- data = data.oxidising / 1000
408
-
409
- display_text(variables[mode], data, unit)
410
-
411
-
412
-
413
- if mode == 5:
414
-
415
- variable = "reduced"
416
-
417
- unit = "kO"
418
-
419
- data = gas.read_all()
420
-
421
- data = data.reducing / 1000
422
-
423
- display_text(variables[mode], data, unit)
424
-
425
-
426
-
427
- if mode == 6:
428
-
429
- variable = "nh3"
430
-
431
- unit = "kO"
432
-
433
- data = gas.read_all()
434
-
435
- data = data.nh3 / 1000
436
-
437
- display_text(variables[mode], data, unit)
438
-
439
-
440
-
441
- if mode == 7:
442
-
443
- variable = "pm1"
444
-
445
- unit = "ug/m3"
446
-
447
- try:
448
-
449
- data = pms5003.read()
450
-
451
- except pmsReadTimeoutError:
452
-
453
- logging.warning("Failed to read PMS5003")
454
-
455
- else:
456
-
457
- data = float(data.pm_ug_per_m3(1.0))
458
-
459
- display_text(variables[mode], data, unit)
460
-
461
-
462
-
463
- if mode == 8:
464
-
465
- variable = "pm25"
466
-
467
- unit = "ug/m3"
468
-
469
- try:
470
-
471
- data = pms5003.read()
472
-
473
- except pmsReadTimeoutError:
474
-
475
- logging.warning("Failed to read PMS5003")
476
-
477
- else:
478
-
479
- data = float(data.pm_ug_per_m3(2.5))
480
-
481
- display_text(variables[mode], data, unit)
482
-
483
-
484
-
485
- if mode == 9:
486
-
487
- variable = "pm10"
488
-
489
- unit = "ug/m3"
490
-
491
- try:
492
-
493
- data = pms5003.read()
494
-
495
- except pmsReadTimeoutError:
496
-
497
- logging.warning("Failed to read PMS5003")
498
-
499
- else:
500
-
501
- data = float(data.pm_ug_per_m3(10))
502
-
503
- display_text(variables[mode], data, unit)
504
-
505
- if mode == 10:
506
-
507
- Everything on one screen
508
-
509
- cpu_temp = get_cpu_temperature()
510
-
511
- Smooth out with some averaging to decrease jitter
512
-
513
- cpu_temps = cpu_temps[1:] + [cpu_temp]
514
-
515
- avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
516
-
517
- raw_temp = bme280.get_temperature()
518
-
519
- raw_data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
520
-
521
- save_data(0, raw_data)
522
-
523
- display_everything()
524
-
525
- raw_data = bme280.get_pressure()
526
-
527
- save_data(1, raw_data)
528
-
529
- display_everything()
530
-
531
- raw_data = bme280.get_humidity()
532
-
533
- save_data(2, raw_data)
534
-
535
- if proximity < 10:
536
-
537
- raw_data = ltr559.get_lux()
538
-
539
- else:
540
-
541
- raw_data = 1
542
-
543
- save_data(3, raw_data)
544
-
545
- display_everything()
546
-
547
- gas_data = gas.read_all()
548
-
549
- save_data(4, gas_data.oxidising / 1000)
550
-
551
- save_data(5, gas_data.reducing / 1000)
552
-
553
- save_data(6, gas_data.nh3 / 1000)
554
-
555
- display_everything()
556
-
557
- pms_data = None
558
-
559
- try:
560
-
561
- pms_data = pms5003.read()
562
-
563
- except (SerialTimeoutError, pmsReadTimeoutError):
564
-
565
- logging.warning("Failed to read PMS5003")
566
-
567
- else:
568
-
569
- save_data(7, float(pms_data.pm_ug_per_m3(1.0)))
570
-
571
- save_data(8, float(pms_data.pm_ug_per_m3(2.5)))
572
-
573
- save_data(9, float(pms_data.pm_ug_per_m3(10)))
574
-
575
- display_everything()
576
-
577
-
578
-
579
- Exit cleanly
580
-
581
- except KeyboardInterrupt:
582
-
583
- sys.exit(0)
584
-
585
-
586
-
587
-
588
-
589
- if __name__ == "__main__":
590
-
591
- main()

2

プログラムの修正

2021/07/31 00:38

投稿

jake0228
jake0228

スコア16

test CHANGED
File without changes
test CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  実行結果のファイルへの記録の方法を教えていただけますでしょうか。どうぞ宜しくお願い致します。
4
4
 
5
+ ```python3
6
+
7
+ コード
8
+
5
9
  !/usr/bin/env python3
6
10
 
7
11
 
@@ -16,7 +20,7 @@
16
20
 
17
21
  try:
18
22
 
19
- # Transitional fix for breaking change in LTR559
23
+ Transitional fix for breaking change in LTR559
20
24
 
21
25
  from ltr559 import LTR559
22
26
 
@@ -68,15 +72,13 @@
68
72
 
69
73
  """)
70
74
 
71
-
72
-
73
- # BME280 temperature/pressure/humidity sensor
75
+ BME280 temperature/pressure/humidity sensor
74
76
 
75
77
  bme280 = BME280()
76
78
 
77
79
 
78
80
 
79
- # PMS5003 particulate sensor
81
+ PMS5003 particulate sensor
80
82
 
81
83
  pms5003 = PMS5003()
82
84
 
@@ -84,71 +86,187 @@
84
86
 
85
87
 
86
88
 
87
-
88
-
89
- # Saves the data to be used in the graphs later and prints to the log
90
-
91
- def save_data(idx, data):
92
-
93
- variable = variables[idx]
94
-
95
- # Maintain length of list
96
-
97
- values[variable] = values[variable][1:] + [data]
98
-
99
- unit = units[idx]
100
-
101
- message = "{}: {:.1f} {}".format(variable[:4], data, unit)
102
-
103
- logging.info(message)
104
-
105
-
106
-
107
-
108
-
109
- # Displays all the text on the 0.96" LCD
110
-
111
- def display_everything():
112
-
113
- draw.rectangle((0, 0, WIDTH, HEIGHT), (0, 0, 0))
114
-
115
- column_count = 2
116
-
117
- row_count = (len(variables) / column_count)
118
-
119
- for i in range(len(variables)):
120
-
121
- variable = variables[i]
122
-
123
- data_value = values[variable][-1]
124
-
125
- unit = units[i]
126
-
127
- x = x_offset + ((WIDTH // column_count) * (i // row_count))
128
-
129
- y = y_offset + ((HEIGHT / row_count) * (i % row_count))
130
-
131
- message = "{}: {:.1f} {}".format(variable[:4], data_value, unit)
132
-
133
- lim = limits[i]
134
-
135
- rgb = palette[0]
136
-
137
- for j in range(len(lim)):
138
-
139
- if data_value > lim[j]:
140
-
141
- rgb = palette[j + 1]
142
-
143
- draw.text((x, y), message, font=smallfont, fill=rgb)
144
-
145
- st7735.display(img)
146
-
147
-
148
-
149
-
150
-
151
- # Get the temperature of the CPU for compensation
89
+ Create ST7735 LCD display class
90
+
91
+ st7735 = ST7735.ST7735(
92
+
93
+ port=0,
94
+
95
+ cs=1,
96
+
97
+ dc=9,
98
+
99
+ backlight=12,
100
+
101
+ rotation=270,
102
+
103
+ spi_speed_hz=10000000
104
+
105
+ )
106
+
107
+
108
+
109
+ Initialize display
110
+
111
+ st7735.begin()
112
+
113
+
114
+
115
+ WIDTH = st7735.width
116
+
117
+ HEIGHT = st7735.height
118
+
119
+
120
+
121
+ Set up canvas and font
122
+
123
+ img = Image.new('RGB', (WIDTH, HEIGHT), color=(0, 0, 0))
124
+
125
+ draw = ImageDraw.Draw(img)
126
+
127
+ font_size_small = 10
128
+
129
+ font_size_large = 20
130
+
131
+ font = ImageFont.truetype(UserFont, font_size_large)
132
+
133
+ smallfont = ImageFont.truetype(UserFont, font_size_small)
134
+
135
+ x_offset = 2
136
+
137
+ y_offset = 2
138
+
139
+
140
+
141
+ message = ""
142
+
143
+
144
+
145
+ The position of the top bar
146
+
147
+ top_pos = 25
148
+
149
+
150
+
151
+ Create a values dict to store the data
152
+
153
+ variables = ["temperature",
154
+
155
+ "pressure",
156
+
157
+ "humidity",
158
+
159
+ "light",
160
+
161
+ "oxidised",
162
+
163
+ "reduced",
164
+
165
+ "nh3",
166
+
167
+ "pm1",
168
+
169
+ "pm25",
170
+
171
+ "pm10"]
172
+
173
+
174
+
175
+ units = ["C",
176
+
177
+ "hPa",
178
+
179
+ "%",
180
+
181
+ "Lux",
182
+
183
+ "kO",
184
+
185
+ "kO",
186
+
187
+ "kO",
188
+
189
+ "ug/m3",
190
+
191
+ "ug/m3",
192
+
193
+ "ug/m3"]
194
+
195
+
196
+
197
+ Define your own warning limits
198
+
199
+ The limits definition follows the order of the variables array
200
+
201
+ Example limits explanation for temperature:
202
+
203
+ [4,18,28,35] means
204
+
205
+ [-273.15 .. 4] -> Dangerously Low
206
+
207
+ (4 .. 18] -> Low
208
+
209
+ (18 .. 28] -> Normal
210
+
211
+ (28 .. 35] -> High
212
+
213
+ (35 .. MAX] -> Dangerously High
214
+
215
+ DISCLAIMER: The limits provided here are just examples and come
216
+
217
+ with NO WARRANTY. The authors of this example code claim
218
+
219
+ NO RESPONSIBILITY if reliance on the following values or this
220
+
221
+ code in general leads to ANY DAMAGES or DEATH.
222
+
223
+ limits = [[4, 18, 28, 35],
224
+
225
+ [250, 650, 1013.25, 1015],
226
+
227
+ [20, 30, 60, 70],
228
+
229
+ [-1, -1, 30000, 100000],
230
+
231
+ [-1, -1, 40, 50],
232
+
233
+ [-1, -1, 450, 550],
234
+
235
+ [-1, -1, 200, 300],
236
+
237
+ [-1, -1, 50, 100],
238
+
239
+ [-1, -1, 50, 100],
240
+
241
+ [-1, -1, 50, 100]]
242
+
243
+
244
+
245
+ RGB palette for values on the combined screen
246
+
247
+ palette = [(0, 0, 255), # Dangerously Low
248
+
249
+ (0, 255, 255), # Low
250
+
251
+ (0, 255, 0), # Normal
252
+
253
+ (255, 255, 0), # High
254
+
255
+ (255, 0, 0)] # Dangerously High
256
+
257
+
258
+
259
+ values = {}
260
+
261
+
262
+
263
+
264
+
265
+
266
+
267
+
268
+
269
+ Get the temperature of the CPU for compensation
152
270
 
153
271
  def get_cpu_temperature():
154
272
 
@@ -164,9 +282,9 @@
164
282
 
165
283
  def main():
166
284
 
167
- # Tuning factor for compensation. Decrease this number to adjust the
285
+ Tuning factor for compensation. Decrease this number to adjust the
168
-
286
+
169
- # temperature down, and increase to adjust up
287
+ temperature down, and increase to adjust up
170
288
 
171
289
  factor = 2.25
172
290
 
@@ -190,7 +308,7 @@
190
308
 
191
309
 
192
310
 
193
- # The main loop
311
+ The main loop
194
312
 
195
313
  try:
196
314
 
@@ -200,7 +318,7 @@
200
318
 
201
319
 
202
320
 
203
- # If the proximity crosses the threshold, toggle the mode
321
+ If the proximity crosses the threshold, toggle the mode
204
322
 
205
323
  if proximity > 1500 and time.time() - last_page > delay:
206
324
 
@@ -212,17 +330,17 @@
212
330
 
213
331
 
214
332
 
215
- # One mode for each variable
333
+ One mode for each variable
216
334
 
217
335
  if mode == 0:
218
336
 
219
- # variable = "temperature"
337
+ variable = "temperature"
220
338
 
221
339
  unit = "C"
222
340
 
223
341
  cpu_temp = get_cpu_temperature()
224
342
 
225
- # Smooth out with some averaging to decrease jitter
343
+ Smooth out with some averaging to decrease jitter
226
344
 
227
345
  cpu_temps = cpu_temps[1:] + [cpu_temp]
228
346
 
@@ -238,7 +356,7 @@
238
356
 
239
357
  if mode == 1:
240
358
 
241
- # variable = "pressure"
359
+ variable = "pressure"
242
360
 
243
361
  unit = "hPa"
244
362
 
@@ -250,7 +368,7 @@
250
368
 
251
369
  if mode == 2:
252
370
 
253
- # variable = "humidity"
371
+ variable = "humidity"
254
372
 
255
373
  unit = "%"
256
374
 
@@ -262,7 +380,7 @@
262
380
 
263
381
  if mode == 3:
264
382
 
265
- # variable = "light"
383
+ variable = "light"
266
384
 
267
385
  unit = "Lux"
268
386
 
@@ -280,7 +398,7 @@
280
398
 
281
399
  if mode == 4:
282
400
 
283
- # variable = "oxidised"
401
+ variable = "oxidised"
284
402
 
285
403
  unit = "kO"
286
404
 
@@ -294,7 +412,7 @@
294
412
 
295
413
  if mode == 5:
296
414
 
297
- # variable = "reduced"
415
+ variable = "reduced"
298
416
 
299
417
  unit = "kO"
300
418
 
@@ -308,7 +426,7 @@
308
426
 
309
427
  if mode == 6:
310
428
 
311
- # variable = "nh3"
429
+ variable = "nh3"
312
430
 
313
431
  unit = "kO"
314
432
 
@@ -322,7 +440,7 @@
322
440
 
323
441
  if mode == 7:
324
442
 
325
- # variable = "pm1"
443
+ variable = "pm1"
326
444
 
327
445
  unit = "ug/m3"
328
446
 
@@ -344,7 +462,7 @@
344
462
 
345
463
  if mode == 8:
346
464
 
347
- # variable = "pm25"
465
+ variable = "pm25"
348
466
 
349
467
  unit = "ug/m3"
350
468
 
@@ -366,7 +484,7 @@
366
484
 
367
485
  if mode == 9:
368
486
 
369
- # variable = "pm10"
487
+ variable = "pm10"
370
488
 
371
489
  unit = "ug/m3"
372
490
 
@@ -386,11 +504,11 @@
386
504
 
387
505
  if mode == 10:
388
506
 
389
- # Everything on one screen
507
+ Everything on one screen
390
508
 
391
509
  cpu_temp = get_cpu_temperature()
392
510
 
393
- # Smooth out with some averaging to decrease jitter
511
+ Smooth out with some averaging to decrease jitter
394
512
 
395
513
  cpu_temps = cpu_temps[1:] + [cpu_temp]
396
514
 
@@ -458,7 +576,7 @@
458
576
 
459
577
 
460
578
 
461
- # Exit cleanly
579
+ Exit cleanly
462
580
 
463
581
  except KeyboardInterrupt:
464
582
 

1

プログラムの記入

2021/07/30 23:08

投稿

jake0228
jake0228

スコア16

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,473 @@
1
1
  raspberry pi で環境センサーを使っています。enviro+というセンサーを利用して以下のプログラムを実行します。その際、24時間のデータを収集したいと思っております。すべてのデータを自動でファイルに出力させることができればと願っています。ターミナル上では10分ほどしか記録が残らないため、ターミナルからコピーすることができません。
2
2
 
3
3
  実行結果のファイルへの記録の方法を教えていただけますでしょうか。どうぞ宜しくお願い致します。
4
+
5
+ !/usr/bin/env python3
6
+
7
+
8
+
9
+ import time
10
+
11
+ import colorsys
12
+
13
+ import sys
14
+
15
+ import ST7735
16
+
17
+ try:
18
+
19
+ # Transitional fix for breaking change in LTR559
20
+
21
+ from ltr559 import LTR559
22
+
23
+ ltr559 = LTR559()
24
+
25
+ except ImportError:
26
+
27
+ import ltr559
28
+
29
+
30
+
31
+ from bme280 import BME280
32
+
33
+ from pms5003 import PMS5003, ReadTimeoutError as pmsReadTimeoutError, SerialTimeoutError
34
+
35
+ from enviroplus import gas
36
+
37
+ from subprocess import PIPE, Popen
38
+
39
+ from PIL import Image
40
+
41
+ from PIL import ImageDraw
42
+
43
+ from PIL import ImageFont
44
+
45
+ from fonts.ttf import RobotoMedium as UserFont
46
+
47
+ import logging
48
+
49
+
50
+
51
+ logging.basicConfig(
52
+
53
+ format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s',
54
+
55
+ level=logging.INFO,
56
+
57
+ datefmt='%Y-%m-%d %H:%M:%S')
58
+
59
+
60
+
61
+ logging.info("""combined.py - Displays readings from all of Enviro plus' sensors
62
+
63
+
64
+
65
+ Press Ctrl+C to exit!
66
+
67
+
68
+
69
+ """)
70
+
71
+
72
+
73
+ # BME280 temperature/pressure/humidity sensor
74
+
75
+ bme280 = BME280()
76
+
77
+
78
+
79
+ # PMS5003 particulate sensor
80
+
81
+ pms5003 = PMS5003()
82
+
83
+ time.sleep(1.0)
84
+
85
+
86
+
87
+
88
+
89
+ # Saves the data to be used in the graphs later and prints to the log
90
+
91
+ def save_data(idx, data):
92
+
93
+ variable = variables[idx]
94
+
95
+ # Maintain length of list
96
+
97
+ values[variable] = values[variable][1:] + [data]
98
+
99
+ unit = units[idx]
100
+
101
+ message = "{}: {:.1f} {}".format(variable[:4], data, unit)
102
+
103
+ logging.info(message)
104
+
105
+
106
+
107
+
108
+
109
+ # Displays all the text on the 0.96" LCD
110
+
111
+ def display_everything():
112
+
113
+ draw.rectangle((0, 0, WIDTH, HEIGHT), (0, 0, 0))
114
+
115
+ column_count = 2
116
+
117
+ row_count = (len(variables) / column_count)
118
+
119
+ for i in range(len(variables)):
120
+
121
+ variable = variables[i]
122
+
123
+ data_value = values[variable][-1]
124
+
125
+ unit = units[i]
126
+
127
+ x = x_offset + ((WIDTH // column_count) * (i // row_count))
128
+
129
+ y = y_offset + ((HEIGHT / row_count) * (i % row_count))
130
+
131
+ message = "{}: {:.1f} {}".format(variable[:4], data_value, unit)
132
+
133
+ lim = limits[i]
134
+
135
+ rgb = palette[0]
136
+
137
+ for j in range(len(lim)):
138
+
139
+ if data_value > lim[j]:
140
+
141
+ rgb = palette[j + 1]
142
+
143
+ draw.text((x, y), message, font=smallfont, fill=rgb)
144
+
145
+ st7735.display(img)
146
+
147
+
148
+
149
+
150
+
151
+ # Get the temperature of the CPU for compensation
152
+
153
+ def get_cpu_temperature():
154
+
155
+ process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE, universal_newlines=True)
156
+
157
+ output, _error = process.communicate()
158
+
159
+ return float(output[output.index('=') + 1:output.rindex("'")])
160
+
161
+
162
+
163
+
164
+
165
+ def main():
166
+
167
+ # Tuning factor for compensation. Decrease this number to adjust the
168
+
169
+ # temperature down, and increase to adjust up
170
+
171
+ factor = 2.25
172
+
173
+
174
+
175
+ cpu_temps = [get_cpu_temperature()] * 5
176
+
177
+
178
+
179
+ delay = 0.5 # Debounce the proximity tap
180
+
181
+ mode = 10 # The starting mode
182
+
183
+ last_page = 0
184
+
185
+
186
+
187
+ for v in variables:
188
+
189
+ values[v] = [1] * WIDTH
190
+
191
+
192
+
193
+ # The main loop
194
+
195
+ try:
196
+
197
+ while True:
198
+
199
+ proximity = ltr559.get_proximity()
200
+
201
+
202
+
203
+ # If the proximity crosses the threshold, toggle the mode
204
+
205
+ if proximity > 1500 and time.time() - last_page > delay:
206
+
207
+ mode += 1
208
+
209
+ mode %= (len(variables) + 1)
210
+
211
+ last_page = time.time()
212
+
213
+
214
+
215
+ # One mode for each variable
216
+
217
+ if mode == 0:
218
+
219
+ # variable = "temperature"
220
+
221
+ unit = "C"
222
+
223
+ cpu_temp = get_cpu_temperature()
224
+
225
+ # Smooth out with some averaging to decrease jitter
226
+
227
+ cpu_temps = cpu_temps[1:] + [cpu_temp]
228
+
229
+ avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
230
+
231
+ raw_temp = bme280.get_temperature()
232
+
233
+ data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
234
+
235
+ display_text(variables[mode], data, unit)
236
+
237
+
238
+
239
+ if mode == 1:
240
+
241
+ # variable = "pressure"
242
+
243
+ unit = "hPa"
244
+
245
+ data = bme280.get_pressure()
246
+
247
+ display_text(variables[mode], data, unit)
248
+
249
+
250
+
251
+ if mode == 2:
252
+
253
+ # variable = "humidity"
254
+
255
+ unit = "%"
256
+
257
+ data = bme280.get_humidity()
258
+
259
+ display_text(variables[mode], data, unit)
260
+
261
+
262
+
263
+ if mode == 3:
264
+
265
+ # variable = "light"
266
+
267
+ unit = "Lux"
268
+
269
+ if proximity < 10:
270
+
271
+ data = ltr559.get_lux()
272
+
273
+ else:
274
+
275
+ data = 1
276
+
277
+ display_text(variables[mode], data, unit)
278
+
279
+
280
+
281
+ if mode == 4:
282
+
283
+ # variable = "oxidised"
284
+
285
+ unit = "kO"
286
+
287
+ data = gas.read_all()
288
+
289
+ data = data.oxidising / 1000
290
+
291
+ display_text(variables[mode], data, unit)
292
+
293
+
294
+
295
+ if mode == 5:
296
+
297
+ # variable = "reduced"
298
+
299
+ unit = "kO"
300
+
301
+ data = gas.read_all()
302
+
303
+ data = data.reducing / 1000
304
+
305
+ display_text(variables[mode], data, unit)
306
+
307
+
308
+
309
+ if mode == 6:
310
+
311
+ # variable = "nh3"
312
+
313
+ unit = "kO"
314
+
315
+ data = gas.read_all()
316
+
317
+ data = data.nh3 / 1000
318
+
319
+ display_text(variables[mode], data, unit)
320
+
321
+
322
+
323
+ if mode == 7:
324
+
325
+ # variable = "pm1"
326
+
327
+ unit = "ug/m3"
328
+
329
+ try:
330
+
331
+ data = pms5003.read()
332
+
333
+ except pmsReadTimeoutError:
334
+
335
+ logging.warning("Failed to read PMS5003")
336
+
337
+ else:
338
+
339
+ data = float(data.pm_ug_per_m3(1.0))
340
+
341
+ display_text(variables[mode], data, unit)
342
+
343
+
344
+
345
+ if mode == 8:
346
+
347
+ # variable = "pm25"
348
+
349
+ unit = "ug/m3"
350
+
351
+ try:
352
+
353
+ data = pms5003.read()
354
+
355
+ except pmsReadTimeoutError:
356
+
357
+ logging.warning("Failed to read PMS5003")
358
+
359
+ else:
360
+
361
+ data = float(data.pm_ug_per_m3(2.5))
362
+
363
+ display_text(variables[mode], data, unit)
364
+
365
+
366
+
367
+ if mode == 9:
368
+
369
+ # variable = "pm10"
370
+
371
+ unit = "ug/m3"
372
+
373
+ try:
374
+
375
+ data = pms5003.read()
376
+
377
+ except pmsReadTimeoutError:
378
+
379
+ logging.warning("Failed to read PMS5003")
380
+
381
+ else:
382
+
383
+ data = float(data.pm_ug_per_m3(10))
384
+
385
+ display_text(variables[mode], data, unit)
386
+
387
+ if mode == 10:
388
+
389
+ # Everything on one screen
390
+
391
+ cpu_temp = get_cpu_temperature()
392
+
393
+ # Smooth out with some averaging to decrease jitter
394
+
395
+ cpu_temps = cpu_temps[1:] + [cpu_temp]
396
+
397
+ avg_cpu_temp = sum(cpu_temps) / float(len(cpu_temps))
398
+
399
+ raw_temp = bme280.get_temperature()
400
+
401
+ raw_data = raw_temp - ((avg_cpu_temp - raw_temp) / factor)
402
+
403
+ save_data(0, raw_data)
404
+
405
+ display_everything()
406
+
407
+ raw_data = bme280.get_pressure()
408
+
409
+ save_data(1, raw_data)
410
+
411
+ display_everything()
412
+
413
+ raw_data = bme280.get_humidity()
414
+
415
+ save_data(2, raw_data)
416
+
417
+ if proximity < 10:
418
+
419
+ raw_data = ltr559.get_lux()
420
+
421
+ else:
422
+
423
+ raw_data = 1
424
+
425
+ save_data(3, raw_data)
426
+
427
+ display_everything()
428
+
429
+ gas_data = gas.read_all()
430
+
431
+ save_data(4, gas_data.oxidising / 1000)
432
+
433
+ save_data(5, gas_data.reducing / 1000)
434
+
435
+ save_data(6, gas_data.nh3 / 1000)
436
+
437
+ display_everything()
438
+
439
+ pms_data = None
440
+
441
+ try:
442
+
443
+ pms_data = pms5003.read()
444
+
445
+ except (SerialTimeoutError, pmsReadTimeoutError):
446
+
447
+ logging.warning("Failed to read PMS5003")
448
+
449
+ else:
450
+
451
+ save_data(7, float(pms_data.pm_ug_per_m3(1.0)))
452
+
453
+ save_data(8, float(pms_data.pm_ug_per_m3(2.5)))
454
+
455
+ save_data(9, float(pms_data.pm_ug_per_m3(10)))
456
+
457
+ display_everything()
458
+
459
+
460
+
461
+ # Exit cleanly
462
+
463
+ except KeyboardInterrupt:
464
+
465
+ sys.exit(0)
466
+
467
+
468
+
469
+
470
+
471
+ if __name__ == "__main__":
472
+
473
+ main()