import network
import urequests
import time
# Configuration
SSID = "Wokwi-GUEST" # Change if needed
PASSWORD = "" # Change if needed
THINGSPEAK_API_KEY = "CS1WH39RFSTPPWAM" # Your API key
# WiFi connection
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Connecting to WiFi...")
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print("\n✅ Connected! IP:", wlan.ifconfig()[0])
return wlan
# Send data to ThingSpeak
def send_to_thingspeak(value):
try:
url = f"https://api.thingspeak.com/update?api_key={THINGSPEAK_API_KEY}&field1={value}"
print(f"Sending to ThingSpeak: Value={value}")
response = urequests.get(url)
if response.status_code == 200:
print(f"✅ Data sent! Entry ID: {response.text}")
else:
print(f"❌ HTTP Error: {response.status_code}")
response.close()
except Exception as e:
print("❌ Error:", e)
# Main program
print("📡 ThingSpeak Number Sender")
wlan = connect_wifi()
for i in range(101): # 0 to 100
if not wlan.isconnected():
print("📶 WiFi disconnected, reconnecting...")
wlan = connect_wifi()
send_to_thingspeak(i)
time.sleep(1) # ThingSpeak free: min 15s between updates
print("✅ Finished sending 0–100")