first commit
This commit is contained in:
commit
8ab713a8ef
|
@ -0,0 +1,189 @@
|
||||||
|
#include <CTBot.h>
|
||||||
|
#include <TinyGPS++.h>
|
||||||
|
#include <SoftwareSerial.h>
|
||||||
|
#include <ezButton.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define BUTTON_PIN 16 // Pin GPIO untuk tombol A
|
||||||
|
#define BUTTON_PIN_1 5 // Pin GPIO untuk tombol B
|
||||||
|
#define BUTTON_PIN_2 4 // Pin GPIO untuk tombol C
|
||||||
|
#define RELAY_PIN 2 // Pin GPIO untuk relay
|
||||||
|
#define LED_PIN 0 // Pin GPIO untuk LED indikator
|
||||||
|
#define LED_PIN_2 14 // Pin GPIO untuk LED indikator BUZZER
|
||||||
|
|
||||||
|
CTBot myBot;
|
||||||
|
String ssid = "SmartKey"; // Nama WiFi Anda
|
||||||
|
String pass = "hondavario"; // Password WiFi Anda
|
||||||
|
String token = "6915507344:AAFfbYop1tVLs_q8Kr0SbpLQ_a_babJKEpw"; // Token bot Telegram
|
||||||
|
SoftwareSerial serial_gps(12, 13); // RX, TX
|
||||||
|
TinyGPSPlus gps;
|
||||||
|
|
||||||
|
ezButton button(BUTTON_PIN);
|
||||||
|
ezButton button1(BUTTON_PIN_1);
|
||||||
|
ezButton button2(BUTTON_PIN_2);
|
||||||
|
|
||||||
|
int ledState = LOW; // Status LED utama (LOW = mati, HIGH = hidup)
|
||||||
|
bool indicatorOn = false; // Status LED indikator (true = hidup, false = mati)
|
||||||
|
bool motorActive = false; // Status mesin motor (true = hidup, false = mati)
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(9600); // Inisialisasi serial
|
||||||
|
serial_gps.begin(9600); // Inisialisasi serial GPS
|
||||||
|
|
||||||
|
pinMode(LED_PIN, OUTPUT); // Set pin untuk LED utama ke mode output
|
||||||
|
pinMode(LED_PIN_2, OUTPUT); // Set pin untuk LED indikator ke mode output
|
||||||
|
pinMode(RELAY_PIN, OUTPUT); // Set pin untuk relay ke mode output
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
digitalWrite(LED_PIN, HIGH);
|
||||||
|
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
digitalWrite(RELAY_PIN, LOW); // Aktifkan relay
|
||||||
|
digitalWrite(LED_PIN_2, LOW);
|
||||||
|
delay(240);
|
||||||
|
digitalWrite(RELAY_PIN, HIGH); // Matikan relay
|
||||||
|
digitalWrite(LED_PIN_2, HIGH);
|
||||||
|
delay(240);
|
||||||
|
}
|
||||||
|
|
||||||
|
button.setDebounceTime(30); // Atur waktu debounce untuk tombol 1
|
||||||
|
button1.setDebounceTime(30); // Atur waktu debounce untuk tombol 2
|
||||||
|
button2.setDebounceTime(30); // Atur waktu debounce untuk tombol 3
|
||||||
|
|
||||||
|
Serial.println("Starting Telegram...");
|
||||||
|
myBot.wifiConnect(ssid, pass);
|
||||||
|
myBot.setTelegramToken(token);
|
||||||
|
if (myBot.testConnection())
|
||||||
|
Serial.println("Terhubung dengan Telegram");
|
||||||
|
else
|
||||||
|
Serial.println("Tidak Terhubung dengan Telegram");
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
button.loop(); // A
|
||||||
|
button1.loop(); // B
|
||||||
|
button2.loop(); // C
|
||||||
|
|
||||||
|
if (button.isPressed()) {
|
||||||
|
if (!motorActive) {
|
||||||
|
turnOnMotor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (button1.isPressed()) {
|
||||||
|
if (motorActive) {
|
||||||
|
turnOffMotor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (button2.isPressed()) {
|
||||||
|
if (!indicatorOn) {
|
||||||
|
indicatorOn = true; // Aktifkan kedipan relay
|
||||||
|
} else {
|
||||||
|
indicatorOn = false; // Matikan kedipan relay
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (indicatorOn) {
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
digitalWrite(LED_PIN_2, LOW);
|
||||||
|
delay(220);
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
digitalWrite(LED_PIN_2, HIGH);
|
||||||
|
delay(220);
|
||||||
|
} else {
|
||||||
|
digitalWrite(RELAY_PIN, HIGH); // Matikan relay jika indicatorOn bernilai false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle pesan Telegram
|
||||||
|
handleTelegramMessages();
|
||||||
|
|
||||||
|
// Handle data GPS
|
||||||
|
handleGPSData();
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleTelegramMessages() {
|
||||||
|
TBMessage msg;
|
||||||
|
if (myBot.getNewMessage(msg)) {
|
||||||
|
if (msg.messageType == CTBotMessageText) {
|
||||||
|
if (msg.text == "/start") {
|
||||||
|
sendWelcomeMessage(msg.sender.id, msg.sender.firstName);
|
||||||
|
} else if (msg.text == "/Lokasi") {
|
||||||
|
sendLocationMessage(msg.sender.id);
|
||||||
|
} else if (msg.text == "/MatikanMotor") {
|
||||||
|
if (motorActive) {
|
||||||
|
turnOffMotor();
|
||||||
|
}
|
||||||
|
} else if (msg.text == "/HidupkanMotor") {
|
||||||
|
if (!motorActive) {
|
||||||
|
turnOnMotor();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleGPSData() {
|
||||||
|
while (serial_gps.available() > 0) {
|
||||||
|
if (gps.encode(serial_gps.read())) {
|
||||||
|
// Handle GPS data if needed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void turnOnMotor() {
|
||||||
|
digitalWrite(LED_PIN, LOW); // Motor On
|
||||||
|
digitalWrite(LED_PIN_2, LOW);
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_PIN_2, HIGH);
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_PIN_2, LOW);
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_PIN_2, HIGH);
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
motorActive = true;
|
||||||
|
myBot.sendMessage(1308574402, "Motor siap untuk dihidupkan"); // Mengirim pesan ke ID Anda
|
||||||
|
|
||||||
|
// Send GPS location after turning on the motor
|
||||||
|
sendLocationMessage(1308574402); // Mengirim pesan lokasi ke ID Anda
|
||||||
|
}
|
||||||
|
|
||||||
|
void turnOffMotor() {
|
||||||
|
digitalWrite(LED_PIN, HIGH); // Motor OFF
|
||||||
|
digitalWrite(LED_PIN_2, LOW);
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_PIN_2, HIGH);
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_PIN_2, LOW);
|
||||||
|
digitalWrite(RELAY_PIN, LOW);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_PIN_2, HIGH);
|
||||||
|
digitalWrite(RELAY_PIN, HIGH);
|
||||||
|
motorActive = false;
|
||||||
|
myBot.sendMessage(1308574402, "Motor telah dimatikan!"); // Mengirim pesan ke ID Anda
|
||||||
|
sendLocationMessage(1308574402);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendWelcomeMessage(int chatId, String firstName) {
|
||||||
|
String welcomeMsg = "Selamat datang, " + firstName + "!";
|
||||||
|
myBot.sendMessage(chatId, welcomeMsg);
|
||||||
|
Serial.println("Pesan Selamat Datang dikirim");
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendLocationMessage(int chatId) {
|
||||||
|
if (gps.location.isValid()) {
|
||||||
|
String latitude = String(gps.location.lat(), 6);
|
||||||
|
String longitude = String(gps.location.lng(), 6);
|
||||||
|
String googleMapsURL = "https://maps.google.com/?q=" + latitude + "," + longitude;
|
||||||
|
String locationMsg = "Lokasi saat ini:\nLatitude: " + latitude + "\nLongitude: " + longitude + "\nGoogle Maps URL: " + googleMapsURL;
|
||||||
|
myBot.sendMessage(chatId, locationMsg);
|
||||||
|
Serial.println("Pesan Lokasi dan Google Maps URL dikirim");
|
||||||
|
} else {
|
||||||
|
myBot.sendMessage(chatId, "GPS tidak dapat menemukan lokasi saat ini.");
|
||||||
|
Serial.println("GPS tidak dapat menemukan lokasi saat ini.");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue