[Conseil] Recevoir des données depuis base de donnée sql en ligne.

Bonjour à tous, :slight_smile:

Voilà je poste ce message aujourd'hui car après pas mal de recherche et d'heure à essayer de régler mon problème je n'y arrive toujours pas...
Je crée aujourd'hui un système hydroponique connecté.
J'ai donc plusieurs capteurs et actionneurs. J'envoi les données des capteurs à une base de données sql en ligne grâce à un fichier php stocké dans mon serveur ovh.

Ensuite je reçois des données toujours pour corriger les valeurs voulues dans mon systèmes.
Exemple : J'ai 7 de PH dans ma cuve et l'utilisateur veut 6, mon arduino comparera la valeur réel du capteur avec la valeur voulu et corrigera ça.

Toute ma partie envoie de données sur la base de données et la partie gestion de mon système fonctionne bien.

Mais voilà j'ai un problème quand il s'agit de recevoir les données... J'arrive à extraire une valeur de ma base de données mais pas plus...
Voici mon code arduino :

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDA, 0x01 };
IPAddress ip(192,168,1,20);

// initialize the library instance:
EthernetClient client;
bool j=1;
const unsigned long requestInterval = 6000;  // delay between requests

char serverName[] = "www.monSiteWeb.fr";

boolean requested;                   // whether you've made a request since connecting
unsigned long lastAttemptTime = 0;   // last time you connected to the server, in milliseconds

String currentLinePh = "";            // string to hold the text from server
String currentLineEc = ""; 
String pHWanted = "";   
String eCWanted = ""; // string to hold the pHWanted
boolean lecturePh = false;       // if you're currently reading the pHWanted
boolean lectureEc = false;  
unsigned long previousMillis = 0;  
const long interval = 10000;









void setup() {
 // reserve space for the strings:
 currentLinePh.reserve(256);
 currentLineEc.reserve(256);
 pHWanted.reserve(150);
 eCWanted.reserve(150);

 // Open serial communications and wait for port to open:
 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }

 // attempt a DHCP connection:
 Serial.println("Attempting to get an IP address using DHCP:");
 if (!Ethernet.begin(mac)) {
   // if DHCP fails, start with a hard-coded address:
   Serial.println("failed to get an IP address using DHCP, trying manually");
   Ethernet.begin(mac, ip);
 }
 Serial.print("Mon adresse:");
 Serial.println(Ethernet.localIP());
 // premiere connection :
 //connectToServerGetData();

}





void loop(){

if (millis() - previousMillis >= interval) {
  connectToServerGetData();
  previousMillis=millis();
  
  
  if(j==1){
    if(pHWanted.length() > 0 && lecturePh==false){
       Serial.print("pHWanted : ");
       Serial.println(pHWanted);
       pHWanted=""; 
    }
 
    if (client.connected()) {
      checke_page_PH(); 
 
      //checke_page_EC(); 
   
    }
   j=0;
  }
  
  else if (j==0){
    
    if(eCWanted.length() > 0 && lectureEc==false){
      Serial.print("eCWanted : ");
      Serial.println(eCWanted);
      eCWanted=""; 
    }
 
    if (client.connected()) {
      //checke_page_PH(); 
      checke_page_EC(); 
      
   
    }
    j=1;
  }
  client.stop();

  

}

}

 /* else if (millis() - lastAttemptTime > requestInterval) {
   // if you're not connected, and two minutes have passed since
   // your last connection, then attempt to connect again:
    connectToServerGetData();
 }*/
















void checke_page_PH(){
 if (client.available()) {
   // read incoming bytes:
   char inCharPh = client.read();

   // add incoming byte to end of line:
   currentLinePh += inCharPh; 

   // si on a un retour chariot, c'est que ce n'est pas encore le debut, on supprime:
   if (inCharPh == '\n') {
     currentLinePh = "";
   } 

   // si on trouve le message <text>, c'est 
   // que le message suit:
   if ( currentLinePh.endsWith("<PH>")) {

     // debut du message, on vide la string message:
     lecturePh = true; 
     pHWanted = "";
     inCharPh = client.read(); // lire le caractere suivant
   }
   // on lit caractere par caractere,
   // et les ajoute au message
   if (lecturePh) {
     if (inCharPh != '<') {
       pHWanted += inCharPh;
     } 
     else {
       // isi vous avez un "<",
       // c'est la fin du message:
       lecturePh = false;
          
       
     }
   }
 }
}

