#include <WiFi.h>
#include <HTTPClient.h>
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#define LCD_ADDRESS 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
// pin
#define FLOW_PIN 34 // Potentiometer for flow rate simulation
#define TRIG_PIN 19 // Ultrasonic trigger pin
#define ECHO_PIN 18 // Ultrasonic echo pin
#define BUZZER_PIN 4 // Buzzer pin
#define RESET_PIN 0
float flowRate = 0.0;
float totalVolume = 0.0;
int tankLevel = 0;
bool lowLevelAlert = false;
bool enteringPlate = true;
bool kmEntered = false;
String plateNumber = "";
String kmReading = "";
String Volume;
String Flow;
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int ledPin = 23;
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte colPins[COLS] = {26, 25, 33, 32};
byte rowPins[ROWS] = {13, 12, 14, 27};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
const char* ssid = "jawir";
const char* password = "mawtauajah";
//const int potPin = 34;
void setup() {
lcd.init();
lcd.backlight();
pinMode(FLOW_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RESET_PIN, INPUT_PULLUP);
pinMode(ledPin, OUTPUT); // Set the LED pin as output
digitalWrite(ledPin, LOW);
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
lcd.setCursor(0, 0);
lcd.print("Please Wait");
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter No. Unit:");
Serial.println("Connected to WiFi");
}
void loop() {
char key = keypad.getKey();
if (key) { // Check for valid key press
if (key == '*') {
// Reset inputs
plateNumber = "";
kmReading = "";
enteringPlate = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter Plate No:");
}
else if (key == '#')
{
if (enteringPlate && plateNumber.length() > 0)
{
// Switch to KM input if Plate number is entered
enteringPlate = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter KM:");
lcd.setCursor(0, 1);
lcd.print(kmReading);
}
else if (!enteringPlate && kmReading.length() > 0)
{
kmEntered = true;
lcd.clear();
//readFlowRate();
displayInfo();
send_data();
checkTankLevel();
}
} else {
if (enteringPlate) {
plateNumber += key;
lcd.setCursor(0, 1);
lcd.print(plateNumber);
} else {
kmReading += key;
lcd.setCursor(0, 1);
lcd.print(kmReading);
}
}
}
if (digitalRead(RESET_PIN) == LOW) {
plateNumber = "";
kmReading = "";
enteringPlate = true;
kmEntered = false;
lowLevelAlert = false;
totalVolume = 0;
flowRate = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Resetting...");
delay(1000); // debounce delay for reset
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Enter No. Unit:");
}
}
void displayInfo() {
int potValue = analogRead(FLOW_PIN);
flowRate = map(potValue, 0, 4095, 0, 1000);
totalVolume += flowRate * 1 / 1000;
Volume = String(totalVolume);
Flow = String(flowRate);
Serial.println("VOL " + Volume);
Serial.println("Flow " + Flow);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Flow: ");
lcd.print(flowRate);
lcd.print(" mL/s");
lcd.setCursor(0, 1);
lcd.print("Total: ");
lcd.print(totalVolume, 2);
lcd.print(" L");
delay(1000);
}
void readFlowRate() {
int potValue = analogRead(FLOW_PIN);
flowRate = map(potValue, 0, 4095, 0, 1000);
totalVolume += flowRate * 1 / 1000;
Volume = String(totalVolume);
Flow = String(flowRate);
Serial.println(Volume);
Serial.println(Flow);
}
void checkTankLevel() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
tankLevel = duration * 0.034 / 2;
if (tankLevel > 300) {
if (!lowLevelAlert) {
lowLevelAlert = true;
digitalWrite(BUZZER_PIN, HIGH);
digitalWrite(ledPin, HIGH);
}
} else {
lowLevelAlert = false;
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(ledPin, LOW);
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Tank Lvl: ");
lcd.print(tankLevel);
lcd.print(" cm");
if (lowLevelAlert) {
lcd.setCursor(0, 1);
lcd.print("Warning: Low LvL!");
}
}
void send_data() {
// Display sending message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sending Data...");
// Data to send
String Data1 = plateNumber;
String Data2 = kmReading;
String Data3 = Flow;
String Data4 = Volume;
// Check WiFi connection status
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// Construct the URL with query parameters
String url = "https://script.google.com/macros/s/AKfycbyKBC0KReRrDaJxGVQqTYWsDXqOnIMfit8huBbvUwhYOxZDBMpRYAUOck2siONvbw0d/exec";
url += "?value1=" + String(Data1);
url += "&value2=" + String(Data2);
url += "&value3=" + String(Data4);
url += "&value4=" + String(Data3);
// Begin HTTP GET request
http.begin(url);
int httpResponseCode = http.GET();
// Check the response code
if (httpResponseCode > 0) {
String response = http.getString(); // Get response from server
Serial.println("Response: " + response); // Optional: log response for debugging
} else {
Serial.println("Error in sending data. HTTP Response Code: " + String(httpResponseCode));
}
// Close the HTTP connection
http.end();
} else {
Serial.println("WiFi not connected");
}
// Optional: Add delay between sends if needed
// delay(10000);
}