0% found this document useful (0 votes)
10 views

Coding

Uploaded by

Andreas Hendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Coding

Uploaded by

Andreas Hendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

D.

KODE PROGRAM SISTEM KEAMANAN RUMAH BERBASIS ARDUINO


DENGAN INTEGRASI BOT TELEGRAM UNTUK MONITORING JARAK
JAUH

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>

// Replace with your network credentials


const char* ssid = "Boto jaya";
const char* password = "andre007";

// Telegram BOT Token (Get from Botfather)


#define BOTtoken
"7327758106:AAGFBUYXo9qTEP_CClszA_bh2ktIjGP00Fo"
#define CHAT_ID "1285064025"

WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Define pins for sensors and buzzer


const int motionSensor = D1;
const int vibrationSensor = D2;
const int flameSensor = D3; // Pin untuk sensor deteksi
api IR 1C90009
const int servoPin = D4; // Pin untuk servo motor
const int buzzerPin = D0; // Pin untuk buzzer

// Define pins for RFID


#define SS_PIN D8 // SDA (SS) pin for RFID
#define RST_PIN 3 // RST pin for RFID (GPIO3)
MFRC522 rfid(SS_PIN, RST_PIN); // Create MFRC522 instance

Servo myServo; // Buat objek Servo

volatile bool flameDetected = false;


bool motionDetected = false;
bool vibrationDetected = false;

unsigned long lastMotionTime = 0;


unsigned long lastVibrationTime = 0;
const unsigned long debounceDelay = 200; // 200 ms
debounce delay
void IRAM_ATTR flameInterrupt() {
flameDetected = true;
}

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

pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);

pinMode(motionSensor, INPUT);
pinMode(vibrationSensor, INPUT);
pinMode(flameSensor, INPUT);
pinMode(buzzerPin, OUTPUT);

Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("Connected to WiFi");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
bot.sendMessage(CHAT_ID, "Terkoneksi ke jaringan", "");

// Initialize Telegram Bot


client.setInsecure();

// Initialize Servo
myServo.attach(servoPin);

// Calibrate Servo to position 0


Serial.println("Calibrating servo to position 0...");
myServo.write(0); // Gerakkan servo ke posisi 0
delay(2000); // Tunggu 2 detik untuk memastikan servo
berada di posisi 0

// Initialize RFID
SPI.begin(); // Inisialisasi SPI bus dengan pin yang
sesuai
rfid.PCD_Init(); // Inisialisasi MFRC522
// Wait for sensor stabilization
Serial.println("Stabilizing sensors...");
delay(5000); // Tunggu 5 detik untuk stabilisasi sensor

// Attach interrupt to flame sensor


attachInterrupt(digitalPinToInterrupt(flameSensor),
flameInterrupt, FALLING); // Deteksi api ketika nilai
berubah dari HIGH ke LOW
}

void handleNewMessages(int numNewMessages) {


Serial.println("handleNewMessages");
Serial.println(String(numNewMessages));

for (int i = 0; i < numNewMessages; i++) {


String chat_id = String(bot.messages[i].chat_id);
if (chat_id != CHAT_ID) {
bot.sendMessage(chat_id, "Unauthorized user", "");
continue;
}

String text = bot.messages[i].text;

if (text == "/servo") {
Serial.println("Servo command received");
bot.sendMessage(CHAT_ID, "Moving servo to 150
degrees for 8 seconds", "");
myServo.write(150);
delay(8000);
myServo.write(0);
delay(1000);
bot.sendMessage(CHAT_ID, "Servo moved back to 0
degrees", "");
}
}
}

void checkSensors() {
unsigned long currentTime = millis();

int isMotionDetected = digitalRead(motionSensor);


int isVibrationDetected = digitalRead(vibrationSensor);
// Debounce motion sensor
if (isMotionDetected == HIGH && (currentTime -
lastMotionTime) > debounceDelay) {
motionDetected = true;
lastMotionTime = currentTime;
} else if (isMotionDetected == LOW && motionDetected) {
motionDetected = false;
lastMotionTime = currentTime;
}

// Debounce vibration sensor


if (isVibrationDetected == HIGH && (currentTime -
lastVibrationTime) > debounceDelay) {
vibrationDetected = true;
lastVibrationTime = currentTime;
} else if (isVibrationDetected == LOW &&
vibrationDetected) {
vibrationDetected = false;
lastVibrationTime = currentTime;
}

// Handle motion detection


if (motionDetected) {
Serial.println("Motion detected!");
digitalWrite(LED_BUILTIN, LOW); // Nyalakan LED

// Send message to Telegram


String message = "Motion detected!";
Serial.println("Sending motion message to
Telegram...");
if (bot.sendMessage(CHAT_ID, message, "")) {
Serial.println("Motion message sent successfully");
} else {
Serial.println("Failed to send motion message");
}
} else {
digitalWrite(LED_BUILTIN, HIGH); // Matikan LED
}

// Handle vibration detection


if (vibrationDetected) {
Serial.println("Vibration detected!");
// // Move servo when vibration detected
// Serial.println("Moving servo...");
// myServo.write(150); // Gerakkan servo ke posisi 150
derajat
// delay(8000); // Tunggu 8 detik
// myServo.write(0); // Kembalikan servo ke posisi
awal
// delay(1000); // Tunggu 1 detik

// Send message to Telegram


String message = "Vibration detected!";
Serial.println("Sending vibration message to
Telegram...");
if (bot.sendMessage(CHAT_ID, message, "")) {
Serial.println("Vibration message sent
successfully");
} else {
Serial.println("Failed to send vibration message");
}
vibrationDetected = false; // Reset vibration
detection after handling
}

// Handle flame detection


if (flameDetected) {
Serial.println("Flame detected!");

// Turn on buzzer
digitalWrite(buzzerPin, HIGH);

// Send message to Telegram


String message = "Flame detected!";
Serial.println("Sending flame message to
Telegram...");
if (bot.sendMessage(CHAT_ID, message, "")) {
Serial.println("Flame message sent successfully");
} else {
Serial.println("Failed to send flame message");
}
flameDetected = false; // Reset flame detection after
handling
// Turn off buzzer after a delay
delay(5000);
digitalWrite(buzzerPin, LOW);
}

// Handle RFID detection


if (rfid.PICC_IsNewCardPresent() &&
rfid.PICC_ReadCardSerial()) {
Serial.println("RFID card detected!");

// Move servo when RFID card detected


Serial.println("Moving servo...");
myServo.write(150); // Gerakkan servo ke posisi 150
derajat
delay(8000); // Tunggu 8 detik
myServo.write(0); // Kembalikan servo ke posisi awal
delay(1000); // Tunggu 1 detik

// Send message to Telegram


String message = "RFID card detected and servo
moved!";
Serial.println("Sending RFID message to Telegram...");
if (bot.sendMessage(CHAT_ID, message, "")) {
Serial.println("RFID message sent successfully");
} else {
Serial.println("Failed to send RFID message");
}
rfid.PICC_HaltA(); // Halt the card to stop reading
}
}

void loop() {
checkSensors();

int numNewMessages =
bot.getUpdates(bot.last_message_received + 1);
while (numNewMessages) {
Serial.println("Got response");
handleNewMessages(numNewMessages);
numNewMessages =
bot.getUpdates(bot.last_message_received + 1);
}
}

You might also like