Sunday, 29 March 2026

Weather Station ThingSpeak

import network import time import dht import urequests from machine import Pin import secret # --- Configuration --- ssid=secret.WIFI_SSID password=secret.WIFI_PASS api_key = secret.THINGSPEAK_API_KEY DHT_PIN = 18 # --- LED Setup --- led_pwr = Pin(15, Pin.OUT) # Red led_wifi = Pin(14, Pin.OUT) # Green led_data = Pin(13, Pin.OUT) # Blue led_pwr.value(1) # --- Setup Sensor --- sensor = dht.DHT11(Pin(DHT_PIN)) # --- WiFi --- wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(ssid, password) while not wlan.isconnected(): led_wifi.toggle() time.sleep(0.5) led_wifi.value(1) # --- Main Loop --- while True: sensor.measure() t = sensor.temperature() h = sensor.humidity() # Print values to Shell print('Temp ', t) print('Humidity ', h) url = f"https://api.thingspeak.com/update?api_key={api_key}&field1={t}&field2={h}" led_data.value(1) response = urequests.get(url) response.close() led_data.value(0) time.sleep(20)

No comments: