質問編集履歴

3

追記しました

2018/03/28 08:24

投稿

tamasa
tamasa

スコア11

test CHANGED
File without changes
test CHANGED
@@ -64,7 +64,11 @@
64
64
 
65
65
  IOError: [Errno 121] Remote I/O error
66
66
 
67
+
68
+
67
- ```pythn
69
+ ```python
70
+
71
+
68
72
 
69
73
  #!/usr/bin/python
70
74
 
@@ -630,10 +634,6 @@
630
634
 
631
635
  f.close()
632
636
 
633
-
634
-
635
- time.sleep(60)
636
-
637
637
 
638
638
 
639
639
  ```

2

追記しました。

2018/03/28 08:24

投稿

tamasa
tamasa

スコア11

test CHANGED
File without changes
test CHANGED
@@ -353,3 +353,287 @@
353
353
 
354
354
 
355
355
  ```
356
+
357
+
358
+
359
+ <追記>
360
+
361
+ 下記のようにスレッドを削除し、while Trueで処理すると動作しますが、二つ目のwhile Trueが同時に実行されません。
362
+
363
+ ```python
364
+
365
+ #!/usr/bin/python
366
+
367
+ #coding: utf-8
368
+
369
+
370
+
371
+ import smbus
372
+
373
+ import time
374
+
375
+ import datetime
376
+
377
+ import os
378
+
379
+ import threading
380
+
381
+
382
+
383
+ bus = smbus.SMBus(1)
384
+
385
+
386
+
387
+ def Tm():
388
+
389
+ time.sleep(0.2)
390
+
391
+ bus.write_i2c_block_data(0x45, 0x2C, [0x06])
392
+
393
+
394
+
395
+ time.sleep(0.2)
396
+
397
+ data = bus.read_i2c_block_data(0x45, 0x00, 6)
398
+
399
+
400
+
401
+ temp = data[0] * 256 + data[1]
402
+
403
+ cTemp = -45 + (175 * temp / 65535.0)
404
+
405
+
406
+
407
+ print "temp : %-6.2f ℃" % (cTemp)
408
+
409
+ return "%.2f" % (cTemp)
410
+
411
+
412
+
413
+ def Hu():
414
+
415
+ time.sleep(0.2)
416
+
417
+ bus.write_i2c_block_data(0x45, 0x2C, [0x06])
418
+
419
+
420
+
421
+ time.sleep(0.2)
422
+
423
+ data = bus.read_i2c_block_data(0x45, 0x00, 6)
424
+
425
+
426
+
427
+
428
+
429
+ humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
430
+
431
+
432
+
433
+ print "hum : %6.2f %" % (humidity)
434
+
435
+ return "%.2f" % (humidity)
436
+
437
+
438
+
439
+ def readData():
440
+
441
+
442
+
443
+ time.sleep(0.2)
444
+
445
+ t = Tm()
446
+
447
+ time.sleep(0.2)
448
+
449
+ h = Hu()
450
+
451
+
452
+
453
+ print("---")
454
+
455
+
456
+
457
+ return t + "," + h
458
+
459
+
460
+
461
+ #time.sleep(5)
462
+
463
+
464
+
465
+ class i2clcd:
466
+
467
+
468
+
469
+ i2c = smbus.SMBus(1)
470
+
471
+ addr = 0x3e
472
+
473
+ contrast = 40 # 0~63
474
+
475
+
476
+
477
+ def __init__(self):
478
+
479
+ time.sleep(1)
480
+
481
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
482
+
483
+ self.i2c.write_byte_data(self.addr, 0, 0x39) # function set(IS=1)
484
+
485
+ self.i2c.write_byte_data(self.addr, 0, 0x14) # internal osc
486
+
487
+ self.i2c.write_byte_data(self.addr, 0,
488
+
489
+ (0x70 | (self.contrast & 0x0f))) # contrast
490
+
491
+ self.i2c.write_byte_data(self.addr, 0,
492
+
493
+ (0x54 | ((self.contrast >> 4) & 0x03))) # contrast/icon/power
494
+
495
+ self.i2c.write_byte_data(self.addr, 0, 0x6B) # follower control
496
+
497
+ # self.i2c.write_byte_data(self.addr, 0, 0x6c) # follower control
498
+
499
+
500
+
501
+ time.sleep(0.2)
502
+
503
+
504
+
505
+ def clear(self):
506
+
507
+ time.sleep(0.3)
508
+
509
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
510
+
511
+ self.i2c.write_byte_data(self.addr, 0, 0x70) # Constract
512
+
513
+ self.i2c.write_byte_data(self.addr, 0, 0x0C) # Display On
514
+
515
+ self.i2c.write_byte_data(self.addr, 0, 0x01) # Clear Display
516
+
517
+ self.i2c.write_byte_data(self.addr, 0, 0x06) # Entry Mode Set
518
+
519
+
520
+
521
+ time.sleep(0.1)
522
+
523
+
524
+
525
+
526
+
527
+ def puts(self, msg):
528
+
529
+
530
+
531
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
532
+
533
+ [self.i2c.write_byte_data(self.addr, 0x40, ord(c)) for c in msg]
534
+
535
+
536
+
537
+ def setaddress(self, line, col):
538
+
539
+ time.sleep(0.5)
540
+
541
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
542
+
543
+ time.sleep(0.5)
544
+
545
+ self.i2c.write_byte_data(self.addr, 0, 0x80 | (0x40 if line > 0 else 0) | col)
546
+
547
+
548
+
549
+ def setcg(self, no, cg):
550
+
551
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
552
+
553
+ self.i2c.write_byte_data(self.addr, 0, 0x40 | (no << 3))
554
+
555
+ [self.i2c.write_byte_data(self.addr, 0x40, c) for c in cg]
556
+
557
+
558
+
559
+ def putcg(self, line, col, no):
560
+
561
+ self.setaddress(line, col)
562
+
563
+ self.i2c.write_byte_data(self.addr, 0x40, no)
564
+
565
+
566
+
567
+
568
+
569
+
570
+
571
+ while True:
572
+
573
+
574
+
575
+ readData()
576
+
577
+
578
+
579
+ data = readData().split(",")
580
+
581
+ t = data[0]
582
+
583
+ h = data[1]
584
+
585
+
586
+
587
+ lcd = i2clcd()
588
+
589
+ lcd.clear()
590
+
591
+ time.sleep(1)
592
+
593
+
594
+
595
+ lcd.setaddress(0, 0)
596
+
597
+ lcd.puts(str(t).rjust(9)+" 'C")
598
+
599
+ lcd.setaddress(1, 0)
600
+
601
+ lcd.puts(str(h).rjust(9)+" %")
602
+
603
+
604
+
605
+ while True:
606
+
607
+
608
+
609
+ dir_path = '/home/pi/dht11-data'
610
+
611
+
612
+
613
+ now = datetime.datetime.now()
614
+
615
+ filename = now.strftime('%Y%m%d')
616
+
617
+ label = now.strftime('%H:%M')
618
+
619
+ csv = readData()
620
+
621
+
622
+
623
+ if not os.path.exists('/home/pi/dht11-data'):
624
+
625
+ os.makedirs('/home/dht11-data')
626
+
627
+ f = open('/home/pi/dht11-data/'+filename+'.csv','a')
628
+
629
+ f.write("'"+label+"',"+csv+"\n")
630
+
631
+ f.close()
632
+
633
+
634
+
635
+ time.sleep(60)
636
+
637
+
638
+
639
+ ```

