Upload files to "/"
This commit is contained in:
commit
07b1637aee
|
@ -0,0 +1,146 @@
|
||||||
|
#include <WiFiManager.h>
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <FirebaseESP32.h>
|
||||||
|
#include <Wire.h>
|
||||||
|
#include <LiquidCrystal_I2C.h>
|
||||||
|
|
||||||
|
// Firebase config
|
||||||
|
#define FIREBASE_HOST "https://tdsmeter-8042a-default-rtdb.firebaseio.com/"
|
||||||
|
#define FIREBASE_AUTH "GnAvtjMBxgEqsEdPDiuCwBmo6dRxBKcr1JIEqEqz"
|
||||||
|
|
||||||
|
// Pin
|
||||||
|
#define TDS_PIN 34
|
||||||
|
#define BUZZER_PIN 26
|
||||||
|
|
||||||
|
// Ambang batas TDS (ppm)
|
||||||
|
#define TDS_THRESHOLD 500
|
||||||
|
|
||||||
|
// Kalibrasi TDS (atur berdasarkan hasil perbandingan)
|
||||||
|
const float VREF = 3.3;
|
||||||
|
const int ADC_RESOLUTION = 4095;
|
||||||
|
float TDS_factor = 0.399; // Ganti sesuai kalibrasi
|
||||||
|
|
||||||
|
// LCD I2C
|
||||||
|
LiquidCrystal_I2C lcd(0x27, 16, 2);
|
||||||
|
|
||||||
|
// Firebase
|
||||||
|
FirebaseData firebaseData;
|
||||||
|
FirebaseConfig firebaseConfig;
|
||||||
|
FirebaseAuth firebaseAuth;
|
||||||
|
|
||||||
|
// Variabel global
|
||||||
|
float tdsValue = 0;
|
||||||
|
float voltage = 0;
|
||||||
|
String status = "";
|
||||||
|
|
||||||
|
// Fungsi scroll teks LCD
|
||||||
|
void scrollText(String text, int row, int delayTime) {
|
||||||
|
int len = text.length();
|
||||||
|
if (len <= 16) {
|
||||||
|
lcd.setCursor(0, row);
|
||||||
|
lcd.print(text);
|
||||||
|
lcd.print(" ");
|
||||||
|
delay(delayTime);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (int i = 0; i <= len - 16; i++) {
|
||||||
|
lcd.setCursor(0, row);
|
||||||
|
lcd.print(text.substring(i, i + 16));
|
||||||
|
delay(delayTime);
|
||||||
|
}
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
// LCD setup
|
||||||
|
lcd.init();
|
||||||
|
lcd.backlight();
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
|
lcd.print("TDS Monitor");
|
||||||
|
delay(2000);
|
||||||
|
lcd.clear();
|
||||||
|
|
||||||
|
// Buzzer setup
|
||||||
|
pinMode(BUZZER_PIN, OUTPUT);
|
||||||
|
digitalWrite(BUZZER_PIN, HIGH); // Awalnya mati
|
||||||
|
|
||||||
|
// WiFi Manager
|
||||||
|
WiFiManager wm;
|
||||||
|
if (!wm.autoConnect("ESP32_Config_AP")) {
|
||||||
|
Serial.println("Gagal konek WiFi");
|
||||||
|
delay(3000);
|
||||||
|
ESP.restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("WiFi Terhubung:");
|
||||||
|
Serial.println(WiFi.localIP());
|
||||||
|
|
||||||
|
// Firebase
|
||||||
|
firebaseConfig.signer.tokens.legacy_token = FIREBASE_AUTH;
|
||||||
|
firebaseConfig.database_url = FIREBASE_HOST;
|
||||||
|
Firebase.begin(&firebaseConfig, &firebaseAuth);
|
||||||
|
Firebase.reconnectWiFi(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// Ambil 10 sampel ADC
|
||||||
|
int total = 0;
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
total += analogRead(TDS_PIN);
|
||||||
|
delay(10);
|
||||||
|
}
|
||||||
|
int analogValue = total / 10;
|
||||||
|
|
||||||
|
// Konversi ke tegangan
|
||||||
|
voltage = analogValue * (VREF / ADC_RESOLUTION);
|
||||||
|
|
||||||
|
// Hitung TDS (pakai rumus polinomial + faktor kalibrasi)
|
||||||
|
tdsValue = (133.42 * voltage * voltage * voltage - 255.86 * voltage * voltage + 857.39 * voltage) * TDS_factor;
|
||||||
|
if (tdsValue < 0) tdsValue = 0;
|
||||||
|
|
||||||
|
// Debug Serial
|
||||||
|
Serial.print("ADC: ");
|
||||||
|
Serial.print(analogValue);
|
||||||
|
Serial.print(" | V: ");
|
||||||
|
Serial.print(voltage, 4);
|
||||||
|
Serial.print(" V | TDS: ");
|
||||||
|
Serial.print(tdsValue, 2);
|
||||||
|
Serial.println(" ppm");
|
||||||
|
|
||||||
|
// LCD tampilan baris 1
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
|
lcd.print("TDS: ");
|
||||||
|
lcd.print(tdsValue, 1);
|
||||||
|
lcd.print(" ppm ");
|
||||||
|
|
||||||
|
// Penilaian status air
|
||||||
|
if (tdsValue <= 50) {
|
||||||
|
status = "Status: Sangat Baik";
|
||||||
|
digitalWrite(BUZZER_PIN, HIGH); // Buzzer mati
|
||||||
|
} else if (tdsValue <= 150) {
|
||||||
|
status = "Status: Baik";
|
||||||
|
digitalWrite(BUZZER_PIN, HIGH);
|
||||||
|
} else if (tdsValue <= TDS_THRESHOLD) {
|
||||||
|
status = "Status: Cukup Baik";
|
||||||
|
digitalWrite(BUZZER_PIN, HIGH);
|
||||||
|
} else {
|
||||||
|
status = "Status: Buruk";
|
||||||
|
digitalWrite(BUZZER_PIN, LOW); // Buzzer menyala
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scroll teks status di baris 2
|
||||||
|
scrollText(status, 1, 250);
|
||||||
|
|
||||||
|
// Kirim data ke Firebase
|
||||||
|
if (Firebase.setFloat(firebaseData, "/TDS/value", tdsValue) &&
|
||||||
|
Firebase.setString(firebaseData, "/TDS/status", status)) {
|
||||||
|
Serial.println("Data terkirim ke Firebase");
|
||||||
|
} else {
|
||||||
|
Serial.print("Gagal kirim: ");
|
||||||
|
Serial.println(firebaseData.errorReason());
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(1000);
|
||||||
|
}
|
Loading…
Reference in New Issue