// Capteur de courant ACS712-30A

// carte ESP32C3 Dev Module

// Procedure de calibration :
// Debranchez toute charge (0A absolu)
// Connectez-vous a l'AP "ESP32_Current" (mdp: 12345678)
// Accedez a http://192.168.4.1
// Calibrer a 0A
// Faire les mesures avec differentes valeurs

#include <WiFi.h>
#include <WebServer.h>

// Point d'acces WiFi
const char *ssid = "ESP32-Courant";
const char *password = "12345678";

// Broche du capteur
const int ACS712_PIN = 2;

// Parametres ADC / capteur
const float VCC = 5.0;
const float Sensitivity = 0.066;  // 66mV/A pour ACS712-30A
const int ADC_RESOLUTION = 4095;
const float ADC_REF_VOLTAGE = 3.3;

WebServer server(80);

// Offset global pour la calibration a zero
float zeroOffset = 0.0;

// Fonction de lecture du courant
float readCurrent() {
  const int NUM_SAMPLES = 100;
  long total = 0;

  for (int i = 0; i < NUM_SAMPLES; i++) {
    total += analogRead(ACS712_PIN);
    delay(1);
  }

  float avgRaw = total / (float)NUM_SAMPLES;
  float voltage = (avgRaw / ADC_RESOLUTION) * ADC_REF_VOLTAGE;

  // Difference par rapport a l'offset
  float adjustedVoltage = voltage - zeroOffset;
  float current = adjustedVoltage / Sensitivity;

  return current;
}

// Fonction pour recalibrer l'offset a zero
void calibrateOffset() {
  const int NUM_SAMPLES = 100;
  long total = 0;

  for (int i = 0; i < NUM_SAMPLES; i++) {
    total += analogRead(ACS712_PIN);
    delay(1);
  }

  float avgRaw = total / (float)NUM_SAMPLES;
  zeroOffset = (avgRaw / ADC_RESOLUTION) * ADC_REF_VOLTAGE;

  Serial.println("Calibration a zero effectuee. Offset = " + String(zeroOffset, 3));
}

// Page HTML
void handleRoot() {
  float current = readCurrent();

  String html = "<!DOCTYPE html><html><head><meta charset='UTF-8'>";
  html += "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
  html += "<meta http-equiv='refresh' content='2'>";
  html += "<title>Mesure Courant</title></head><body>";
  html += "<h2>Mesure de courant (ACS712-30A) : ";
  html += "<strong>" + String(current, 2) + " A</strong></h2>";
  html += "<form action='/calibrate' method='POST'>";
  html += "<button type='submit'>Calibration 0A</button>";
  html += "</form>";
  html += "</body></html>";

  server.send(200, "text/html", html);
}

// Gestion du bouton de calibration
void handleCalibrate() {
  calibrateOffset();
  server.sendHeader("Location", "/");
  server.send(303);
}

void setup() {
  Serial.begin(115200);
  WiFi.softAP(ssid, password);
  Serial.println("Point d'acces WiFi actif");
  Serial.print("Adresse IP : ");
  Serial.println(WiFi.softAPIP());

  analogReadResolution(12);
  analogSetAttenuation(ADC_11db);

  delay(2000);  // Stabilisation

  // Calibration initiale (a vide)
  calibrateOffset();

  // Serveur web
  server.on("/", handleRoot);
  server.on("/calibrate", HTTP_POST, handleCalibrate);
  server.begin();
  Serial.println("Serveur Web lance !");
}

void loop() {
  server.handleClient();
}