Upload files to "Kode Program ESP32"
This commit is contained in:
parent
219479c3d4
commit
8b2a5d2a64
|
@ -0,0 +1,114 @@
|
|||
#include <TinyGPS++.h>
|
||||
#include <HardwareSerial.h>
|
||||
|
||||
// Definisikan Serial untuk LoRa dan GPS
|
||||
HardwareSerial SerialAT(2); // Menggunakan UART2 untuk LoRa
|
||||
HardwareSerial GPSSerial(1); // Menggunakan UART1 untuk GPS
|
||||
|
||||
TinyGPSPlus gps;
|
||||
|
||||
const int buttonPin = 18; // Pin untuk pushbutton
|
||||
bool buttonState = false; // Variabel untuk menyimpan status pushbutton
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
// Inisialisasi Serial untuk LoRa
|
||||
SerialAT.begin(9600, SERIAL_8N1, 4, 5);
|
||||
|
||||
// Inisialisasi Serial untuk GPS
|
||||
GPSSerial.begin(9600, SERIAL_8N1, 16, 17);
|
||||
|
||||
// Inisialisasi pin pushbutton sebagai input
|
||||
pinMode(buttonPin, INPUT_PULLUP);
|
||||
|
||||
Serial.println("GPS dan LoRa Mulai");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Membaca data dari modul GPS
|
||||
while (GPSSerial.available()) {
|
||||
gps.encode(GPSSerial.read());
|
||||
}
|
||||
|
||||
// Membaca status pushbutton
|
||||
buttonState = digitalRead(buttonPin);
|
||||
|
||||
// Jika pushbutton ditekan dan lokasi GPS valid, tampilkan dan kirim melalui LoRa
|
||||
if (buttonState == LOW) { // LOW berarti tombol ditekan jika menggunakan INPUT_PULLUP
|
||||
if (gps.location.isValid()) {
|
||||
// Menampilkan lokasi GPS
|
||||
Serial.print(F("Location: "));
|
||||
Serial.print(gps.location.lat(), 6);
|
||||
Serial.print(F(","));
|
||||
Serial.println(gps.location.lng(), 6);
|
||||
|
||||
// Mengirim lokasi GPS melalui LoRa dengan tanda pushbutton ditekan
|
||||
int snr = getSNR();
|
||||
int rssi = getRSSI();
|
||||
unsigned long delayValue = calculateDelay();
|
||||
|
||||
SerialAT.print(gps.location.lat(), 6);
|
||||
SerialAT.print(",");
|
||||
SerialAT.print(gps.location.lng(), 6);
|
||||
SerialAT.print(",");
|
||||
SerialAT.print("PUSH,");
|
||||
SerialAT.print(snr);
|
||||
SerialAT.print(",");
|
||||
SerialAT.print(rssi);
|
||||
SerialAT.print(",");
|
||||
SerialAT.println(delayValue);
|
||||
|
||||
// Menampilkan nilai SNR, RSSI, dan Delay
|
||||
Serial.print(F("SNR: "));
|
||||
Serial.println(snr);
|
||||
Serial.print(F("RSSI: "));
|
||||
Serial.println(rssi);
|
||||
Serial.print(F("Delay: "));
|
||||
Serial.println(delayValue);
|
||||
|
||||
} else {
|
||||
Serial.println(F("INVALID"));
|
||||
}
|
||||
}
|
||||
|
||||
delay(500);
|
||||
}
|
||||
|
||||
int getSNR() {
|
||||
// Nilai SNR dihitung berdasarkan kekuatan sinyal GPS
|
||||
// Asumsi: Semakin kuat sinyal GPS, semakin baik SNR-nya
|
||||
// Ini hanya contoh sederhana, Anda mungkin memerlukan algoritma yang lebih kompleks
|
||||
if (gps.hdop.isValid()) {
|
||||
return (int)(100.0 / gps.hdop.hdop()); // Contoh konversi HDOP ke SNR
|
||||
} else {
|
||||
return -1; // Nilai SNR tidak valid
|
||||
}
|
||||
}
|
||||
|
||||
int getRSSI() {
|
||||
// Nilai RSSI diambil dari kualitas sinyal GPS
|
||||
// Asumsi: Semakin rendah HDOP, semakin baik RSSI-nya
|
||||
// Ini hanya contoh sederhana, Anda mungkin memerlukan algoritma yang lebih kompleks
|
||||
if (gps.hdop.isValid()) {
|
||||
return (int)(-50.0 * gps.hdop.hdop()); // Contoh konversi HDOP ke RSSI
|
||||
} else {
|
||||
return -100; // Nilai RSSI tidak valid
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long calculateDelay() {
|
||||
// Hitung delay berdasarkan waktu antara saat pushbutton ditekan dan saat GPS memberikan data yang valid
|
||||
static unsigned long lastPressTime = 0;
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
if (buttonState == LOW) {
|
||||
if (lastPressTime == 0) {
|
||||
lastPressTime = currentTime;
|
||||
}
|
||||
return currentTime - lastPressTime;
|
||||
} else {
|
||||
lastPressTime = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
#include <TinyGPS++.h>
|
||||
#include <HardwareSerial.h>
|
||||
#include <WiFi.h>
|
||||
#include <FirebaseESP32.h>
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
// Informasi Wi-Fi
|
||||
const char* ssid = "atfalprm";
|
||||
const char* password = "sakarepmuwes";
|
||||
|
||||
// Informasi Firebase
|
||||
#define FIREBASE_HOST "tomboldarurat-d4533-default-rtdb.firebaseio.com"
|
||||
#define FIREBASE_AUTH "your_firebase_auth_token"
|
||||
|
||||
HardwareSerial SerialAT(2);
|
||||
TinyGPSPlus gps;
|
||||
|
||||
const int ledPin = 12;
|
||||
const int buzzerPin = 13;
|
||||
|
||||
FirebaseData firebaseData;
|
||||
FirebaseConfig firebaseConfig;
|
||||
FirebaseAuth firebaseAuth;
|
||||
|
||||
WiFiUDP ntpUDP;
|
||||
NTPClient timeClient(ntpUDP, "pool.ntp.org", 25200, 60000); // UTC+7
|
||||
|
||||
const long utcOffsetInSeconds = 25200; // UTC+7 jam (7 * 3600 detik)
|
||||
const unsigned long alarmDuration = 30000; // 30 detik
|
||||
unsigned long alarmStartTime = 0;
|
||||
bool alarmActive = false;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
SerialAT.begin(9600, SERIAL_8N1, 4, 5);
|
||||
pinMode(ledPin, OUTPUT);
|
||||
pinMode(buzzerPin, OUTPUT);
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
Serial.print("Menghubungkan ke Wi-Fi");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println();
|
||||
Serial.println("Terhubung ke Wi-Fi");
|
||||
|
||||
firebaseConfig.host = FIREBASE_HOST;
|
||||
firebaseConfig.signer.tokens.legacy_token = FIREBASE_AUTH;
|
||||
|
||||
Firebase.begin(&firebaseConfig, &firebaseAuth);
|
||||
|
||||
// Mulai klien NTP
|
||||
timeClient.begin();
|
||||
timeClient.setTimeOffset(utcOffsetInSeconds); // Setel offset waktu
|
||||
|
||||
// Cek koneksi Firebase
|
||||
if (Firebase.ready()) {
|
||||
Serial.println("Terhubung ke Firebase");
|
||||
} else {
|
||||
Serial.println("Gagal terhubung ke Firebase");
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (SerialAT.available()) {
|
||||
String receivedData = SerialAT.readStringUntil('\n');
|
||||
Serial.println("Diterima dari LoRa: " + receivedData);
|
||||
|
||||
int firstCommaIndex = receivedData.indexOf(',');
|
||||
int secondCommaIndex = receivedData.indexOf(',', firstCommaIndex + 1);
|
||||
int thirdCommaIndex = receivedData.indexOf(',', secondCommaIndex + 1);
|
||||
int fourthCommaIndex = receivedData.indexOf(',', thirdCommaIndex + 1);
|
||||
int fifthCommaIndex = receivedData.indexOf(',', fourthCommaIndex + 1);
|
||||
|
||||
if (firstCommaIndex != -1 && secondCommaIndex != -1 && thirdCommaIndex != -1 && fourthCommaIndex != -1 && fifthCommaIndex != -1) {
|
||||
String lat = receivedData.substring(0, firstCommaIndex);
|
||||
String lng = receivedData.substring(firstCommaIndex + 1, secondCommaIndex);
|
||||
String pushState = receivedData.substring(secondCommaIndex + 1, thirdCommaIndex);
|
||||
String snr = receivedData.substring(thirdCommaIndex + 1, fourthCommaIndex);
|
||||
String rssi = receivedData.substring(fourthCommaIndex + 1, fifthCommaIndex);
|
||||
String delayValue = receivedData.substring(fifthCommaIndex + 1);
|
||||
|
||||
Serial.print("Latitude: ");
|
||||
Serial.print(lat);
|
||||
Serial.print(", Longitude: ");
|
||||
Serial.println(lng);
|
||||
Serial.print("Push State: ");
|
||||
Serial.println(pushState);
|
||||
Serial.print("SNR: ");
|
||||
Serial.println(snr);
|
||||
Serial.print("RSSI: ");
|
||||
Serial.println(rssi);
|
||||
Serial.print("Delay: ");
|
||||
Serial.println(delayValue);
|
||||
|
||||
// Mulai alarm
|
||||
alarmStartTime = millis();
|
||||
alarmActive = true;
|
||||
|
||||
// Sinkronisasi waktu dengan server NTP
|
||||
timeClient.update();
|
||||
|
||||
// Mendapatkan waktu saat ini dari klien NTP
|
||||
time_t epochTime = timeClient.getEpochTime();
|
||||
struct tm * timeinfo;
|
||||
timeinfo = localtime(&epochTime);
|
||||
char timeStr[20];
|
||||
sprintf(timeStr, "%02d-%02d-%04d %02d:%02d:%02d", timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_year + 1900, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
|
||||
|
||||
// Membuat objek JSON untuk mengirim data
|
||||
FirebaseJson json;
|
||||
json.set("/latitude", lat);
|
||||
json.set("/longitude", lng);
|
||||
json.set("/time", timeStr);
|
||||
json.set("/pushState", pushState);
|
||||
json.set("/snr", snr);
|
||||
json.set("/rssi", rssi);
|
||||
json.set("/delay", delayValue);
|
||||
|
||||
// Mengirim data ke Firebase di path /location dengan kunci unik
|
||||
if (Firebase.push(firebaseData, "/location", json)) {
|
||||
Serial.println("Data dikirim ke Firebase");
|
||||
} else {
|
||||
Serial.print("Gagal mengirim data ke Firebase: ");
|
||||
Serial.println(firebaseData.errorReason());
|
||||
}
|
||||
|
||||
} else {
|
||||
Serial.println("Format data tidak benar");
|
||||
}
|
||||
|
||||
// Membersihkan buffer masuk
|
||||
while (SerialAT.available()) {
|
||||
SerialAT.read();
|
||||
}
|
||||
}
|
||||
|
||||
// Mengatur waktu alarm
|
||||
if (alarmActive) {
|
||||
if (millis() - alarmStartTime < alarmDuration) {
|
||||
digitalWrite(ledPin, HIGH);
|
||||
digitalWrite(buzzerPin, HIGH);
|
||||
} else {
|
||||
digitalWrite(ledPin, LOW);
|
||||
digitalWrite(buzzerPin, LOW);
|
||||
alarmActive = false;
|
||||
}
|
||||
}
|
||||
|
||||
delay(500);
|
||||
}
|
Loading…
Reference in New Issue