Problème d'envois à ma base de donnée

Bonjour je suis élève de Sti2d en SIN et j'ai un problème lors de l'envois de mes données dans ma base de donnée voici mon code arduino:

#include <SPI.h>
#include <SoftwareSerial.h>
#include <Ethernet2.h>   //Inclure la bibliothèque Ethernet

byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x4F, 0x63 };  //90-A2-DA-0F-15-15
EthernetClient client;
EthernetServer server(80);  //Initialise le serveur Ethernet, port 80 par défaut pour HTTP
int  val = 0; 
char code[10]; 
int bytesread = 0;

void setup()
{
Serial1.begin(2400); //Affichage écran du PC
Serial.begin(9600);
Ethernet.begin(mac);   //Démarer la connexion Ethernet et le serveur analogRead(CAPTEUR_TEMP_PIN);
pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
digitalWrite(2, LOW);                  // Activate the RFID reader
}  

void loop()
{
if(Serial1.available() > 0) {          // if data available from reader 
if((val = Serial1.read()) == 10) {   // check for header 
bytesread = 0; 
while(bytesread<10) {              // read 10 digit code 
if( Serial1.available() > 0) { 
val = Serial1.read(); 
if((val == 10)||(val == 13)) {
// if header or stop bytes before the 10 digit reading 
break;                       // stop reading 
} 
code[bytesread] = val;         // add the digit           
bytesread++;                   // ready to read next digit  
} 
} 
if(bytesread == 10) {
if (client.connect("172.16.0.11",80)){
Serial.print(code);  // remplacer par l'adresse IP de votre serveur-ordi 
client.print("GET /tsti2d1/ganderatz/projet/add.php?");
client.print("id="); 
client.print(code);
client.println( " HTTP/1.1"); 
client.println( "Host: 172.16.0.11" );
client.println( "Content-Type: application/x-www-form-urlencoded" );
client.println( "Connection: close" );
client.println(); 
client.println();
client.stop(); 
digitalWrite(2, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
delay(1500);                       // wait for a bit 
digitalWrite(2, LOW);
delay(3000);
bytesread = 0; 
}
}
if (client.connected()) {
client.stop();  // DISCONNECT FROM THE SERVER
}
}
}
}

Mon programme bloque au niveau du "client.print(code)" car la variable code ne s'envoit pas a la base de donnée cependant si j'écris "client.print(val)" qui est une autre variable du programme les données sont bien envoyé. Je ne comprends pas car la variable code s'affiche bien dans le port série quand j'écris Serial.print(code) .

Tu n'es pas AU BON ENDROIT

Suite à venir :
ou tu fais ce qu'il faut en lisant les règles du forum francophone par exemple et tu aura peut-être des réponses.
ou tu ne fais rien et le message ira dans le" bakapoubelle".

Bonjour je suis élève de Sti2d en SIN et j'ai un problème lors de l'envois de mes données dans ma base de donnée voici mon code arduino:

#include <SPI.h>
#include <SoftwareSerial.h>
#include <Ethernet2.h>   //Inclure la bibliothèque Ethernet

byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x4F, 0x63 };  //90-A2-DA-0F-15-15
EthernetClient client;
EthernetServer server(80);  //Initialise le serveur Ethernet, port 80 par défaut pour HTTP
int  val = 0; 
char code[10]; 
int bytesread = 0;

void setup()
{
Serial1.begin(2400); //Affichage écran du PC
Serial.begin(9600);
Ethernet.begin(mac);   //Démarer la connexion Ethernet et le serveur analogRead(CAPTEUR_TEMP_PIN);
pinMode(2,OUTPUT);   // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin 
digitalWrite(2, LOW);                  // Activate the RFID reader
}  

