import time import urequests import machine import ujson import dht from onewire import…
- User
import time import urequests import machine import ujson import dht from onewire import OneWire from ds18x20 import DS18X20 import neopixel import BME280 as bme280 from machine import ADC, Pin import network
===========================
Configuration and Constants
===========================
WiFi Configuration
WIFI_SSID = "LAPTOP-K2GEAL20 6366" # Replace with your WiFi SSID WIFI_PASSWORD = "0}F6g981" # Replace with your WiFi Password
ThingSpeak settings
THINGSPEAK_API_KEY = "L8D704IJUKPH8IB5" THINGSPEAK_URL = "http://api.thingspeak.com/update"
Sensor Pins
DHT_PIN = 2 # GPIO4 for DHT22 DS18B20_PIN = 22 # GPIO5 for DS18B20 NEOPIXEL_PIN = 6 # GPIO2 for NeoPixel I2C_SCL_PIN = 17 # GPIO15 for BME280 SCL I2C_SDA_PIN = 16 # GPIO14 for BME280 SDA UV_SENSOR_PIN = 26 # GPIO28 for UV Sensor (Analog)
NeoPixel configuration
NEOPIXEL_COUNT = 12 # Total number of NeoPixels UV_LED_RANGE = range(0, 6) # LEDs 0-5 for UV TEMP_LED_RANGE = range(6, 12) # LEDs 6-11 for Temperature
ThingSpeak rate limit (in seconds)
THINGSPEAK_DELAY = 30
===========================
Initialization
===========================
def connect_wifi(ssid, password): """ Connect to a WiFi network. """ wlan = network.WLAN(network.STA_IF) # Create a station interface wlan.active(True)
if not wlan.isconnected(): print(f"Connecting to WiFi SSID: {ssid}") wlan.connect(ssid, password)
timeout = 20
while not wlan.isconnected() and timeout > 0: print("Waiting for connection...") time.sleep(1) timeout -= 1 if wlan.isconnected(): print("Connected to WiFi") print("Network Configuration:", wlan.ifconfig()) else: raise RuntimeError("Failed to connect to WiFi")connect_wifi(WIFI_SSID, WIFI_PASSWORD)
Initialize sensors
dht_sensor = dht.DHT22(Pin(DHT_PIN))
ow = OneWire(Pin(DS18B20_PIN)) ds_sensor = DS18X20(ow) ds_roms = ds_sensor.scan() if not ds_roms: print("Warning: No DS18B20 devices found.")
np = neopixel.NeoPixel(Pin(NEOPIXEL_PIN), NEOPIXEL_COUNT)
try: i2c_2 = machine.I2C(0, scl=Pin(I2C_SCL_PIN), sda=Pin(I2C_SDA_PIN), freq=40000) bme = bme280.BME280(i2c=i2c_2) except Exception as e: print("Error initializing BME280:", e) bme = None
try: uv_sensor = ADC(UV_SENSOR_PIN) print("UV sensor initialized.") except Exception as e: print("Error initializing UV sensor:", e) uv_sensor = None
===========================
Utility Functions
===========================
def set_neopixel_color_uv(uv_intensity): """ Set NeoPixel colors for UV indicators based on UV intensity. """ if uv_intensity is None: color = (0, 0, 0) elif uv_intensity > 75: color = (255, 0, 0) elif uv_intensity > 25: color = (255, 255, 0) else: color = (255, 0, 255)
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.