void tick(); void getConsoApparente(); //Ticker timer(tick, 10000); void tick() { } void traitement() { #ifdef TUYA getConsoApparente(); #endif #ifdef SERIAL_XY6020L //if (slider_on == false) { // double cc_tmp = abs(conso_apparente / TENSION_CHARGE) / 3.0; double cc_tmp = abs(conso_apparente) > 3.0 * TENSION_CHARGE ? abs(conso_apparente / TENSION_CHARGE) / 3.0 : abs(conso_apparente / TENSION_CHARGE); boolean en_charge = (digitalRead(PIN_CHARGE) == HIGH); delay(10); boolean en_injection = (digitalRead(PIN_INJECTION) == HIGH); delay(10); last_message = String(lastTimeRead) + " CHG=" + String(en_charge) + " INJ=" + String(en_injection); #ifdef SERIAL_XY6020L_2 #define START_INJECTION 15 #define STOP_INJECTION -15 #else #define START_INJECTION 230 #define STOP_INJECTION (production > 50 ? -15 : -60) #endif if ((conso_apparente < STOP_INJECTION || tension_batterie <= 24.8) && en_injection && cc2 <= 0) { last_message += " stopInjection"; //stopInjection(); cc2 = 0; digitalWrite(PIN_INJECTION, LOW); } else if (conso_apparente < -15 && !en_charge && !en_injection && production > 15) { // charge(); last_message += " début charge"; cc2 = 0; // digitalWrite(PIN_INJECTION, LOW); digitalWrite(PIN_CHARGE, HIGH); } else if (conso_apparente > 15 && en_charge && cc <= 0) { // stopCharge(); last_message += " fin charge"; cc = 0; digitalWrite(PIN_CHARGE, LOW); } else if (conso_apparente > START_INJECTION && !en_injection && tension_batterie > 25.2 && !en_charge) { // injection(); last_message += " début injection"; digitalWrite(PIN_INJECTION, HIGH); // digitalWrite(PIN_CHARGE, LOW); cc = 0; } else { //last_message += " aucune action"; // + String(START_INJECTION) + " " + String(tension_batterie) + " " + String(conso_apparente > START_INJECTION); } en_charge = (digitalRead(PIN_CHARGE) == HIGH); delay(10); en_injection = (digitalRead(PIN_INJECTION) == HIGH); Serial.print("en_charge="); Serial.print(en_charge); Serial.print(" en_injection="); Serial.println(en_injection); delay(10); // Conso > 10 Conso < -10 // cc > 0 cc - cc + // cc = 0 cc + // cc2 > 0 cc2 + cc2 - // cc2 = 0 cc2 + // boolean en_charge = cc > 0 || (cc == 0 && conso_apparente < -10); // boolean en_injection = cc == 0 && (cc2 > 0 && conso_apparente > 10); if (en_charge) { if (conso_apparente > 10 && cc > 0) { cc -= cc_tmp; last_message += " cc-=" + String(cc, 2) + " " + String(cc_tmp, 2); setCC(xy, cc); } else if (conso_apparente < -10) { last_message += " cc+=" + String(cc, 2) + " " + String(cc_tmp, 2); cc += cc_tmp; setCC(xy, cc); } } else { setCC(xy, 0); } Serial.print("cc="); Serial.println(cc); #endif #ifdef SERIAL_XY6020L_2 // double cc_tmp2 = abs(conso_apparente / TENSION_INJECTION) / 3.0; double cc_tmp2 = abs(conso_apparente) > 3.0 * TENSION_INJECTION ? abs(conso_apparente / TENSION_INJECTION) / 3.0 : abs(conso_apparente / TENSION_INJECTION); if (en_injection) { if (conso_apparente > 10) { cc2 += cc_tmp2; last_message += " cc2+=" + String(cc2, 2) + " " + String(cc_tmp2, 2); setCC2(xy2, cc2); } else if (conso_apparente < -10 && cc2 > 0) { cc2 -= cc_tmp2; last_message += " cc2-=" + String(cc2, 2) + " " + String(cc_tmp2, 2); setCC2(xy2, cc2); } } else { setCC2(xy2, 0); } Serial.print("cc2="); Serial.println(cc2); #endif // } // else { // last_message = "Slider is on"; // } } void getConsoApparente() { double conso = 0; if (WiFi.status() == WL_CONNECTED) { String url = String(MICRO_SERVICE_ADDRESS); #ifdef ESP8266 HTTPClient http; http.begin(url); #else HTTPClient http; http.begin(url.c_str()); #endif http.addHeader("Content-Type", "application/json"); String json = "{\"action\":\"on\"}"; // ou "{\"action\":\"off\"}" int httpResponseCode = http.POST(json); if (httpResponseCode > 0) { String response = http.getString(); Serial.println(httpResponseCode); Serial.println(response); StaticJsonDocument<200> doc; // Parse le JSON DeserializationError error = deserializeJson(doc, response); if (error) { Serial.print(F("Failed to parse JSON: ")); Serial.println(error.f_str()); //return 0.0; } // if (String(doc["conso_apparente"]).toFloat() < 0 && abs(String(doc["conso_apparente"]).toFloat()) > String(doc["production"]).toFloat()) { // Serial.println("Erreur de retour Tuya"); // } // else { if (String(doc["conso_apparente"]).toFloat() != 0 || String(doc["production"]).toFloat() != 0) { conso_apparente = String(doc["conso_apparente"]).toFloat(); //[0]; production = String(doc["production"]).toFloat(); //[0]; } // } Serial.println(conso); } else { Serial.print("Erreur lors de l'envoi de la requête POST: "); Serial.println(httpResponseCode); } http.end(); } }