Thanks for all the answers!
What are you using ? The Arduino or the ESP ?
I use the Arduino IDE to write a program for the ESP.
Looks like you are creating a URL for either a POST or a GET request.
Not the complete URL. The ESP will communicate with another ESP using ESP-Now. The second ESP is via a serial link connected to a third ESP that actually creates a real URL to communicate with Domoticz on a Raspberry Pi. There are two reasons for this setup. First, the ESP-Now connection is faster and less battery consuming than using only one ESP that talks directly with the RPi. Second, this way the ESP's outside the house only have the SSID in the software but not my network password. The stupid thing is that I have no real problem creating this setup, but I got confused on using strings (written with a capitol or not). Maybe that's just because I'm used to Lazarus/Pascal where you hardly have to think about string issues.
Most applications like this that I found on the internet use a record to pack the data from the sensor, with the sensortype as one of those fields. On the receiver side you have to extract the fields from the record and translate this data to a JSON string based on the sensortype. My approach was to send the variable part of this JSON as a string/String to the receiver:
The receiver completes the URL with the network address and port number of Domoticz and with the fixed part of the JSON:
The receiver doesn't need to translate the record which has one big advantage: you don't have to change the receiver software when a new sensor type is added.
Using Strings you can declare an object with a variable size in a different part of your memory, and this is where fragmentation may occur. Keep in mind, fragmentation only occurs if you increase the size of different 'Strings' alternatingly. If you create a String, add more String parts to that, and then let the whole thing out of scope, you will not have fragmented anything in the end.
Two questions. First, how to let the whole thing go out of scope? Second, is it a solution to create the String with a length which is big enough. Something like:
String theJsonPart = "012345678890123456789012345678890123456789";
for a string of 40 characters and the use it in my program to create a meaningful string:
theJsonPart = "idx="; // the string now has 4 characters, followed by 36 non-used memory bytes
theJsonPart += IDX;
theJsonPart += "&value=";
theJsonPart += VALUE;
and so on.