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