質問編集履歴

1

一部修正

2020/08/03 08:06

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -40,7 +40,7 @@
40
40
 
41
41
 
42
42
 
43
- ```python3
43
+ ```python
44
44
 
45
45
  #!/usr/bin/python3
46
46
 
@@ -56,394 +56,4 @@
56
56
 
57
57
 
58
58
 
59
- #I2C Address設定
60
-
61
- bus_number = 1
62
-
63
- i2c_address = 0x76
64
-
65
- bus = SMBus(bus_number)
66
-
67
-
68
-
69
- #データ格納変数定義
70
-
71
- MAX_DATA=50
72
-
73
- #データ格納変数定義
74
-
75
- digT = []
76
-
77
- digP = []
78
-
79
- digH = []
80
-
81
- t_fine = 0.0
82
-
83
-
84
-
85
- date=datetime.datetime.today()
86
-
87
-
88
-
89
- #コンストラクタ定義
90
-
91
- #config, ctrl_meas, ctrl_muレジスタ設定
92
-
93
- def init():
94
-
95
- #レジスタ書き込み値
96
-
97
- osrs_t = 1 #Temperature oversampling x 1
98
-
99
- osrs_p = 1 #Pressure oversampling x 1
100
-
101
- osrs_h = 1 #Humidity oversampling x 1
102
-
103
- mode = 3 #Normal mode
104
-
105
- t_sb = 5 #Tstandby 1000ms
106
-
107
- filter = 0 #Filter off
108
-
109
- spi3w_en = 0 #3-wire SPI Disable
110
-
111
- #レジスタ設定
112
-
113
- ctrl_meas_reg = (osrs_t << 5) | (osrs_p << 2) | mode
114
-
115
- config_reg = (t_sb << 5) | (filter << 2) | spi3w_en
116
-
117
- ctrl_hum_reg = osrs_h
118
-
119
- #レジスタアドレス指定
120
-
121
- writeReg(0xF2,ctrl_hum_reg)
122
-
123
- writeReg(0xF4,ctrl_meas_reg)
124
-
125
- writeReg(0xF5,config_reg)
126
-
127
-
128
-
129
- #Data(レジスタ設定値)書き込み
130
-
131
- def writeReg(reg_address, data):
132
-
133
- bus.write_byte_data(i2c_address,reg_address,data)
134
-
135
-
136
-
137
- #Calibration (補正データ取得)
138
-
139
- def get_calib_param():
140
-
141
- calib = []
142
-
143
-
144
-
145
- for i in range (0x88,0x88+24):
146
-
147
- calib.append(bus.read_byte_data(i2c_address,i))
148
-
149
- calib.append(bus.read_byte_data(i2c_address,0xA1))
150
-
151
- for i in range (0xE1,0xE1+7):
152
-
153
- calib.append(bus.read_byte_data(i2c_address,i))
154
-
155
-
156
-
157
- digT.append((calib[1] << 8) | calib[0])
158
-
159
- digT.append((calib[3] << 8) | calib[2])
160
-
161
- digT.append((calib[5] << 8) | calib[4])
162
-
163
- digP.append((calib[7] << 8) | calib[6])
164
-
165
- digP.append((calib[9] << 8) | calib[8])
166
-
167
- digP.append((calib[11]<< 8) | calib[10])
168
-
169
- digP.append((calib[13]<< 8) | calib[12])
170
-
171
- digP.append((calib[15]<< 8) | calib[14])
172
-
173
- digP.append((calib[17]<< 8) | calib[16])
174
-
175
- digP.append((calib[19]<< 8) | calib[18])
176
-
177
- digP.append((calib[21]<< 8) | calib[20])
178
-
179
- digP.append((calib[23]<< 8) | calib[22])
180
-
181
- digH.append( calib[24] )
182
-
183
- digH.append((calib[26]<< 8) | calib[25])
184
-
185
- digH.append( calib[27] )
186
-
187
- digH.append((calib[28]<< 4) | (0x0F & calib[29]))
188
-
189
- digH.append((calib[30]<< 4) | ((calib[29] >> 4) & 0x0F))
190
-
191
- digH.append( calib[31] )
192
-
193
-
194
-
195
- for i in range(1,2):
196
-
197
- if digT[i] & 0x8000:
198
-
199
- digT[i] = (-digT[i] ^ 0xFFFF) + 1
200
-
201
-
202
-
203
- for i in range(1,8):
204
-
205
- if digP[i] & 0x8000:
206
-
207
- digP[i] = (-digP[i] ^ 0xFFFF) + 1
208
-
209
-
210
-
211
- for i in range(0,6):
212
-
213
- if digH[i] & 0x8000:
214
-
215
- digH[i] = (-digH[i] ^ 0xFFFF) + 1
216
-
217
-
218
-
219
- #温度・湿度・気圧データ格納レジスタRead
220
-
221
- def readData():
222
-
223
- data = []
224
-
225
- for i in range (0xF7, 0xF7+8):
226
-
227
- data.append(bus.read_byte_data(i2c_address,i))
228
-
229
- pres_raw = (data[0] << 12) | (data[1] << 4) | (data[2] >> 4)
230
-
231
- temp_raw = (data[3] << 12) | (data[4] << 4) | (data[5] >> 4)
232
-
233
- hum_raw = (data[6] << 8) | data[7]
234
-
235
-
236
-
237
- compensate_T(temp_raw)
238
-
239
- compensate_P(pres_raw)
240
-
241
- compensate_H(hum_raw)
242
-
243
-
244
-
245
- #温度データtemperature算出
246
-
247
- def compensate_T(adc_T):
248
-
249
- global t_fine
250
-
251
- ary_temp=["0,0,0,0,0,0\n" for i in range(MAX_DATA)]
252
-
253
- v1 = (adc_T / 16384.0 - digT[0] / 1024.0) * digT[1]
254
-
255
- v2 = (adc_T / 131072.0 - digT[0] / 8192.0) * (adc_T / 131072.0 - digT[0] / 8192.0) * digT[2]
256
-
257
- t_fine = v1 + v2
258
-
259
- temperature = t_fine / 5120.0
260
-
261
- print("Raspi_temp : %-6.4f degree" % (temperature))
262
-
263
-
264
-
265
- fp=open("/home/pi/dev/data/Raspi_temp.csv","rb")
266
-
267
- line=fp.readline()
268
-
269
- while line:
270
-
271
- ary_temp.pop(0)
272
-
273
- ary_temp.append(line)
274
-
275
- line=fp.readline()
276
-
277
- ary_temp.pop(0)
278
-
279
- ary_temp.append(str(date.year)+","+str(date.month)+","+str(date.day)+","+str(date.hour)+","+str(date.minute)+","+str(temperature)+"\n")
280
-
281
- fp.close()
282
-
283
-
284
-
285
- fp=open("/home/pi/dev/data/Raspi_temp.csv","w")
286
-
287
- fp.writelines(ary_temp)
288
-
289
- fp.close()
290
-
291
-
292
-
293
- #湿度データhum算出
294
-
295
- def compensate_H(adc_H):
296
-
297
- global t_fine
298
-
299
- ary_hum=["0,0,0,0,0,0\n" for i in range(MAX_DATA)]
300
-
301
- hum = t_fine - 76800.0
302
-
303
- if hum != 0:
304
-
305
- hum = (adc_H - (digH[3] * 64.0 + digH[4]/16384.0 * hum)) * (digH[1] / 65536.0 * (1.0 + digH[5] / 67108864.0 * hum * (1.0 + digH[2] / 67108864.0 * hum)))
306
-
307
- else:
308
-
309
- return 0
310
-
311
- hum = hum * (1.0 - digH[0] * hum / 524288.0)
312
-
313
- if hum > 100.0:
314
-
315
- hum = 100.0
316
-
317
- elif hum < 0.0:
318
-
319
- hum = 0.0
320
-
321
- print("Raspi_hum : %6.2f %%" % (hum))
322
-
323
-
324
-
325
- fp=open("/home/pi/dev/data/Raspi_hum.csv","rb")
326
-
327
- line=fp.readline()
328
-
329
- while line:
330
-
331
- ary_hum.pop(0)
332
-
333
- ary_hum.append(line)
334
-
335
- line=fp.readline()
336
-
337
- ary_hum.pop(0)
338
-
339
- ary_hum.append(str(date.year)+","+str(date.month)+","+str(date.day)+","+str(date.hour)+","+str(date.minute)+","+str(hum)+"\n")
340
-
341
- fp.close()
342
-
343
-
344
-
345
- fp=open("/home/pi/dev/data/Raspi_hum.csv","w")
346
-
347
- fp.writelines(ary_hum)
348
-
349
- fp.close()
350
-
351
-
352
-
353
- #Raspi_pressure
354
-
355
- def compensate_P(adc_P):
356
-
357
- global t_fine
358
-
359
- pressure = 0.0
360
-
361
- ary_pressure=["0,0,0,0,0,0\n" for i in range(MAX_DATA)]
362
-
363
-
364
-
365
- v1 = (t_fine / 2.0) - 64000.0
366
-
367
- v2 = (((v1 / 4.0) * (v1 / 4.0)) / 2048) * digP[5]
368
-
369
- v2 = v2 + ((v1 * digP[4]) * 2.0)
370
-
371
- v2 = (v2 / 4.0) + (digP[3] * 65536.0)
372
-
373
- v1 = (((digP[2] * (((v1 / 4.0) * (v1 / 4.0)) / 8192)) / 8) + ((digP[1] * v1) / 2.0)) / 262144
374
-
375
- v1 = ((32768 + v1) * digP[0]) / 32768
376
-
377
-
378
-
379
- if v1 == 0:
380
-
381
- return 0
382
-
383
- pressure = ((1048576 - adc_P) - (v2 / 4096)) * 3125
384
-
385
- if pressure < 0x80000000:
386
-
387
- pressure = (pressure * 2.0) / v1
388
-
389
- else:
390
-
391
- pressure = (pressure / v1) * 2
392
-
393
- v1 = (digP[8] * (((pressure / 8.0) * (pressure / 8.0)) / 8192.0)) / 4096
394
-
395
- v2 = ((pressure / 4.0) * digP[7]) / 8192.0
396
-
397
- pressure = pressure + ((v1 + v2 + digP[6]) / 16.0)
398
-
399
-
400
-
401
- print("Raspi_pressure : %7.2f hPa" % (pressure/100))
402
-
403
-
404
-
405
- fp=open("/home/pi/dev/data/Raspi_pressure.csv","rb")
406
-
407
- line=fp.readline()
408
-
409
- while line:
410
-
411
- ary_pressure.pop(0)
412
-
413
- ary_pressure.append(line)
414
-
415
- line=fp.readline()
416
-
417
- ary_pressure.pop(0)
418
-
419
- ary_pressure.append(str(date.year)+","+str(date.month)+","+str(date.day)+","+str(date.hour)+","+str(date.minute)+","+str(pressure/100)+"\n")
420
-
421
- fp.close()
422
-
423
-
424
-
425
- fp=open("/home/pi/dev/data/Raspi_pressure.csv","w")
426
-
427
- fp.writelines(ary_pressure)
428
-
429
- fp.close()
430
-
431
-
432
-
433
- init()
434
-
435
- get_calib_param()
436
-
437
-
438
-
439
- if __name__ == '__main__':
440
-
441
- try:
442
-
443
- readData()
444
-
445
- except KeyboardInterrupt:
446
-
447
- pass
448
-
449
59
  ```