Client.print() GET command with error

Hello,

I am completing an RFID code RDM6300 and found an error that I can not solve.

The project works with an arduino in communication with a mysql database for registration of input and output of users.

the final part of the code is the registration on the page SALVARDADOS.PHP, however for some reason the data is not being saved.

I did some tests, and I verified that the problem is in the variable usuario_servidor, but I could not identify what the problem was. If I replace this variable with any String, the command succeeds.

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress server(192,168,0,10);
 
EthernetClient client;
EthernetClient client2;

//Inicializa a serial nos pinos 2 (RX) e 3 (TX)
SoftwareSerial RFID(2, 3);

//Caracteres para leitura no RDM6300
int Led_amarelo = 4;
int Led_verde = 5;
int Led_vermelho = 6;
int Rele_porta = 7;
uint8_t Payload[6]; // used for read comparisons
String rfid_in = "";
String nome = "";

RDM6300 RDM6300(Payload);
  
String codigo;        
String usuario_servidor;        
boolean fim = false;  
boolean pregunta = true;

String readString;
String identificador;  
 
 
void setup() {

  pinMode(Led_amarelo, OUTPUT);
  pinMode(Led_verde, OUTPUT);
  pinMode(Led_vermelho, OUTPUT);
  pinMode(Rele_porta, OUTPUT);
  //Inicializa a serial para o leitor RDM6300
  RFID.begin(9600);
  
  //Inicializa a serial para comunicacao com o PC
  Serial.begin(9600);
  delay(1000);

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac);
  }

  delay(1000);
   
  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
 
}
 
void loop() {
    inicial();
}

void inicial (){
      if (pregunta == true) {
      Serial.print("Encoste o cartão RFID... ");
      }
      
    pregunta = false;

    while (RFID.available() > 0){

        digitalWrite(Led_amarelo, HIGH);
        uint8_t c = RFID.read();
        if (RDM6300.decode(c)){chamada();}
                       
        }
        if (RFID.available() == 0){
          digitalWrite(Led_amarelo, LOW);  
          memset(Payload, 0, sizeof(Payload));
        }
          
}

void chamada(){
  //MOSTRA A TAG CONVERTIDA PARA DECIMAL NA PORTA SERIAL
          unsigned long result = 
          ((unsigned long int)Payload[1]<<24) + 
          ((unsigned long int)Payload[2]<<16) + 
          ((unsigned long int)Payload[3]<<8) + 
          Payload[4];              
        Serial.println();
        String resultado_serial = String(result);
        if (resultado_serial.length() == 7){Serial.println(resultado_serial);}
       
        identificador = result;
        requisicao();
        pregunta = true;
                
}


int requisicao() {
 
  if (client.connect(server, 8090)) {
    Serial.println("Conectando...");
    client.print("GET /arduinodb/rfid.php?rfid=");
    client.print(identificador);
    client.println(" HTTP/1.0");
    client.println("Host: 192.168.0.10");
    client.println("Connection: close");
    client.println();

  }
  else {

    Serial.println("Conexão falhou");
    Serial.println("Desconectando");
   
  }
 
  delay(1000);
  
  while (client.available()) {
    char c = client.read();
    codigo += c;

    fim = true;
   
 
  }
  
  if (fim)  {
   //Serial.println(codigo);
    int comprimento = codigo.length();
    int posicion = codigo.indexOf("*");
    usuario_servidor = "";
 
    for (int i = posicion + 1; i < comprimento; i ++){
      if (codigo[i] == '^') i = comprimento;
      else usuario_servidor += codigo[i];
    }

    fim = false;

    // verifica se o usuário está permitido
    if(usuario_servidor.length() == 0){
      if (identificador.length() > 6){
      Serial.println("Acesso negado: Usuário não encontrado.");
      digitalWrite(Led_vermelho, HIGH);
      delay(4000);
      digitalWrite(Led_vermelho, LOW);
      }
      
      
    } else {
      Serial.println("Acesso liberado: " + usuario_servidor);
            
        if (client2.connect(server, 8090)) {
        client.print("GET /arduinodb/salvardados.php?");
        client.print("rfid=");
        client.print(identificador);
        client.print("&nome=");
        client.print(usuario_servidor);
        client.println(" HTTP/1.0");
        client.println("Host: 192.168.0.10");
        client.println("Connection: close");
        client.println();

        client2.stop();
        } else {
          Serial.println("Falha na conexão");
        }
    
      digitalWrite(Led_verde, HIGH);
      digitalWrite(Rele_porta, HIGH);
      delay(4000);
      digitalWrite(Led_verde, LOW);
      digitalWrite(Rele_porta, LOW);

      Serial.println("Desconectando");
      
    }
    client.stop(); 
  }
  codigo="";
  return 1;
}

