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)



  
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
led1:A
led1:C
led2:A
led2:C
led3:A
led3:C
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
pir1:VCC
pir1:OUT
pir1:GND