#include <DHT.h> // Imports DHT library
#include <LiquidCrystal_I2C.h> // Imports LCD library
#include <WiFi.h> // Imports WiFi library
#include <WiFiClient.h> // Imports Client library
#include <WebServer.h> // Imports Server library
#include <uri/UriBraces.h> // Imports UriBraces Server
// Creates LCD Object
LiquidCrystal_I2C lcd (0x27, 16, 2);
// Defining LED Pins
#define coldLED 16
#define hotLED 17
#define goodLED 18
#define powerLED 26
#define chargerOneLED 32
#define chargerTwoLED 33
// Defining Button
#define chargerPinOne 34
#define chargerPinTwo 35
#define powerPin 4
bool chargerPinOneState = false;
bool chargerPinTwoState = false;
bool powerPinState = false;
// Defining DHT22
#define DHTPIN 27
#define DHTTYPE DHT22
// Creates DHT Object
DHT dht(DHTPIN, DHTTYPE);
// Defines WiFi SSID and Password
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel
#define WIFI_CHANNEL 6
// Creates WebServer object
WebServer server(80);
void setup() {
Serial.begin(9600); // Starts the console
// Starts the LCD
Wire.begin(23, 22);
lcd.init();
lcd.backlight();
dht.begin(); // Starts the DHT
// Initializes output components
pinMode(chargerOneLED, OUTPUT);
pinMode(chargerTwoLED, OUTPUT);
pinMode(coldLED, OUTPUT);
pinMode(hotLED, OUTPUT);
pinMode(goodLED, OUTPUT);
pinMode(powerLED, OUTPUT);
// Initializes input components
pinMode(chargerPinOne, INPUT);
pinMode(chargerPinTwo, INPUT);
pinMode(powerPin, INPUT);
// Starts WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Waits for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
} // End of while loop
Serial.println(" Connected!");
// Prints when connection is successful
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("HTTP server started");
} // End of setup function
void loop() {
delay(5000); // Updates every 5 seconds
Serial.println("");
/* This section simulates turning on and off the locker's
functions */
powerPinState = digitalRead(powerPin);
if(powerPinState == HIGH) {
digitalWrite(powerLED, HIGH);
Serial.println("Power is on!");
/* This section of the code simulates the
charging ports of the locker */
chargerPinOneState = digitalRead(chargerPinOne);
chargerPinTwoState = digitalRead(chargerPinTwo);
// For charging port 1
if(chargerPinOneState == HIGH) {
digitalWrite(chargerOneLED, HIGH); // Turns LED on
Serial.println("Charging Port 1 is in used!");
} else {
digitalWrite(chargerOneLED, LOW); // Turns LED off
Serial.println("Charging Port 1 is not in used!");
} // End of if-else for charging port 1
// For charging port 2
if(chargerPinTwoState == HIGH) {
digitalWrite(chargerTwoLED, HIGH); // Turns LED on
Serial.println("Charging Port 2 is in used!");
} else {
digitalWrite(chargerTwoLED, LOW); // Turns LED off
Serial.println("Charging Port 2 is not in used!");
} // End if if-else for charging port 2
/* This section of the code simulates
the reading and adjusting of the
temperature and humidity of the
locker using an LCD */
// Reads the humidity and temperature in Celcius
float humidity = dht.readHumidity();
float temp = dht.readTemperature();
// Error handling statement
if (isnan(humidity) || isnan(temp)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
} // End of if statement
// LED Status if hot
if(temp > 40) {
digitalWrite(hotLED, HIGH);
digitalWrite(goodLED, LOW);
digitalWrite(coldLED, LOW);
Serial.println("Temperature is too hot!");
} // End of if statement
// LED Status if cold
if(temp < 30) {
digitalWrite(hotLED, LOW);
digitalWrite(goodLED, LOW);
digitalWrite(coldLED, HIGH);
Serial.println("Temperature is too cold!");
} // End of if statement
// LED Status if good
if(temp > 30 && temp < 40) {
digitalWrite(hotLED, LOW);
digitalWrite(goodLED, HIGH);
digitalWrite(coldLED, LOW);
Serial.println("Temperature is just right!");
} // End of if statement
// Displays the temperature in LCD
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.setCursor(10,0);
lcd.print(temp);
// Displays the humidity in LCD
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.setCursor(10,1);
lcd.print(humidity);
// Handles the server
server.handleClient();
delay(2);
} else {
digitalWrite(powerLED, LOW);
Serial.println("Power is off!");
Serial.println("Please turn it on first!");
} // End of if-else statement
} // End of loop function