Float Data - Webhook Response Help

I have a webhook response coming back as a float as shown below.

I’m trying to use this handler function to turn that into a float variable:

But this always returns this in the serial terminal:

I got this from a @rickkas7 example shown below but maybe I am misunderstanding something.

How do best get this float data pushed into a variable?

what happens when you just print data?

what happens when you declare BTCprice as a double?

double BTCprice = atof(data);

It didn't work.

I ended up setting this a Global Variable:

String BTClatest;

And then using this in the webhook handler:

void myHandler(const char *event, const char *data) {  // This is called when the Photon receives the webhook data.

  BTClatest = String(data);                  // Get the latest price of Bitcoin

}

It may not be best but it's working for now which feels better than it not :slight_smile:

Have you tried that?
It's a vital step to understand what's happening before being able to answer this

Best print with some means to judge whether there are any non-visible characters embedded.

atof() should work. If it doesn't there's probably something wrong with the incoming data.

And as you know, Strings ...

Why not use char BTClatest[16] and strncpy(BTClatest, data, sizeof(BTClatest) -1)?

However your original issue is probably that you are originally declaring float BTCprice; as a local variable which will vanish as soon you leave the function your global version of it will never be updated.

2 Likes

Of course your right :smile:

The serial prints look like this:

So I'm getting the Quotes before and after the number.

That explains why the atof() was not working :slight_smile:

I had the " " accidentally added to my webhook response template. I removed them and now I only get the digits :wink:

This short but sweet line of code works perfectly now for pushing the webhook response into the Global float variable!

BitCoinSpotPrice = atof(data);

Thanks for helping me figure this out :thumbsup:

3 Likes