2 – Les fonctions
//-------------------------------------- Sous programmes -------------------------------
void ecriture_json()
{ // Créer un document JSON avec la capacité définie
JsonDocument doc;
// Assigner des valeurs aux variables
doc["T1_Debut_EV1"] = T1_Debut_EV1;
doc["T1_Debut_M_EV1"] = T1_Debut_M_EV1;
doc["T1_Nb_Cycle_EV1"] = T1_Nb_Cycle_EV1;
doc["T1_Periode_Cycle_EV1"] = T1_Periode_Cycle_EV1;
doc["T1_Duree_Cycle_EV1"] = T1_Duree_Cycle_EV1;
// Convertir le document JSON en chaîne JSON
String output;
serializeJson(doc, output);
// Enregistrer la chaîne JSON dans l'EEPROM
for (size_t i = 0; i < output.length(); ++i)
{
EEPROM.write(i, output[i]);
}
EEPROM.commit(); // N'oubliez pas de faire un commit après avoir écrit dans l'EEPROM
Serial.println("Données enregistrées dans l'EEPROM !");
}
void lecture_json()
{
// Lire la chaîne JSON enregistrée dans l'EEPROM
String input;
for (int i = 0; i < 200; ++i)
{
input += char(EEPROM.read(i));
}
// Convertir la chaîne JSON en document JSON
JsonDocument doc;
DeserializationError error = deserializeJson(doc, input);
if (error)
{
Serial.print("Erreur lors de la lecture du JSON : ");
Serial.println(error.c_str());
return;
}
// Extraire les valeurs du document JSON
T1_Debut_EV1 = doc["T1_Debut_EV1"].as<String>();
T1_Debut_M_EV1 = doc["T1_Debut_M_EV1"].as<String>();
T1_Nb_Cycle_EV1 = doc["T1_Nb_Cycle_EV1"].as<String>();
T1_Periode_Cycle_EV1 = doc["T1_Periode_Cycle_EV1"].as<String>();
T1_Duree_Cycle_EV1 = doc["T1_Duree_Cycle_EV1"].as<String>();
Serial.print(" lecture du JSON ok ");
}
String readHeure()
{
if (val_heure == "")
{
val_heure = "En attente signal GPS";
return val_heure;
}
else
{
// Serial.println(val_heure);
return val_heure;
}
}
String readDate()
{
// Serial.println(val_date);
return val_date;
}
String readAHTTemperature()
{
aht.getEvent(&humidity, &temp);
float temperatureInt = temp.temperature;
if (isnan(temperatureInt))
{
Serial.println("Echec de la lecture du capteur AHT!");
return "--";
}
else
{
// Serial.print("température :");
// Serial.println(temperatureInt);
return String(temperatureInt);
}
}
String readAHTHumidity()
{
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
aht.getEvent(&humidity, &temp);
humidite = humidity.relative_humidity;
if (isnan(humidite))
{
Serial.println("Echec de la lecture du capteur AHT");
return "--";
}
else
{
// Serial.print("Humidité :");
// Serial.println(humidite);
return String(humidite);
}
}
String outputState(int output)
{
if (digitalRead(output))
{
return "checked";
}
else
{
return "";
}
}
void GestionZero()
{
zh = "";
if (Hour < 10)
{
zh = "0";
}
zmn = "";
if (Minute < 10)
{
zmn = "0";
}
zs = "";
if (Second < 10)
{
zs = "0";
}
zmois = "";
if (Month < 10)
{
zmois = "0";
}
}
void Affiche_gpio()
{
Serial.print("GPIO: ");
Serial.print(inputMessage1);
Serial.print(" - Etat: ");
Serial.println(inputMessage2);
Serial.print(" - Mode Am ");
Serial.println(Mode_AM);
}
void Clignotement(int temps)
{
digitalWrite(led, LOW); // led clignote en attende de synchronisation avec le WIFI
delay(temps);
digitalWrite(led, HIGH);
delay(temps);
}
void affiche_ecran()
{
display.setCursor(0, 0);
display.clearDisplay();
display.print(val_date);
display.print(" ");
display.println(val_heure);
display.setCursor(0, 24);
display.print("Mem libre:");
display.print(ESP.getFreeHeap()); // Imprimez la quantité de mémoire libre
// display.println(" octets");
display.display();
}
String readOut()
{
String output = String(val_out);
String state = String(val_state);
return output + "&" + state;
}
// Replaces placeholder with button section in your web page
String processor(const String &var)
{
if (var == "T1_Debut_EV1")
{
return T1_Debut_EV1;
}
if (var == "T1_Debut_M_EV1")
{
return T1_Debut_M_EV1;
}
if (var == "T1_Nb_Cycle_EV1")
{
return T1_Nb_Cycle_EV1;
}
if (var == "T1_Periode_Cycle_EV1")
{
return T1_Periode_Cycle_EV1;
}
if (var == "T1_Duree_Cycle_EV1")
{
return T1_Duree_Cycle_EV1;
}
if (var == "HEURE")
{
return readHeure();
}
if (var == "DATE")
{
return readDate();
}
if (var == "TEMPERATURE")
{
return readAHTTemperature();
}
else if (var == "HUMIDITY")
{
return readAHTHumidity();
}
if (var == "BUTTONPLACEHOLDER")
{
String buttons = "";
// buttons += "<h4>Manu/Auto</h4><label class="switch"><input type="checkbox" onchange="toggleMode(this)" id="modeSwitch" " + outputState(1) + "><span class="slider"></span></label>";
buttons += "<h4>Manu/Auto</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="4" id="modeSwitch"" + outputState(1) + "><span class="slider"></span></label>";
buttons += "<div id="manualControls">";
buttons += "<h4>Arret/Marche EV1</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="3" " + outputState(1) + "><span class="slider"></span></label>";
buttons += "<h4>Arret/Marche EV2</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="2" " + outputState(1) + "><span class="slider"></span></label>";
buttons += "<h4>Arret/Marche EV3</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="1" " + outputState(1) + "><span class="slider"></span></label>";
buttons += "<h4>Arret/Marche EV4</h4><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" id="0" " + outputState(1) + "><span class="slider"></span></label>";
buttons += "</div>";
return buttons;
}
return String();
}