It looks like you are assigning an unsigned long to a String identificador inside chamada():

Look at the difference between the two lines I marked:

void chamada(){
  //MOSTRA A TAG CONVERTIDA PARA DECIMAL NA PORTA SERIAL
          unsigned long result = 
          ((unsigned long int)Payload[1]<<24) + 
          ((unsigned long int)Payload[2]<<16) + 
          ((unsigned long int)Payload[3]<<8) + 
          Payload[4];              
        Serial.println();
        String resultado_serial = String(result);
//     ^^^^^^^^^^^^^^^^^^^^^^^^^^
// converting unsigned long to a String and assigning it to a string variable

        if (resultado_serial.length() == 7){Serial.println(resultado_serial);}
       
        identificador = result;
//     ^^^^^^^^^^^^^^^^^^^^^^^^^^
// NOT converting unsigned long to a String and assigning it to a string variable
        requisicao();
        pregunta = true;
                
}


int requisicao() {
 
  if (client.connect(server, 8090)) {
    Serial.println("Conectando...");
    client.print("GET /arduinodb/rfid.php?rfid=");
    client.print(identificador);
    client.println(" HTTP/1.0");
    client.println("Host: 192.168.0.10");
    client.println("Connection: close");
    client.println();

  }
  else {

    Serial.println("Conexão falhou");
    Serial.println("Desconectando");
   
  }
 
  delay(1000);
  
  while (client.available()) {
    char c = client.read();
    codigo += c;

    fim = true;
   
 
  }
  
  if (fim)  {
   //Serial.println(codigo);
    int comprimento = codigo.length();
    int posicion = codigo.indexOf("*");
    usuario_servidor = "";
 
    for (int i = posicion + 1; i < comprimento; i ++){
      if (codigo[i] == '^') i = comprimento;
      else usuario_servidor += codigo[i];
    }

    fim = false;

    // verifica se o usuário está permitido
    if(usuario_servidor.length() == 0){
      if (identificador.length() > 6){
      Serial.println("Acesso negado: Usuário não encontrado.");
      digitalWrite(Led_vermelho, HIGH);
      delay(4000);
      digitalWrite(Led_vermelho, LOW);
      }
      
      
    } else {
      Serial.println("Acesso liberado: " + usuario_servidor);
            
        if (client2.connect(server, 8090)) {
        client.print("GET /arduinodb/salvardados.php?");
        client.print("rfid=");
        client.print(identificador);
        client.print("&nome=");
        client.print(usuario_servidor);
        client.println(" HTTP/1.0");
        client.println("Host: 192.168.0.10");
        client.println("Connection: close");
        client.println();

        client2.stop();
        } else {
          Serial.println("Falha na conexão");
        }
    
      digitalWrite(Led_verde, HIGH);
      digitalWrite(Rele_porta, HIGH);
      delay(4000);
      digitalWrite(Led_verde, LOW);
      digitalWrite(Rele_porta, LOW);

      Serial.println("Desconectando");
      
    }
    client.stop(); 
  }
  codigo="";
  return 1;
}

[/quote]

This may be a code error, but that's not the point.

the error is mainly in something to do with the "usuario_servidor".

But I do not know exactly what.

If I remove the part of Client.connect () involving the GET for SALVARDADOS.PHP, the code flows perfectly.

        if (client2.connect(server, 8090)) {
        client.print("GET /arduinodb/salvardados.php?");
        client.print("rfid=");
        client.print(identificador);
        client.print("&nome=");
        client.print(usuario_servidor);
        client.println(" HTTP/1.0");
        client.println("Host: 192.168.0.10");
        client.println("Connection: close");
        client.println();

You are connecting with client2, but printing info to client

this is not the problem as I start the client2 through the EthernetClient.

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress server(192,168,0,10);
 
EthernetClient client;
EthernetClient client2;

//Inicializa a serial nos pinos 2 (RX) e 3 (TX)
SoftwareSerial RFID(2, 3);

I insist that the problem is in USUARIO_SERVIDOR, because if I replace this variable with a string, the command is executed and the data is read in the database. So, I believe the error is there, but what ???

OK,

Can someone help? I have discovered that the USUARIO_SERVIDOR variable is skipping a line. I did the test with Serial.print () and it appeared on the serial port ...

      Serial.print(".");
      Serial.print(usuario_servidor);
      Serial.print(".");

SERIAL

.Ferreira 
.

how to avoid this line break?

The response from the server comes back with a newline at the end so your variable is not “user”, but “user\n”.

You need to test for that and trim it off.

for (int i = posicion + 1; i < comprimento; i ++) {
      if (codigo[i] == '^' || codigo[i[ == '\n') i = comprimento;
      else usuario_servidor += codigo[i];
    }

or something similar

Perfectly. This is exactly what I was looking for. Now my CODE works.

Thanks.