import network
import time
from machine import Pin
from umqtt.simple import MQTTClient
# MQTT Server Parameters
MQTT_CLIENT_ID = "micropython-movment-demo"
MQTT_BROKER = "broker.mqttdashboard.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_TOPIC = "wokwi-movement"
sensor = Pin(26, Pin.IN)
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="")
time.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!")
#location of the LEDs on the microcontroller
led1 = Pin(15, Pin.OUT)
led2 = Pin(2, Pin.OUT)
led3 = Pin(4, Pin.OUT)
print("If it detects movement, the LEDs will turn on")
#Location of the sensor
sensor = Pin(26, Pin.IN)
while True:
if sensor.value() == 1:
led1.value(1)
led2.value(1)
led3.value(1)
print("a movement was detected")
client.publish(MQTT_TOPIC, "Detected movement")
else:
led1.value(0)
led2.value(0)
led3.value(0)
time.sleep(0.5)