0% found this document useful (0 votes)
12 views9 pages

Program Hery

The document contains two Arduino programs: one for controlling a relay system with RFID and buttons, and another for a Wemos device that handles GPS data and Telegram messaging. The first program manages the relay state based on button presses or RFID tags, while the second program connects to WiFi and Telegram to send GPS coordinates and respond to commands. Both programs utilize various libraries for communication and control, including SPI, LiquidCrystal, and TinyGPS.

Uploaded by

rici riansyah
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)
12 views9 pages

Program Hery

The document contains two Arduino programs: one for controlling a relay system with RFID and buttons, and another for a Wemos device that handles GPS data and Telegram messaging. The first program manages the relay state based on button presses or RFID tags, while the second program connects to WiFi and Telegram to send GPS coordinates and respond to commands. Both programs utilize various libraries for communication and control, including SPI, LiquidCrystal, and TinyGPS.

Uploaded by

rici riansyah
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

Program Arduino

// Komunikasi Wemos & Arduino


#define dataGPS digitalRead(3)
#define dataGPS1 digitalRead(4)

// Modul RFID
#include <SPI.h>
#include <MFRC522.h>

#define RST_PIN 8
#define SS_PIN 10

MFRC522 mfrc522(SS_PIN, RST_PIN);

// LCD 16x2
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Modul Keyless
#define tbA digitalRead(2)
#define tbB digitalRead(9)

// Buzzer
#define buzzer A7

// Modul Relay
#define relay A3
bool relayState = LOW;

void beep() {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
}

void beep2() {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
}

void beep3() {
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
}

void setup() {
Serial.begin(9600);
lcd.begin();
SPI.begin();
mfrc522.PCD_Init();

pinMode(2, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(buzzer, OUTPUT);
pinMode(relay, OUTPUT);

digitalWrite(relay, LOW);
Serial.println("Menunggu tombol atau NFC tag...");
}

void loop() {
// Tombol tbA ditekan -> Relay ON
if (tbA == HIGH || dataGPS) {
Serial.println("Tombol A ditekan, Relay ON");
digitalWrite(relay, HIGH);
relayState = HIGH;
beep();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem");
lcd.setCursor(1, 1);
lcd.print("Kelistrikan ON");
delay(3000);
lcd.clear();
}

// Tombol tbB ditekan -> Relay OFF


if (tbB == HIGH || dataGPS1) {
Serial.println("Tombol B ditekan, Relay OFF");
digitalWrite(relay, LOW);
relayState = LOW;
beep2();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem");
lcd.setCursor(0, 1);
lcd.print("Kelistrikan OFF");
delay(3000);
lcd.clear();
}
dataRFID();

lcd.setCursor(2, 0);
lcd.print("Silahkan Tag");
lcd.setCursor(4, 1);
lcd.print("ID Anda");
}

void dataRFID() {
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~ */
// cek kartu RFID baru
if (!mfrc522.PICC_IsNewCardPresent()) {
return;
}
// memilih kartu RFID
if (!mfrc522.PICC_ReadCardSerial()) {
return;
}
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~ */
String content = "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++) {
content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
content.concat(String(mfrc522.uid.uidByte[i], HEX));
}
content.toUpperCase();
Serial.println(content.substring(1));
delay(100);

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Data ID RFID


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
//> ID 1 ----------------- Card UID: 26 EB B0 0C
------------------//
if (content.substring(1) == "04 5D 53 D2 7E 5B 80") {
beep3();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Identitas Tidak");
lcd.setCursor(0, 1);
lcd.print("Teridentifikasi");
delay(3000);
lcd.clear();
} else if (content.substring(1) == "C5 61 07 2B") {
relayState = !relayState;
digitalWrite(relay, relayState);
Serial.println("C5 61 07 2B");

if (relayState) {
Serial.println("ID NFC cocok, Relay ON");
beep();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem");
lcd.setCursor(1, 1);
lcd.print("Kelistrikan ON");
delay(3000);
lcd.clear();
} else {
Serial.println("ID NFC cocok, Relay OFF");
beep2();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem");
lcd.setCursor(0, 1);
lcd.print("Kelistrikan OFF");
delay(3000);
lcd.clear();
}

delay(500); // Mencegah pembacaan berulang


} else if (content.substring(1) == "D3 20 0A 28") {
relayState = !relayState;
digitalWrite(relay, relayState);

if (relayState) {
Serial.println("ID NFC cocok, Relay ON");
beep();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem");
lcd.setCursor(1, 1);
lcd.print("Kelistrikan ON");
delay(3000);
lcd.clear();
} else {
Serial.println("ID NFC cocok, Relay OFF");
beep2();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Sistem");
lcd.setCursor(0, 1);
lcd.print("Kelistrikan OFF");
delay(3000);
lcd.clear();
}
delay(500); // Mencegah pembacaan berulang
} else {
beep3();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Identitas Tidak");
lcd.setCursor(0, 1);
lcd.print("Teridentifikasi");
delay(3000);
lcd.clear();
Serial.println("Tidak Terdeteksi");
}
}

Program Wemos

