from machine import Pin, SoftI2C, PWM
from i2c_lcd import I2cLcd
import time
import _thread
import dht
# FAN
# fan = Pin(32, Pin.OUT)
# fan.off()
# SERVO
servo = PWM(Pin(32), freq=50) # ใช้ GPIO32 ในการควบคุมเซอร์โว
servo.duty(77) # เซอร์โวอยู่ในตำแหน่งเริ่มต้น (กลาง)
# LCD
i2c = SoftI2C(scl=Pin(22), sda=Pin(21),freq=100000)
lcd = I2cLcd(i2c, 0x27,2,16)
time.sleep(1)
lcd.clear()
# DHT11
d = dht.DHT11(Pin(23))
t = 0
h = 0
def check_temp():
print('Check Temp Starting...')
global t
global h
while True:
try:
d.measure()
time.sleep(2)
# t = d.temperature()
t = 40
h = d.humidity()
temp = 'Temp: {:.0f} C'.format(t)
humid = 'Humidity: {:.0f} %'.format(h)
print('DHT11:', t, h)
time.sleep(5)
except:
pass
# START
text = 'Starting...'
lcd.putstr(text)
# RUN
global fan_status
fan_status = 'OFF'
def show_lcd():
while True:
try:
temp = 'Temp: {:.0f} C'.format(t)
humid = 'Humidity: {:.0f} %'.format(h)
# fan_s = 'FAN : {:}'.format(fan_status)
fan_s = 'SERVO: {:}'.format(fan_status)
lcd.clear()
lcd.putstr(fan_s)
time.sleep(2)
lcd.clear()
lcd.putstr(temp)
lcd.move_to(0,1)
lcd.putstr(humid)
time.sleep(2)
except:
pass
def auto_fan():
global fan_status
while True:
if t > 30:
time.sleep(1)
# fan.on()
servo.duty(40) # หมุนเซอร์โวไปตำแหน่งที่กำหนด
fan_status = 'ON (AUTO)'
time.sleep(10) # TURN ON FAN 10 sec
# fan.off()
servo.duty(77) # หมุนเซอร์โวกลับตำแหน่งเริ่มต้น (กลาง)
fan_status = 'OFF (AUTO)'
time.sleep(10)
else:
pass
_thread.start_new_thread(check_temp,())
_thread.start_new_thread(show_lcd,())
_thread.start_new_thread(auto_fan,())