Getting strange characters when reading HTTP put from client

Hi there,

I’ve no idea what is happening here. I’m receiving http from an android phone which reads fine into a string, but comes out as gibberish when read into an array. I’ve ran out of SDRAM hence my desire to change the strings to compile time defined arrays. Any help as to why this is happening and how to solve it would be greatly appreciated.

Output on serial monitor from character array:

void TCP_Connection() {
  unsigned long currentMillis = millis();
  unsigned long interval = 1000;
  unsigned long counter = 0;
  char HTTPArray1[300];
    client0 = server0.available(); // Create a client connection
    if (client0) {
        while (client0.connected()) {
            if (client0.available()) {
                char c = client0.read();
                delay(1);
                HTTPArray1 [counter] = c;
                delay(1);
                counter++;
                    if (c == '!') {
                        server0.write("acknowledged");//send acknowleged to the sender
                        client0.stop();
                        Serial.println("Client Stop");
                    } else //store characters to string
                    {
		    HTTPArray1 [counter] = c;
                    counter++;
                    }
                }
            }
        }
       if (currentMillis - previousMillis >= interval) {
       previousMillis = currentMillis;
       for (counter = 0; counter < 249; counter++)
       {
       Serial.print(HTTPArray1[counter]);
       }
       } 
    }

Output on serial monitor from character array:

The Serial Monitor application displays TEXT that can be cut and pasted. Text can then be scaled up when shown in a browser, unlike your picture. DO NOT POST PICTURES OF TEXT AGAIN.

HTTPArray is (apparently) a char array. It is NOT a string. A string is a NULL terminated array of chars. Your array is NOT null terminated.

The NULL is a stop sign to all functions that expect a string. Since you didn't put the stop sign in the array, the functions that expect a string happily keep printing beyond the end of the array.

Do not pass non-strings to functions that expect strings, and then expect the functions to do what they are intended to do with strings.

Phalanxguard:
Hi there,

I’ve no idea what is happening here. I’m receiving http from an android phone which reads fine into a string, but comes out as gibberish when read into an array. I’ve ran out of SDRAM hence my desire to change the strings to compile time defined arrays. Any help as to why this is happening and how to solve it would be greatly appreciated.

change this:

       if (currentMillis - previousMillis >= interval) {

to:

       HTTPArray1 [counter] = 0;
       if (currentMillis - previousMillis >= interval) {

and this:

       for (counter = 0; counter < 249; counter++)
       {
       Serial.print(HTTPArray1[counter]);
       }

to:

       for (counter1 = 0; counter1 < counter; counter1++)
       {
           Serial.print(HTTPArray1[counter1]);
       }

Hi there, thanks for trying to help! I tried your suggestions. Still having the same issue with the gibberish text. Is there any reason that characters wouldn’t be picked up correctly? It still seems bizarre that a string wouldn’t manipulate them correctly but a character array wouldn’t…

Output:

Note that I had to change one aspect of the code you suggested which was the “Counter1<counter” in the for loop as when that was used there was no serial output at all! Sorry if the code is a little untidy, it’s a work in progress.

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

EthernetServer server0(6969); //server port
EthernetClient client0;
String readString;
//char server1[] = "checkip.dyndns.org"; //server to ping to get external ip address
//char server2[] = "dynupdate.no-ip.com"; //ddns server to update 
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
EthernetClient client1, client2;

void setup() {
    byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED}; //physical mac address
    byte ip[] = {192,168,1,177}; // ip in lan if DHCP fails
    byte DNS[] = {192,168,1,254}; //DNS server address (Required for UNO)
    byte gateway[] = {192,168,1,254}; // internet access via router
    byte subnet[] = {255,255,255,0}; //subnet mask
    int net = 0; //status of network
    int cnt = 0; //counter

    delay(3000);
    Serial.begin(9600); //enable serial data print  

    /*while (!Serial) 
    {
    	delay(100); // wait for serial port to connect. Needed for Leonardo only
    }*/

    while ((net != 1) && (cnt < 3)) //Attempt to get an IP from DHCP three times
    {
        Serial.println("Trying to connect...");
        delay(100);
        net = Ethernet.begin(mac);
        cnt++;
    }

    if (net == 0) //If failed three times, pick an IP
    {
        Serial.println("Tried DHCP three times and failed");
        Ethernet.begin(mac, ip, DNS, gateway, subnet);
        Serial.print("IP was FORCED and is ");
        Serial.println(Ethernet.localIP());
    } else 
    {
        Serial.print("IP number assigned by DHCP is ");
        Serial.println(Ethernet.localIP());
    }

    server0.begin();
    //getip(); //ping a server to get external ip
}

void loop() {
    //updateIP();
    TCP_Connection();
}

void TCP_Connection() {
  unsigned long currentMillis = millis();
  unsigned long interval = 1000;
  unsigned long counter = 0;
  char HTTPArray1[300];
    client0 = server0.available(); // Create a client connection
    if (client0) {
        while (client0.connected()) {
            if (client0.available()) {
                char c = client0.read();
                HTTPArray1 [counter] = c;
                counter++;
                    if (c == '!') {
                        server0.write("acknowledged");//send acknowleged to the sender
                        client0.stop();
                        Serial.println("Client Stop");
                    } else //store characters to string
                    {
		    HTTPArray1 [counter] = c;
                    counter++;
                    }
                }
            }
        }
       HTTPArray1 [counter] = 0;
       if (currentMillis - previousMillis >= interval) {
       previousMillis = currentMillis;
       for (int forCounter = 0; forCounter < 249; forCounter++)
       {
           Serial.print(HTTPArray1[forCounter]);
       }
       } 
    }

Why do you insist on posting pictures of text?

Your issue is that even though your char array has 300 elements, you aren’t filling all 300 and you are attempting to print out 249 (not sure why you used 249 instead of 299) of those chars. Some of which haven’t been assigned any value so they just contain garbage. The solution is to for-loop up to the last index that you used in the char array.

Change this:

 if (currentMillis - previousMillis >= interval) {
       previousMillis = currentMillis;
       for (int forCounter = 0; forCounter < 249; forCounter++)
       {
           Serial.print(HTTPArray1[forCounter]);
       }
}

To this:

 if (currentMillis - previousMillis >= interval) {
       previousMillis = currentMillis;
       for (int forCounter = 0; forCounter < counter; forCounter++)
       {
           Serial.print(HTTPArray1[forCounter]);
       }
}

Phalanxguard:
Note that I had to change one aspect of the code you suggested which was the “Counter1<counter” in the for loop as when that was used there was no serial output at all! Sorry if the code is a little untidy, it’s a work in progress.

Which means that counter==0, so you have received no characters.

Change it back to counter1 < counter (or in your case, forCounter < counter), then work out why counter is 0 (that is, why you have received no characters).

and yes, post the text inside code tags, not an image of the text, so we can read it.

Thanks guys, it took a bit of work but your loop solution worked. It also didn't help that my baud rate was set incorrectly :roll_eyes: I've got the system working very efficiently now.

However, my next problem now is resolving an IP from a DDNS service (no-ip). I have an arduino as a server in my house and an arduino as a client sending a command. I have the arduino acting as a server updating no-ip with my IP address correctly, but I'm not sure how to query the DDNS with the hostname of the server with the client and get it to return an IP address. I imagine it's done using either a HTTP GET request or a UDP packet containing the hostname, but I can't find any info on how to do it. It would be very helpful if anyone has done this before or seen it done before and could shed some light. I've tried looking everywhere to no avail, and no-ip only supply info on how to update the host not how to query it. I assume this is because a web browser normally does this. I'm starting to get out of my depth here.