# Complete MicroPython code

# Import necessary modules
import network
from umqtt.simple import MQTTClient
import machine
import time
import neopixel

# WiFi credentials
WIFI_SSID = "Wokwi-GUEST"
WIFI_PASSWORD = ""

# MQTT broker details
MQTT_SERVER = "broker.hivemq.com"
MQTT_CLIENT_ID = "clientId-aGnXY2AzhB"
MQTT_TOPIC = "your/topic"

# Initialize NeoPixel
NUM_PIXELS = 1
np = neopixel.NeoPixel(machine.Pin(2), NUM_PIXELS)  # Adjust pin number as per your connection

# Initialize Servo motor
servo = machine.PWM(machine.Pin(13), freq=50)  # Adjust pin number as per your connection

# Connect to WiFi
def connect_wifi():
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print('Connecting to WiFi...')
        wlan.connect(SSID, PASSWORD)
        while not wlan.isconnected():
            pass
    print('WiFi connected:', wlan.ifconfig())

# MQTT callback function
def on_message(topic, msg):
    if topic == MQTT_TOPIC.encode():
        msg = msg.decode()
        if msg == "on":
            np[0] = (255, 0, 0)  # Set NeoPixel color to red
            np.write()
        elif msg == "rotate":
            servo.duty(40)  # Adjust duty cycle to rotate servo
            time.sleep(1)
            servo.duty(115)  # Adjust duty cycle to return servo to original position
            time.sleep(1)

# Connect to WiFi
connect_wifi()

# Connect to MQTT broker
client = MQTTClient("esp32", MQTT_SERVER)
client.connect()

# Subscribe to MQTT topic
client.set_callback(on_message)
client.subscribe(MQTT_TOPIC)

# Main loop
while True:
    client.check_msg()