import network
import machine
import ssd1306
import utime
# Configure the pins for temperature sensor and LEDs
temp_pin = 26 # Pin for the temperature sensor
led_pin_20 = 20 # Pin for LED 1
led_pin_22 = 22 # Pin for LED 2
# Initialize the pins
temp_sensor = machine.ADC(temp_pin)
led_20 = machine.Pin(led_pin_20, machine.Pin.OUT)
led_22 = machine.Pin(led_pin_22, machine.Pin.OUT)
# Initialize the OLED display
i2c = machine.I2C(0, scl=machine.Pin(1), sda=machine.Pin(0), freq=400000)
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Connect to Wi-Fi network
wifi_ssid = "YourWiFiSSID"
wifi_password = "YourWiFiPassword"
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(wifi_ssid, wifi_password)
# Main loop
while True:
# Read the temperature value
temp_value = temp_sensor.read_u16() * 3.3 / (65535)
# Convert the temperature value to degrees Celsius
temperature = 27 - (temp_value - 0.706) / 0.001721
# Check if temperature is above 30°C and switch on LED on port 20
if temperature > 30:
led_20.value(1)
led_22.value(0)
print("Temperature is above 30°C: LED on port 20 switched on")
# Check if temperature is below 35°C and switch on LED on port 22
elif temperature < 35:
led_20.value(0)
led_22.value(1)
print("Temperature is below 35°C: LED on port 22 switched on")
else:
led_20.value(0)
led_22.value(0)
print("Temperature is between 30°C and 35°C: LEDs turned off")
# Display temperature on OLED
oled.fill(0)
oled.text("Temp: {:.1f} C".format(temperature), 0, 0)
oled.show()
# Wait for 1 second before next measurement
utime.sleep(1)