update 2 file code fith format ino
This commit is contained in:
parent
4364746286
commit
b2923e84a9
|
@ -135,3 +135,227 @@ void calibrateSensor() {
|
|||
|
||||
Serial.println("Kalibrasi selesai!");
|
||||
}
|
||||
|
||||
|
||||
v2
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <FirebaseESP8266.h>
|
||||
#include <SPI.h>
|
||||
#include <MFRC522.h>
|
||||
#include <Servo.h>
|
||||
|
||||
// WiFi Credentials
|
||||
#define WIFI_SSID "KONTRAKAN OYI"
|
||||
#define WIFI_PASSWORD "warkopoyi"
|
||||
|
||||
// Firebase Configuration
|
||||
#define FIREBASE_HOST "smartcab-8bb42-default-rtdb.firebaseio.com"
|
||||
#define FIREBASE_AUTH "kiiQoFa6Ckp7bL2oRLbaTSGQth9z0PgN64Ybv8dw"
|
||||
|
||||
FirebaseConfig config;
|
||||
FirebaseAuth auth;
|
||||
FirebaseData firebaseData;
|
||||
|
||||
// RFID Configuration
|
||||
#define SS_PIN D4 // Pin SDA RFID
|
||||
#define RST_PIN D3 // Pin RST RFID
|
||||
MFRC522 mfrc522(SS_PIN, RST_PIN);
|
||||
|
||||
// Servo Configuration
|
||||
Servo myServo;
|
||||
#define SERVO_PIN D2 // Pin servo
|
||||
|
||||
// **ID kartu yang diizinkan**
|
||||
String kartuTerdaftar = "53ed8434";
|
||||
bool servoTerbuka = false; // Status awal servo (tertutup)
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
// Koneksi ke WiFi
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
Serial.print("Menghubungkan ke WiFi");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("\nWiFi Terhubung!");
|
||||
|
||||
// Konfigurasi Firebase
|
||||
config.host = FIREBASE_HOST;
|
||||
config.signer.tokens.legacy_token = FIREBASE_AUTH;
|
||||
Firebase.begin(&config, &auth);
|
||||
Firebase.reconnectWiFi(true);
|
||||
|
||||
// Inisialisasi RFID
|
||||
SPI.begin();
|
||||
mfrc522.PCD_Init();
|
||||
|
||||
// Inisialisasi Servo
|
||||
myServo.attach(SERVO_PIN);
|
||||
myServo.write(0); // Posisi awal servo terkunci
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Cek apakah kartu RFID terdeteksi
|
||||
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
|
||||
Serial.println("Kartu Terdeteksi!");
|
||||
|
||||
// Membaca UID kartu
|
||||
String rfidUID = "";
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||
rfidUID += String(mfrc522.uid.uidByte[i], HEX);
|
||||
}
|
||||
|
||||
Serial.print("UID: ");
|
||||
Serial.println(rfidUID);
|
||||
|
||||
// **Cek apakah kartu terdaftar**
|
||||
if (rfidUID == kartuTerdaftar) {
|
||||
Serial.println("Kartu Terdaftar!");
|
||||
|
||||
if (!servoTerbuka) {
|
||||
Serial.println("Membuka kunci...");
|
||||
myServo.write(90);
|
||||
servoTerbuka = true;
|
||||
Firebase.setString(firebaseData, "/smartcab/servo_status", "Terbuka");
|
||||
} else {
|
||||
Serial.println("Mengunci kunci...");
|
||||
myServo.write(0);
|
||||
servoTerbuka = false;
|
||||
Firebase.setString(firebaseData, "/smartcab/servo_status", "Terkunci");
|
||||
}
|
||||
|
||||
// Kirim status ke Firebase
|
||||
Firebase.setString(firebaseData, "/smartcab/last_access", "Terdaftar");
|
||||
Firebase.setString(firebaseData, "/smartcab/status_device", rfidUID);
|
||||
}
|
||||
else {
|
||||
Serial.println("Kartu Tidak Terdaftar! Mengunci servo...");
|
||||
|
||||
// Paksa servo terkunci jika kartu tidak dikenal
|
||||
myServo.write(0);
|
||||
servoTerbuka = false;
|
||||
Firebase.setString(firebaseData, "/smartcab/servo_status", "Terkunci");
|
||||
Firebase.setString(firebaseData, "/smartcab/last_access", "Tidak Terdaftar");
|
||||
Firebase.setString(firebaseData, "/smartcab/status_device", rfidUID);
|
||||
}
|
||||
|
||||
mfrc522.PICC_HaltA(); // Hentikan komunikasi RFID
|
||||
mfrc522.PCD_StopCrypto1();
|
||||
}
|
||||
|
||||
delay(500);
|
||||
}
|
||||
|
||||
|
||||
v3
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <FirebaseESP8266.h>
|
||||
#include <SPI.h>
|
||||
#include <MFRC522.h>
|
||||
#include <Servo.h>
|
||||
|
||||
// WiFi Credentials
|
||||
#define WIFI_SSID "KONTRAKAN OYI"
|
||||
#define WIFI_PASSWORD "warkopoyi"
|
||||
|
||||
// Firebase Configuration
|
||||
#define FIREBASE_HOST "smartcab-8bb42-default-rtdb.firebaseio.com"
|
||||
#define FIREBASE_AUTH "kiiQoFa6Ckp7bL2oRLbaTSGQth9z0PgN64Ybv8dw"
|
||||
|
||||
FirebaseConfig config;
|
||||
FirebaseAuth auth;
|
||||
FirebaseData firebaseData;
|
||||
|
||||
// RFID Configuration
|
||||
#define SS_PIN D4 // Pin SDA RFID
|
||||
#define RST_PIN D3 // Pin RST RFID
|
||||
MFRC522 mfrc522(SS_PIN, RST_PIN);
|
||||
|
||||
// Servo Configuration
|
||||
Servo myServo;
|
||||
#define SERVO_PIN D2 // Pin servo
|
||||
|
||||
// **ID kartu yang diizinkan**
|
||||
String kartuTerdaftar = "53ed8434";
|
||||
bool servoTerbuka = false; // Status awal servo (tertutup)
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
|
||||
// Koneksi ke WiFi
|
||||
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
|
||||
Serial.print("Menghubungkan ke WiFi");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print(".");
|
||||
delay(500);
|
||||
}
|
||||
Serial.println("\nWiFi Terhubung!");
|
||||
|
||||
// Konfigurasi Firebase
|
||||
config.host = FIREBASE_HOST;
|
||||
config.signer.tokens.legacy_token = FIREBASE_AUTH;
|
||||
Firebase.begin(&config, &auth);
|
||||
Firebase.reconnectWiFi(true);
|
||||
|
||||
// Inisialisasi RFID
|
||||
SPI.begin();
|
||||
mfrc522.PCD_Init();
|
||||
|
||||
// Inisialisasi Servo
|
||||
myServo.attach(SERVO_PIN, 500, 2500); // Min pulse width = 500µs, Max pulse width = 2500µs
|
||||
myServo.write(0);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Cek apakah kartu RFID terdeteksi
|
||||
if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial()) {
|
||||
Serial.println("Kartu Terdeteksi!");
|
||||
|
||||
// Membaca UID kartu
|
||||
String rfidUID = "";
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||
rfidUID += String(mfrc522.uid.uidByte[i], HEX);
|
||||
}
|
||||
|
||||
Serial.print("UID: ");
|
||||
Serial.println(rfidUID);
|
||||
|
||||
// **Cek apakah kartu terdaftar**
|
||||
if (rfidUID == kartuTerdaftar) {
|
||||
Serial.println("Kartu Terdaftar!");
|
||||
|
||||
if (!servoTerbuka) {
|
||||
Serial.println("Membuka kunci...");
|
||||
myServo.write(180);
|
||||
servoTerbuka = true;
|
||||
Firebase.setString(firebaseData, "/smartcab/servo_status", "Terbuka");
|
||||
} else {
|
||||
Serial.println("Mengunci kunci...");
|
||||
myServo.write(0);
|
||||
servoTerbuka = false;
|
||||
Firebase.setString(firebaseData, "/smartcab/servo_status", "Terkunci");
|
||||
}
|
||||
|
||||
// Kirim status ke Firebase
|
||||
Firebase.setString(firebaseData, "/smartcab/last_access", "Terdaftar");
|
||||
Firebase.setString(firebaseData, "/smartcab/status_device", rfidUID);
|
||||
}
|
||||
else {
|
||||
Serial.println("Kartu Tidak Terdaftar! Mengunci servo...");
|
||||
|
||||
// Paksa servo terkunci jika kartu tidak dikenal
|
||||
myServo.write(0);
|
||||
servoTerbuka = false;
|
||||
Firebase.setString(firebaseData, "/smartcab/servo_status", "Terkunci");
|
||||
Firebase.setString(firebaseData, "/smartcab/last_access", "Tidak Terdaftar");
|
||||
Firebase.setString(firebaseData, "/smartcab/status_device", rfidUID);
|
||||
}
|
||||
|
||||
mfrc522.PICC_HaltA(); // Hentikan komunikasi RFID
|
||||
mfrc522.PCD_StopCrypto1();
|
||||
}
|
||||
|
||||
delay(500);
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Google\Cloud\Firestore\Admin\V1\CreateDatabaseRequest;
|
||||
use GuzzleHttp\Promise\Create;
|
||||
use Illuminate\Console\Command;
|
||||
use Kreait\Firebase\Factory;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class FetchFirebaseData extends Command
|
||||
{
|
||||
protected $signature = 'firebase:fetch';
|
||||
|
@ -17,78 +17,74 @@ public function handle()
|
|||
try {
|
||||
// Inisialisasi Firebase
|
||||
$firebase = (new Factory)
|
||||
->withServiceAccount(storage_path('app/smartcab-8bb42-firebase-adminsdk-fbsvc-de33a8e45b.json'))
|
||||
->withDatabaseUri(env('FIREBASE_DATABASE_URL'))
|
||||
->createDatabase();
|
||||
// Ambil data terbaru dari Firebase
|
||||
$securityData = $firebase->getReference('security')->getValue() ?? [];
|
||||
$smartcabData = $firebase->getReference('smartcab')->getValue() ?? [];
|
||||
$dht11Data = $firebase->getReference('dht11')->getValue() ?? [];
|
||||
->withServiceAccount(storage_path('app/smartcab-8bb42-firebase-adminsdk-fbsvc-de33a8e45b.json'))
|
||||
->withDatabaseUri(env('FIREBASE_DATABASE_URL'))
|
||||
->createDatabase();
|
||||
|
||||
// Baca history data yang sudah ada
|
||||
$historyData = [];
|
||||
if (Storage::exists('reports.json')) {
|
||||
$historyData = json_decode(Storage::get('reports.json'), true) ?: [];
|
||||
// Pastikan $historyData adalah array
|
||||
if (!is_array($historyData)) {
|
||||
$historyData = [];
|
||||
}
|
||||
}
|
||||
// Ambil data terbaru dari Firebase
|
||||
$securityData = $firebase->getReference('security')->getValue() ?? [];
|
||||
$smartcabData = $firebase->getReference('smartcab')->getValue() ?? [];
|
||||
$dht11Data = $firebase->getReference('dht11')->getValue() ?? [];
|
||||
|
||||
// Ambil data terakhir jika ada
|
||||
$lastEntry = !empty($historyData) ? end($historyData) : null;
|
||||
// Baca history data yang sudah ada
|
||||
$historyData = [];
|
||||
if (Storage::exists('reports.json')) {
|
||||
$historyData = json_decode(Storage::get('reports.json'), true) ?: [];
|
||||
if (!is_array($historyData)) {
|
||||
$historyData = [];
|
||||
}
|
||||
}
|
||||
|
||||
// Cek apakah ada perubahan pada security atau smartcab
|
||||
$hasChanges = false;
|
||||
if ($lastEntry === null) {
|
||||
// Jika belum ada data sama sekali, simpan data pertama
|
||||
$hasChanges = true;
|
||||
} else {
|
||||
// Bandingkan security dan smartcab dengan data terakhir
|
||||
$securityChanged = $this->hasDataChanged($lastEntry['security'] ?? [], $securityData);
|
||||
$smartcabChanged = $this->hasDataChanged($lastEntry['smartcab'] ?? [], $smartcabData);
|
||||
$hasChanges = $securityChanged || $smartcabChanged;
|
||||
}
|
||||
// Ambil data terakhir jika ada
|
||||
$lastEntry = !empty($historyData) ? end($historyData) : null;
|
||||
|
||||
// Hanya simpan jika ada perubahan
|
||||
if ($hasChanges) {
|
||||
$newData = [
|
||||
'timestamp' => now()->toIso8601String(),
|
||||
'security' => $securityData,
|
||||
'smartcab' => $smartcabData
|
||||
];
|
||||
// Cek apakah ada perubahan pada security atau smartcab
|
||||
$hasChanges = false;
|
||||
if ($lastEntry === null) {
|
||||
$hasChanges = true;
|
||||
} else {
|
||||
$securityChanged = $this->hasDataChanged($lastEntry['security'] ?? [], $securityData);
|
||||
$smartcabChanged = $this->hasDataChanged($lastEntry['smartcab'] ?? [], $smartcabData);
|
||||
$hasChanges = $securityChanged || $smartcabChanged;
|
||||
}
|
||||
|
||||
if (!empty($dht11Data)) {
|
||||
$newData['dht11'] = $dht11Data;
|
||||
}
|
||||
// Hanya simpan jika ada perubahan
|
||||
if ($hasChanges) {
|
||||
$newData = [
|
||||
'id' => Str::uuid()->toString(), // Generate ID unik
|
||||
'timestamp' => now()->toIso8601String(),
|
||||
'security' => $securityData,
|
||||
'smartcab' => $smartcabData
|
||||
];
|
||||
|
||||
$historyData[] = $newData;
|
||||
Storage::put('reports.json', json_encode($historyData, JSON_PRETTY_PRINT));
|
||||
$this->info('Data baru tersimpan karena ada perubahan pada security atau smartcab');
|
||||
|
||||
// Debug info
|
||||
if (isset($securityChanged) && $securityChanged) {
|
||||
$this->info('Perubahan terdeteksi pada security');
|
||||
}
|
||||
if (isset($smartcabChanged) && $smartcabChanged) {
|
||||
$this->info('Perubahan terdeteksi pada smartcab');
|
||||
}
|
||||
} else {
|
||||
$this->info('Tidak ada perubahan pada security atau smartcab, data tidak disimpan');
|
||||
}
|
||||
if (!empty($dht11Data)) {
|
||||
$newData['dht11'] = $dht11Data;
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Error: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
$historyData[] = $newData;
|
||||
Storage::put('reports.json', json_encode($historyData, JSON_PRETTY_PRINT));
|
||||
$this->info('Data baru tersimpan dengan ID: ' . $newData['id']);
|
||||
|
||||
private function hasDataChanged($oldData, $newData)
|
||||
{
|
||||
$flags = defined('JSON_SORT_KEYS') ? JSON_SORT_KEYS : 0;
|
||||
$oldJson = json_encode($oldData, $flags);
|
||||
$newJson = json_encode($newData, $flags);
|
||||
|
||||
if (isset($securityChanged) && $securityChanged) {
|
||||
$this->info('Perubahan terdeteksi pada security');
|
||||
}
|
||||
if (isset($smartcabChanged) && $smartcabChanged) {
|
||||
$this->info('Perubahan terdeteksi pada smartcab');
|
||||
}
|
||||
} else {
|
||||
$this->info('Tidak ada perubahan pada security atau smartcab, data tidak disimpan');
|
||||
}
|
||||
|
||||
return $oldJson !== $newJson;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Error: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private function hasDataChanged($oldData, $newData)
|
||||
{
|
||||
$oldJson = json_encode($oldData);
|
||||
$newJson = json_encode($newData);
|
||||
|
||||
return $oldJson !== $newJson;
|
||||
}
|
||||
}
|
|
@ -7,7 +7,6 @@
|
|||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
|
||||
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
||||
<style>
|
||||
/* CSS untuk memastikan tabel responsif */
|
||||
.table-responsive {
|
||||
overflow-x: auto;
|
||||
}
|
||||
|
@ -21,12 +20,9 @@
|
|||
@php
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
// Konversi array ke koleksi
|
||||
$reportsCollection = collect($reports);
|
||||
|
||||
// Pagination manual
|
||||
$reportsCollection = collect($reports)->sortByDesc('timestamp');
|
||||
$currentPage = request()->get('page', 1);
|
||||
$perPage = 10; // Jumlah item per halaman
|
||||
$perPage = 1000;
|
||||
$paginatedReports = new LengthAwarePaginator(
|
||||
$reportsCollection->forPage($currentPage, $perPage),
|
||||
$reportsCollection->count(),
|
||||
|
@ -52,22 +48,17 @@
|
|||
@foreach($paginatedReports as $key => $report)
|
||||
<tr>
|
||||
<td>{{ $paginatedReports->firstItem() + $key }}</td>
|
||||
<td>
|
||||
<!-- Format tanggal sesuai dengan pop-up -->
|
||||
<script>
|
||||
document.write(new Date("{{ $report['timestamp'] }}").toLocaleString());
|
||||
</script>
|
||||
</td>
|
||||
<td>{{ date('d-m-Y H:i:s', strtotime($report['timestamp'])) }}</td>
|
||||
<td>
|
||||
@if($loop->first)
|
||||
Gerakan
|
||||
@elseif($report['security']['motion'] !== $reports[$key - 1]['security']['motion'])
|
||||
@elseif($key > 0 && $report['security']['motion'] !== $reports[$key - 1]['security']['motion'])
|
||||
Gerakan
|
||||
@elseif($report['security']['status'] !== $reports[$key - 1]['security']['status'])
|
||||
@elseif($key > 0 && $report['security']['status'] !== $reports[$key - 1]['security']['status'])
|
||||
Status Keamanan
|
||||
@elseif($report['smartcab']['last_access'] !== $reports[$key - 1]['smartcab']['last_access'])
|
||||
@elseif($key > 0 && $report['smartcab']['last_access'] !== $reports[$key - 1]['smartcab']['last_access'])
|
||||
Akses Terakhir
|
||||
@elseif($report['smartcab']['servo_status'] !== $reports[$key - 1]['smartcab']['servo_status'])
|
||||
@elseif($key > 0 && $report['smartcab']['servo_status'] !== $reports[$key - 1]['smartcab']['servo_status'])
|
||||
Status Servo
|
||||
@else
|
||||
-
|
||||
|
@ -76,30 +67,29 @@
|
|||
<td>
|
||||
@if($loop->first)
|
||||
{{ ucfirst($report['security']['motion']) }}
|
||||
@elseif($report['security']['motion'] !== $reports[$key - 1]['security']['motion'])
|
||||
@elseif($key > 0 && $report['security']['motion'] !== $reports[$key - 1]['security']['motion'])
|
||||
{{ ucfirst($report['security']['motion']) }}
|
||||
@elseif($report['security']['status'] !== $reports[$key - 1]['security']['status'])
|
||||
@elseif($key > 0 && $report['security']['status'] !== $reports[$key - 1]['security']['status'])
|
||||
{{ ucfirst($report['security']['status']) }}
|
||||
@elseif($report['smartcab']['last_access'] !== $reports[$key - 1]['smartcab']['last_access'])
|
||||
@elseif($key > 0 && $report['smartcab']['last_access'] !== $reports[$key - 1]['smartcab']['last_access'])
|
||||
{{ ucfirst($report['smartcab']['last_access']) }}
|
||||
@elseif($report['smartcab']['servo_status'] !== $reports[$key - 1]['smartcab']['servo_status'])
|
||||
@elseif($key > 0 && $report['smartcab']['servo_status'] !== $reports[$key - 1]['smartcab']['servo_status'])
|
||||
{{ ucfirst($report['smartcab']['servo_status']) }}
|
||||
@else
|
||||
Tidak ada perubahan
|
||||
@endif
|
||||
</td>
|
||||
<td>
|
||||
<button class="btn btn-primary btn-sm" onclick="showDetail({{ json_encode($report) }})">
|
||||
<button class="btn btn-primary btn-sm" onclick='showDetail(@json($report))'>
|
||||
Lihat Detail
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<!-- PAGINATION -->
|
||||
<div class="d-flex justify-content-center">
|
||||
{{ $paginatedReports->links('pagination::bootstrap-4') }}
|
||||
</div>
|
||||
|
@ -119,7 +109,6 @@
|
|||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body" id="modalBodyContent">
|
||||
<!-- Data akan dimasukkan melalui JavaScript -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -128,9 +117,7 @@
|
|||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
function showDetail(report) {
|
||||
// Format tanggal sesuai dengan locale browser
|
||||
let formattedDate = new Date(report.timestamp).toLocaleString();
|
||||
|
||||
let detailHtml = `
|
||||
<strong>Tanggal waktu:</strong> ${formattedDate}<br>
|
||||
<strong>Gerakan:</strong> ${report.security.motion}<br>
|
||||
|
@ -141,7 +128,6 @@ function showDetail(report) {
|
|||
<strong>Kelembaban:</strong> ${report.dht11.humidity}%<br>
|
||||
<strong>Suhu:</strong> ${report.dht11.temperature}°C
|
||||
`;
|
||||
|
||||
document.getElementById('modalBodyContent').innerHTML = detailHtml;
|
||||
let detailModal = new bootstrap.Modal(document.getElementById('detailModal'));
|
||||
detailModal.show();
|
||||
|
@ -149,4 +135,4 @@ function showDetail(report) {
|
|||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
|
Loading…
Reference in New Issue