#G4 4TD.2
import machine #เรียกใช้คำสัง machine
import dht #เรียกใช้คำสัง DHT
from machine import Pin, SoftI2C #เรียกใช้คำสั่ง Pin,SoftI2C
from lcd_api import LcdApi #เรียกใช้คำสัง lcd_api,LcdApi
from i2c_lcd import I2cLcd #เรียกใช้คำสัง I2cLcd
from time import sleep #เรียกใช้คำสั่ง time,sleep
from machine import PWM
sensor = dht.DHT22(Pin(33)) #sensor = dht.DHT11(Pin(33))
b = Pin(14, Pin.OUT)
y = Pin(27, Pin.OUT)
r = Pin(26, Pin.OUT)
p32 = Pin(32, Pin.OUT)
def sound_h():
buzzer = PWM(p32)
buzzer.freq(1047)
buzzer.duty(50)
sleep(1)
buzzer.duty(0)
buzzer.deinit()
def sound_m():
buzzer = PWM(p32)
buzzer.freq(500)
buzzer.duty(50)
sleep(1)
buzzer.duty(0)
buzzer.deinit()
def sound_l():
buzzer = PWM(p32)
buzzer.freq(100)
buzzer.duty(50)
sleep(1)
buzzer.duty(0)
buzzer.deinit()
def get_temp(): #ฟังก์ชั่น
sleep(1) #หน่วงเวลา
sensor.measure() #เปิดใช้งาน sensor
temp = sensor.temperature() # กำหนดให้ temp เก็บข้อมูลอุณหภูมิ
return temp #กำหนดให้ส่งออกข้อมูล
def convert_CtoF(temp):
temp_f = temp * (9/5) + 32.0
return temp_f
def get_temp_f(): #ฟังก์ชั่น
sleep(1) #หน่วงเวลา
sensor.measure() #เปิดใช้งาน sensor
temp = sensor.temperature() #กำหนดให้ temp เก็บข้อมูลอุณหภูมิ
temp_f = temp * (9/5) + 32.0 #แปลงข้อมูลองศา เซลเซียส เป็น ฟาเรนไฮต์
return temp_f #กำหนดให้ส่งออกข้อมูล
def get_humi(): #ฟังก์ชั่น
sleep(1) #หน่วงเวลา
sensor.measure() #เปิดใช้งาน sensor
humi = sensor.humidity() #กำหนดให้ temp เก็บข้อมูลค่าความชื้น
return humi #กำหนดให้ส่งออกข้อมูล
def show_text(text_temp,text_temp_f,text_humi) : #ฟังก์ชั่น show ข้อความบนจอ lcd
lcd.putstr("Temperature="+str(text_temp)+"C") # แสดงผลTemerature C
if text_temp > 35:
r.on()
sound_h()
r.off()
sleep(0.5)
elif (text_temp > 20) & (text_temp <=35) :
y.on()
sound_m()
y.off()
sleep(0.5)
elif text_temp <= 20:
b.on()
sound_l()
b.off()
sleep(0.5)
lcd.clear() #ลบข้อความบนจอ lcd
lcd.putstr("Temperature="+str(text_temp_f)+"F") # แสดงผลTemerature temp F
if text_temp > 35:
r.on()
sound_h()
r.off()
sleep(0.5)
elif (text_temp > 20) & (text_temp <=35) :
y.on()
sound_m()
y.off()
sleep(0.5)
elif text_temp <= 20:
b.on()
sound_l()
b.off()
sleep(0.5)
lcd.clear() #ลบข้อความบนจอ lcd
lcd.putstr("Humidity="+str(text_humi)+"%") # แสดงผลditHumiy temp %
if text_temp > 35:
r.on()
sound_h()
sleep(0.5)
r.off()
sleep(0.5)
elif (text_temp > 20) & (text_temp <=35) :
y.on()
sound_m()
sleep(0.5)
y.off()
sleep(0.5)
elif text_temp <= 20:
b.on()
sound_l()
sleep(0.5)
b.off()
sleep(0.5)
lcd.clear() #ลบข้อความบนจอ lcd
"""while True:
if text_temp > 30:
y.on()
sleep(0.5)
y.off()
sleep(0.5)
elif text_temp <= 30:
b.on()
sleep(0.5)
b.off()
sleep(0.5)"""
I2C_ADDR = 0x27 #ความกกว้างของ lcd
totalRows = 2 #ความสูงของ Rows แสดงผลลายตั้ง
totalColumns = 16 #ความยาวของ Columns ลายนอน
i2c = SoftI2C(scl=Pin(12), sda=Pin(13), freq=10000) #กำหนดให้ scl=Pin(12), sda=Pin(13)
lcd = I2cLcd(i2c, I2C_ADDR, totalRows, totalColumns)
while True: #วนซ้ำไม่รู้จบ
temperature = get_temp() #ตัวแปร temperature เก็บค่า Temp ที่ return มา
temperature_f = get_temp_f()
humidity = get_humi()
show_text(temperature,temperature_f,humidity) # แสดงผล Temerature,ditHumiy บน lcd