I'm loading a buffer with different data recorded by sensors and sending them to a server. But I also wanted to log when the data was sent on the client side, so I formatted the time of my RTC clock and tried to include it in the buffer, but this caused a problem. The data returned by the sensors are floating point numbers, but the timestamps are integers. When trying to send the buffer including both the datatypes, it is not received by the server. Under is the code which puts the data into the buffer:

char buffer[256] = {};

sprintf(buffer, "path?myfile.txt,%02d/%02d/%02d %02d:%02d:%02d,Temp=%lf,Humidity=%lf,Pressure=%lf,Light=%lf",rtc.getDay(),rtc.getMonth(),rtc.getYear(),rtc.getHours(),rtc.getMinutes(),rtc.getSeconds(),temperature,humidity,pressure,illuminance);

I am pretty sure the mixing of the data types or the formatting is the problem. I know it's not the buffer size because I have tested it with multiple sizes. The weird thing is that I can print the buffer out in the serial monitor without any problem. It is also formated the right way in the serial monitor:

"path?myfile.txt,10/02/23 17:05:16, Temp=26.863487,Humidity=24.886541,Pressure=99.868286,Light=8.387096

I have never experienced a situation where one buffer is sent successfully and another one is not. If I for example send this buffer instead:

sprintf(buffer, "/cgi-bin/tof.cgi?zuluz.txt,Temp=%lf,Humidity=%lf,Pressure=%lf,Ligth=%lf",temperature,humidity,pressure,illuminance);

Everything works as it should. What do you think the problem is? And is there a good workaround for this? If this problem comes from the server itself, I can not do anything about it since I have no access to its backend code.

My board is the MKR NB 1500 using a MKR ENV shield to log the data.

Edit: Here is the full function which actually writes to the server:

void write_server()
    Serial.println("Sending the buffer");
    char buffer[256] = {}; 

    client.print("GET ");
    sprintf(buffer, "path?myfile.txt,%02d/%02d/%02d %02d:%02d:%02d,Temp=%lf,Humidity=%lf,Pressure=%lf,Light=%lf",rtc.getDay(),rtc.getMonth(),rtc.getYear(),rtc.getHours(),rtc.getMinutes(),rtc.getSeconds(),temperature,humidity,pressure,illuminance);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println("Connection: close");
    Serial.println("Buffer sent");

The only diffrence from the actual code is the path on the server and the file name in which the data is stored.

  • the space in the timestamp is the problem. replace it with %20 (correctly escaped for printf pattern). (and path should begin with /, but I guess it is missing just because of the obfuscation for the post)
    – Juraj
    Feb 10 at 17:29
  • In addition to what Juraj said, a proper HTTP query string should be in the format as /test/demo_form.php?name1=value1&name2=value2, so you are missing a key for you datetime string, and , is not a valid delimiter and should be replaced by &. Try path?<filename>=myfile.txt&<datetime>=10/02/23%2017:05:16&Temp=26.863487&Humidity=24.886541&Pressure=99.868286&Light=8.387096, replace <filename> and <datetime> with whatever your server is expecting.
    – hcheung
    Feb 12 at 5:57


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.