'''
Computer Room Air Condition(CRAC) units are specifically designed for server rooms
and data centers to cool the air and maintain specific temperature levels.
This project aims to realize a Smart CRAC unit with Data Logging capability and Fire
Alarm to alert security personnel once it has been triggered.
'''
import network
import time
import dht
from machine import Pin, SPI, RTC
from picozero import Servo
import sdcard
import os
#Initialize Servo, DHT, LED
dht_sensor = dht.DHT22(Pin(14))
servo = Servo(28)
led1 = Pin(9, Pin.OUT)
led2 = Pin(15, Pin.OUT)
log = 6 #Specified no.of Log values
# Initialize RTC
rtc = RTC()
# Initialize SPI
spi = SPI(0,
baudrate=1000000,
polarity=0,
phase=0,
sck=Pin(2),
mosi=Pin(3),
miso=Pin(0))
# Initialize CS pin
cs = Pin(13, Pin.OUT)
# Initialize the SD card
sd = sdcard.SDCard(spi, cs)
# Mount the SD card to the file system
os.mount(sd, "/sd")
# Open a file on the SD card and write to it
with open("/sd/test.txt", "w") as file:
file.write("Date and Time Temperature\n")
#Function to connect to Wifi
def connectToWifi():
print("Connecting to WiFi", end="")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Wokwi-GUEST", "")
while not wlan.isconnected():
print(".", end="")
time.sleep(0.1)
print(" Connected!")
print(wlan.ifconfig())
# Function to read the DHT22 sensor
def read_dht_sensor():
try:
dht_sensor.measure()
return dht_sensor.temperature()
except Exception as e:
print("Failed to read from DHT sensor:", e)
return None
#Function to perform air conditioning
def hvac():
a = 0
while (a < log):
# Read temperature and humidity
temperature = read_dht_sensor()
servo.min()
time.sleep(3)
if temperature is not None:
# Get current date and time from RTC
year, month, day, weekday, hours, minutes, seconds, subseconds = rtc.datetime()
with open("/sd/test.txt", "a") as fl:
fl.write(f"{year}-{month}-{day} {hours}:{minutes}:{seconds} {temperature}°C\n")
print(f"Temperature: {temperature}°C\n")
time.sleep(1)
if temperature < 50:
print("Server Room Temperature is Stable!!\n\n")
led2.value(0)
elif 50 <= temperature < 70:
print("Server Room Temperature is Rising!!")
print("Cooling System is Activated!!\n\n")
led2.value(1)
servo.max()
time.sleep(2)
elif temperature >=70:
print("Fire Alarm has been Triggered!!")
print("Take Necessary Action!!")
led2.value(0)
led1.value(1)
time.sleep(2)
led1.value(0)
a+=1
#Function Call
connectToWifi()
hvac()
# Read the file back to verify the contents
with open("/sd/test.txt", "r") as file:
print("Information from the Data Logged File:\n\n")
time.sleep(1)
print(file.read())
print("\n\n")
time.sleep(1)
# Unmount the SD card
os.umount("/sd")
Servo Motor for Ventilation