Spark Core Http Client Library

Got it.
I replaced

 sprintf(buf, "/GrabData/New/?MCID=%s&Amps1=%.1f\n",s,Amps1);

with

 sprintf(buf, "/GrabData/New/?MCID=%s&Amps1=%.1f\n",s.c_str(),Amps1);

This is a dangerous thing to do tho' - I'm not even sure this can build at all - I expect you have changed the declaration of s along with the code.

Strings are no char * or const char * but have an cast operator (const char*) overload that makes your first line valid code, but s is not a String object anymore, so you shouldn't (and supposedly can't even) call StringClass methods on it.

You should not even rely on const char* s = someString.c_str(); or const char* s = (const char*)someString; to keep up with any change of someString or to be const either.
As long someString stays in place, you might see changes reflected in s, but once someString gets relocated due to growing out of its current heap position s will reference freed space, which might get invalidated (possibly causing a hard fault) or taken up by a subsequently created different heap variable.

Good catch yes I also updated the ā€œconst char* s = System.deviceID();ā€ to

double Amps1 = 21;
double Amps2 = 22;
String s = System.deviceID();

char buf[50];

void setup() {
  Serial.begin(9600);
}

void loop() {
  sprintf(buf, "/GrabData/New/?MCID=%s&Amps1=%.1fAmps2=%.1f\n",s.c_str(),Amps1,Amps2);
  Serial.print(buf);
  delay(1000);
}

Sorry to revive such an old thread, but Iā€™m having this same problem, and I canā€™t quite tell if it was resolved here. I want to be able to update my request.path, and Iā€™m trying to use format() like thisā€¦

request.path.format("/weatherstation/updateweatherstation.php?ID=KCADALYC8&dateutc=now&winddir=%d&windspeedmph=%d&windgustmph=%d&softwaretype=vwsversionxx&action=updateraw HTTP/1.1", wind_dir, wind_speed, wind_gust);

But when I print out request.path it appears to be completely empty.

Any tips would be greatly appreciated.

Why do you not try it the way shown above with sprintf()?
String::format() does return a new String obeject - it doesnā€™t alter the string in place.

I have no good excuse. It must have been too late at night. I think I saw the sprintf to buf, but didnā€™t see that that was then copied to request.path. Iā€™m pretty familiar with working with char[], but not strings.

In any event, that solved it. Thanks very much.

The HttpClient.h doesnt seem to exist for particle photon ! I get this errror when I try to compile.

testapp.ino:1:24: HttpClient.h: No such file or directory

on what device you are running your code on ?

It does exist and there is no distiction of platform as far as visibility is concerned.
What IDE are you using?
Have you actually imported the library?

I am using webide as of now.

If you post a SHARE THIS REVISION link we may be able to find the reason for your issue.

Nevermind I got it working. I inclded the HttpClient.cpp and HttpClient.h files along with my project. Thank you

1 Like