Group2 IOT

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 13

FPT UNIVERSITY

SOFTWARE ENGINEERING SPECIALIST


SUBJECT: INTERNET OF THINGS
-----------  ----------

ANTI–THEFT WARNING PROJECT


GROUP 2
CLASS: SE1889

Ha Noi, 11st October, 2024


Table of contents
Page
Part 1: Introduction to Member of the Group
……………………………………………………………. 2
Part 2: Introduction to Project
……………………………………………………………. 2
Part 3: Hardware Required
……………………………………………………………. 2
Part 4: Circuit
……………………………………………………………. 3
Part 5: Code
……………………………………………………………. 4
Part 6: Conclusion
…………………………………………………………… 12

1|Page
Part 1: Introduction to member of the group
Meet Our Team – Group 2

 Nguyen Van Nam - Leader – HE180863


 Nguyen Van Duc – Member – HE181691
 Do Manh Tuan – Member – HE181577

Part 2: Introduction to project


Anti – thieft warning project allows you to get the alert by an alarm sound
and activate a light signal when an object approaches. It serves as an effective
security measure, alerting users to nearby movement and helping prevent
potential intrusions. The combination of sound and light ensures that the alert
is noticeable, even from a distance, enhancing safety and awareness in the
monitored area.
Along with this, the system includes a card reader to identify authorized
individuals. When a registered person is recognized, the light changes color,
and the alarm sound stops. This feature allows for seamless access for
authorized users while maintaining security, ensuring that only approved
individuals can enter without triggering the alert.

Part 3: Hardware Required


 Arduino UNO R3 ATMEGA16U2 KIT
 LCD1602 Blue 5V
 I2C Converter Module for LCD1602
 LM35 Temperature Sensor
 6x6x10MM DIP 2-pin Push Button
 1/4W 5% 330Ω Resistor
 SRF05 Ultrasonic Sensor
 Jumper Wires
 830-Point Breadboard
 RGB LED
 5V Buzzer 9.5x12MM
 RFID Module RC522 13.56MHz
 Nodemcu IOT Module ESP8266 ESP-12E CP2102

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>

const int trigger = 2;


const int echo = 3;
const int buzzer = 5;
const int redLED = 6;
const int greenLED = 7;
const int blueLED = 8;
const int rst = 9;
const int sda = 10;
const int btn = 4;
const int lm35 = A0;

const int maxDistance = 400;

LiquidCrystal_I2C lcd(0x27, 16, 2);


NewPing sonar(trigger, echo, maxDistance);
MFRC522 mfrc522(sda, rst);

bool isProtected = true;


unsigned long lastAccessTime = 0;
unsigned long lastDetectionTime = 0;
int previousHumanState = -1;
bool isHotAlerted = false;
bool isAlert = false;
void setup() {
Serial.begin(9600);
Wire.begin();
SPI.begin();
mfrc522.PCD_Init();
lcd.init();
pinMode(buzzer, OUTPUT);
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
pinMode(blueLED, OUTPUT);
pinMode(btn, INPUT_PULLUP);
lcd.backlight();
displayWelcomeMessage();
}

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;
}

const String whiteList[] = {


"B37897E2",
"F45F5FB9",
"83AE00A2",
"F30BDF0E"
};
bool checkRFID(String id) {
for (const String &whitelistedID : whiteList) {
if (id.equalsIgnoreCase(whitelistedID)) return true;
}
return false;
}

void soundBuzzer(int volume, int duration) {

5|Page
analogWrite(buzzer, volume);
delay(duration);
analogWrite(buzzer, 0);
}

void displayLCD(const String &message, int row) {


lcd.setCursor(0, row);
lcd.print(" ");
lcd.setCursor(0, row);
lcd.print(message);
}

void controlLEDs(bool red, bool green, bool blue) {


digitalWrite(redLED, red ? HIGH : LOW);
digitalWrite(greenLED, green ? HIGH : LOW);
digitalWrite(blueLED, blue ? HIGH : LOW);
}

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 sendToMCU(String message) {


Serial.println(message);
}

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 (lastDetectionTime != 0 && millis() - lastDetectionTime > 5000) {

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();
}

String rfidID = getRFID();


if (rfidID.length() > 0) {
isAlert = false;
if (checkRFID(rfidID)) {
sendToMCU("Access Granted for: " + rfidID);
lastAccessTime = millis();
lastDetectionTime = 0;
displayLCD("Access Granted", 0);
displayLCD(rfidID, 1);
controlLEDs(false, true, false);
} else {
sendToMCU("Access Denied for: " + rfidID);
displayLCD("Access Denied", 0);
displayLCD(rfidID, 1);
}
}
}

nodeMCU Code:

8|Page
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>

const char* ssid = "DESKTOP-UPS32K0 2461";


const char* password = "1234567890";

const char* botToken = "7617930953:AAEA4uR9JsM18p5qYlfi1tHote09A-GR0yg";


const char* chatID = "7164609344";

void setup() {
Serial.begin(9600);

WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi");

while (WiFi.status() != WL_CONNECTED) {


delay(1000);
Serial.print(".");
}

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");
}
}

void sendTelegramMessage(String message) {


WiFiClientSecure client;
client.setInsecure();
delay(1000);

String url = String("https://api.telegram.org/bot") + botToken + "/sendMessage?chat_id=" + chatID +


"&text=" + urlEncode(message);
Serial.println(url);

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");
}
}

String urlEncode(String str) {


String encodedString = "";
char c;
char code0;
char code1;
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c == ' ') {
encodedString += '+';
} else if (isalnum(c)) {
encodedString += c;
} else {
code1 = (c & 0xF) + '0';
if ((c & 0xF) > 9) code1 = (c & 0xF) - 10 + 'A';
c = (c >> 4) & 0xF;
code0 = c + '0';
if (c > 9) code0 = c - 10 + 'A';
encodedString += '%';
encodedString += code0;
encodedString += code1;
}
}
return encodedString;
}

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 project successfully utilizes the Ultrasonic Sensor to accurately


detect the proximity of objects, enhancing security measures through real-
time alerts.
 An Arduino serves as the central controller, processing input from the
ultrasonic sensor and managing output to other components effectively.
 The implementation of an LCD Screen (compatible with the Hitachi
HD44780 driver) provides clear feedback to users, displaying messages based
on card scanning results and proximity detection.
 The RGB LED visually communicates the system's status, changing
colors to indicate different proximity levels and alarm conditions.
 The integration of a Card Reader facilitates secure access control,
allowing for personalized user interaction and streamlined entry management.
 Overall, the combination of these components creates an efficient, user-
friendly monitoring system that exemplifies the principles of IoT and
enhances security functionality.

The End

12 | P a g e

You might also like