from machine import Pin, I2C
import ssd1306
import network
import urequests
from time import sleep
# Configuration de l'écran OLED
SCREEN_WIDTH = 128
SCREEN_HEIGHT = 64
OLED_ADDR = 0x3C # Adresse I2C par défaut de l'écran OLED
# Initialisation de l'I2C pour l'écran OLED
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
display = ssd1306.SSD1306_I2C(SCREEN_WIDTH, SCREEN_HEIGHT, i2c)
# Configuration de ThingSpeak
THINGSPEAK_API_KEY = "SIRVXMC5LQJDK168"
THINGSPEAK_CHANNEL_ID = "2696212"
THINGSPEAK_URL = f"https://api.thingspeak.com/update?api_key={THINGSPEAK_API_KEY}"
motion = False
def handle_interrupt(pin):
global motion
motion = True
global interrupt_pin
interrupt_pin = pin
# Configuration des broches
led = Pin(12, Pin.OUT)
pir = Pin(14, Pin.IN)
pir.irq(trigger=Pin.IRQ_RISING, handler=handle_interrupt)
# Connexion au Wi-Fi
def connect_wifi(ssid, password):
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
sleep(1)
print('Connected to WiFi:', wlan.ifconfig())
# Remplacez les valeurs ci-dessous par vos informations Wi-Fi
connect_wifi("Wokwi-GUEST", "")
while True:
if motion:
print('Motion detected! Interrupt caused by:', interrupt_pin)
led.value(1)
display.fill(0)
display.text("Motion detected!", 0, 0, 1)
display.show()
# Envoi des données à ThingSpeak
response = urequests.get(THINGSPEAK_URL + f"&field1=1") # Envoie une valeur pour le champ 1
print("Data sent to ThingSpeak:", response.text)
sleep(20) # Durée pendant laquelle le LED reste allumé
led.value(0)
display.fill(0)
display.text("Motion Stopped!", 0, 0, 1)
display.show()
print('Motion Stopped!')
motion = False