ラズベリーパイにセンサーを接続して取得したデータをpythonにて液晶のlcdディスプレイに数秒ごとに表示させて、かつcsvファイルに数分ごとに保存したいと考えています。cronにてcsvファイルを保存するソースコードを作成し、回しましたがエラーが出て何度修正しても止まります。lcdの表示のみでは止まりません。
そこでthreading関数で実行したいのですが、下記のソースコードを実行してもエラーが表示されて動きません。
どう修正すれば良いかお教えください。プログラミング初心者です。python2.7です。どうか宜しくお願い致します。
(エラー)
temp : 27.79 ℃
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "dht11_faq.py", line 128, in func2
csv = readData()
File "dht11_faq.py", line 41, in readData
t = Tm()
File "dht11_faq.py", line 17, in Tm
data = bus.read_i2c_block_data(0x45, 0x00, 6)
IOError: [Errno 5] Input/output error
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "dht11_faq.py", line 106, in func1
readData()
File "dht11_faq.py", line 43, in readData
h = Hu()
File "dht11_faq.py", line 27, in Hu
bus.write_i2c_block_data(0x45, 0x2C, [0x06])
IOError: [Errno 121] Remote I/O error
python
1 2#!/usr/bin/python 3#coding: utf-8 4 5import smbus 6import time 7import datetime 8import os 9import threading 10 11bus = smbus.SMBus(1) 12 13def Tm(): 14 time.sleep(0.2) 15 bus.write_i2c_block_data(0x45, 0x2C, [0x06]) 16 17 time.sleep(0.2) 18 data = bus.read_i2c_block_data(0x45, 0x00, 6) 19 20 temp = data[0] * 256 + data[1] 21 cTemp = -45 + (175 * temp / 65535.0) 22 23 print "temp : %-6.2f ℃" % (cTemp) 24 return "%.2f" % (cTemp) 25 26def Hu(): 27 time.sleep(0.2) 28 bus.write_i2c_block_data(0x45, 0x2C, [0x06]) 29 30 time.sleep(0.2) 31 data = bus.read_i2c_block_data(0x45, 0x00, 6) 32 33 34 humidity = 100 * (data[3] * 256 + data[4]) / 65535.0 35 36 print "hum : %6.2f %" % (humidity) 37 return "%.2f" % (humidity) 38 39def readData(): 40 41 time.sleep(0.2) 42 t = Tm() 43 time.sleep(0.2) 44 h = Hu() 45 46 print("---") 47 48 return t + "," + h 49 50 #time.sleep(5) 51 52class i2clcd: 53 54 i2c = smbus.SMBus(1) 55 addr = 0x3e 56 contrast = 40 # 0~63 57 58 def __init__(self): 59 time.sleep(1) 60 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 61 self.i2c.write_byte_data(self.addr, 0, 0x39) # function set(IS=1) 62 self.i2c.write_byte_data(self.addr, 0, 0x14) # internal osc 63 self.i2c.write_byte_data(self.addr, 0, 64 (0x70 | (self.contrast & 0x0f))) # contrast 65 self.i2c.write_byte_data(self.addr, 0, 66 (0x54 | ((self.contrast >> 4) & 0x03))) # contrast/icon/power 67 self.i2c.write_byte_data(self.addr, 0, 0x6B) # follower control 68 # self.i2c.write_byte_data(self.addr, 0, 0x6c) # follower control 69 70 time.sleep(0.2) 71 72 def clear(self): 73 time.sleep(0.3) 74 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 75 self.i2c.write_byte_data(self.addr, 0, 0x70) # Constract 76 self.i2c.write_byte_data(self.addr, 0, 0x0C) # Display On 77 self.i2c.write_byte_data(self.addr, 0, 0x01) # Clear Display 78 self.i2c.write_byte_data(self.addr, 0, 0x06) # Entry Mode Set 79 80 time.sleep(0.1) 81 82 83 def puts(self, msg): 84 85 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 86 [self.i2c.write_byte_data(self.addr, 0x40, ord(c)) for c in msg] 87 88 def setaddress(self, line, col): 89 time.sleep(0.5) 90 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 91 time.sleep(0.5) 92 self.i2c.write_byte_data(self.addr, 0, 0x80 | (0x40 if line > 0 else 0) | col) 93 94 def setcg(self, no, cg): 95 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 96 self.i2c.write_byte_data(self.addr, 0, 0x40 | (no << 3)) 97 [self.i2c.write_byte_data(self.addr, 0x40, c) for c in cg] 98 99 def putcg(self, line, col, no): 100 self.setaddress(line, col) 101 self.i2c.write_byte_data(self.addr, 0x40, no) 102 103 104def func1(): 105 while True: 106 107 readData() 108 109 data = readData().split(",") 110 t = data[0] 111 h = data[1] 112 113 lcd = i2clcd() 114 lcd.clear() 115 time.sleep(1) 116 117 lcd.setaddress(0, 0) 118 lcd.puts(str(t).rjust(9)+" 'C") 119 lcd.setaddress(1, 0) 120 lcd.puts(str(h).rjust(9)+" %") 121 122def func2(): 123 124 dir_path = '/home/pi/dht11-data' 125 126 now = datetime.datetime.now() 127 filename = now.strftime('%Y%m%d') 128 label = now.strftime('%H:%M') 129 csv = readData() 130 131 if not os.path.exists('/home/pi/dht11-data'): 132 os.makedirs('/home/dht11-data') 133 f = open('/home/pi/dht11-data/'+filename+'.csv','a') 134 f.write("'"+label+"',"+csv+"\n") 135 f.close() 136 137 138if __name__ == "__main__": 139 thread_1 = threading.Thread(target=func1) 140 thread_2 = threading.Thread(target=func2) 141 142 thread_1.start() 143 thread_2.start() 144
<追記>
下記のようにスレッドを削除し、while Trueで処理すると動作しますが、二つ目のwhile Trueが同時に実行されません。
python
1#!/usr/bin/python 2#coding: utf-8 3 4import smbus 5import time 6import datetime 7import os 8import threading 9 10bus = smbus.SMBus(1) 11 12def Tm(): 13 time.sleep(0.2) 14 bus.write_i2c_block_data(0x45, 0x2C, [0x06]) 15 16 time.sleep(0.2) 17 data = bus.read_i2c_block_data(0x45, 0x00, 6) 18 19 temp = data[0] * 256 + data[1] 20 cTemp = -45 + (175 * temp / 65535.0) 21 22 print "temp : %-6.2f ℃" % (cTemp) 23 return "%.2f" % (cTemp) 24 25def Hu(): 26 time.sleep(0.2) 27 bus.write_i2c_block_data(0x45, 0x2C, [0x06]) 28 29 time.sleep(0.2) 30 data = bus.read_i2c_block_data(0x45, 0x00, 6) 31 32 33 humidity = 100 * (data[3] * 256 + data[4]) / 65535.0 34 35 print "hum : %6.2f %" % (humidity) 36 return "%.2f" % (humidity) 37 38def readData(): 39 40 time.sleep(0.2) 41 t = Tm() 42 time.sleep(0.2) 43 h = Hu() 44 45 print("---") 46 47 return t + "," + h 48 49 #time.sleep(5) 50 51class i2clcd: 52 53 i2c = smbus.SMBus(1) 54 addr = 0x3e 55 contrast = 40 # 0~63 56 57 def __init__(self): 58 time.sleep(1) 59 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 60 self.i2c.write_byte_data(self.addr, 0, 0x39) # function set(IS=1) 61 self.i2c.write_byte_data(self.addr, 0, 0x14) # internal osc 62 self.i2c.write_byte_data(self.addr, 0, 63 (0x70 | (self.contrast & 0x0f))) # contrast 64 self.i2c.write_byte_data(self.addr, 0, 65 (0x54 | ((self.contrast >> 4) & 0x03))) # contrast/icon/power 66 self.i2c.write_byte_data(self.addr, 0, 0x6B) # follower control 67 # self.i2c.write_byte_data(self.addr, 0, 0x6c) # follower control 68 69 time.sleep(0.2) 70 71 def clear(self): 72 time.sleep(0.3) 73 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 74 self.i2c.write_byte_data(self.addr, 0, 0x70) # Constract 75 self.i2c.write_byte_data(self.addr, 0, 0x0C) # Display On 76 self.i2c.write_byte_data(self.addr, 0, 0x01) # Clear Display 77 self.i2c.write_byte_data(self.addr, 0, 0x06) # Entry Mode Set 78 79 time.sleep(0.1) 80 81 82 def puts(self, msg): 83 84 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 85 [self.i2c.write_byte_data(self.addr, 0x40, ord(c)) for c in msg] 86 87 def setaddress(self, line, col): 88 time.sleep(0.5) 89 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 90 time.sleep(0.5) 91 self.i2c.write_byte_data(self.addr, 0, 0x80 | (0x40 if line > 0 else 0) | col) 92 93 def setcg(self, no, cg): 94 self.i2c.write_byte_data(self.addr, 0, 0x38) # function set(IS=0) 95 self.i2c.write_byte_data(self.addr, 0, 0x40 | (no << 3)) 96 [self.i2c.write_byte_data(self.addr, 0x40, c) for c in cg] 97 98 def putcg(self, line, col, no): 99 self.setaddress(line, col) 100 self.i2c.write_byte_data(self.addr, 0x40, no) 101 102 103 104while True: 105 106 readData() 107 108 data = readData().split(",") 109 t = data[0] 110 h = data[1] 111 112 lcd = i2clcd() 113 lcd.clear() 114 time.sleep(1) 115 116 lcd.setaddress(0, 0) 117 lcd.puts(str(t).rjust(9)+" 'C") 118 lcd.setaddress(1, 0) 119 lcd.puts(str(h).rjust(9)+" %") 120 121while True: 122 123 dir_path = '/home/pi/dht11-data' 124 125 now = datetime.datetime.now() 126 filename = now.strftime('%Y%m%d') 127 label = now.strftime('%H:%M') 128 csv = readData() 129 130 if not os.path.exists('/home/pi/dht11-data'): 131 os.makedirs('/home/dht11-data') 132 f = open('/home/pi/dht11-data/'+filename+'.csv','a') 133 f.write("'"+label+"',"+csv+"\n") 134 f.close() 135
回答2件
あなたの回答
tips
プレビュー