inifrom machine import Pin, I2C
import ssd1306
from time import sleep
import dht
from hcsr04 import HCSR04
# Set up I2C communication for OLED (scl=22, sda=21)
i2c = I2C(-1, scl=Pin(22), sda=Pin(21))
# Define OLED width and height
oled_width = 128
oled_height = 64
# Initialize OLED display
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
# Initialize DHT22 sensors
sensorDHT1 = dht.DHT22(Pin(14))
sensorDHT2 = dht.DHT22(Pin(27))
# Initialize HCSR04 ultrasonic sensors
sensorSR04_1 = HCSR04(trigger_pin=26, echo_pin=25)
sensorSR04_2 = HCSR04(trigger_pin=33, echo_pin=32)
# Initialize LED
ledBoard = Pin(2, Pin.OUT)
# Function to convert Celsius to Kelvin
def celsius_to_kelvin(celsius):
return celsius + 273.15
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# Variable to keep track of which screen to display
screen_index = 0
while True:
try:
# Toggle LED
ledBoard.value(0) # Turn on LED
sleep(1)
ledBoard.value(1) # Turn off LED
# Read data from sensors
sensorDHT1.measure()
temp1 = sensorDHT1.temperature()
hum1 = sensorDHT1.humidity()
sensorDHT2.measure()
temp2 = sensorDHT2.temperature()
hum2 = sensorDHT2.humidity()
distance1 = sensorSR04_1.distance_cm()
distance2 = sensorSR04_2.distance_cm()
# Convert temperatures to Kelvin and Fahrenheit
temp1_k = celsius_to_kelvin(temp1)
temp1_f = celsius_to_fahrenheit(temp1)
temp2_k = celsius_to_kelvin(temp2)
temp2_f = celsius_to_fahrenheit(temp2)
# Clear OLED screen for new data
oled.fill(0)
# Display data on the OLED based on the screen_index
if screen_index == 0:
# Screen 1: Display DHT22 #1
oled.text("Sensor DHT22 #1", 0, 0)
oled.text("Temp: %3.1f C" % temp1, 0, 10)
oled.text("Temp: %3.1f F" % temp1_f, 0, 20)
oled.text("Temp: %3.1f K" % temp1_k, 0, 30)
oled.text("Hum: %3.1f %%" % hum1, 0, 40)
elif screen_index == 1:
# Screen 2: Display DHT22 #2
oled.text("Sensor DHT22 #2", 0, 0)
oled.text("Temp: %3.1f C" % temp2, 0, 10)
oled.text("Temp: %3.1f F" % temp2_f, 0, 20)
oled.text("Temp: %3.1f K" % temp2_k, 0, 30)
oled.text("Hum: %3.1f %%" % hum2, 0, 40)
elif screen_index == 2:
# Screen 3: Display Ultrasonic #1
oled.text("Ultrasonic #1", 0, 0)
oled.text("Distance: %3.1f cm" % distance1, 0, 10)
elif screen_index == 3:
# Screen 4: Display Ultrasonic #2
oled.text("Ultrasonic #2", 0, 0)
oled.text("Distance: %3.1f cm" % distance2, 0, 10)
# Show the updated screen on OLED
oled.show()
# Print results to console for reference
if screen_index == 0:
print("Sensor DHT22 #1:")
print("Temperature: %3.1f °C, %3.1f °F, %3.1f K" % (temp1, temp1_f, temp1_k))
print("Humidity: %3.1f %%\n" % hum1)
elif screen_index == 1:
print("Sensor DHT22 #2:")
print("Temperature: %3.1f °C, %3.1f °F, %3.1f K" % (temp2, temp2_f, temp2_k))
print("Humidity: %3.1f %%\n" % hum2)
elif screen_index == 2:
print("Ultrasonic Sensor #1 Distance:", distance1, "cm\n")
elif screen_index == 3:
print("Ultrasonic Sensor #2 Distance:", distance2, "cm\n")
# Move to the next screen index, cycling back to 0 after 3
screen_index = (screen_index + 1) % 4
# Wait before showing the next screen
sleep(2)
except OSError as e:
print('Failed to read sensor.')
sleep(2) # Add delay before retrying.