from machine import Pin
import time
import urequests
import network
URL = "https://webhook.site/6d8251fb-ed69-4118-8d54-682b502d84eb"
pir = Pin(13, Pin.IN)
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
print("Conectando ao WiFi...", end="")
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print("\nConectado!")
print("IP:", wlan.ifconfig()[0])
def get_timestamp():
return time.time()
def post_api(momento):
payload = {
"momento": momento,
"movimento": True
}
try:
response = urequests.post(URL, json=payload)
print("Status:", response.status_code)
response.close()
except Exception as e:
print("Erro:", e)
def main():
print("=== PROJETO PIR ===")
connect_wifi()
last_state = 0
while True:
current = pir.value()
# Detecta mudança de estado (LOW → HIGH)
if current == 1 and last_state == 0:
print("🚶 Movimento detectado!")
post_api(get_timestamp())
elif current == 0 and last_state == 1:
print("Parou o movimento")
last_state = current
time.sleep(0.5) # controla o loop
if __name__ == "__main__":
main()