Value of char * is shown even where it was not called using Arduino

1

This is exactly what I'm using:

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

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

  // Tentativa frustada, possui o mesmo problema:
  //uint32_t i;
  //uint8_t* TotalRequisicao;
  ///////////////////////////////////////////////

int i;
char* TotalRequisicao;

IPAddress ip(192, 166, 0, 23);
EthernetServer server(80);

void setup(){

  Serial.begin(9600);

  Ethernet.begin(mac, ip);
  server.begin();

  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());

}

void loop(){

  EthernetClient client = server.available();

  if(client){

      while(client.connected()){

              Serial.println("Novo cliente!");

              i = 0;
              TotalRequisicao = "";

              while(char LerRequisicao = client.read()){

                  if(LerRequisicao == '\n'){

                      Serial.println();
                      Serial.println(TotalRequisicao);
                      break; 

                  }else{

                      TotalRequisicao[i] = LerRequisicao;
                      i++;

                  }

              }

              client.println();     
              client.println("HTTP/1.1 200 OK");
              client.println("Content-Type: text/html");
              client.println("Connection: close");
              client.println();
              client.println("<!DOCTYPE HTML>");
              client.println("<html>");
              client.println("<head>");
              client.println("</head>");
              client.println("</html>");


              break;


    }

  delay(1);
  client.stop();

  }

}

Results:

This is what is happening by running some requests for the IP.

In Arduino's Serial Monitor:

IP: 192.166.0.23
Novo cliente!

GET / HTTP/1.1
nte!GET / HTTP/1.1
nte!
GET / HTTP/1.1
nte!HTTP/1.1
nte!
GET / HTTP/1.1
nte!

In response to the request:

GET / HTTP/1.1nte!HTTP/1.1 200 OK
GET / HTTP/1.1nte!Content-Type: text/html
GET / HTTP/1.1nte!Connection: close
GET / HTTP/1.1nte!
GET / HTTP/1.1nte!<!DOCTYPE HTML>
GET / HTTP/1.1nte!<html>
GET / HTTP/1.1nte!<head>
GET / HTTP/1.1nte!</head>
GET / HTTP/1.1nte!</html>
GET / HTTP/1.1nte!

Expected responses:

This was what I would have wanted, understand the purpose of the above code:

In Arduino's Serial Monitor:

GET / HTTP/1.1

In response to the request:

HTTP/1.1 200 OK
Content-Type: text/html
Connection: close

<!DOCTYPE HTML>
<html>
<head>
</head>
</html>

I'm using the Ethernet Shield V1 and also test with W5100 module in an Arduino UNO, both have gone bad. However, the problem is being caused by char* TotalRequisicao , since by making it a comment //TotalRequisicao[i] = LerRequisicao; the problem is not displayed, but the goal is not reached either. :

My idea with TotalRequisicao[i] = LerRequisicao; would be to fully get GET / HTTP/1.1 in a single string (a char* ), thus making it easier to identify which page / file was called.

But for some reason the information of TotalRequisição has some "collision" and spread to other places where the variable was never even called.

PS: Even removing Serial.println(TotalRequisicao); , but keeping TotalRequisicao[i] = LerRequisicao; the problem persists.

What would be the reason for this and how can it be fixed?

    
asked by anonymous 16.02.2017 / 04:49

1 answer

1

The reason and the explanation of why this occurred I do not know, honestly, what is really a problem , after all this can happen again and I will not know why.

However, the solution was to use readStringUntil('\n'); and apparently this fixed the problem.

In this way it was enough to use:

TotalRequisicao = client.readStringUntil('\n');

Also change to String :

String TotalRequisicao;

This now returned GET / HTTP/1.1 in the "console" and returned only the HTML (and Header) to the client. ;)

    
16.02.2017 / 05:43