from machine import Pin
import dht
import time
x = 0
dd = { # dados
'int': 2,
'pin': {
'sen': 15,
'led': [23, 21, 19, 18, 17],
'bot' : 12
},
'FIM': {
"TE" : ['frio', 'médio', 'quente', 'fervendo'],
'PAZ' : [],
'clique' : 0
}
}
Dis = { # dispositivos
'dht22': dht.DHT22(Pin(dd['pin']['sen'])),
'ledc': Pin(dd['pin']['led'][0], Pin.OUT), #cyan
'lede': Pin(dd['pin']['led'][1], Pin.OUT), #red velvet
'ledc2': Pin(dd['pin']['led'][2], Pin.OUT), #purple raze
'ledc3': Pin(dd['pin']['led'][3], Pin.OUT), #pink floyd
'ledc4': Pin(dd['pin']['led'][4], Pin.OUT), #Blue Lock
'bot0' : Pin(dd['pin']['bot'], Pin.IN, Pin.PULL_UP)
}
def lersens():
try:
Dis['dht22'].measure()
return {
'temp': Dis['dht22'].temperature(),
'umi': Dis['dht22'].humidity(),
'templer': time.time()
}
except Exception:
return {
'temp': None,
'umi': None,
'templer': time.time()
}
def salvar(ler):
if ler['temp'] is not None and ler['temp'] < 20:
dd['FIM']['PAZ'].append([ler['temp'], ler['umi'], dd['FIM']['TE'][0], ler['templer']])
elif ler['temp'] is not None and ler['temp'] < 40:
dd['FIM']['PAZ'].append([ler['temp'], ler['umi'], dd['FIM']['TE'][1], ler['templer']])
elif ler['temp'] is not None and ler['temp'] < 60:
dd['FIM']['PAZ'].append([ler['temp'], ler['umi'], dd['FIM']['TE'][2], ler['templer']])
elif ler['temp'] is not None and ler['temp'] <= 80:
dd['FIM']['PAZ'].append([ler['temp'], ler['umi'], dd['FIM']['TE'][3], ler['templer']])
else:
print('error')
dd['FIM']['clique'] += 1
def alert(ler):
if ler['temp'] is not None and ler['temp'] < 20 : #friozinho
Dis['ledc'].on()
Dis['ledc2'].off()
Dis['ledc3'].off()
Dis['ledc4'].off()
Dis['lede'].off()
elif ler['temp'] is not None and ler['temp'] < 40 : #quentinho
Dis['ledc'].off()
Dis['ledc2'].on()
Dis['ledc3'].off()
Dis['ledc4'].off()
Dis['lede'].off()
elif ler['temp'] is not None and ler['temp'] < 60 : #pelano
Dis['ledc'].off()
Dis['ledc2'].off()
Dis['ledc3'].on()
Dis['ledc4'].off()
Dis['lede'].off()
elif ler['temp'] is not None and ler['temp'] <= 80: #FOGO
Dis['ledc'].off()
Dis['ledc2'].off()
Dis['ledc3'].off()
Dis['ledc4'].on()
Dis['lede'].off()
elif ler['temp'] is None: #sensor zuado
Dis['ledc'].off()
Dis['ledc2'].off()
Dis['ledc3'].off()
Dis['ledc4'].off()
Dis['lede'].on()
else: #acontece nada fejoada
Dis['ledc'].off()
Dis['ledc2'].off()
Dis['ledc3'].off()
Dis['ledc4'].off()
Dis['lede'].off()
'''
def alert(ler):
if ler['temp'] is not None and ler['temp'] > dd['temL']:
Dis['ledc'].on()
Dis['lede'].off()
elif ler['temp'] is not None:
Dis['ledc'].off()
Dis['lede'].off()
else:
Dis['ledc'].off()
Dis['lede'].on()
'''
while True:
ler = lersens()
'''print("Leitura atual:")
for chave, valor in ler.items():
print(chave, ":", valor)'''
alert(ler)
if Dis['bot0'].value() == 1: #clique do botão
salvar(ler)
i = dd['FIM']['clique'] - 1
print('momento clique', i + 1)
print(dd['FIM']['PAZ'][i][0], '°C')
print(dd['FIM']['PAZ'][i][1], '%')
print('Status de temperatura:', dd['FIM']['PAZ'][i][2])
print('momento da leitura:', dd['FIM']['PAZ'][i][3])
else:
x = x
time.sleep(dd["int"])