print("Project: Goal 9 (Industries, Inovation & Infrastructure)")
print("Dateline: 22/11/2024")
print("Created By: Jd")
from machine import Pin, I2C
from time import sleep
import dht
import oled_library
# Initialize DHT22 sensor
dht_sensor = dht.DHT22(Pin(15)) # Connect DHT22 data pin to GPIO15
# Initialize I2C and OLED display
i2c = I2C(0, scl=Pin(22), sda=Pin(21)) # Use GPIO22 (SCL) and GPIO21 (SDA)
oled_width = 128
oled_height = 64
oled = oled_library.SSD1306_I2C(oled_width, oled_height, i2c)
def display_data(temp, hum):
"""
Display temperature and humidity data on the OLED.
"""
oled.fill(0) # Clear the screen
oled.text("Comfort Monitor", 0, 0)
oled.text("Temp: {:.1f} C".format(temp), 0, 20)
oled.text("Humidity: {:.1f}%".format(hum), 0, 40)
oled.show()
def main():
while True:
try:
# Read data from DHT22
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
# Display data
display_data(temperature, humidity)
# Adjust the delay as needed
sleep(2)
except Exception as e:
print("Error:", e)
oled.fill(0)
oled.text("Sensor Error!", 0, 20)
oled.show()
sleep(2)
# Run the main function
main()