import network
import time
import urequests
import ujson
# Wi-Fi Wokwi
SSID = "Wokwi-GUEST"
PASSWORD = ""
# ====== Webhook.site ======
WEBHOOK_URL = "https://webhook.site/3e41a2fb-36c6-4e38-b73f-2ba4f5031019" # <-- colle ton URL unique ici
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
wlan.connect(SSID, PASSWORD)
print("Connexion Wi-Fi...")
t0 = time.ticks_ms()
while not wlan.isconnected():
if time.ticks_diff(time.ticks_ms(), t0) > 15000:
raise RuntimeError("Timeout Wi-Fi")
time.sleep(0.2)
print("✅ Connecté :", wlan.ifconfig())
def send_json_webhook(payload):
headers = {
"Content-Type": "application/json"
}
# Convertir dict -> JSON string
data_json = ujson.dumps(payload)
#ujson.loads(json_string) -->str JSON → dict
print("\nHTTPS POST -> Webhook.site")
print("URL:", WEBHOOK_URL)
print("Payload:", data_json)
r = urequests.post(WEBHOOK_URL, data=data_json, headers=headers)
print("Status:", r.status_code)
print("Réponse:", r.text)
r.close()
connect_wifi()
time.sleep(1)
counter = 0
while True:
try:
counter += 1
# Exemple de données IoT (comme si ça venait d'un capteur)
payload = {
"device": "esp32-TST",
"count": counter,
"temperature": 25.4, # exemple
"humidity": 58, # exemple
"timestamp": time.time()
}
send_json_webhook(payload)
except Exception as e:
print("Erreur:", e)
time.sleep(10)