Why in the Serial port do I see only one letter in a GET request?

Hello,

I am new to this forum and am trying to learn the language and use of Arduino. I am trying to understand Serial communication with a localhost server.

I have an Arduino Uno R3 connected to an Ethernet Shield W5100, which in turn is connected to a simple router functioning as a switch.

The intent of the code is that by typing the ID (column of variables on the MySql server) into the Serial port the user can check the RFID code parameter (another column in the server table).

The first part I got. I can get connection to the server, but at the moment of the HTTP request through the GET command, only a single letter, in the case of the letter “H”, is displayed on the Serial port, which is the beginning of the complete return.

When I test using the WebClient Ethernet library example, Serial returns the entire page to me.

I’ve tried everything, I searched the net and found no solution, can anyone help me?

Many thanks (sorry for bad english)

ARDUINO CODE

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress server(192,168,100,106);
 
EthernetClient client;
  
String codigo;        
String nombre;        
boolean fin = false;  
boolean pregunta = true;

char inData[8]; 
int identificador; 
byte index = 0; 
 
 
void setup() {
  Serial.begin(9600);
  delay(1000);

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

  delay(500);
   
  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
 
}
 
void loop() {
    if (pregunta == true) Serial.print("Qual o ID? ");
    pregunta = false;
    if (Serial.available()>0){

       if(index < 7)
      {
          identificador = Serial.read();
          inData[index] = identificador;
          index++;
          inData[index] = '\0';
      }

        Serial.println(identificador-48);
        requisicao();
        pregunta = true;
  }
       
 
}
 
int requisicao() {
 
  if (client.connect(server, 80)) {
    Serial.println("Conectado");
    client.print("GET /arduinodb/index.php?id=");
    client.print(identificador);
    client.println(" HTTP/1.0");
    client.println("Host: 192.168.100.106");
    client.println("Connection: close");
    client.println();

  }
  else {

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

    fin = true;
   
 
  }
  
  while (fin)  {
   Serial.println(codigo);
    int longitud = codigo.length();
    int posicion = codigo.indexOf("");
    nombre = "";
 
    for (int i = posicion + 0; i < longitud; i ++){
      if (codigo[i] == ';') i = longitud;
      else nombre += codigo[i];
    }

    fin = false;
    Serial.println("Código RFID: " + nombre);
    Serial.println("Desconectando");
    client.stop();
  }
  codigo="";
  return 1;
}

SERIAL PORT

IP: 192.168.0.10
Qual o ID? 2
Conectado
H
Código RFID: H
Desconectando
Qual o ID?

THE SAME TEST ON WEBCLIENT EXAMPLE

connecting...
connected
HTTP/1.1 200 OK
Date: Sat, 10 Mar 2018 23:20:59 GMT
Server: Apache/2.4.29 (Win32) OpenSSL/1.1.0g PHP/7.2.2
X-Powered-By: PHP/7.2.2
Content-Length: 10
Connection: close
Content-Type: text/html; charset=UTF-8

0q1w2e3r 

disconnecting.

in this case, “0q1w2e3r” is the RFID code on server data.

It seems like you are telling it to print whenever a single character arrives. I suspect your variable fin should only be set to true when the entire message has been received.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. Receiving data with client.read() will work the same way.

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

Thanks I Will check This.

Edit:

"...I suspect your variable fin should only be set to true when the entire message has been received."

How can I do This?

oswaldocostaneto:
How can I do This?

See the link I gave you.

...R

Thanks for the tip with the variable "fin". At the end of the day, the problem was very close to that.

CHANGED "IF" FOR "WHILE"

  while (client.available()) {
    char c = client.read();
    codigo += c;

    fin = true;

CHANGED "IF" FOR "WHILE"

if (fin)  {

and it's ok now!

thanks!

I'm not sure exactly what you did as you have not posted a complete program. Usually I advise people to replace WHILE with IF.

Your while (client.available()) { will only work if the complete message has already arrived before you call that line.

...R