2017 멀티캠퍼스/Raspberry Pi

#12 라즈베리파이 온습도 센서

꿈꾸는어린이 2018. 1. 30. 22:32

//

일시 : 2018.01.11

내용 : 온,습도 센서

//

I2C TEMP / HUMI

  • 온습도 센서

  • Read_address와 Write_address가 다름

  • Datasheet 참고




실습

- 현재 온,습도 출력

  
import smbus2 as smbus
import time

bus = smbus.SMBus(1)
addr = 0x40

cmd_temp = 0xf3
cmd_humi = 0xf5
soft_reset = 0xfe
temp = 0.0
humi = 0.0
val = 0
data = [0, 0]

try:
bus.write_byte (addr, soft_reset)
time.sleep(0.05)

   # myMotor = motor()
while True:
# temperature
bus.write_byte(addr, cmd_temp) # 온도를읽어라
time.sleep(0.260)  # 데이터시트에 정해져있는 시간

for i in range(0,2,1):
data[i] = bus.read_byte(addr)

val = data[0] << 8 | data[1]
temp = -46.85+175.72/65536*val
       
       # humidity
bus.write_byte(addr, cmd_humi)
time.sleep(0.260)

for i in range(0,2,1):
data[i] = bus.read_byte(addr)

val = data[0] << 8 | data[1]
humi = -6.0+125.0/65536*val;

print 'temp : %.2f, humi : %.2f' %(temp, humi)
time.sleep(1)
       # myMotor.humid_check(temp, humi)

except KeyboardInterrupt:
pass



- 모터와 연동

  
import RPi.GPIO as GPIO
import time

class motor :
   GPIO.setmode(GPIO.BCM)
   GPIO.setwarnings(False)
   
   GPIO_RP = 4
   GPIO_RN = 25
   GPIO_EN = 12
   
   
   def __init__(self) :
       GPIO.setup(self.GPIO_RP, GPIO.OUT)
       GPIO.setup(self.GPIO_RN, GPIO.OUT)
       GPIO.setup(self.GPIO_EN, GPIO.OUT)

   def humid_check(self, temp, humi):
       print ('temp : %.2f, humi : %.2f' %(temp, humi))
       time.sleep(0.01)
       
       if (humi < 40) :
           ##stop
           GPIO.output(self.GPIO_RP, False)
           GPIO.output(self.GPIO_RN, False)
           GPIO.output(self.GPIO_EN, True)
           time.sleep(1)
       elif (humi <= 50):
           ##cw
           GPIO.output(self.GPIO_RP, True)
           GPIO.output(self.GPIO_RN, False)
           GPIO.output(self.GPIO_EN, True)
           time.sleep(1)
       else :
           ##ccw
           GPIO.output(self.GPIO_RP, False)
           GPIO.output(self.GPIO_RN, True)
           GPIO.output(self.GPIO_EN, True)
           time.sleep(1)



- 클래스 연습

  
import smbus2 as smbus
import time

class tempHumi:
   
   def __init__(self):
       self.bus = smbus.SMBus(1)
       self.addr = 0x40
       soft_reset = 0xfe
       self.bus.write_byte (self.addr, soft_reset)
       time.sleep(0.05)

   def readTemp(self):
       # temperature
       cmd_temp = 0xf3
       temp = 0.0
       data = [0, 0]
       self.bus.write_byte(self.addr, cmd_temp)
       time.sleep(0.260)
       
       for i in range(0,2,1):
           data[i] = self.bus.read_byte(self.addr)

       val = data[0] << 8 | data[1]
       temp = -46.85+175.72/65536*val
       return temp

   def readHumi(self):
       # humidity
       cmd_humi = 0xf5
       humi = 0.0
       data = [0, 0]
       self.bus.write_byte(self.addr, cmd_humi)
       time.sleep(0.260)
       
       for i in range(0,2,1):
           data[i] = self.bus.read_byte(self.addr)

       val = data[0] << 8 | data[1]
       humi = -6.0+125.0/65536*val;
       return humi
   
try:
   if __name__ == "__main__":
       
       iTempHumi=tempHumi()
       
       while True:
           temp=iTempHumi.readTemp()
           print("temp : %d" % temp, end=", ")
           
           humi=iTempHumi.readHumi()
           print("humi : %d" % humi)
           time.sleep(1)
           
except KeyboardInterrupt:
   pass
finally:
   pass