Interférence entre variables

Bonjour à tous,

Je vous présente vite fait mon projet, celui ci consiste à rendre "connecté" des chauffages électrique :

Le but est d'utiliser le fil pilote pour mettre en marche, eco ou arrêt le chauffage.
Je compte utiliser un ESP8266 qui gérera la commande du fil pilote et embarquera une page web pour régler les périodes de chauffage en fonction d'un planning mais également pouvoir sélectionner le mode de fonctionnement : arrêt complet du chauffage, marche forcée et mode automatique (en fonction du planning).
La gestion de température, je garde celle intégrée au chauffage pour le moment.

J'ai déjà fait une petite page web pour la sélection du mode de chauffe ainsi que le planning, j’arrive a envoyer les données a l’Arduino en incluant toute les datas dans l'URL.

Sauf que je rencontre un problème, j'ai l'impression que la variable _mode et le tableau _prog rentre en conflit ...
D'une part la fin du tableau prend des valeurs autres que 0, 1 ou 2, et lorsque je change certaine valeurs dans le tableau depuis la page web, cela change la valeur de la variable _mode, et inversement, si je change le mode ON, OFF ou AUTO, cela change des valeurs dans le tableau.

Si quelqu'un peu m'orienter vers la résolution de ce problème ...

Voici le code :
#include <Arduino.h>
#include <ESP8266WiFi.h>

// Replace with your network credentials
#define SSID  "xxxxx" 
#define PASSWORD  "xxxxx"

// Set web server port number to 80
WiFiServer server(80);

enum {OFF, ON, ECO, AUTO};

byte _mode;
byte _day, _hour;
byte _prog[7][24];

