'''
# SENSOR DHT 22 #
import dht
from machine import Pin

#sensor = dht.DHT11(Pin(13))
sensor = dht.DHT22(Pin(13))

sensor.measure() # faz a leitura 
print(sensor.temperature())
print(sensor.humidity())
'''

from machine import Pin
from time import sleep
import dht 

#--------------------------------------------------------------------------------#
# CONFIGURAÇÃO DO SERVIDOR MQTT
#--------------------------------------------------------------------------------#

MQTT_BROKER    = "177.153.58.71"     # IFAM 
#MQTT_BROKER    = "broker.hivemq.com"  # HiveMQ
MQTT_USER      = "aluno"
MQTT_PASSWORD  = "teste123"
MQTT_CLIENT_ID = ""
MQTT_TOPIC     = "sd/sensor5"
MQTT_PORT = 1883

#--------------------------------------------------------------------------------#
# CONEXÃO DA REDE WIFI WOKWI-CONVIDADO
#--------------------------------------------------------------------------------#

def wifi_connect():
  print("Connecting", end="")

  sta_if = network.WLAN(network.STA_IF)
  sta_if.active(True)
  sta_if.connect('Wokwi-GUEST', '')

  while not sta_if.isconnected():
    print(".", end="")
    time.sleep(0.3)

  print(" Wi-Fi connected!")

sensor = dht.DHT22(Pin(15))
#sensor = dht.DHT11(Pin(13))

leitura = 0
tempC = 0.0
tempF = 0.0
hum = 0.0

wifi_connect()
client = subscribe()

while True:
  try:
    client.wait_msg()
    sensor.measure()
    tempC = sensor.temperature()
    hum = sensor.humidity()
    tempF = tempC * (9/5) + 32.0  
    
    print('')
    print('Leitura:', leitura)
    print('Temperatura: %3.1f C'  %tempC)
    print('Temperatura: %3.1f F'  %tempF)
    print('Humidade:    %3.1f %%' %hum)
    sleep(2)
    leitura += 1    
    
    
  except OSError as e:
    print('Failed to read sensor.')



'''

#--------------------------------------------------------------------------------#
# SISTEMA DE CONTROLE DE DATACENTER
#--------------------------------------------------------------------------------#

import network
import time
from umqtt.simple import MQTTClient
from machine import Pin

#--------------------------------------------------------------------------------#
# CONFIGURAÇÃO DO SERVIDOR MQTT
#--------------------------------------------------------------------------------#

MQTT_BROKER    = "177.153.58.71"     # IFAM 
#MQTT_BROKER    = "broker.hivemq.com"  # HiveMQ
MQTT_USER      = "aluno"
MQTT_PASSWORD  = "teste123"
MQTT_CLIENT_ID = ""
MQTT_TOPIC     = "sd/sensor5"
MQTT_PORT = 1883

#--------------------------------------------------------------------------------#
# CONEXÃO DA REDE WIFI WOKWI-CONVIDADO
#--------------------------------------------------------------------------------#

def wifi_connect():
  print("Connecting", end="")

  sta_if = network.WLAN(network.STA_IF)
  sta_if.active(True)
  sta_if.connect('Wokwi-GUEST', '')

  while not sta_if.isconnected():
    print(".", end="")
    time.sleep(0.3)

  print(" Wi-Fi connected!")

#--------------------------------------------------------------------------------#
# MENSAGEM DE CONFIGURAÇÃO DO LED
#--------------------------------------------------------------------------------#

def message_arrived(topic, msg):
  if (msg == b'1'):
      led.value(1)
  if (msg == b'0'):
      led.value(0)

#--------------------------------------------------------------------------------#
# SUBSCRIBER
#--------------------------------------------------------------------------------#

def subscribe():
    client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, port=MQTT_PORT, user=MQTT_USER, password=MQTT_PASSWORD, keepalive=0)
 
    # Subscribed messages will be delivered to this callback
    client.set_callback(message_arrived)
    client.connect()
    client.subscribe(MQTT_TOPIC)
    print("Conected on %s, topic subscribed: %s " % (MQTT_BROKER, MQTT_TOPIC))
    return client

#--------------------------------------------------------------------------------#
# PROGRAMA PRINCIPAL
#--------------------------------------------------------------------------------#

led = Pin(14, Pin.OUT)
led.value(0)

wifi_connect()
client = subscribe()

while True:
  client.wait_msg()
  #time.sleep(0.5)      # BLINK LED

'''