float to const char* error

Hello all,

Very new to programming for the Arduino and have a problem I can't seem to resolve and would appreciate any help.

From an example I downloaded and edited for my wifi and mqtt server I was able to make the following work. "35" was just an arbitrary value I sent to see if it would show up in openHab and it does!

snprintf (msg, 75, "35", value);
Serial.print("Current Temp: ");
Serial.println(msg);
client.publish("/openhab/shoptemp", msg);

Then I connected a DHT22 sensor and am able to read the temperature and humidity and output to the serial monitor window. But it fails when trying to prepare for sending to the mqtt server. Here's what I've changed from the above example.

temp = (dht.readTemperature() * 9.0)/5.0 + 32.0;
snprintf (msg, 75, temp, value);
Serial.print("Current Temp: ");
Serial.print(msg);
Serial.println("");
client.publish("/openhab/shoptemp", msg);

It fails on the second line with "cannot convert 'float' to 'const char*' for argument '3' to 'int snprintf(char*, size_t, const char*, ...)'"

After googling to try and find what that means I'm just not getting how to fix it...

Thanks

Scot

temp = (dht.readTemperature() * 9.0)/5.0 + 32.0;
snprintf (msg, 75, temp, value);

The third argument is how to format the data - it needs to be a char pointer/array.

Thanks for the quick reply, but unfortunately you're talking over my head and I don't understand.

Can you provide an example of what you mean?

Thanks

For quick help and reliable links that inform how to use standard functions, use google.
Read up on how to use snprintf here.

jremington:
Google is the quickest and most reliable way to help you learn how to use standard functions.

Read up on how to use snprintf here.

Thanks for that, but I had read that before posting and am not getting it...

I think if that's over your head this project is over your head... Maybe start with the basics?

+1. Why try to use snprintf() if you don't understand it? Use Serial.print() and move forward.

Appreciate the links and suggestions, but what I really need is an example of how to fix this.

I've spent several hours over the past 3 days putting this project together and then trying to figure out how to resolve the error and can't. As stated above this programming language is very new to me, I'm just starting out with this.

Why am I using that, because it was in the working example I found online...

If there is a better way to do it, then please show me.

Thanks

p912s:
Appreciate the links and suggestions, but what I really need would like is an example of how to fix this. Aka, a pre-made program for my needs.

I've spent several hours over the past 3 days putting copy pasting this project together and then trying to figure out how to resolve the error and can't. As stated above this programming language is very new to me, I'm just starting out with this over my head complicated project.

Why am I using that, because it was in the working example I found online...

If there is a better way to do it, then please show me.

Thanks

Fixed the post for you.

Aka, if you don't know what something does don't just use it. Find out what it really does.

septillion:
Fixed the post for you.

Aka, if you don't know what something does don't just use it. Find out what it really does.

Ok, you're the smartest one in the room and you come here to feel superior to others and not provide any real help.

Congrats and have a nice day.

First, what do you see after these two statement:

    temp = (dht.readTemperature() * 9.0)/5.0 + 32.0;
    snprintf (msg, 75, temp, value);

when you try to use Serial.print() to see msg? What happens if you do this:

    char buffer[12];

    temp = (dht.readTemperature() * 9.0)/5.0 + 32.0;
    dtostrf(temp, 7, 3, buffer);
    snprintf (msg, 75, buffer, value);

More importantly, why does this change things?

not provide any real help.

Most forum members don't like to write code for people who won't bother to learn the basics, or spend a lot of time trying to teach the basics in a post. The teaching part has already been done many times and is freely available on the web.

Consider also that snprintf() is possibly the most complex, versatile function in the C/C++ language.

If you want to purchase code-writing help, post on the "gigs and collaborations" section of the forum.

@econjack: the example you posted won't compile or work. Not sure what you are trying to do.

@jremington - justify your comments however you want. I participate in several forums and answer basic questions all the time for those starting out rather than berate them. Guess we just disagree on how one should conduct themselves on a forum. If you didn't want to help, you could have passed by and not said anything...

jremington:
@econjack: the example you posted won't compile or work. Not sure what you are trying to do.

Actually, this worked quite well.

econjack:
More importantly, why does this change things?

Thanks for the help, when I get back home tonight I'll study this to try and understand what it did.

Thanks again.

I'll study this to try and understand what it did.

Excellent idea!

@jremington: I didn't want to give him the solution, just nudge him towards the right path.