// AWIN- TECH  is the team (@manojmedengg and @priscilla05062000) which develope this project.
// this project is based on the smart cattle monitering system for percision livestock forming
// in this project the to major sensor are used that is tmprature and humidity sensor and acceal and gyroscopic sensor 
// the potetio meter is used insted of the pulse sensor 
// by using this project we  can able to know about  a temprature, pulse, position of the cow
// in future the gps tracking system is attached to this so we can able to watch the cattle from remot

// import the libraies from the library manager
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <WiFi.h>
#include <MPU6050.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>

// Replace with your Blynk project details (get these from Blynk app)
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
char auth[] = "";

// Replace with your WiFi credentials
char ssid[] = "";
char pass[] = "";

// DHT22 sensor settings
#define DHTPIN A0
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

// MPU6050 settings
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;

// Potentiometer pin (adjust based on your hardware setup)
#define POTPIN 32
// I2C LCD settings
LiquidCrystal_I2C lcd(0x27, 16, 2);  // Address may vary depending on your LCD module

void setup() {
  Serial.begin(115200);

  // Initialize DHT sensor
  dht.begin();

  // Initialize MPU6050
  Wire.begin();
  mpu.initialize();
  // Initialize LCD
  lcd.begin(16, 2);
  lcd.backlight();

  // Initialize Blynk
  Blynk.begin(auth, ssid, pass);

  // Print initial message to LCD
  lcd.setCursor(0, 0);
  lcd.print("Smart Cattle");
  lcd.setCursor(0, 1);
  lcd.print("Monitoring");

  delay(2000);
}

void loop() {
  Blynk.run();

  // Read DHT22 sensor
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // Check if any reads failed and exit early (to avoid NaN values)
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Read MPU6050 sensor
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  // Read potentiometer (consider mapping the value to a more realistic pulse range)
  int potValue = analogRead(34);
  float estimatedPulse = map(potValue, 0, 130, 48, 84); // Assuming 0-130 range for potentiometer

  // Print values to LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Temp: ");
  lcd.print(temperature);
  lcd.print(" C");

  lcd.setCursor(0, 1);
  lcd.print("Hum: ");
  lcd.print(humidity);
  lcd.print(" %");

  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Ax: ");
  lcd.print(ax);

  lcd.setCursor(0, 1);
  lcd.print("Ay: ");
  lcd.print(ay);

  delay(2000);

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Az: ");
  lcd.print(az);

  lcd.setCursor(0, 1);
  lcd.print("Pulse: ");
  lcd.print(estimatedPulse);
  lcd.print(" BPM");  // Clarify that this is an estimate

  delay(2000);

  // Send data to Blynk virtual pins (adjust pin numbers if needed)
  Blynk.virtualWrite(V1, temperature);
  Blynk.virtualWrite(V2, humidity);
  Blynk.virtualWrite(V3, ax);
}