from machine import Pin
from time import sleep_ms, ticks_ms, localtime, time
from ntptime import settime
from dht import DHT22
from rede import Rede
from json import dumps, loads
from umqtt.simple import MQTTClient
th = DHT22(Pin(14))
Sensor = Pin(Pin(4))
start = ticks_ms()
t = b'ifrs/rg/auto/2004s2/telemetria2024328900'
t2 = b'ifrs/rg/auto/2004s2/ocorrencia2024328900'
conteudo = { "Temperatura":50,
"Umidade":5,
"quando": 15,
}
conteudo2 = {"Movimento":1,
"data": 2,
}
texto = dumps(conteudo)
ts = localtime()
print (f'{ts[0]}-{ts[1]}-{ts[2]}T{ts[3]}:{ts[4]}:{ts[5]}')
print ('\n\nConectando a rede\n\n')
r = Rede ('Wokwi-GUEST','')
r.conectar(tentativas=20)
if r.conectado:
print("conetado!")
settime()
print(f'Data/Hora: {time()}')
dados = MQTTClient('jlbgjr1295' , 'broker.hivemq.com')
dados.connect()
else:
print('nao conetado')
visto = False
while True:
if Sensor.value() and not visto:
ts = localtime()
conteudo2['data']= (f'{ts[0]}-{ts[1]}-{ts[2]})T{ts[3]}:{ts[4]}:{ts[5]}')
conteudo2["Movimento"]
visto = True
m = dumps(conteudo2)
dados.publish(t2, m.encode())
elif not Sensor.value():
visto = False
if (ticks_ms() - start) >= 60000:
th.measure()
conteudo['tem']= th.temperature()
conteudo["umi"]= th.humidity()
ts = localtime()
conteudo['quando'] = f'{ts[0]}-{ts[1]}-{ts[2]})T{ts[3]}:{ts[4]}:{ts[5]}'
start = ticks_ms()
c = dumps(conteudo)
dados.publish(t, c.encode())