// Define the analog pin where the ACS712 sensor is connected
const int currentSensorPin = A0;

// Calibration factor for ACS712 (change according to your sensor)
const float sensitivity = 0.185; // Sensitivity in volts per ampere for ACS712-5A

// Variables to store readings
float current = 0.0;
float totalCurrent = 0.0;
unsigned long startTime = 0;
unsigned long interval = 600*3; // 1 minute in milliseconds
float totalTime = 0.0;
float initTime = 0.0;

float battCapacity = 20 * 60 * 60;

float totalCurrentDrawOverTime = 0.0;

void setup() {
  Serial.begin(9600);
  initTime = millis();
  startTime = millis(); // Record the start time
}

void loop() {
  // Read the raw analog value from the current sensor
  int rawValue = analogRead(currentSensorPin);

  // Convert the raw analog value to voltage
  float voltage = (rawValue / 1024.0) * 5.0;

  // Convert the voltage to current
  current = (voltage - 2.5) / sensitivity * 0.2/2;

  // Accumulate current over time to calculate total current draw
  totalCurrent += current;

  // Print the current draw every second
  Serial.print("Current: ");
  Serial.print(current);
  Serial.println(" A");

  // Check if the specified interval has passed
  if (millis() - startTime >= interval) {
    // Calculate the total current draw (in Ampere-seconds)
    float totalCurrentDraw = totalCurrent;

    totalTime = millis() - initTime;

    // Print the total current draw over the specified interval
    Serial.print("Total Current Draw in ");
    Serial.print(totalCurrentDraw);
    Serial.println(" A·s"); // Ampere-seconds

    totalCurrentDrawOverTime += totalCurrentDraw;
    Serial.print("Overall Current:");
    Serial.print(totalCurrentDrawOverTime);
    Serial.print(" A.s");

    Serial.println("*******************");
    Serial.print("Used Battery Percent: ");
    float battPercent = totalCurrentDrawOverTime / battCapacity * 100;

    Serial.print(battPercent);
    Serial.print("% ");

    Serial.print("in ");
    int min = totalTime/(60*1000);
    int sec = (static_cast<int>(totalTime) % (60 * 1000)) / 1000;// Calculate seconds

    Serial.print(min);
    Serial.print(" min ");
    Serial.print(sec);
    Serial.print(" sec");
    Serial.println();
    Serial.print("Power Consumption: ");
    float power = totalCurrentDrawOverTime * 72/3600;
    Serial.print(power);
    Serial.print(" W");
    Serial.println("________________________");

    // Reset for the next interval
    totalCurrent = 0.0;
    startTime = millis();
  
  }

  delay(1000); // Wait for 1 second before taking the next reading
}
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:8
esp:9
esp:10
esp:11
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:20
esp:21
esp:26
esp:33
esp:34
esp:35
esp:36
esp:37
esp:38
esp:39
esp:40
esp:41
esp:42
esp:45
esp:46
esp:3V3
esp:5V
esp:GND.1
esp:TX
esp:RX
esp:RST
esp:GND.2