blynkkk / blynk-library

Blynk library for IoT boards. Works with Arduino, ESP32, ESP8266, Raspberry Pi, Particle, ARM Mbed, etc.
https://blynk.io
MIT License
3.84k stars 1.39k forks source link

virtualWrite of double/float not working, sending %f #14

Closed CptanPanic closed 6 years ago

CptanPanic commented 9 years ago

So I am using an esp8266 and trying to do a virtualWrite of a double/float, and all I get on blynk app is %.3f. I did some more investigating, and found that this is actually a problem with the printf not supporting %f. I am not sure this is just a esp8266 thing, or maybe even an arduino issue. I found this function http://pastebin.com/FdBFk3F4 that should just work instead of snprintf(). You can also see this issue reported at:

http://community.blynk.cc/t/how-to-send-float-number-to-android-app/761/4

https://github.com/esp8266/Arduino/issues/341

CptanPanic commented 9 years ago

Here is a function that should work.

char * dtostrf(double number, signed char width, unsigned char prec, char *s) {

    if(isnan(number)) {
        strcpy(s, "nan");
        return s;
    }
    if(isinf(number)) {
        strcpy(s, "inf");
        return s;
    }

    if(number > 4294967040.0 || number < -4294967040.0) {
        strcpy(s, "ovf");
        return s;
    }
    char* out = s;
    // Handle negative numbers
    if(number < 0.0) {
        *out = '-';
        ++out;
        number = -number;
    }

    // Round correctly so that print(1.999, 2) prints as "2.00"
    double rounding = 0.5;
    for(uint8_t i = 0; i < prec; ++i)
        rounding /= 10.0;

    number += rounding;

    // Extract the integer part of the number and print it
    unsigned long int_part = (unsigned long) number;
    double remainder = number - (double) int_part;
    out += sprintf(out, "%d", int_part);

    // Print the decimal point, but only if there are digits beyond
    if(prec > 0) {
        *out = '.';
        ++out;
    }

    while(prec-- > 0) {
        remainder *= 10.0;
        if((int)remainder == 0){
                *out = '0';
                 ++out;
        }
    }
    sprintf(out, "%d", (int) remainder);

    return s;
}
vshymanskyy commented 9 years ago

Thank you! I will fix it soon.

vshymanskyy commented 9 years ago

Should be fixed now on master branch. Thanks.

CptanPanic commented 9 years ago

Yes I verified it works. Thanks.

Zuijdam commented 6 years ago

And in latest version 0.5.3 it is broken again :( reverting back to 0.5.2 fixed it

vshymanskyy commented 6 years ago

Thanks. Sorry will fix that asap

vshymanskyy commented 6 years ago

@Zuijdam Maybe you need to update your ESP8266 Arduino Core. What version do you use?

vshymanskyy commented 6 years ago

@Zuijdam the bug is confirmed, but it is a bug of ESP8266 Arduino Core. You should update to the latest ESP8266 Arduino Core to resolve this.

Zuijdam commented 6 years ago

Thank for the update. I will check it and reply here.

johnddawson commented 6 years ago

I am having an issue with Blynk virtual write using TIVA 129 board and Energia. If I try to send a float the app display remains blank. I have to convert to an integer then it works. How can I send a float?

vshymanskyy commented 6 years ago

@johnddawson it means that specific board has a bug (on TI side). Anyway, a simple workaround would be to enable BLYNK_USE_INTERNAL_DTOSTRF. You'll need to dig into the library a little bit, to figure it out.

johnddawson commented 6 years ago

To enable, do you just enter #define BLYNK_USE_INTERNAL_DTOSTRFin the sketch?

On Tuesday, October 2, 2018 1:11 PM, Volodymyr Shymanskyy <notifications@github.com> wrote:

@johnddawson it means that specific board has a bug (on TI side). Anyway, a simple workaround would be to enable BLYNK_USE_INTERNAL_DTOSTRF.— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

vshymanskyy commented 6 years ago

@johnddawson no it won't work. You'll have to put it in BlynkConfig.h

vshymanskyy commented 5 years ago

BLYNK_USE_INTERNAL_DTOSTRF is now enabled automatically for all TI boards (Energia IDE)