import network
import urequests
from machine import Pin
from time import sleep
# إعدادات الواي فاي
ssid = "Wokwi-GUEST"
password = ""
# مفتاح الكتابة في ThingSpeak
api_key = "LWZY7N2A1Q8ETPGJ"
# الاتصال بشبكة الواي فاي
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print("⏳ جاري الاتصال بالواي فاي...", end="")
while not wlan.isconnected():
print(".", end="")
sleep(0.5)
print("\n✅ تم الاتصال! IP:", wlan.ifconfig()[0])
# إرسال البيانات إلى ThingSpeak
def send_to_thingspeak(led_state):
url = f"http://api.thingspeak.com/update?api_key={api_key}&field1={led_state}" # ← http مو https
try:
response = urequests.get(url)
print("📤 تم الإرسال إلى ThingSpeak. رد:", response.text)
response.close()
except Exception as e:
print("⚠️ خطأ في الإرسال:", e)
# الحلقة الرئيسية
def main():
connect_wifi()
led = Pin(2, Pin.OUT)
state = 0
while True:
state = 1 - state # تبديل بين 0 و 1
led.value(state)
print("💡 حالة الـ LED:", "ON" if state else "OFF")
send_to_thingspeak(state)
sleep(5) # ThingSpeak يقبل إرسال كل 15 ثانية على الأقل
# استدعاء دالة main
main()
import network
import urequests
from machine import Pin
from time import sleep
ssid = "WiFi_Name"
password = "WiFi_Password"
api_key = "LWZY7N2A1Q8ETPGJ"
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
print("connection ...", end="")
attempts = 0
while not wlan.isconnected() and attempts < 20:
print(".", end="")
sleep(1)
attempts += 1
if wlan.isconnected():
print("\n connect with IP:", wlan.ifconfig()[0])
else:
print("\n fail to connect with IP .. ")
return False
return True
def send_to_thingspeak(led_state):
url = f"http://api.thingspeak.com/update?api_key={api_key}&field1={led_state}"
try:
response = urequests.get(url)
print(" Send to ThingSpeak :", response.text)
response.close()
except Exception as e:
print(" fail in send:", e)
def main():
if not connect_wifi():
return
led = Pin(2, Pin.OUT)
state = 0
while True:
state = 1 - state
led.value(state)
print(" LED Status:", "ON" if state else "OFF")
send_to_thingspeak(state)
sleep(16)
main()