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

# MQTT connection parameters
MQTT_CLIENT_ID = "mqtt_subscriber_1"  
MQTT_BROKER = "broker.emqx.io"      
MQTT_USER = ""                     
MQTT_PASSWORD = ""                 
MQTT_TOPIC_DATA = "/UNI466/Finanazwa/data_sensor"
MQTT_TOPIC_LED_CONTROL = "/UNI466/Finanazwa/aktuasi_led"

# Pin configuration
sensor1 = dht.DHT22(Pin(15))
sensor2 = dht.DHT22(Pin(2))
led = Pin(25, Pin.OUT)     

# Function to connect the ESP32 to a WiFi network
def connect_to_wifi():
    print("Connecting to WiFi", end="")
    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(True)
    sta_if.connect('Wokwi-GUEST', '')
    timeout = 10  # Set timeout to 10 seconds
    while not sta_if.isconnected() and timeout > 0:
        time.sleep(1)
        timeout -= 1
        print(".", end="")
    if sta_if.isconnected():
        print(" Connected!")
        print("IP Address:", sta_if.ifconfig()[0])
    else:
        print(" Failed to connect to WiFi. Please check settings.")

# Function to handle incoming MQTT messages
def message_callback(topic, msg):
    print(f"Received message on topic {topic.decode()}: {msg.decode()}")
    if topic.decode() == MQTT_TOPIC_LED_CONTROL:
        command = msg.decode().strip().upper()  # Normalisasi pesan
        if command == "ON":
            print("Turning LED ON")
            led.on()
        elif command == "OFF":
            print("Turning LED OFF")
            led.off()
        else:
            print(f"Unknown command: {command}")

# Function to connect the ESP32 to an MQTT broker
def connect_to_mqtt():
    """
    Connects the ESP32 to the MQTT broker using the configured parameters.
    """
    print("Connecting to the MQTT broker... ", end="")
    client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, user=MQTT_USER, password=MQTT_PASSWORD)
    client.set_callback(message_callback)  # Set the callback function for incoming messages
    client.connect()  
    client.subscribe(MQTT_TOPIC_LED_CONTROL)  # Subscribe to the LED control topic
    print(f"Successfully connected to the MQTT broker and subscribed to {MQTT_TOPIC_LED_CONTROL}!")
    return client  

# Main function
def main():
    connect_to_wifi()
    client = connect_to_mqtt()

    prev_weather = ""
    while True:
        try:
            print("Measuring weather conditions... ", end="")
            sensor1.measure()
            sensor2.measure()
            message = ujson.dumps({
                "temp1": sensor1.temperature(),
                "humidity1": sensor1.humidity(),
                "temp2": sensor2.temperature(),
                "humidity2": sensor2.humidity(),
            })
            if message != prev_weather:
                print("Updated!")
                print("Reporting to MQTT topic {}: {}".format(MQTT_TOPIC_DATA, message))
                client.publish(MQTT_TOPIC_DATA, message)
                prev_weather = message
            else:
                print("No change")

            # Check for new messages (e.g., LED control)
            client.check_msg()
            time.sleep(1)

        except Exception as e:
            print("Error:", e)
            time.sleep(5)  # Retry after a delay


# Run the main function
if __name__ == "__main__":
    main()