import network
import time
from machine import Pin
import dht
import ujson
from umqtt.simple import MQTTClient
from utime import sleep
# MQTT Server Parameters
MQTT_CLIENT_ID = "clientId-pTBF9VbB8R"
MQTT_BROKER = "mqtt-dashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "/UNI319/Raffa_Nairaya/data_sensor"
MQTT_TOPIC_LED = "/UNI319/Raffa_Nairaya/aktuasi_led"
sensor = dht.DHT22(Pin(15))
led = Pin(13, Pin.OUT)
# WIFI Connection
print("Connecting to WiFi", end="")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect('Wokwi-GUEST', '')
while not wifi.isconnected():
print(".", end="")
time.sleep(1)
print(" Connected!")
# MQTT Server connection
def on_message(topic, message):
topic = str(topic, "UTF-8")
message = str(message, "UTF-8")
if message == "ON":
pin15.on()
elif message == "OFF":
pin15.off()
print("Connecting to MQTT server...", end="")
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
client.set_callback(on_message)
client.connect()
client.subscribe(MQTT_TOPIC_LED)
print("Connected!")