void setup() {
  Serial.begin(115200);

  // Connect to Wi-Fi network with SSID and PASSWORD
  Serial.print("Connecting to ");
  Serial.println(SSID);
  WiFi.begin(SSID, PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address 
  Serial.println("");
  Serial.println("WiFi connected at IP address:");
  Serial.println(WiFi.localIP());

  // Start Web Server
  server.begin();
}

// Main loop
void loop(){
  // Create a client and listen for incoming clients
  WiFiClient client = server.available();   
  
  //Si client connecté
  if (client) {
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();

    if (request.indexOf("/?mode=") != -1) {
    Serial.println("MAJ mode & planning");
    update_(request, "/?prog=");
    }
    
  }
  
  // Display GPIO status
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println("");
  client.print(page_web());

}

//FONCTION MISE A JOUR DONEES
void update_(String val, String valSearch){
//Mise à jour mode  
  if (val.indexOf("/?mode=off") != -1) _mode = OFF;
  else if (val.indexOf("/?mode=on") != -1) _mode = ON;
  else if (val.indexOf("/?mode=auto") != -1) _mode = AUTO;

//Mise à jour planning
  int index = val.indexOf(valSearch) + valSearch.length();
  Serial.print("Index : ");
  Serial.println(index);
    for (int i = 1; i <= 7; i++){
      for (int j = 0; j <= 23; j++){
        if (val[index] == '0') _prog[i][j] = OFF;
        else if (val[index] == '1') _prog[i][j] = ON;
        else if (val[index] == '2') _prog[i][j] = ECO;
        index++;
      } 
    }

//DEBUG
  for (int i = 1; i <= 7; i++){
    Serial.println(i);
    for (int j = 0; j <= 23; j++){
      Serial.print(_prog[i][j]);
    }
    Serial.println("");
  }
  
}

//FONCTION GENERATION PAGE WEB
String page_web(){
//Déclaration des variables
  String PAGEWEB;

//Génération page WEB :
PAGEWEB += "<!DOCTYPE HTML>";
PAGEWEB += "<html>";
  
//HEADER
  PAGEWEB += "<head>";
  PAGEWEB +=  "<meta charset='utf-8' />";
  PAGEWEB +=  "<meta name='viewport' content='width=device-width, initial-scale=1.0'>";
  PAGEWEB +=  "<title>CHAUFFAGE : Chambre ~~~</title>";
  PAGEWEB +=  "<link rel='icon' type='image/png' sizes='16x16' href='https://nsa40.casimages.com/img/2021/07/01/21070112422541371.jpg'>"; //https://nsa40.casimages.com/img/2021/06/29//210629010148557539.jpg
  PAGEWEB +=  "<link rel='apple-touch-icon' type='image/png' sizes='16x16' href='https://nsa40.casimages.com/img/2021/07/01/21070112422541371.jpg'>";
  

  //CSS
  PAGEWEB +=  "<style type'text/css'>";
  PAGEWEB +=    "#title {font-family: Calibri; color: white; font-size: 150%; font-weight: bold; text-transform: uppercase;}";
  PAGEWEB +=    "#tab_planning {border-collapse: collapse}";
  PAGEWEB +=    ".cells_planning {border: 0px solid black;font-weight: bold;text-align: center}";
  PAGEWEB +=    ".prog {border: 1px solid black;}";
  PAGEWEB +=    ".div_title {margin-left: auto; margin-right: auto; margin-top: 1px; display: table; width: 505px; height: 50px; background-color: #2196F3; vertical-align:middle;}";
  PAGEWEB +=    ".div_title span{vertical-align:middle; display: table-cell; text-align: center;}";
  PAGEWEB +=    ".div_encadrement {margin: 0 auto; width: 30em; border: 2px solid #333; box-shadow: 3px 3px 1px #444; padding: 8px 12px; background-color: #ffffff;}";
  PAGEWEB +=    ".switch-field {display: flex; margin-top: 10px; margin-bottom: 10px; margin-left: auto; margin-right: auto; overflow: hidden;}";
  PAGEWEB +=    ".switch-field input {position: absolute !important; clip: rect(0, 0, 0, 0); height: 1px; width: 1px; border: 0; overflow: hidden;}";
  PAGEWEB +=    ".switch-field label {background-color: #ffffff; color: black; font-weight: bold; font-size: 14px; line-height: 1; text-align: center; padding: 8px 16px; margin-right: -1px; border: 1px solid rgba(0, 0, 0, 0.2); box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px rgba(255, 255, 255, 0.1); transition: all 0.1s ease-in-out;}";
  PAGEWEB +=    ".switch-field input:checked + label {background-color: #2196F3; color: white; font-weight: bold; box-shadow: none;}";
  PAGEWEB +=    ".switch-field label:first-of-type {border-radius: 0px 0 0 0px;}";
  PAGEWEB +=    ".switch-field label:last-of-type {border-radius: 0 0px 0px 0;}";
  PAGEWEB +=    "input {background-color: #2196F3; border-radius: 0px; border: 0px; color: #FFFF; text-align: center; font-weight: bold; padding: 6px 20px; margin: 2px 5px; cursor: pointer; text-transform: uppercase; position: relative;}";
  PAGEWEB +=  "</style>";

  PAGEWEB += "</head>";

//BODY
  PAGEWEB += "<body>";

  //TITTLE
  PAGEWEB += "<div class='div_title'>";
  PAGEWEB +=    "<span id='title'>Chauffage connecté</span>";
  PAGEWEB += "</div>";
  PAGEWEB += "<p> </p>";

  //MODE
  PAGEWEB += "<div class='div_encadrement'>";
  PAGEWEB +=    "<div class='switch-field'>";
  PAGEWEB +=      "<input type='radio' id='mode_off' name='mode_select' value='off' ";
    if (_mode == OFF) PAGEWEB += "checked";
  PAGEWEB +=      "/>";
  PAGEWEB +=      "<label for='mode_off'>OFF</label>";
  PAGEWEB +=      "<input type='radio' id='mode_on' name='mode_select' value='on' ";
    if (_mode == ON) PAGEWEB += "checked";
  PAGEWEB +=      "/>";
  PAGEWEB +=      "<label for='mode_on'>ON</label>";
  PAGEWEB +=      "<input type='radio' id='mode_auto' name='mode_select' value='auto' ";
    if (_mode == AUTO) PAGEWEB += "checked";
  PAGEWEB +=      "/>"; 
  PAGEWEB +=      "<label for='mode_auto'>AUTO</label>";
  PAGEWEB +=    "</div>"; 

  //TABLEAU PLANNING
  PAGEWEB +="<p id='planning'>Planning</p>";
  PAGEWEB +="<table id='tab_planning'>";
  PAGEWEB +=  "<tr class='cells_planning'>";
  PAGEWEB +=    "<td></td>";
    
    //--> Génération ligne heures
  for (byte i = 0; i <= 23; i++){
    PAGEWEB += "<td id='tab'>";
    if (i < 10) PAGEWEB += "0" + String(i);
    else PAGEWEB += String(i);
    PAGEWEB += "</td>";
  }
  PAGEWEB +=  "<tr/>";

    //--> Génération lignes jours
  String tab_day[] = {"L", "M", "M", "J", "V", "S", "D"};
  byte index = 0;

  for (byte i = 1; i <= 7; i++){
    PAGEWEB +=  "<tr class='cells_planning'>";
    PAGEWEB +=    "<td>" + tab_day[i-1] + "</td>";

    for (byte j = 0; j <= 23; j++){
      String color;
      if (_prog[i][j] == OFF) color = "#FFFFFF";
      else if (_prog[i][j] == ON) color = "#2196F3";
      else if (_prog[i][j] == ECO) color = "#FFFF";
      PAGEWEB += "<td class='prog' style='color:rgba(1,1,1,0);' onclick='color(" + String(index) + ");' bgColor ='" + color + "'id='" + String(index) + "'>00</td>";
      index ++; 
    }
    PAGEWEB +=  "</tr>";
  }
  PAGEWEB +="</table></br>";

  PAGEWEB +="<a><input type='button' value='Valider' onclick='update_planning();'>";
  PAGEWEB +="</a>";

//JAVASCRIPT
  PAGEWEB +="<script type='text/javascript'>";

    //FUNCTION COLOR
  PAGEWEB +=  "function color(id_prog){";
  PAGEWEB +=  "var colored = document.getElementById(id_prog).bgColor;";

  PAGEWEB += "if (colored === '#FFFFFF'){";
  PAGEWEB +=    "colored = '#2196F3';";
  PAGEWEB +=    "}";
  PAGEWEB +=  "else if (colored === '#2196F3'){";
  PAGEWEB +=    "colored = '#FFFF';";
  PAGEWEB +=    "}";
  PAGEWEB +=  "else {";
  PAGEWEB +=    "colored = '#FFFFFF';";
  PAGEWEB +=    "}";
  PAGEWEB +=  "document.getElementById(id_prog).bgColor = colored;";
  PAGEWEB +=  "}";

    //FUNCTION UPDATE
  PAGEWEB +=  "function update_planning(){";
  PAGEWEB +=  "var mode = '?mode=';";
  PAGEWEB +=  "var programmation = '/?prog=';";
  PAGEWEB +=  "var data;";

  PAGEWEB +=  "if (document.getElementById('mode_off').checked){";
  PAGEWEB +=    "mode = mode + 'off';";
  PAGEWEB +=  "}";
  PAGEWEB +=  "else if (document.getElementById('mode_on').checked){";
  PAGEWEB +=    "mode = mode + 'on';";
  PAGEWEB +=  "}";
  PAGEWEB +=  "else if (document.getElementById('mode_auto').checked){";
  PAGEWEB +=    "mode = mode + 'auto';";
  PAGEWEB +=  "}";

  PAGEWEB +=  "for (let i = 0; i <= 167; i++) {";
  PAGEWEB +=    "if (document.getElementById(i).bgColor === '#FFFFFF'){";
  PAGEWEB +=    "programmation = programmation + '0';";
  PAGEWEB +=    "}";
  PAGEWEB +=    "else if (document.getElementById(i).bgColor === '#2196F3'){";
  PAGEWEB +=    "programmation = programmation + '1';";
  PAGEWEB +=    "}";
  PAGEWEB +=    "else if (document.getElementById(i).bgColor === '#FFFF'){";
  PAGEWEB +=    "programmation = programmation + '2';";
  PAGEWEB +=    "}";
  PAGEWEB +=  "}";
  
  PAGEWEB +=  "data = mode + programmation + '/&random=' + Math.trunc(Math.random() * 1000000);";
  //PAGEWEB +=  "data = programmation + '&random=' + Math.trunc(Math.random() * 1000000);";
  PAGEWEB +=  "document.location.href = data;";
  PAGEWEB +=  "}";  

  PAGEWEB +="</script>";
  
  PAGEWEB += "</body>";

  PAGEWEB += "</html>";
  
  
  return (PAGEWEB);
}

PS: j'ai essayé de commenter toute la page web avec sa variable string, en pensant que cela était du a cette variable qui prend beaucoup de place, mais le problème persiste.

Merci d'avance !

for (int i = 1; i <= 7; i++){
      for (int j = 0; j <= 23; j++){
        if (val[index] == '0') _prog[i][j] = OFF;

Le tableau est défini avec 7 éléments

byte _prog[7][24];

Donc l'indice i devrait aller de 0 à 6.

3 Likes

Erreur bête ... merci !

On peut rester planté durant des heures devant sans s'en rendre compte ...

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.