from machine import I2C, Pin, ADC, PWM,SoftI2C
from i2c_lcd import I2cLcd
from lcd_api import LcdApi
from time import sleep
import dht, _thread, sys, threading
#declara os valores do lcd para seu funcionamento
I2C_ADDR = 0x27
totalRows = 2
totalColumns = 16
i2c = SoftI2C (scl=Pin(19), sda=Pin(18), freq=10000)
lcd = I2cLcd (i2c, I2C_ADDR, totalRows, totalColumns)
#variaveis
refence_values = {'ldr' : -1, 'humidity' : -1, 'temperature' : -1, 'temperature_ntc': -1}
ValoresSensores = {}
EMERGENCIA = False
#declaração panic button
PanicButton = Pin(34, Pin.IN)
#declaração ldr
PinoLdr = Pin(2, Pin.IN)
ldr_v = ADC(PinoLdr)
#declaração led
Motor = Pin(26, Pin.OUT)
LedPWM = PWM(Pin(14, Pin.OUT))
#declaração dht
PinoDHT = Pin(4, Pin.IN)
DHT = dht.DHT22(PinoDHT)
#declaração NTC
PinoNTC = Pin(33, Pin.IN)
ntc_v = ADC(PinoNTC)
# declara os valores necessarios para a conversão da leitura do ldr em lux
GAMMA = 0.7
RL10 = 50
def func(valor : float, variavel : float) -> float:
return valor / variavel
def ensureHumidity(target : float) -> bool:
valorDHT = ValoresSensores['humidity']
if valorDHT >= target:
valor = valorDHT
sleep(2)
if valor <= ValoresSensores['humidity']:
return True
def ensureTemp(target : float) -> bool:
valorDHT = ValoresSensores['temperature']
if valorDHT >= target:
valor = valorDHT
sleep(2)
if valor >= ValoresSensores['temperature']:
return True
def compare(lastest_value, comparator : str) -> bool:
sleep(0.1)
if lastest_value != refence_values[comparator]:
refence_values[comparator] = lastest_value
return True
def ensureLight(target : int, lux : bool) -> bool:
if valorLDR <= target:
valor = valorLDR
sleep(2)
if valor >= converte_ldr(valor_ldr()):
return True
def ControlaBrilho(objeto, value : float):
objeto.duty(int(value))
def irq_panic():
EMERGENCIA = True
PanicButton.irq(lambda t:irq_panic(), trigger=Pin.IRQ_FALLING)
def LeitorSensores():
def leituraNtc() -> float:
return ntc_v.read()
def valor_ldr() -> int:
return ldr_v.read()
def DhtTemp() -> float:
DHT.measure()
return DHT.temperature()
def DhtHumidity() -> float:
DHT.measure()
return DHT.humidity()
while True:
ValoresSensores['ldr'] = valor_ldr()
ValoresSensores['humidity'] = DhtHumidity()
ValoresSensores['temperature'] = DhtTemp()
ValoresSensores['temperature_ntc'] = leituraNtc()
def Display():
def converte_ldr(bits_ldr) -> int:
valor = bits_ldr
voltage = valor / 4095 * 5
resistance = 2000 * voltage / (1 - voltage / 5)
lux = pow(RL10 * 1000 * pow(10, GAMMA) / resistance, (1 / GAMMA))
return lux
def converte_ntc(x) -> int:
temp_ntc = 110 -0.0789*x + 2.59E-05*x**2 -3.76E-09*x**3
return temp_ntc
while True:
temp_ntc = converte_ntc(ValoresSensores['temperature_ntc'])
lux = converte_ldr(ValoresSensores['ldr'])
if compare(lux, 'ldr'):
print('Quantidade de Lux atual:', lux)
lcd.putstr(f"Lux atual:{lux:.2f}")
sleep(1)
lcd.clear()
if compare(ValoresSensores['humidity'], 'humidity'):
print('Humidade atual:', ValoresSensores['humidity'])
lcd.putstr(f"Humi atual:{ValoresSensores['humidity']:.2f}")
sleep(1)
lcd.clear()
if compare(ValoresSensores['temperature'], 'temperature'):
print('Temperatura atual:', ValoresSensores['temperature'])
lcd.putstr(f"TempDHT11:{ValoresSensores['temperature']:.2f}")
sleep(1)
lcd.clear()
if compare(temp_ntc, 'temperature_ntc'):
print('Temperatura atual do NTC:', temp_ntc)
lcd.putstr(f"TempNTC:{temp_ntc:.2f}")
sleep(1)
lcd.clear()
_thread.start_new_thread(LeitorSensores, ())
sleep(1)
_thread.start_new_thread(Display, ())
def main():
while True:
if EMERGENCIA:
Motor.off()
print('Emergéncia acionada!')
lcd.putstr("Emergéncia acionada")
sys.exit()
if ensureHumidity(60):
Motor.on()
else:
Motor.off()
ControlaBrilho(LedPWM, func(ValoresSensores['ldr'], 4))
if __name__ == '__main__':
main()