164 lines
4.7 KiB
C++
164 lines
4.7 KiB
C++
#include <WiFi.h>
|
|
#include <WiFiClientSecure.h>
|
|
#include <UniversalTelegramBot.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
const char* ssid = "ragamulya";
|
|
const char* password = "ragamulyapratama";
|
|
|
|
// inisialisasi Bot Token
|
|
#define BOTtoken "7067525572:AAHYFVB9PzGCGuB07TteNHDprgCt1yNI8SI" // Bot Token dari BotFather
|
|
|
|
// chat id dari @myidbot
|
|
#define CHAT_ID "6288849710"
|
|
|
|
WiFiClientSecure client;
|
|
UniversalTelegramBot bot(BOTtoken, client);
|
|
|
|
int botRequestDelay = 100;
|
|
unsigned long lastTimeBotRan;
|
|
|
|
#include <TinyGPS++.h>
|
|
#include <HardwareSerial.h>
|
|
|
|
HardwareSerial GPSSerial(1);
|
|
TinyGPSPlus gps;
|
|
|
|
// Inisialisasi pin buzzer
|
|
const int buzzerPin = 23;
|
|
// Inisialisasi pin push button
|
|
const int buttonPin = 26;
|
|
bool buttonState = false;
|
|
bool lastButtonState = false;
|
|
|
|
// Variabel untuk menyimpan histori lokasi
|
|
String locationHistory[5];
|
|
int historyIndex = 0;
|
|
|
|
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;
|
|
Serial.println(text);
|
|
|
|
String from_name = bot.messages[i].from_name;
|
|
while(GPSSerial.available()) {
|
|
gps.encode(GPSSerial.read());
|
|
}
|
|
if (gps.charsProcessed() > 10) {
|
|
float currentLat = gps.location.lat();
|
|
float currentLng = gps.location.lng();
|
|
|
|
if (text == "/start") {
|
|
String control = "Selamat Datang, " + from_name + ".\n";
|
|
control += "Gunakan Commands Di Bawah Untuk Mengetahui Lokasi Anak \n\n";
|
|
control += "/start Untuk Memulai \n";
|
|
control += "/lokasi Untuk Mengetahui lokasi saat ini \n";
|
|
control += "/buzzer Untuk Menyalakan Buzzer\n";
|
|
control += "/matikan Untuk Mematikan Buzzer\n";
|
|
control += "/history Untuk Melihat 5 Histori Terakhir\n";
|
|
bot.sendMessage(chat_id, control, "");
|
|
}
|
|
|
|
if (text == "/lokasi"){
|
|
String lokasi = "lokasi : https://www.google.com/maps/place/";
|
|
lokasi +=String(currentLat,6);
|
|
lokasi +=",";
|
|
lokasi +=String(currentLng,6);
|
|
bot.sendMessage(chat_id, lokasi, "");
|
|
|
|
// Menyimpan lokasi ke dalam histori
|
|
locationHistory[historyIndex] = lokasi;
|
|
historyIndex = (historyIndex + 1) % 5;
|
|
}
|
|
|
|
// Menyalakan buzzer saat menerima pesan /buzzer
|
|
if (text == "/buzzer") {
|
|
digitalWrite(buzzerPin, HIGH);
|
|
bot.sendMessage(chat_id, "Buzzer telah dinyalakan.", "");
|
|
}
|
|
|
|
// Mematikan buzzer saat menerima pesan /matikan
|
|
if (text == "/matikan") {
|
|
digitalWrite(buzzerPin, LOW);
|
|
bot.sendMessage(chat_id, "Buzzer telah dimatikan.", "");
|
|
}
|
|
|
|
// Mengirimkan histori lokasi saat menerima pesan /history
|
|
if (text == "/history") {
|
|
String historyMsg = "Histori Lokasi:\n";
|
|
for (int i = 0; i < 5; i++) {
|
|
if (locationHistory[i] != "") {
|
|
historyMsg += String(i + 1) + ". " + locationHistory[i] + "\n";
|
|
}
|
|
}
|
|
bot.sendMessage(chat_id, historyMsg, "");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
pinMode(buzzerPin, OUTPUT); // Mengatur pin buzzer sebagai output
|
|
pinMode(buttonPin, INPUT_PULLUP); // Mengatur pin push button sebagai input dengan resistor pull-up internal
|
|
GPSSerial.begin(9600, SERIAL_8N1, 16, 17);
|
|
// Koneksi Ke Wifi
|
|
WiFi.mode(WIFI_STA);
|
|
WiFi.begin(ssid, password);
|
|
#ifdef ESP32
|
|
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
|
|
#endif
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(1000);
|
|
Serial.println("Connecting to WiFi..");
|
|
}
|
|
// Print ESP32 Local IP Address
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void loop() {
|
|
// Membaca status tombol tekan
|
|
buttonState = digitalRead(buttonPin);
|
|
|
|
// Jika tombol tekan ditekan (LOW) dan status sebelumnya HIGH
|
|
if (buttonState == LOW && lastButtonState == HIGH) {
|
|
sendLocation();
|
|
delay(2000); // Debouncing delay
|
|
}
|
|
|
|
lastButtonState = buttonState;
|
|
|
|
if (millis() > lastTimeBotRan + botRequestDelay) {
|
|
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);
|
|
}
|
|
lastTimeBotRan = millis();
|
|
}
|
|
}
|
|
|
|
void sendLocation() {
|
|
float currentLat = gps.location.lat();
|
|
float currentLng = gps.location.lng();
|
|
String lokasi = "Lokasi : https://www.google.com/maps/place/";
|
|
lokasi +=String(currentLat,6);
|
|
lokasi +=",";
|
|
lokasi +=String(currentLng,6);
|
|
bot.sendMessage(CHAT_ID, lokasi, "");
|
|
|
|
// Menyimpan lokasi ke dalam histori
|
|
locationHistory[historyIndex] = lokasi;
|
|
historyIndex = (historyIndex + 1) % 5;
|
|
}
|