#include <WiFi.h>
#include "ThingSpeak.h"
#include "Ultrasonic.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h> // Include the LiquidCrystal library
const int TRIG_PIN = 5; // Trig pin of ultrasonic sensor connected to D5
const int ECHO_PIN = 18; // Echo pin of ultrasonic sensor connected to D18
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2432865;
const char* myApiKey = "EOVTFMFK9869ZPVY";
const char* server = "api.thingspeak.com";
WiFiClient client;
Ultrasonic ultrasonic(TRIG_PIN, ECHO_PIN);
float vol = 0.0, l_minute;
unsigned long currentTime;
unsigned long cloopTime;
float threshold = 250.0; // Threshold volume in liters for stopping the meter
bool thresholdReached = false; // Flag to indicate if threshold is reached
const int meterNumber = 123456; // Meter number
int meterStatusNumeric = 1; // Meter status: 1 for ON, 0 for OFF
float billAmount = 0.0; // Bill amount to be paid
// Initialize the LiquidCrystal object with the I2C address
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address 0x27, 16 columns, 2 rows
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Connect to WiFi
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
// Initialize ThingSpeak
ThingSpeak.begin(client);
// Initialize time variables
currentTime = millis();
cloopTime = currentTime;
// Initialize LCD
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0);
lcd.print("Water Meter");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000); // Delay for 2 seconds
}
void loop() {
currentTime = millis();
if (currentTime >= (cloopTime + 1000) && !thresholdReached) {
cloopTime = currentTime;
unsigned int distance = ultrasonic.distanceRead(); // Measure distance using ultrasonic sensor
// Calculate flow rate and total volume consumed
l_minute = distance * 0.1; // Assuming 1 cm of water level change corresponds to 0.1 L/min flow rate (adjust this factor based on your setup)
vol = vol + l_minute;
// Update ThingSpeak fields
ThingSpeak.setField(1, l_minute); // Flow rate in L/min
ThingSpeak.setField(2, vol); // Total volume consumed in Liters
// Additional fields
ThingSpeak.setField(3, meterNumber); // Meter number
// Update meter status field based on numeric value
ThingSpeak.setField(4, meterStatusNumeric); // Meter status
// Send data to ThingSpeak
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
// Print flow rate and total volume consumed
Serial.println("Flow Rate: " + String(l_minute) + " L/min");
Serial.println("Total Volume: " + String(vol) + " L");
// Print data push status
if (x == 200) {
Serial.println("Data pushed successfully");
} else {
Serial.println("Push error: " + String(x));
}
Serial.println("---");
// Display messages on LCD based on water flow status
if (vol >= 250) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Threshold Reached");
lcd.setCursor(0, 1);
lcd.print("Meter Stopped");
thresholdReached = true;
} else if (vol >= 200 && vol < threshold) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Near Threshold");
lcd.setCursor(0, 1);
lcd.print("Alert Sent!");
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distribution");
lcd.setCursor(0, 1);
lcd.print("In Progress");
}
}
delay(10000); // Adjust delay as per requirement
}