void loop()
{
if(Serial1.available() > 0) {          // if data available from reader 
if((val = Serial1.read()) == 10) {   // check for header 
bytesread = 0; 
while(bytesread<10) {              // read 10 digit code 
if( Serial1.available() > 0) { 
val = Serial1.read(); 
if((val == 10)||(val == 13)) {
// if header or stop bytes before the 10 digit reading 
break;                       // stop reading 
} 
code[bytesread] = val;         // add the digit           
bytesread++;                   // ready to read next digit  
} 
} 
if(bytesread == 10) {
if (client.connect("172.16.0.11",80)){
Serial.print(code);  // remplacer par l'adresse IP de votre serveur-ordi 
client.print("GET /tsti2d1/ganderatz/projet/add.php?");
client.print("id="); 
client.print(code);
client.println( " HTTP/1.1"); 
client.println( "Host: 172.16.0.11" );
client.println( "Content-Type: application/x-www-form-urlencoded" );
client.println( "Connection: close" );
client.println(); 
client.println();
client.stop(); 
digitalWrite(2, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
delay(1500);                       // wait for a bit 
digitalWrite(2, LOW);
delay(3000);
bytesread = 0; 
}
}
if (client.connected()) {
client.stop();  // DISCONNECT FROM THE SERVER
}
}
}
}

Mon programme bloque au niveau du "client.print(code)" car la variable code ne s'envoit pas a la base de donnée cependant si j'écris "client.print(val)" qui est une autre variable du programme les données sont bien envoyé. Je ne comprends pas car la variable code s'affiche bien dans le port série quand j'écris Serial.print(code) .

la barre d'espace et tab sont cassées sur votre ordinateur? pourquoi tasser le code comme cela?... c'est illisible .. appuyez sur ctrl-T ou cmd-T pour indenter...

#include <SPI.h>
#include <SoftwareSerial.h>
#include <Ethernet2.h>   //Inclure la bibliothèque Ethernet

byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x4F, 0x63 };  //90-A2-DA-0F-15-15
EthernetClient client;
EthernetServer server(80);  //Initialise le serveur Ethernet, port 80 par défaut pour HTTP
int  val = 0;
char code[10];
int bytesread = 0;

void setup()
{
  Serial1.begin(2400); //Affichage écran du PC
  Serial.begin(9600);
  Ethernet.begin(mac);   //Démarer la connexion Ethernet et le serveur analogRead(CAPTEUR_TEMP_PIN);
  pinMode(2, OUTPUT);  // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(2, LOW);                  // Activate the RFID reader
}

