cnbu12oZsPUsXwMqS8ozdUYI5DesA4e6nbQsVzgG
Code Blogger Firebase (Database)
Sharing Code Blogger dengan Firebase (database)
Cara mudah menghubungkan Blog dengan Database, disini anda akan mempelajari memaksimalkan kekuatan Firebase dan Blogger

IOT by Firebase Control ESP32

Kali ini mimin mencoba membuat IOT (Internet of Thing) dengan menggabungkan ESP32 dengan Firebase. berikut kode yang sudah mimin buat agar ESP32 mengisi database firebase, dengan mengukur kekuatan sinyal WIFI dan menyimapanya di database
  
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <FirebaseESP32.h>

// Konfigurasi OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Konfigurasi pin I2C
#define SDA_PIN 21
#define SCL_PIN 22

// Informasi Wi-Fi
const char* ssid = "Redmi Note 12";
const char* password = "Password";

// Informasi Firebase
#define FIREBASE_HOST "loginweb-324161-default-rtdb.firebaseio.com"
#define FIREBASE_AUTH "z9tOBDei1f1Ykquw6TJ44kfA80B9cSX2RjSSVrUWI"
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

// Konfigurasi NTP
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 7 * 3600; // WIB
const int daylightOffset_sec = 0;

// Pin LED di board ESP32
const int ledPin = 2;

// Interval pengukuran dan pengecekan status (ms)
unsigned long previousMeasurementMillis = 0;
unsigned long previousWiFiReconnectMillis = 0;
unsigned long previousCheckMillis = 0;
const long measurementInterval = 10000; // 10 detik
const long reconnectInterval = 10000;  // 10 detik
const long checkInterval = 2000;       // 2 detik

bool measurementEnabled = false;

void connectToWiFi() {
  Serial.println("Mencoba menghubungkan ke WiFi...");
  display.clearDisplay();
  display.println("Mencoba WiFi...");
  display.display();

  WiFi.begin(ssid, password);

  int attempt = 0;
  while (WiFi.status() != WL_CONNECTED && attempt < 20) { // 10 detik timeout
    delay(500);
    Serial.print(".");
    attempt++;
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nTerhubung ke WiFi!");
    Serial.printf("IP Address: %s\n", WiFi.localIP().toString().c_str());
    display.clearDisplay();
    display.println("WiFi Terhubung!");
    display.printf("IP: %s\n", WiFi.localIP().toString().c_str());
    display.display();

    configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
    Serial.println("Sinkronisasi waktu selesai.");
  } else {
    Serial.println("\nGagal terhubung ke WiFi!");
    display.clearDisplay();
    display.println("Gagal terhubung!");
    display.display();
  }
}

String getCurrentTime() {
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) {
    Serial.println("Gagal mendapatkan waktu lokal");
    return "1970-01-01 00:00:00";
  }
  char buffer[20];
  strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeinfo);
  return String(buffer);
}

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
  digitalWrite(ledPin, LOW);

  Wire.begin(SDA_PIN, SCL_PIN);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED tidak ditemukan!");
    while (true);
  }

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  display.setCursor(0, 0);
  display.println("Inisialisasi...");
  display.display();
  delay(1000);

  connectToWiFi();

  config.host = FIREBASE_HOST;
  config.signer.tokens.legacy_token = FIREBASE_AUTH;
  Firebase.begin(&config, &auth);
}

void loop() {
  unsigned long currentMillis = millis();

  // Cek WiFi dan reconnect jika terputus
  if (WiFi.status() != WL_CONNECTED && currentMillis - previousWiFiReconnectMillis >= reconnectInterval) {
    previousWiFiReconnectMillis = currentMillis;
    Serial.println("WiFi terputus. Mencoba menyambung kembali...");
    connectToWiFi();
  }

  // Cek status di Firebase setiap 2 detik
  if (currentMillis - previousCheckMillis >= checkInterval) {
    previousCheckMillis = currentMillis;

    if (Firebase.getString(fbdo, "/setting/parameter/RSSI")) {
      String status = fbdo.stringData();
      Serial.printf("Status RSSI: %s\n", status.c_str());

      measurementEnabled = (status == "ON");
    } else {
      Serial.printf("Gagal membaca status: %s\n", fbdo.errorReason().c_str());
    }
  }

  // Kirim data RSSI jika pengukuran diaktifkan
  if (measurementEnabled && currentMillis - previousMeasurementMillis >= measurementInterval) {
    previousMeasurementMillis = currentMillis;

    if (WiFi.status() == WL_CONNECTED) {
      int rssi = WiFi.RSSI();
      String timestamp = getCurrentTime();

      FirebaseJson json;
      json.set("RSSI", rssi);
      json.set("timestamp", timestamp);

      if (Firebase.pushJSON(fbdo, "/wifi_rssi", json)) {
        Serial.println("Data RSSI berhasil dikirim ke Firebase.");
        Serial.printf("RSSI: %d, Timestamp: %s\n", rssi, timestamp.c_str());
      } else {
        Serial.println("Gagal mengirim data ke Firebase.");
        Serial.println(fbdo.errorReason());
      }
    } else {
      Serial.println("WiFi tidak terhubung, pengukuran dihentikan.");
    }
  }

  delay(10);
}
  
  
Berikut Contoh pengukuranya Firebase RSSI Value

Test Pengukuran RSSI:

Post a Comment