1

ソースコードを記載しました

2018/03/28 06:14

投稿

tamasa
tamasa

スコア11

test CHANGED
File without changes
test CHANGED
@@ -1,31 +1,355 @@
1
- Pythonで2つ処理を各々一定時間ごとに処理したくプログラミングしました。ある程度の時間は動きます途中でエラーが出て止まってしいます。
2
-
3
-  表示されるエラー前後にtime.sleep()を入れて処理時間を調整てみましたが、様々な場所からエラーがもぐらたた状態になり
4
-
5
-
6
-
7
- 2つのプログラムは、
8
-
9
-
10
-
11
- 1.5秒ごとに表示を繰り返す(while True関数)。これをdef readData()として定義しています。
12
-
13
-
14
-
15
- 2.1のソースコードをimportして、readData()を読み込み、5分ごとにcrontabで回しています。
16
-
17
-
18
-
19
- 1のみ動作させるとエラーは出ず、実行できます。2と同時に実行するとエラーが出て止まります。
20
-
21
-
22
-
23
- python2.7です。
24
-
25
-
26
-
27
- プログラミングは初心者です。
28
-
29
-
30
-
31
- 宜しくお願い致します。
1
+ ラズベリーパイにセンサーを接続して取得したデータをpythonにて液晶lcdディスプレイに数秒ごとに表示させてかつcsvファイルに数分ごとに保存したいと考えています。cronにてcsvファイルを保存するソースコードを作成、回しましたがエラーが出て何度修正しても止まます。lcdの表示のみでは止まりません。
2
+
3
+  そこでthreading関数で実行したいのですが、下記のソースコードを実行してもエラーが表示されきません
4
+
5
+  どう修正すれば良いかお教えください。プログラミング初心者です。python2.7です。どうか宜しくお願い致します。
6
+
7
+
8
+
9
+ (エラー)
10
+
11
+ temp : 27.79 ℃
12
+
13
+ Exception in thread Thread-2:
14
+
15
+ Traceback (most recent call last):
16
+
17
+ File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
18
+
19
+ self.run()
20
+
21
+ File "/usr/lib/python2.7/threading.py", line 754, in run
22
+
23
+ self.__target(*self.__args, **self.__kwargs)
24
+
25
+ File "dht11_faq.py", line 128, in func2
26
+
27
+ csv = readData()
28
+
29
+ File "dht11_faq.py", line 41, in readData
30
+
31
+ t = Tm()
32
+
33
+ File "dht11_faq.py", line 17, in Tm
34
+
35
+ data = bus.read_i2c_block_data(0x45, 0x00, 6)
36
+
37
+ IOError: [Errno 5] Input/output error
38
+
39
+
40
+
41
+ Exception in thread Thread-1:
42
+
43
+ Traceback (most recent call last):
44
+
45
+ File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
46
+
47
+ self.run()
48
+
49
+ File "/usr/lib/python2.7/threading.py", line 754, in run
50
+
51
+ self.__target(*self.__args, **self.__kwargs)
52
+
53
+ File "dht11_faq.py", line 106, in func1
54
+
55
+ readData()
56
+
57
+ File "dht11_faq.py", line 43, in readData
58
+
59
+ h = Hu()
60
+
61
+ File "dht11_faq.py", line 27, in Hu
62
+
63
+ bus.write_i2c_block_data(0x45, 0x2C, [0x06])
64
+
65
+ IOError: [Errno 121] Remote I/O error
66
+
67
+ ```pythn
68
+
69
+ #!/usr/bin/python
70
+
71
+ #coding: utf-8
72
+
73
+
74
+
75
+ import smbus
76
+
77
+ import time
78
+
79
+ import datetime
80
+
81
+ import os
82
+
83
+ import threading
84
+
85
+
86
+
87
+ bus = smbus.SMBus(1)
88
+
89
+
90
+
91
+ def Tm():
92
+
93
+ time.sleep(0.2)
94
+
95
+ bus.write_i2c_block_data(0x45, 0x2C, [0x06])
96
+
97
+
98
+
99
+ time.sleep(0.2)
100
+
101
+ data = bus.read_i2c_block_data(0x45, 0x00, 6)
102
+
103
+
104
+
105
+ temp = data[0] * 256 + data[1]
106
+
107
+ cTemp = -45 + (175 * temp / 65535.0)
108
+
109
+
110
+
111
+ print "temp : %-6.2f ℃" % (cTemp)
112
+
113
+ return "%.2f" % (cTemp)
114
+
115
+
116
+
117
+ def Hu():
118
+
119
+ time.sleep(0.2)
120
+
121
+ bus.write_i2c_block_data(0x45, 0x2C, [0x06])
122
+
123
+
124
+
125
+ time.sleep(0.2)
126
+
127
+ data = bus.read_i2c_block_data(0x45, 0x00, 6)
128
+
129
+
130
+
131
+
132
+
133
+ humidity = 100 * (data[3] * 256 + data[4]) / 65535.0
134
+
135
+
136
+
137
+ print "hum : %6.2f %" % (humidity)
138
+
139
+ return "%.2f" % (humidity)
140
+
141
+
142
+
143
+ def readData():
144
+
145
+
146
+
147
+ time.sleep(0.2)
148
+
149
+ t = Tm()
150
+
151
+ time.sleep(0.2)
152
+
153
+ h = Hu()
154
+
155
+
156
+
157
+ print("---")
158
+
159
+
160
+
161
+ return t + "," + h
162
+
163
+
164
+
165
+ #time.sleep(5)
166
+
167
+
168
+
169
+ class i2clcd:
170
+
171
+
172
+
173
+ i2c = smbus.SMBus(1)
174
+
175
+ addr = 0x3e
176
+
177
+ contrast = 40 # 0~63
178
+
179
+
180
+
181
+ def __init__(self):
182
+
183
+ time.sleep(1)
184
+
185
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
186
+
187
+ self.i2c.write_byte_data(self.addr, 0, 0x39) # function set(IS=1)
188
+
189
+ self.i2c.write_byte_data(self.addr, 0, 0x14) # internal osc
190
+
191
+ self.i2c.write_byte_data(self.addr, 0,
192
+
193
+ (0x70 | (self.contrast & 0x0f))) # contrast
194
+
195
+ self.i2c.write_byte_data(self.addr, 0,
196
+
197
+ (0x54 | ((self.contrast >> 4) & 0x03))) # contrast/icon/power
198
+
199
+ self.i2c.write_byte_data(self.addr, 0, 0x6B) # follower control
200
+
201
+ # self.i2c.write_byte_data(self.addr, 0, 0x6c) # follower control
202
+
203
+
204
+
205
+ time.sleep(0.2)
206
+
207
+
208
+
209
+ def clear(self):
210
+
211
+ time.sleep(0.3)
212
+
213
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
214
+
215
+ self.i2c.write_byte_data(self.addr, 0, 0x70) # Constract
216
+
217
+ self.i2c.write_byte_data(self.addr, 0, 0x0C) # Display On
218
+
219
+ self.i2c.write_byte_data(self.addr, 0, 0x01) # Clear Display
220
+
221
+ self.i2c.write_byte_data(self.addr, 0, 0x06) # Entry Mode Set
222
+
223
+
224
+
225
+ time.sleep(0.1)
226
+
227
+
228
+
229
+
230
+
231
+ def puts(self, msg):
232
+
233
+
234
+
235
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
236
+
237
+ [self.i2c.write_byte_data(self.addr, 0x40, ord(c)) for c in msg]
238
+
239
+
240
+
241
+ def setaddress(self, line, col):
242
+
243
+ time.sleep(0.5)
244
+
245
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
246
+
247
+ time.sleep(0.5)
248
+
249
+ self.i2c.write_byte_data(self.addr, 0, 0x80 | (0x40 if line > 0 else 0) | col)
250
+
251
+
252
+
253
+ def setcg(self, no, cg):
254
+
255
+ self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0)
256
+
257
+ self.i2c.write_byte_data(self.addr, 0, 0x40 | (no << 3))
258
+
259
+ [self.i2c.write_byte_data(self.addr, 0x40, c) for c in cg]
260
+
261
+
262
+
263
+ def putcg(self, line, col, no):
264
+
265
+ self.setaddress(line, col)
266
+
267
+ self.i2c.write_byte_data(self.addr, 0x40, no)
268
+
269
+
270
+
271
+
272
+
273
+ def func1():
274
+
275
+ while True:
276
+
277
+
278
+
279
+ readData()
280
+
281
+
282
+
283
+ data = readData().split(",")
284
+
285
+ t = data[0]
286
+
287
+ h = data[1]
288
+
289
+
290
+
291
+ lcd = i2clcd()
292
+
293
+ lcd.clear()
294
+
295
+ time.sleep(1)
296
+
297
+
298
+
299
+ lcd.setaddress(0, 0)
300
+
301
+ lcd.puts(str(t).rjust(9)+" 'C")
302
+
303
+ lcd.setaddress(1, 0)
304
+
305
+ lcd.puts(str(h).rjust(9)+" %")
306
+
307
+
308
+
309
+ def func2():
310
+
311
+
312
+
313
+ dir_path = '/home/pi/dht11-data'
314
+
315
+
316
+
317
+ now = datetime.datetime.now()
318
+
319
+ filename = now.strftime('%Y%m%d')
320
+
321
+ label = now.strftime('%H:%M')
322
+
323
+ csv = readData()
324
+
325
+
326
+
327
+ if not os.path.exists('/home/pi/dht11-data'):
328
+
329
+ os.makedirs('/home/dht11-data')
330
+
331
+ f = open('/home/pi/dht11-data/'+filename+'.csv','a')
332
+
333
+ f.write("'"+label+"',"+csv+"\n")
334
+
335
+ f.close()
336
+
337
+
338
+
339
+
340
+
341
+ if __name__ == "__main__":
342
+
343
+ thread_1 = threading.Thread(target=func1)
344
+
345
+ thread_2 = threading.Thread(target=func2)
346
+
347
+
348
+
349
+ thread_1.start()
350
+
351
+ thread_2.start()
352
+
353
+
354
+
355
+ ```