Data not posting due to format

Marcio Valenzuela on 22 Oct 2021
Latest activity Edit by Christopher Stapels on 26 Oct 2021

I have a project that collects data from a CO2 sensor and stored it in these ints:

    CO2PPM = (int)data[2] * 256 + (int)data[3];
    temperature = (int)data[4] - 40;  

which are defined as ints. Then just before the http request, they are processed like this:

       static char outstr3[15];
       static char outstr4[15];
       String dataString3 = dtostrf(CO2PPM, 8, 2, outstr3);
       String dataString4 = dtostrf(temperature, 8, 2, outstr4);

and finally used in the GET request:

          wifly.println("GET /update?api_key=apikey&field1="dataString4"&field2="+dataString3+" HTTP/1.1");

which fails to successfully register the data into TS. However if I hardcode it like this:

          wifly.println("GET /update?api_key=apikey&field1=23.45&field2=67.89 HTTP/1.1");

it works, the data is registered.

Any idea how to fix this issue?

Christopher Stapels
Christopher Stapels on 25 Oct 2021

Have you outputted the datastring3 and 4 to the serial monitor to ensure they are building correctly? I would consider building the full update string first, possibly with sprintf. sprintf(wholBigString,"GET /update?api_key=apikey&field1=%d&field2=%dHTTP/1.1",value1,value2) Serial.println(wholeBigString"); Then your final command is just wifly.println(wholeBigString);

Marcio Valenzuela
Marcio Valenzuela on 26 Oct 2021

I doesnt output, that's the problem. I get blank lines for:

data = String("data is =" + dataString3+" and "+dataString4+".");    
Serial.println(data);

where data is defined as String data; and dataString3 & 4 are defined locally as String dataString3 = "12.34";

If I use your suggestion:

sprintf(astring,"here is the data %d and %d",dataString3,dataString4);

I get the error:

cannot convert 'String' to 'char*' for argument '1' to 'int sprintf(char*, const char*, ...)'
Christopher Stapels
Christopher Stapels on 26 Oct 2021

%d is for integers. Use the integers directly. If you need to use %s for strings or characters but I'm not sure this conversion is correct or needed.

       static char outstr3[15];
       static char outstr4[15];
       String dataString3 = dtostrf(CO2PPM, 8, 2, outstr3);
       String dataString4 = dtostrf(temperature, 8, 2, outstr4);

I suggest you look at the help for sprintf, google is a really good friend there.

Marcio Valenzuela
Marcio Valenzuela on 26 Oct 2021

yes i remember using sprintf in the past, i just overlooked that. It doesnt work with %d or %s. I tried but i still get:

CO2TempUV4Thingspeak:174:68: error: cannot convert 'String' to 'char*' for argument '1' to 'int sprintf(char*, const char*, ...)'
        sprintf(astring,"here is the data", dataString3, dataString4);
                                                                    ^
exit status 1
cannot convert 'String' to 'char*' for argument '1' to 'int sprintf(char*, const char*, ...)'

Im looking here: https://www.mathworks.com/help/matlab/ref/sprintf.html

Christopher Stapels
Christopher Stapels on 26 Oct 2021 (Edited on 26 Oct 2021)

Sorry! I had the syntax incorrect. I may edit the above to remove my tracks.

wholeBigString=sprintf(,"GET /update?api_key=apikey&field1=%d&field2=%dHTTP/1.1",value1,value2);