Failing to copy simple array of capacitance values into HTTP string

Here's the draw loop of my program. The call to capacitiveSensor is using this library Arduino Playground - HomePage

void loop() {
  long capacitance =  cs_4_2.capacitiveSensor(30);
  capacitances[capacitancesIndex++] = capacitance;
  Serial.println(capacitance);
  if (capacitancesIndex < 20) {
    delay(200);
  } else {
    capacitancesIndex = 0;
    sendToServer();
  }  
}

The sendToServer method first makes sure that the Wi-Fi shield is still connected to the network (and reconnects if it is not) and then tries to send the array of integer values read over HTTP to my server. That's not working so I have put in lots of Serial.println to diagnose what is happening.

      String httpMssg = "GET /default.aspx?store=[{name:\"";
      Serial.println(httpMssg);
      httpMssg = httpMssg + plateName;
      Serial.println(httpMssg);
      httpMssg = httpMssg + "\",values=[";
      Serial.println(httpMssg);
      for(int i = 0; i < 19; i++) {
        httpMssg = httpMssg + capacitances[i] + ",";
        Serial.println(httpMssg);
      }
      httpMssg = httpMssg + capacitances[19] + "]}] HTTP/1.1";
      Serial.println(httpMssg);

In the first run my array of capacitance values is [0, 31, 33, 0, 77, 70, 39, 47, 57, 23, 17, 61, 0, 88, 84, 62, 63, 98, 4, 42] and so the results of all those println calls that I expect to see is:

GET /default.aspx?store=[{name:"
GET /default.aspx?store=[{name:"DefaultPlate
GET /default.aspx?store=[{name:"DefaultPlate",values=[
GET /default.aspx?store=[{name:"DefaultPlate",values=[0,
[...]
GET /default.aspx?store=[{name:"DefaultPlate",values=[0,31,33,0,77,70,39,47,57,23,17,61,0,88,84,62,63,98,4,42]}] HTTP/1.1

But what I actually see is

(N.B. I cannot it paste from the serial monitor, some of it is unpastable bytes.) It looks like I have multiple threads writing at once, is that the case? Where are thy coming from? Is 'loop' called by more than one thread? I am stumped; any help gratefully received.

I am stumped; any help gratefully received.

It doesn't help that you don't identify WHAT you are printing. It doesn't help that you are using the String class.

Correct those two issues, and see if the problem persists.

You do NOT need to send the message to the client or server as one packet.

Not sure just what you want the end product of the capacitance values to be or nshould look like. I suggest you take two concurrent paths, one using a dummy capacitance value string in a web page for web testing, and a second path of being able to generate the desired capacitance values in a string and displaying that string in the serial monitor.

PaulS:
It doesn't help that you don't identify WHAT you are printing.

Sorry I do not understand your point here. In the question I say that I am trying to send capacitance values by printing "the array of integer values read " to an HTTP connection, and I give the example string "GET /default.aspx?store=[{name:"DefaultPlate",values=[0,31,33,0,77,70,39,47,57,23,17,61,0,88,84,62,63,98,4,42]}] HTTP/1.1". What "WHAT" is it that I do not identify?

PaulS:
It doesn't help that you are using the String class.

That's interesting - why should I not use the String class, and if I want to build up a string whose length is not known in advance what should I be doing?

PaulS:
You do NOT need to send the message to the client or server as one packet.

Good point, this may alleviate the need for me to build the string locally first at all. I'll try that.

PaulS:
Correct those two issues, and see if the problem persists.

Yessir.

zoomkat:
I suggest you take two concurrent paths, one using a dummy capacitance value string in a web page for web testing, and a second path of being able to generate the desired capacitance values in a string and displaying that string in the serial monitor.

Sadly I had already got both paths working, though I think my understanding of the String class needs more work.

In the question I say that I am trying to send capacitance values by printing "the array of integer values read " to an HTTP connection, and I give the example string "GET /default.aspx?store=[{name:"DefaultPlate",values=[0,31,33,0,77,70,39,47,57,23,17,61,0,88,84,62,63,98,4,42]}] HTTP/1.1". What "WHAT" is it that I do not identify?

Some stuff appears in the Serial Monitor. You are asking us to guess which Serial.print() statement caused which text to appear. I don't play those guessing games.

You have something like:

Serial.println(myString);

I would have:

Serial.print("Contents of my string: [");
Serial.print(myString);
Serial.println("]");

You would see:

some random words appear

I would see:

Contents of my string: [some random words appear]

When the random words appear, you are left to guess where they came from. I know.

You can, too, with only a little effort.

That's interesting - why should I not use the String class, and if I want to build up a string whose length is not known in advance what should I be doing?

There is only one member of the forum who does not accept that dynamic allocation and freeing of String instances causes memory corruption problems. That is not me. There is a well known problem with the fact that when a String goes out of scope, and the memory is deallocated, ultimately using free(), which has a major bug that corrupts the free list.

In your case, you DO know how long the string will be. You know what you are sending up front, and you now how many values you are converting to strings, and you know the maximum length of any of those strings. So, you could, if you wanted to, determine the maximum length of the string.

There is a well known problem with the fact that when a String goes out of scope, and the memory is deallocated, ultimately using free(), which has a major bug that corrupts the free list.

So is use of Strings causing the problem with the OP's code, or is the String issue just a senior ramble in this instance? :wink: