import network
from time import sleep
from machine import Pin,PWM
import dht
import ujson
from umqtt.simple import MQTTClient
import _thread
# MQTT Server Parameters
MQTT_CLIENT_ID = "biufiaudbad"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC1 = "msgEsp1pEsp2"
MQTT_TOPIC2 = "msgEsp1pEsp2_2"
print("Connecting to WiFi", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
sleep(0.1)
print(" Connected!")
print("Connecting to MQTT server... ", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.connect()
print("Connected!")
dht1 = dht.DHT22(Pin(14))
dht2 = dht.DHT22(Pin(15))
servo1 = PWM(Pin(27), freq=50, duty=0)
servo2 = PWM(Pin(23), freq=50, duty=0)
chave = Pin(13,Pin.IN)
led = Pin(2,Pin.OUT)
def ambiente1():
while True:
sleep(2)
dht1.measure() # mede dht1
#print("Lendo dht1")
# Ajustar o servo1 com base na temperatura
if (dht1.temperature() >= 60):
# Abertura do servo em 50 graus
duty = int(((50 / 180) * 2 + 0.5) / 20 * 1023)
else:
# Fechamento do servo
duty = int(((0 / 180) * 2 + 0.5) / 20 * 1023)
servo1.duty(duty)
# Publicar mensagens baseadas na umidade
if (dht1.humidity() <= 20):
msg = "DHT1 Umidade baixa!"
print(msg)
client.publish(MQTT_TOPIC1, str(msg))
else:
msg = "DHT1 Umidade normal!"
client.publish(MQTT_TOPIC1, str(msg))
def ambiente2():
while True:
sleep(3)
dht2.measure() # mede dht2
#print("Lendo dht2")
# Ajustar o servo2 com base na temperatura
if (dht2.temperature() >= 60):
# Abertura do servo em 180 graus
duty = int(((180 / 180) * 2 + 0.5) / 20 * 1023)
else:
# Fechamento do servo
duty = int(((0 / 180) * 2 + 0.5) / 20 * 1023)
servo2.duty(duty)
# Publicar mensagens baseadas na umidade
if (dht2.humidity() <= 20):
msg = "DHT2 Umidade baixa!"
print(msg)
client.publish(MQTT_TOPIC2, str(msg))
else:
msg = "DHT2 Umidade normal!"
client.publish(MQTT_TOPIC2, str(msg))
_thread.start_new_thread(ambiente1,());
_thread.start_new_thread(ambiente2,());