from time import sleep
import dht
from machine import ADC, Pin, PWM
import math
buzzer_pin = Pin(14, Pin.OUT)
buzzer_pwm = PWM(buzzer_pin)
teplomer = dht.DHT22(Pin(32))
adc = ADC(Pin(27))
adc.atten(ADC.ATTN_11DB)
led= Pin(26, Pin.OUT)
led1= Pin(25, Pin.OUT)
rl10 = 50e3 #LDR resistance at 10lux
gamma = 0.7 #log(Ra/Rb) / log(La/Lb)
def calculate_resistance():
value = adc.read()
voltage_ratio = value / (4095 - value)
return 10e3 * voltage_ratio
#R = R_10 * (lux / 10) ^ -γ => R10/R=(10/lux)^−γ => R/R10=(10/lux)^γ => (R10/R)^(1/γ)=10/lux => lux=10*(R10/R)^(1/γ)
def calculate_lux(odpor):
return 10 * math.pow(rl10/odpor, 1/gamma)
while True:
odpor = calculate_resistance()
teplomer.measure()
temp = teplomer.temperature()
hum = teplomer.humidity()
print('Temperature: %3.1f C' %temp)
print('Humidity: %3.1f %%' %hum)
resistance = calculate_resistance()
lux=round(calculate_lux(resistance),1)
if lux >= 1000 or temp>=50:
led.value(1)
sleep(5)
led.value(0)
buzzer_pwm.duty(50)
else:
buzzer_pwm.duty(0)