Group2 IOT
Group2 IOT
Group2 IOT
1|Page
Part 1: Introduction to member of the group
Meet Our Team – Group 2
Part 4: Circuit
2|Page
3|Page
Part 5: Code
Arduino Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
#include <SPI.h>
#include <MFRC522.h>
4|Page
void displayWelcomeMessage() {
lcd.setCursor(0, 0);
lcd.print("Welcome to my");
lcd.setCursor(0, 1);
lcd.print("home security");
delay(2000);
lcd.clear();
}
float getTemperature() {
return analogRead(lm35) * (5.0 / 1023.0) * 100;
}
int checkHuman() {
int distance = sonar.ping_cm();
if (distance > 0) {
if (distance < 100) return 1;
if (distance < 200) return 2;
return 3;
}
return 0;
}
String getRFID() {
String id = "";
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
for (byte i = 0; i < mfrc522.uid.size; i++) {
id += String(mfrc522.uid.uidByte[i], HEX);
}
mfrc522.PICC_HaltA();
}
return id;
}
5|Page
analogWrite(buzzer, volume);
delay(duration);
analogWrite(buzzer, 0);
}
void noHuman() {
displayLCD("No human", 0);
displayLCD("Detected", 1);
controlLEDs(false, false, false);
}
void humanFar() {
displayLCD("Human is far", 0);
displayLCD("away", 1);
controlLEDs(false, false, true);
}
void humanNear() {
displayLCD("Human is near", 0);
displayLCD("away", 1);
controlLEDs(false, true, false);
for (int i = 0; i < 3; i++) {
soundBuzzer(50, 1000);
delay(300);
}
}
void humanClose() {
displayLCD("Human is very", 0);
displayLCD("close", 1);
controlLEDs(true, false, false);
for (int i = 0; i < 3; i++) {
soundBuzzer(200, 1000);
}
}
void toggleProtection(bool status) {
6|Page
isProtected = status;
displayLCD(status ? "Protect is on" : "Protect is off", 0);
displayLCD(status ? "Press key to off" : "Press key to on", 1);
delay(2000);
}
void loop() {
if (digitalRead(btn) == LOW) {
if (isProtected && millis() - lastAccessTime < 60000) {
toggleProtection(false);
sendToMCU("protection is off");
} else if (!isProtected) {
toggleProtection(true);
sendToMCU("protection is on");
}
}
if (!isProtected) {
return;
}
float temperature = getTemperature();
if (temperature > 50 && !isHotAlerted) {
isHotAlerted = true;
sendToMCU("Temperature is too high");
displayLCD("Temperature", 0);
displayLCD("is too high", 1);
controlLEDs(true, false, false);
soundBuzzer(200, 1000);
return;
}
if (lastAccessTime != 0 && millis() - lastAccessTime > 60000) {
lastAccessTime = 0;
} else if (lastAccessTime != 0) {
return;
}
int currentHumanState = checkHuman();
if (currentHumanState != previousHumanState && lastDetectionTime == 0) {
previousHumanState = currentHumanState;
switch (currentHumanState) {
case 0:
noHuman();
break;
case 1:
sendToMCU("Human detected");
lastDetectionTime = millis();
controlLEDs(true, false, false);
7|Page
displayLCD("Scan RFID Now", 0);
displayLCD("Please wait...", 1);
break;
case 2:
humanNear();
break;
default:
humanFar();
break;
}
}
if (!isAlert) {
sendToMCU("Human detected but no RFID after 5s");
isAlert = true;
}
humanClose();
}
if (lastDetectionTime != 0 && millis() - lastDetectionTime > 60000) {
isAlert = false;
sendToMCU("Human detection timeout after 1 minute");
lastDetectionTime = 0;
noHuman();
}
nodeMCU Code:
8|Page
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");
Serial.println("\nConnected successfully!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
sendTelegramMessage("NodeMCU has connected to WiFi!");
}
void loop() {
if (Serial.available() > 0) {
String dataStr = Serial.readStringUntil('\n');
sendTelegramMessage(dataStr);
Serial.println("ok");
}
}
if (client.connect("api.telegram.org", 443)) {
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
9|Page
"Host: api.telegram.org\r\n" +
"Connection: close\r\n\r\n");
Serial.println("Message sent successfully!");
} else {
Serial.println("Unable to connect to Telegram API");
}
}
Program Description:
1. setup() function
o Initialize Communication: Set up serial connection, I2C, and SPI
communication, and initialize devices.
o Pin Setup: Set modes for LED control pins and the buzzer.
o Display Welcome Message: Show a greeting message on the LCD to
inform the user that the security system is ready.
2. Helper Functions
o displayWelcomeMessage(): Displays a welcome message on the LCD at
startup.
o getTemperature(): Reads temperature from the LM35 sensor and returns
the temperature value.
10 | P a g e
o checkHuman(): Uses the ultrasonic sensor to detect a person and returns
the detected distance in centimeters.
o getRFID(): Reads the RFID code from the RFID reader and returns it as a
string.
o checkRFID(String id): Checks if the RFID code is on the allowed list.
o soundBuzzer(int volume, int duration): Activates the buzzer with a
specified volume and duration.
o displayLCD(const String &message, int row): Displays a specific
message on a specified row of the LCD screen.
o controlLEDs(bool red, bool green, bool blue): Controls the status of the
red, green, and blue LEDs.
o noHuman(), humanFar(), humanNear(), humanClose(): Functions that
display messages and control the LEDs and buzzer according to the
detected distance of a person (near, far, or no one present).
o toggleProtection(bool status): Turns the protection mode on or off,
displays the status on the LCD, and waits for the user to press a button to
toggle.
o sendToMCU(String message): Sends a message to the MCU via serial
communication.
3. loop() function - Main Operations
o Button Check: If the user presses the button, the system toggles between
protection mode on/off and displays the appropriate message.
o Temperature Check: If the temperature exceeds 50°C and no warning has
been issued, the system issues a high-temperature warning via the LED,
buzzer, and sends a message to the MCU.
o Access Time Check: Monitors if one minute has passed since the last
access, resetting if expired.
o Person Detection:
Uses the ultrasonic sensor to check for a person’s presence.
Based on the distance, the system displays the corresponding
message and prompts for an RFID card scan.
RFID Scan Warning after 5 Seconds: If a person is detected but
no RFID card is scanned within 5 seconds, the system issues a
warning.
Detection Timeout Warning after 1 Minute: If no activity from a
person is detected within 1 minute, the system stops the alert and
resets variables.
o RFID Card Check: If an RFID code is detected:
If the code is valid, the system grants access, turns on the green
LED, displays "Access Granted," and updates the access time.
If the code is invalid, the system denies access and displays "Access
Denied."
11 | P a g e
Part 6: Conclusion
The End
12 | P a g e