print("Title: Mini Project")
print("Date: 25/1/2023")
print("Created By: Khaii")
print("Smart Study Hub Monitoring System")
from machine import Pin, SoftI2C, ADC, PWM
from utime import sleep
import dht
import OLED_library
import servo_library
class LDR:
"""This class read a value from a light dependent resistor (LDR)"""
def __init__(self, pin, min_value=0, max_value=100):
"""
Initializes a new instance.
:parameter pin A pin that's connected to an LDR.
:parameter min_value A min value that can be returned by value() method.
:parameter max_value A max value that can be returned by value() method.
"""
if min_value >= max_value:
raise Exception('Min value is greater or equal to max value')
# initialize ADC (analog to digital conversion)
self.adc = ADC(Pin(pin))
# set 11dB input attenuation (voltage range roughly 0.0v - 3.6v)
self.adc.atten(ADC.ATTN_11DB)
self.min_value = min_value
self.max_value = max_value
def read(self):
"""
Read a raw value from the LDR.
:return A value from 0 to 4095.
"""
return self.adc.read()
def value(self):
"""
Read a value from the LDR in the specified range.
:return A value from the specified [min, max] range.
"""
#return (self.max_value - self.min_value) * self.read() / 4095
return self.read()
# Pin declaration
ldrTest = LDR(27)
#LDR_Pin = Pin(27, Pin.IN)
Pin_scl_sda = SoftI2C(scl=Pin(21), sda=Pin(22))
led_red = Pin(2, Pin.OUT) # Red LED pin
led_sleep = Pin(15, Pin.OUT) # Yellow LED for night
sensor = dht.DHT22(Pin(4)) # DHT22 sensor pin
# Create instances for OLED and servo modules
skrin = OLED_library.SSD1306_I2C(width=128, height=64, i2c=Pin_scl_sda)
blade = servo_library.Servo(pin=Pin(13)) # Servo motor pin declaration
# Turn on the red LED when starting the program
led_red.on()
def read_dht22(sensor):
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
temp_f = temperature * (9/5) + 32.0
return temperature, temp_f, humidity
except OSError as e:
print('Failed to read sensor:', e)
return None, None, None
def display_environment(temperature, humidity):
skrin.fill(0)
skrin.text("Temp: {:.1f}C".format(temperature), 0, 10, 1)
skrin.text("Hum: {:.1f}%".format(humidity), 0, 30, 1)
if temperature > 22 :
skrin.text("Air-cond on", 10, 50, 1) # Change to indicate air-cond on
else:
skrin.text("Air-cond off", 10, 50, 1) # Change to indicate air-cond off
skrin.show()
def display_condition(condition):
skrin.fill(0)
skrin.text("Light Condition:", 0, 10, 1)
skrin.text(condition, 10, 20, 1)
if condition == "Night":
if temperature > 22 :
skrin.text("Air-cond on", 10, 50, 1) # Change to indicate air-cond on
else:
skrin.text("Air-cond off", 10, 50, 1) # Change to indicate air-cond off
led_red.off()
led_sleep.on() # Turn on yellow LED for night
blade.move(0) # Move servo to initial position to turn off air conditioner
elif condition == "Midnight":
if temperature > 22 :
skrin.text("Air-cond on", 10, 50, 1) # Change to indicate air-cond on
else:
skrin.text("Air-cond off", 10, 50, 1) # Change to indicate air-cond off
led_red.off()
led_sleep.off() # Turn off yellow LED for day
else:
if temperature > 22 :
skrin.text("Air-cond on", 10, 50, 1) # Change to indicate air-cond on
else:
skrin.text("Air-cond off", 10, 50, 1) # Change to indicate air-cond off
led_red.on() # Turn on red LED for day
led_sleep.off() # Turn off yellow LED for day
skrin.show()
try:
while True:
# Read temperature and humidity
temperature, temp_f, humidity = read_dht22(sensor)
# Display environment data
display_environment(temperature, humidity)
# Delay before reading light condition again
sleep(5) # Display for 5 seconds
# Read light condition
#ldr_value = LDR_Pin.value()
ldr_value = ldrTest.value()
print(ldr_value)
if ldr_value >= 2000: # Night condition
condition = "Night"
display_condition(condition)
sleep(3)
skrin.fill(0)
skrin.text("Time to Sleep", 0, 30, 1)
skrin.text("Turn off light!!", 0, 40, 1)
skrin.show()
# Sequence of movements for the servo motor to turn 360 degrees like a fan
for _ in range(4):
blade.move(0) # Move to 0 degrees
sleep(0.5)
blade.move(90) # Move to 90 degrees
sleep(0.5)
blade.move(180) # Move to 180 degrees
sleep(0.5)
blade.move(270) # Move to 270 degrees
sleep(0.5)
elif 200 < ldr_value < 2000: # Midnight condition
condition = "Midnight"
display_condition(condition)
else:
condition = "Day"
display_condition(condition)
sleep(3)
skrin.fill(0)
skrin.text("Good Morning", 10, 30, 1)
skrin.text("Work time!!", 10, 40, 1)
skrin.show()
# Delay before next iteration
sleep(3)
except KeyboardInterrupt:
# Return the servo to its initial position
blade.move(0)
finally:
# Turn off LEDs and close OLED display when exiting
led_red.off()
led_sleep.off()
skrin.poweroff()