void checke_page_EC(){
 if (client.available()) {
   // read incoming bytes:
   char inCharEc = client.read();

   // add incoming byte to end of line:
   currentLineEc += inCharEc; 

   // si on a un retour chariot, c'est que ce n'est pas encore le debut, on supprime:
   if (inCharEc == '\n') {
     currentLineEc = "";
   } 

   // si on trouve le message <text>, c'est 
   // que le message suit:
   if ( currentLineEc.endsWith("<EC>")) {

     // debut du message, on vide la string message:
     lectureEc = true; 
     eCWanted = "";
     inCharEc = client.read(); // lire le caractere suivant
   }
   // on lit caractere par caractere,
   // et les ajoute au message
   if (lectureEc) {
     if (inCharEc != '<') {
       eCWanted += inCharEc;
     } 
     else {
       // isi vous avez un "<",
       // c'est la fin du message:
       lectureEc = false;
          
       
     }
   }
 }
}

void connectToServerGetData() {
 // attempt to connect, and wait a millisecond:
 Serial.println("connecting to server...");
 if (client.connect(serverName, 80)) {
   Serial.println("making HTTP request...");
   // make HTTP GET request:
   client.println("GET /Script/getData.php HTTP/1.1");
   client.println("Host: www.monSiteWeb.fr");
   client.println();
 }
 // remettre le compteur a zero pour la prochaine connexion:
 //lastAttemptTime = millis();
 delay(1);
}

Et mon script php getData.php :

<?php
    
    //$con = mysqli_connect("localhost", "xxxxxx", "xxxxxxx", "xxxxxx");
    $con = mysqli_connect("xxxxxx.mysql.db", "xxxxxx", "xxxx", "xxxx");
    
    $statement = mysqli_prepare($con, "SELECT * FROM sensor WHERE id =  1");
    mysqli_stmt_execute($statement);
    
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement,$id,$system_id,$pH,$pump,$lumino,$tempWater,$eC,$pHWanted,$eCWanted,$luminoD,$luminoF);
     if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

    
    $response = array();
    $response["success"] = false;  
    
    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true; 
        $response["id"] = $id;
        $response["system_id"] = $system_id;
        $response["pH"] = $pH;
        $response["pump"] = $pump;
        $response["lumino"] = $lumino;
        $response["tempWater"] = $tempWater;
        $response["eC"] = $eC;
        $response["pHWanted"] = $pHWanted;
        $response["eCWanted"] = $eCWanted;
        $response["luminoD"] = $luminoD;
        $response["luminoF"] = $luminoF;

    }
    
    //echo json_encode($response);
echo("$pHWanted;$eCWanted");
echo("<PH>".$pHWanted."</PH>");
echo("<EC>".$eCWanted."</EC>");
echo("<LUM>".$lumino."</LUM>");


?>

Lorsque je fais la requête du Ph ou de l'EC tout seul, mon code fonctionne parfaitement. Mais dès que je veux récupérer deux variables le client n'est plus available.

J'ai testé avec la ligne suivante dans mon code :

Serial.println(client.read());

Il me renvoi automatiquement :

-1

J'utilise un automate CONTROLLINO MAXI automation (C'est une arduino mega en version industrielle.)
Il a un port ethernet utilisant le shield ethernet W5100.

Est-ce que quelqu'un aurait une idée pour m'aider ?

Merci à vous ! :smiley:

PS: Ceci est mon premier post donc j'espère qu'il n'y aura pas de bavure sur la façon de présenter... :slight_smile: