Error without logic

Hello! I have code and stupid error. When i change ANYTHING in this code, last Serial.println(url); in httpRequest function is GET /my�ot/listen/temperature/log/?deviceToken=4b1cbe83c4e05d55a7be5c72bc922ab9&temperature=22.81 HTTP/1.1 and contains this stupid character �. I can change if (sensors.getTempCByIndex(0) != lastTemperature && true) to if (sensors.getTempCByIndex(0) != lastTemperature) and character is here. I can add note, error is here. I can add new line, that stupid characted is here! WHERE IS ERROR?

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

#define sourcePin 9
char subUrl[] = "/myiot";
char deviceKey[] = "4b1cbe83c4e05d55a7be5c72bc922ab9";
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
char url[] = "";
char temperatureBuffer[] = "TEMPERATURE";
float lastTemperature;
// initialize the library instance:
EthernetClient client;
OneWire oneWire(sourcePin);
DallasTemperature sensors(&oneWire);
IPAddress server(192,168,0,204);

unsigned long lastConnectionTime = 0;             // last time you connected to the server, in milliseconds

void setup() {
  // start serial port:
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  delay(1000);
  // start the Ethernet connection using a fixed IP address and DNS server:
  Ethernet.begin(mac);
  sensors.begin();
  // print the Ethernet board/shield's IP address:
  Serial.print("My IP address: ");
  Serial.println(Ethernet.localIP());
}

void loop() {
  // if there's incoming data from the net connection.
  // send it out the serial port.  This is for debugging
  // purposes only:
  sensors.requestTemperatures();
  Serial.println(sensors.getTempCByIndex(0));
  // if ten seconds have passed since your last connection,
  // then connect again and send data:
  if (sensors.getTempCByIndex(0) != lastTemperature && true) {
    httpRequest();
  }

}

// this method makes a HTTP connection to the server:
void httpRequest() {
  // close any connection before send a new request.
  // This will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection:
  Serial.println("Connecting...");
  Serial.println(subUrl);
  if (client.connect(server, 80)) {
    for(int i = 0; i < sizeof(url); i++) {
      url[i] = (char)0;
    }
    dtostrf(sensors.getTempCByIndex(0), 2, 2, temperatureBuffer);
    strcat(url, "GET ");
    strcat(url, subUrl);
    strcat(url, "/listen/temperature/log/?deviceToken=");
    strcat(url, deviceKey);
    strcat(url, "&temperature=");
    strcat(url, temperatureBuffer);
    strcat(url, " HTTP/1.1");
    client.println(url);
    client.println("Host: 192.168.0.204");
    client.println("User-Agent: arduino-ethernet");
    client.println("Connection: close");
    client.println();
    Serial.println(url);
    // note the time that the connection was made:
    lastTemperature = sensors.getTempCByIndex(0);
    lastConnectionTime = millis();
    Serial.println("Connection success");
  } else {
    // if you couldn't make a connection:
    Serial.println("connection failed");
  }
}

I do not get the same error as you say. This sort of thing normally happens when you copy code from a web site and you get unprintable characters.

Try copying from your own post and see.

By the way this ; is an error:-

while (!Serial) {
    ;
  }
    strcat(url, "GET ");
    strcat(url, subUrl);

These two statements and those that follow add characters on the end of the string pointed to by 'url'.
But:

char url[] = "";

all you have done is declare that url is a pointer to a character string and is initialized to point to a string which contains only a NULL character - i.e. it is only one character long. You can't add more characters to the end.
You need to declare 'url' to be a character array and specify that it is long enough to hold all the characters that you're going to append. For example:

char url[128];

The 128 is a guess, you'll have to figure out how long the string can really be.

Your 'for' loop which initializes 'url' to all NULLs only needs to set the first character to NULL.

url[0] = (char)0;

Pete

Yes, you've stomped all over memory and one of the variables you've stomped over has
subsequently been updated, stomping back over your rogue string. Declare a character array using
a #define'd length and check against that value every time you write to the array to avoid
writing past the end.

C and C++ don't check array bounds so you have to do it yourself.

Grumpy_Mike:
By the way this ; is an error:-

while (!Serial) {

;
  }

No, it's an idiom for waiting until Serial is connected. A little more clever-pants than it needs to be IMO, but that's C++ for you. you'll see that construct often.

But that semicolon is totally redundant, remove it and it makes no difference.

No, it's an idiom for waiting until Serial is connected. A little more clever-pants than it needs to be IMO, but that's C++ for you. you'll see that construct often.

That is pure C.

Grumpy_Mike:
But that semicolon is totally redundant, remove it and it makes no difference.

True. But, having it there shows that the programmer was aware that nothing needed to happen in the while loop. Without it, it looks like the curly braces were accidentally left empty.

Of course, a comment accomplishes the same thing.

Though I'm pretty sure that the compiler optimizes away the useless statement.

el_supremo:

char url[128];

The 128 is a guess, you'll have to figure out how long the string can really be.

Your 'for' loop which initializes 'url' to all NULLs only needs to set the first character to NULL.

url[0] = (char)0;

Pete

Thank you Pete, it works! ^^