Char* value is shown even where it was not called using Arduino

Asked

Viewed 49 times

1

This is exactly what I’m wearing:

#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 occurring running some requests to the IP.

On 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 answers:

That was what for me would be the desired, understand as the purpose of the above code:

On 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 the Module W5100 in an Arduino UNO, both went bad. However the problem is being caused by char* TotalRequisicao, once making it a comment //TotalRequisicao[i] = LerRequisicao; the problem is not presented, but the goal is also not achieved. :<

My idea with the TotalRequisicao[i] = LerRequisicao; would fully obtain the 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 TotalRequisição has some "collision" and spread to other places where the variable has never even been called.

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

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

1 answer

1

The reason and the explanation of why it occurred I do not know yet, honestly, what in fact is a problem, after all this may happen again and I won’t know why.


Meanwhile 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 switch to String:

String TotalRequisicao;

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.