0

I'm working on a home automation project using Arduino and MQTT.

I've found a problem and my limited programming knowledge find a way to solve it.

I can send text trough MQTT normally, using

client.Publish("some/topic","Some text here");

But when I try to send a variable (int, char, etc), the compiler says "invalid conversion from 'char' to 'const char*' [-fpermissive]"

How can I store DHT11 sensor reading in const char* to send it?

BTW, I've found this code where

char *tempC;
tempC = dtostrf(((((analogRead(tempPinIn) * 5.0) / 1024) - 0.5) * 100), 5, 2, message_buffer);
client.publish("arduino/temperature",tempC);

is used. But the extra sensor calibration makes it even harder for me to understand whats going on.

1 Answer 1

1

The function dtostrf() is prototyped as:

char *dtostrf (double          val,
               signed char     width,
               unsigned char   prec,
               char           *sout);

Where:    
   val      Your float variable;
   width    Length of the string that will be created INCLUDING decimal point;
   prec     Number of digits after the deimal point to print;
   sout     Destination of output buffer;

An example of usage is as follows:

/* OPTION ONE */

    char msgBuffer[20];           // make sure this is big enough to hold your string
    char *pointer_to_created_string;

    float testFloat = 123.45;

    pointer_to_created_string = dtostrf(testFloat, 6, 2, msgBuffer);
    client.Publish("...topic...", pointer_to_created_string);

/* OPTION TWO */

    char msgBuffer[20];           // make sure this is big enough to hold your string

    float testFloat = 123.45;

    client.Publish("...topic...", dtostrf(testFloat, 6, 2, msgBuffer));

Don't be put off by the code in the link you included in your question. The first argument sent to dtostrf (everything before the first comma) is nothing more than a some calculations bundled into one single line.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.