diff --git a/src/main.cpp b/src/main.cpp index 8879d76..3048b96 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,8 +74,8 @@ unsigned long otaRebootTime = 0; // Variable pour le mode JOUR - mémoriser si la température max a été atteinte aujourd'hui bool maxTempReachedToday = false; -// Variables pour la gestion PWM avec timer (période 1s) -const unsigned long PWM_PERIOD = 1000; // Période de 1 seconde en ms +// Variables pour la gestion PWM avec timer (période 100ms) +const unsigned long PWM_PERIOD = 100; // Période de 100ms en ms volatile unsigned long pwmCycleCount = 0; // Compteur de cycles PWM volatile unsigned long pwmOnTime = 0; // Temps ON en ms pour le cycle actuel volatile unsigned long pwmElapsed = 0; // Temps écoulé dans le cycle actuel en ms @@ -415,12 +415,12 @@ bool checkAuthentication(AsyncWebServerRequest *request) { return false; } -// Interruption timer appelée toutes les 10ms (100Hz) pour gérer le PWM +// Interruption timer appelée toutes les 1ms (1000Hz) pour gérer le PWM void IRAM_ATTR onTimerISR() { // ESP32: Protéger l'accès aux variables partagées portENTER_CRITICAL_ISR(&timerMux); - pwmElapsed += 10; + pwmElapsed += 1; // Si on dépasse la période, recommencer un nouveau cycle if (pwmElapsed >= PWM_PERIOD) { @@ -449,7 +449,7 @@ void fetchEnphaseData() { WiFiClientSecure client; client.setInsecure(); // Ignorer la vérification du certificat SSL HTTPClient http; - String url = "https://" + enphaseGatewayIP + "/ivp/meters/reports/consumption"; + String url = "https://" + enphaseGatewayIP + "/ivp/meters/readings"; http.begin(client, url); http.addHeader("Accept", "application/json"); http.addHeader("Authorization", "Bearer " + enphaseToken); @@ -460,14 +460,12 @@ void fetchEnphaseData() { JsonDocument doc; DeserializationError error = deserializeJson(doc, payload); if (!error && doc.is() && doc.size() >= 2) { - float total = doc[0]["cumulative"]["currW"].as(); - float net = doc[1]["cumulative"]["currW"].as(); - solarProduction = round(total - net); // Production des panneaux arrondie + solarProduction = round(doc[0]["activePower"].as()); // Production des panneaux arrondie if (solarProduction < 0) { solarProduction = 0; // Éviter les valeurs négatives } - powerImport = round(net); // Consommation totale enedis arrondie - powerNetConsumption = round(total); // Consommation de la maison + powerImport = round(doc[1]["activePower"].as()); // Consommation totale enedis arrondie + powerNetConsumption = round(powerImport+solarProduction); // Consommation de la maison Serial.printf("Enphase - Total: %.1fW, Panneaux: %.1fW, Enedis: %.1fW\n", powerNetConsumption, solarProduction, powerImport); enphaseConnectionError = false; @@ -1599,15 +1597,15 @@ void setup() { Serial.print("Accédez à http://"); Serial.println(WiFi.localIP()); - // Initialiser le timer pour le PWM (interruption toutes les 10ms = 100Hz) + // Initialiser le timer pour le PWM (interruption toutes les 1ms = 1000Hz) // ESP32: Utiliser hw_timer // Timer 0, prescaler 80 (80MHz / 80 = 1MHz = 1µs), compte vers le haut timer = timerBegin(0, 80, true); timerAttachInterrupt(timer, &onTimerISR, true); - // Déclencher toutes les 10000µs = 10ms = 100Hz - timerAlarmWrite(timer, 10000, true); + // Déclencher toutes les 1000µs = 1ms = 1000Hz + timerAlarmWrite(timer, 1000, true); timerAlarmEnable(timer); - Serial.println("Timer PWM initialisé (100Hz)"); + Serial.println("Timer PWM initialisé (1000Hz)"); // Créer la tâche FreeRTOS pour Enphase sur le cœur 1 // Priority: 2 (plus élevée que tskIDLE_PRIORITY=0 mais moins que le loop=1)