#include <WiFi.h>
#include <HTTPClient.h>
// ---------------- PIN DEFINITIONS ----------------
#define TRIG_MAIN 5
#define ECHO_MAIN 18
#define TRIG_SUB 27
#define ECHO_SUB 14
#define TDS_PIN 32
#define POT_PRESSURE1 34
#define POT_PRESSURE2 33
#define POT_FLOW1 35
#define POT_FLOW2 36
#define LED1 25
#define LED2 26
// ---------------- WIFI + FIREBASE ----------------
const char* ssid = "Wokwi-GUEST"; // WiFi SSID
const char* password = ""; // WiFi Password
// Replace with your Firebase Realtime Database URL
// Example: "https://your-project-id-default-rtdb.firebaseio.com"
String firebaseHost = "https://hydronet-monitor-default-rtdb.firebaseio.com";
String firebaseAuth = "KwfkB1eVB2szYTYMRtvPbbrgUZo5fahyDMXUEQ3"; // Optional if not public
// ---------------- VARIABLES ----------------
float distanceMain, distanceSub;
float waterLevelMain, waterLevelSub;
float tdsValue, pressure1, pressure2, flow1, flow2;
const float tankHeight = 400.0;
void setup() {
Serial.begin(115200);
// --- Pin Modes ---
pinMode(TRIG_MAIN, OUTPUT);
pinMode(ECHO_MAIN, INPUT);
pinMode(TRIG_SUB, OUTPUT);
pinMode(ECHO_SUB, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
// --- WiFi Connection ---
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi Connected!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
// Function to measure distance using Ultrasonic Sensor
float getDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000); // timeout 30ms
float distance = duration * 0.034 / 2; // cm
return distance;
}
void loop() {
// --- Measure Water Levels ---
distanceMain = getDistance(TRIG_MAIN, ECHO_MAIN);
distanceSub = getDistance(TRIG_SUB, ECHO_SUB);
waterLevelMain = tankHeight - distanceMain;
waterLevelSub = tankHeight - distanceSub;
if (waterLevelMain < 0) waterLevelMain = 0;
if (waterLevelSub < 0) waterLevelSub = 0;
// --- Read TDS ---
int tdsRaw = analogRead(TDS_PIN);
tdsValue = (tdsRaw / 4095.0) * 1000; // ppm scale (example calibration)
// --- Read Pressure & Flow ---
pressure1 = analogRead(POT_PRESSURE1) / 40.95; // 0–100
pressure2 = analogRead(POT_PRESSURE2) / 40.95;
flow1 = analogRead(POT_FLOW1) / 40.95; // 0–100
flow2 = analogRead(POT_FLOW2) / 40.95;
// --- LED Logic ---
if (pressure2 > pressure1) {
digitalWrite(LED1, HIGH);
digitalWrite(LED2, LOW);
} else {
digitalWrite(LED1, LOW);
digitalWrite(LED2, HIGH);
}
// --- Print to Serial ---
Serial.println("------ SENSOR DATA ------");
Serial.printf("Main Tank Level: %.2f cm\n", waterLevelMain);
Serial.printf("Sub Tank Level : %.2f cm\n", waterLevelSub);
Serial.printf("TDS Value : %.2f ppm\n", tdsValue);
Serial.printf("Pressure1 : %.2f\n", pressure1);
Serial.printf("Pressure2 : %.2f\n", pressure2);
Serial.printf("Flow1 : %.2f\n", flow1);
Serial.printf("Flow2 : %.2f\n", flow2);
Serial.println("--------------------------");
// --- Upload to Firebase (Sequential POST) ---
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Each POST creates a new unique entry under "TankData"
String url = firebaseHost + "/TankData.json?auth=" + firebaseAuth;
http.begin(url);
http.addHeader("Content-Type", "application/json");
// Add a timestamp field to identify entries
unsigned long timestamp = millis() / 1000;
String payload = "{";
payload += "\"timestamp\": " + String(timestamp) + ",";
payload += "\"MainTank_Level\": " + String(waterLevelMain) + ",";
payload += "\"SubTank_Level\": " + String(waterLevelSub) + ",";
payload += "\"TDS\": " + String(tdsValue) + ",";
payload += "\"Pressure1\": " + String(pressure1) + ",";
payload += "\"Pressure2\": " + String(pressure2) + ",";
payload += "\"Flow1\": " + String(flow1) + ",";
payload += "\"Flow2\": " + String(flow2);
payload += "}";
int httpCode = http.POST(payload);
if (httpCode > 0) {
Serial.printf("✅ Firebase POST Response Code: %d\n", httpCode);
Serial.println("Data pushed successfully!");
} else {
Serial.printf("⚠️ HTTP Error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.println("❌ WiFi disconnected, skipping upload.");
}
delay(3000); // 3 seconds between uploads
}