From 1bbe1be1b18251c4a031155e39fa2b85395d44cd Mon Sep 17 00:00:00 2001 From: Dea_Fitri_Qurrota_Ayun Date: Thu, 27 Jun 2024 15:13:44 +0700 Subject: [PATCH] Upload files to "/" --- jatuh_kode.ino | 221 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 jatuh_kode.ino diff --git a/jatuh_kode.ino b/jatuh_kode.ino new file mode 100644 index 0000000..f3bf00c --- /dev/null +++ b/jatuh_kode.ino @@ -0,0 +1,221 @@ +//------------------------LIBRARY----------------------// +#include +#include +#include +#include +#include +#include +#include + +//-------------------Kredensial jaringan WiFi-----------------------// +#define WIFI_SSID "dejow" +#define WIFI_PASSWORD "ooodejow" + +//------------------KONFIGURASI TELEGRAM------------------// +#define BOT_TOKEN "7199714276:AAGJecg0q5CDAs4eMcj300Weof_8PU2G-QI" +#define CHAT_ID "6584276923" +const unsigned long BOT_MTBS = 1000; // mean time between scan messages +X509List cert(TELEGRAM_CERTIFICATE_ROOT); +WiFiClientSecure secured_client; +UniversalTelegramBot bot(BOT_TOKEN, secured_client); +unsigned long bot_lasttime; // last time messages' scan has been done + +//-----------------KONFIGURASI GPS------------------// +#include +#include +SoftwareSerial GPSSerial(D7, D8); +TinyGPSPlus gps; + +//-----------------KONFIGURASI MPU6050----------------// +unsigned long lastTime = 0; +float oldZ = 8; +Adafruit_MPU6050 mpu; + +//-----------------PIN BUZZER DAN PUSHBUTTON-----------------// +#define buzer D5 +#define tekan D6 +#define tekan1 D4 + +//----------------LOGIKA PUSH BUTTON------------------// +volatile bool buttonPressed = false; +volatile bool buttonPressed1 = false; + +//--------------VOID PUSH BUTTON-----------------// +void ICACHE_RAM_ATTR handleButtonPress() { + buttonPressed = true; +} + +void ICACHE_RAM_ATTR handleButtonPress1() { + buttonPressed1 = true; +} +//---------------------------------------------------------------------// +void setup() +{ + Serial.begin(115200); + GPSSerial.begin(9600); // Inisialisasi komunikasi serial dengan modul GPS + Serial.println("Initialize MPU6050"); + + pinMode(buzer, OUTPUT); + pinMode(tekan, INPUT_PULLUP); + pinMode(tekan1, INPUT_PULLUP); + + // Mencoba untuk terhubung ke jaringan Wifi: + Serial.print("Menghubungkan ke Wifi SSID "); + Serial.print(WIFI_SSID); + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + secured_client.setTrustAnchors(&cert); // Tambahkan sertifikat root untuk api.telegram.org + + while (WiFi.status() != WL_CONNECTED) + { + Serial.print("."); + delay(500); + } + Serial.print("\nWiFi terhubung. Alamat IP: "); + Serial.println(WiFi.localIP()); + + Serial.print("Mengambil waktu: "); + configTime(0, 0, "pool.ntp.org"); // dapatkan waktu UTC melalui NTP + time_t now = time(nullptr); + while (now < 24 * 3600) + { + Serial.print("."); + delay(100); + now = time(nullptr); + } + Serial.println(now); + + bot_lasttime = millis(); // Inisialisasi waktu pemindaian pesan + + while (!Serial) + delay(10); // will pause Zero, Leonardo, etc until serial console opens + + Serial.println("Adafruit MPU6050 test!"); + + // Try to initialize! + if (!mpu.begin()) { + Serial.println("Failed to find MPU6050 chip"); + while (1) { + delay(10); + } + } + + attachInterrupt(digitalPinToInterrupt(tekan), handleButtonPress, FALLING); + attachInterrupt(digitalPinToInterrupt(tekan1), handleButtonPress1, FALLING); +} + +//-----------------------------------------------------------------------------------// +void handleNewMessages(int numNewMessages) +{ + for (int i = 0; i < numNewMessages; i++) + { + String chat_id = bot.messages[i].chat_id; + String text = bot.messages[i].text; + + String from_name = bot.messages[i].from_name; + if (text == "/lokasi") + { + while (GPSSerial.available()) + { + gps.encode(GPSSerial.read()); + } + + if (gps.location.isValid()) + { + 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, ""); + } + else + { + bot.sendMessage(chat_id, "Lokasi GPS tidak valid, coba lagi.", ""); + } + } + else if ( text == "/buzer_on") + { + digitalWrite(buzer, HIGH); + bot.sendMessage(chat_id, "BUZER ON"); + } + else if (text == "/options") + { + String keyboardJson = "[[\"/lokasi\", \"/buzer_on\"],[\"/status\", \"/lokasi\"]]"; + bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true); + } + + } +} + +//--------------------------------------------------------------------------------------------// +void loop() +{ + sensors_event_t a, g, temp; + mpu.getEvent(&a, &g, &temp); + unsigned long currentTime = millis(); + float jarakDetik = (currentTime - lastTime) / 1000.0; // Time difference in seconds + + if (jarakDetik > 2) { + + if (oldZ - a.acceleration.z > 7 && (a.acceleration.x > 7 || a.acceleration.x < -7 || a.acceleration.y > 8 || a.acceleration.y < -8)) { + digitalWrite(buzer, HIGH); + Serial.println("Jatuh Terdeteksi"); + sendLocation(); + } + + Serial.println(oldZ); + Serial.println(a.acceleration.z); + + lastTime = currentTime; + oldZ = a.acceleration.z; + } + + if(buttonPressed) { + buttonPressed = false; + digitalWrite(buzer, LOW); + } + if(buttonPressed1) { + buttonPressed1 = false; + sendLocation(); + Serial.print("terkirim"); + } + + if (millis() - bot_lasttime > BOT_MTBS) + { + int numNewMessages = bot.getUpdates(bot.last_message_received + 1); + + while (numNewMessages) + { + Serial.println("menerima respon"); + handleNewMessages(numNewMessages); + numNewMessages = bot.getUpdates(bot.last_message_received + 1); + } + + bot_lasttime = millis(); + } + + while (GPSSerial.available()) + { + gps.encode(GPSSerial.read()); + } + + if (gps.location.isUpdated()) + { + } +} + +//----------------------------------------------------------------// +void sendLocation() { + float currentLat = gps.location.lat(); + float currentLng = gps.location.lng(); + String lokasi =" BUTUH PERTOLONGAN \n"; + lokasi +="Lokasi : https://www.google.com/maps/place/"; + lokasi +=String(currentLat,6); + lokasi +=","; + lokasi +=String(currentLng,6); + digitalWrite(buzer ,HIGH); + + bot.sendMessage(CHAT_ID, lokasi, ""); +} +//---------------------------------------------------------------//