#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <time.h>
// ======================= CONFIG WI-FI =======================
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// ======================= CONFIG FIREBASE ====================
#define DATABASE_URL "https://buoy-4f252-default-rtdb.asia-southeast1.firebasedatabase.app"
#define DB_SECRET "hDMIteIWu5ev3TkPFiGSh1vazuBxK7WFWv67qQRB"
// ====================== PIN DEFINITIONS ======================
const int ONE_WIRE_PIN = 15; // DS18B20
const int LDR_PIN = 34; // LDR analog
// =================== SENSOR INITIALIZATION ===================
OneWire oneWire(ONE_WIRE_PIN);
DallasTemperature sensors(&oneWire);
// =================== NTP CONFIG (WIB = GMT+7) ===================
const long GMT_OFFSET = 7 * 3600;
const int DAYLIGHT_SHIFT = 0;
// --------------------------------------------------------------
// Initialize NTP Time
// --------------------------------------------------------------
void initTime()
{
Serial.println("Sinkronisasi waktu melalui NTP...");
configTime(GMT_OFFSET, DAYLIGHT_SHIFT, "pool.ntp.org", "id.pool.ntp.org");
// Menunggu sampai waktu tersinkron
while (time(nullptr) < 100000)
{
Serial.print(".");
delay(500);
}
Serial.println("\nWaktu berhasil tersinkron!");
}
// --------------------------------------------------------------
// Generate timestamp WIB dalam format: YYYY-MM-DD HH:MM:SS
// --------------------------------------------------------------
String getTimestamp()
{
time_t now = time(nullptr);
struct tm* ti = localtime(&now);
char buffer[25];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", ti);
return String(buffer);
}
// --------------------------------------------------------------
// Kirim ke Firebase (temperature, light, timestamp WIB saja)
// --------------------------------------------------------------
void sendToFirebase(float suhu, int ldrValue)
{
if (WiFi.status() != WL_CONNECTED)
{
Serial.println("WiFi tidak terhubung. Data tidak dikirim.");
return;
}
HTTPClient http;
String url = String(DATABASE_URL) + "/sensorLogs.json?auth=" + DB_SECRET;
http.begin(url);
http.addHeader("Content-Type", "application/json");
// Waktu lokal WIB
String timestamp = getTimestamp();
// JSON body
String json = "{";
json += "\"temperature\": " + String(suhu) + ",";
json += "\"light\": " + String(ldrValue) + ",";
json += "\"timestamp\": \"" + timestamp + "\"";
json += "}";
int res = http.POST(json);
Serial.println("Firebase POST Code: " + String(res));
if (res > 0)
Serial.println("Response: " + http.getString());
else
Serial.println("Gagal mengirim data!");
http.end();
}
// ================================ SETUP ================================
void setup()
{
Serial.begin(115200);
delay(500);
// ---------------------- Wi-Fi -----------------------
Serial.println("Menghubungkan ke WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("\nWiFi Terhubung!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// ---------------------- NTP Init ---------------------
initTime();
// ---------------------- Sensor Init ------------------
sensors.begin();
Serial.println("=== Sistem Sensor + Firebase Siap ===");
}
// ================================ LOOP ================================
void loop()
{
// ---- 1. Baca suhu ----
sensors.requestTemperatures();
float suhu = sensors.getTempCByIndex(0);
// ---- 2. Baca LDR ----
int nilaiLDR = analogRead(LDR_PIN);
// ---- 3. Debug Serial ----
Serial.println("--------------------------------");
Serial.println("Waktu WIB : " + getTimestamp());
Serial.println("Suhu Air : " + String(suhu) + " °C");
Serial.println("Nilai LDR : " + String(nilaiLDR));
Serial.println("Mengirim ke Firebase...");
// ---- 4. Kirim ke Firebase ----
sendToFirebase(suhu, nilaiLDR);
delay(2000);
}
Loading
ds18b20
ds18b20