void loop()
{
  if (Serial1.available() > 0) {         // if data available from reader
    if ((val = Serial1.read()) == 10) {  // check for header
      bytesread = 0;
      while (bytesread < 10) {           // read 10 digit code
        if ( Serial1.available() > 0) {
          val = Serial1.read();
          if ((val == 10) || (val == 13)) {
            // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit
          bytesread++;                   // ready to read next digit
        }
      }
      if (bytesread == 10) {
        if (client.connect("172.16.0.11", 80)) {
          Serial.print(code);  // remplacer par l'adresse IP de votre serveur-ordi
          client.print("GET /tsti2d1/ganderatz/projet/add.php?");
          client.print("id=");
          client.print(code);
          client.println( " HTTP/1.1");
          client.println( "Host: 172.16.0.11" );
          client.println( "Content-Type: application/x-www-form-urlencoded" );
          client.println( "Connection: close" );
          client.println();
          client.println();
          client.stop();
          digitalWrite(2, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
          delay(1500);                       // wait for a bit
          digitalWrite(2, LOW);
          delay(3000);
          bytesread = 0;
        }
      }
      if (client.connected()) {
        client.stop();  // DISCONNECT FROM THE SERVER
      }
    }
  }
}

Votre variable code est une cString mais vous ne mettez pas de caractère nul en fin de chaîne... donc ça va balancer plein de trucs jusqu'au prochain '\0' quand vous faites le print

Sinon n'utilsez pas des chiffres magiques... ça c'est mocheif ((val == 10) || (val == 13)) {autant écrireif ((val == '\n') || (val == '\r')) {et on comprend tout de suite mieux ce que vous testez

Vous pouvez "gagner" un print dans ce code

          client.print("GET /tsti2d1/ganderatz/projet/add.php?");
          client.print("id=");
          client.print(code);

en mettant "id=" dans le premier print...

          client.print("GET /tsti2d1/ganderatz/projet/add.php? id=");
          client.print(code);

Merci pour vos conseils, j'ai donc changé les partis concerné voici le résultat mais toujours le même problème pour l'envois à la base de donnée...

#include <SPI.h>
#include <SoftwareSerial.h>
#include <Ethernet2.h>   //Inclure la bibliothèque Ethernet

byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x4F, 0x63 };  //90-A2-DA-0F-15-15
EthernetClient client;
EthernetServer server(80);  //Initialise le serveur Ethernet, port 80 par défaut pour HTTP
int  val = 0;
char code[10];
int bytesread = 0;

void setup()
{
  Serial1.begin(2400); //Affichage écran du PC
  Serial.begin(9600);
  Ethernet.begin(mac);   //Démarer la connexion Ethernet et le serveur analogRead(CAPTEUR_TEMP_PIN);
  pinMode(2, OUTPUT);  // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(2, LOW);                  // Activate the RFID reader
}

void loop()
{
  if (Serial1.available() > 0) {         // if data available from reader
    if ((val = Serial1.read()) == 10) {  // check for header
      bytesread = 0;
      while (bytesread < 10) {           // read 10 digit code
        if ( Serial1.available() > 0) {
          val = Serial1.read();
          if ((val == '\n') || (val == '\r')) {
            // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit
          bytesread++;                   // ready to read next digit
        }
      }
      if (bytesread == 10) {
        if (client.connect("172.16.0.11", 80)) { // remplacer par l'adresse IP de votre serveur-ordi
          client.print("GET /tsti2d1/ganderatz/projet/add.php?id=");
          client.print(code);
          client.println( " HTTP/1.1");
          client.println( "Host: 172.16.0.11" );
          client.println( "Content-Type: application/x-www-form-urlencoded" );
          client.println( "Connection: close" );
          client.println();
          client.println();
          client.stop();
          digitalWrite(2, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
          delay(1500);                       // wait for a bit
          digitalWrite(2, LOW);
          delay(3000);
          bytesread = 0;
        }
      }
      if (client.connected()) {
        client.stop();  // DISCONNECT FROM THE SERVER
      }
    }
  }
}

je ne vois pas la modif correspondant à ce commentaire

Votre variable code est une cString mais vous ne mettez pas de caractère nul en fin de chaîne... donc ça va balancer plein de trucs jusqu'au prochain '\0' quand vous faites le print

si le code n'est pas de l'ASCII sur 10 caractères, alors vous ne pouvez pas balancer du binaire dans le header HTTP, faut l'encoder par exemple en base64

Mais comment je suis censer faire pour encoder en base64 je ne sais pas du tout comment faire :confused:

quand vous écrivez

 char  message[] = "bonjour";

vous déclarez ce qu'on appelle un cString. un tableau de caractère qui a pour vocation d'afficher une chaîne. en C ou C++ les cStrings (pour savoir où elles finissent) ont un caractère caché à la fin qui est nul (le caractère '\0').

Si vous bâtissez votre cString à la main, il vous faut 11 caractères dans le tableau et forcer le dernier caractère à être ce fameux '\0'

EDIT: rajout d'un petit indice pour la chasse au bug ci dessous dans le post 11....

     bytesread = 0;
      while (bytesread < 10) {           // read 10 digit code
        if ( Serial1.available() > 0) {
          val = Serial1.read();
          if ((val == 10) || (val == 13)) {
            // if header or stop bytes before the 10 digit reading
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit
          bytesread++;                   // ready to read next digit
        }
      }
      code[bytesread] = 0;          //  <- ICI

Et terminateur '\0' pour la variable code à la fin de la boucle de lecture, on s'en dispense ?

Remarque : tu as déjà entendu parler d'indentation ?

Dupliquer les questions :

Et en plus, 2 posts pour la même question .

Merci beaucoup.... 2 Semaine que je galère seulement pour cette petite ligne :cry:

Voici donc le code qui fonctionne pour les intérréssés

#include <SPI.h>
#include <SoftwareSerial.h>
#include <Ethernet2.h>   //Inclure la bibliothèque Ethernet

byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0x4F, 0x63 };  //90-A2-DA-0F-15-15
EthernetClient client;
EthernetServer server(80);  //Initialise le serveur Ethernet, port 80 par défaut pour HTTP
int  val = 0;
char code[10];
int bytesread = 0;

void setup()
{
  Serial1.begin(2400); //Affichage écran du PC
  Serial.begin(9600);
  Ethernet.begin(mac);   //Démarer la connexion Ethernet et le serveur analogRead(CAPTEUR_TEMP_PIN);
  pinMode(2, OUTPUT);  // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
  digitalWrite(2, LOW);                  // Activate the RFID reader
}

void loop()
{
  if (Serial1.available() > 0) {         // if data available from reader
    if ((val = Serial1.read()) == 10) {  // check for header
      bytesread = 0;
      while (bytesread < 10) {           // read 10 digit code
        if ( Serial1.available() > 0) {
          val = Serial1.read();
          if ((val == 10) || (val == 13)) {
            break;                       // stop reading
          }
          code[bytesread] = val;         // add the digit
          bytesread++;                   // ready to read next digit
        }
      }
      code[bytesread] = 0;
      if (bytesread == 10) {
        if (client.connect("172.16.0.11", 80)) {
          Serial.print(code);
          client.print("GET /tsti2d1/ganderatz/projet/add.php?id=");
          client.print(code);
          client.println( " HTTP/1.1");
          client.println( "Host: 172.16.0.11" );
          client.println( "Content-Type: application/x-www-form-urlencoded" );
          client.println( "Connection: close" );
          client.println();
          client.println();
          client.stop();
          digitalWrite(2, HIGH);                  // deactivate the RFID reader for a moment so it will not flood
          delay(1500);                       // wait for a bit
          digitalWrite(2, LOW);
          delay(3000);
          bytesread = 0;
        }
      }
      if (client.connected()) {
        client.stop();  // DISCONNECT FROM THE SERVER
      }
    }
  }
}

Il y a encore un bug et je vous ai expliqué ci-dessus ce à quoi il fallait faire attention ...

      code[bytesread] = 0;
      if (bytesread == 10) {