// Sensor GPS
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
const int RXPin = 5, TXPin = 4;
const uint32_t GPSBaud = 9600;
SoftwareSerial gps_module(RXPin, TXPin);
TinyGPSPlus gps;

// Kontrol LED
#define LED_PIN 14
#define LED_PIN2 12
bool ledState = false;

// Aplikasi Telegram
#include <ESP8266WiFi.h>
#include <WiFiClientSecureBearSSL.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

const char* ssid = "TEKNIK ELEKTRO";


const char* password = "Bermutu@2024#";

#define BOTtoken "7940874771:AAF0PpRCxtTelyCdIPe_sA1iu4-Gs1Xhp-c"


#define CHAT_ID "-1002344745086"
BearSSL::WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

void setup() {
// Setup LED
pinMode(LED_PIN, OUTPUT);
pinMode(LED_PIN2, OUTPUT);
digitalWrite(LED_PIN, LOW);
digitalWrite(LED_PIN2, LOW);

Serial.begin(9600);
Serial.println("Starting TelegramBot...");

// Koneksi ke WiFi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setInsecure(); // Disable certificate verification for
ESP8266
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println(WiFi.localIP());

// Setup GPS
gps_module.begin(GPSBaud);
delay(500);

bot.sendMessage(CHAT_ID, "Terhubung ke Telegram!", "");


Serial.println("Berhasil terhubung ke Telegram");
}

void loop() {
handleTelegramMessages();
}

// Variabel global untuk menyimpan lokasi terakhir


float lastLatitude = 0.0;
float lastLongitude = 0.0;
// Fungsi untuk mengirim koordinat GPS saat diminta
void displayInfo() {
unsigned long start = millis();
bool gpsAvailable = false;

// Tunggu data GPS selama maksimal 10 detik


while (millis() - start < 10000) {
while (gps_module.available() > 0) {
if (gps.encode(gps_module.read())) {
gpsAvailable = true;
}
}
if (gps.location.isValid()) {
float latitude = gps.location.lat();
float longitude = gps.location.lng();

// Simpan lokasi terakhir


lastLatitude = latitude;
lastLongitude = longitude;
delay(100);

String googleMapsLink = "https://maps.google.com/?q=" +


String(lastLatitude, 6) + "," + String(lastLongitude, 6);

// String googleMapsLink = "https://maps.google.com/?q=" +


String(latitude, 6) + "," + String(longitude, 6);
Serial.println("LAT: " + String(lastLatitude, 6) + " LONG: " +
String(lastLongitude, 6));
bot.sendMessage(CHAT_ID, "📍 Lokasi GPS Terdeteksi:\n" +
googleMapsLink, "");
return; // Keluar setelah berhasil mengirim lokasi
}
}

// Jika tidak ada data GPS valid setelah 10 detik


if (!gpsAvailable || !gps.location.isValid()) {
Serial.println("Sinyal GPS tidak valid!");
if (lastLatitude != 0.0 && lastLongitude != 0.0) {
// Kirim lokasi terakhir yang tersimpan
String lastLocationLink = "https://maps.google.com/?q=" +
String(lastLatitude, 6) + "," + String(lastLongitude, 6);
bot.sendMessage(CHAT_ID, "⚠️Sinyal GPS tidak valid!\n📍 Mengirim
lokasi terakhir:\n" + lastLocationLink, "");
Serial.println("Mengirim lokasi terakhir yang tersimpan.");
} else {
bot.sendMessage(CHAT_ID, "⚠️ Sinyal GPS tidak valid dan tidak ada
lokasi sebelumnya yang tersimpan!", "");
Serial.println("Tidak ada lokasi terakhir yang tersimpan.");
}
}
}

// Fungsi untuk membaca perintah dari Telegram


void handleTelegramMessages() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);

while (numNewMessages) {
Serial.println("Menerima Pesan Baru");
for (int i = 0; i < numNewMessages; i++) {
String chat_id = bot.messages[i].chat_id;
String text = bot.messages[i].text;

Serial.println("Pesan Diterima: " + text);

if (text == "/relayon") {
ledState = true;
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
bot.sendMessage(chat_id, "💡 Kelistrikan Motor ON!", "");
Serial.println("LED ON");
} else if (text == "/relayoff") {
ledState = false;
digitalWrite(LED_PIN2, HIGH);
delay(1000);
digitalWrite(LED_PIN2, LOW);
bot.sendMessage(chat_id, "💡 Kelistrikan Motor OFF!", "");
Serial.println("LED OFF");
} else if (text == "/status") {
String statusMessage = ledState ? "💡 Kelistrikan Motor ON" : "💡
Kelistrikan Motor OFF";
bot.sendMessage(chat_id, statusMessage, "");
Serial.println("Status");
} else if (text == "/gps") {
bot.sendMessage(chat_id, "⏳ Mencari lokasi GPS, tunggu
sebentar...", "");
displayInfo();
} else {
bot.sendMessage(chat_id, "? Perintah tidak dikenal.\nGunakan:\
n/relayon - Kelistrikan Motor ON\n/relayoff - Kelistrikan Motor
OFF\n/status - Cek status kelistrikan motor\n/gps - Kirim lokasi GPS",
"");
}
}
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
}
}

You might also like