How to send float number to android app?

I am doing a VirtualWrite with a float and all i get on the android app for Value and Gauge is %.3f is displayed. I did a pull yesterday, and am building on esp8266 standalone.

1 Like

Could you please show your sketch? Floats works ok for me.

I’m having the same problem. I’m floating the temperature from a ds18b20 sensor and using virtual write but same problem.

I’ve been working a bit around with this issue myself to.
I to can’t get a float printed to value widget. And i dont know if supposed to work but at least for know i do a little workaround by converting to string. (Code in bottom of post)

Simple example that prints the %.3f error (using esp8266 and android):

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

char auth[] = "";

float testVal = 12.34;

SimpleTimer timer;

void setup() 
{
  Serial.begin(9600);
  Blynk.begin(auth, "", "");
  timer.setInterval(3000, readTemp);
}


void loop()
{
  Blynk.run();
  timer.run();
}


void readTemp()
{
  Blynk.virtualWrite(5, testVal);
}

@vshymanskyy hymanskyy helped me in this thread with a related problem http://community.blynk.cc/t/compile-stalls-when-using-virtualwrite-on-a-string-variable/669

To summarize, i wanted to read a DS18B20 sensor and convert it to string (for Thingspeak) also i wanted the value printed in a value widget. Since i couldn’t get it to print the float value i used the value converted to string. For that you should use dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
The example below prints the value with 2 decimal.

Hope it can be helpfull. :smile:

#include <OneWire.h>
#include <DallasTemperature.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

//Data wire plugged to pin 2 (temp sensor)
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
char auth[] = "";

SimpleTimer timer;

void setup() 
{
  Serial.begin(9600);
  Blynk.begin(auth, "", "");
  sensors.begin();

  timer.setInterval(3000, readTemp);
}


void loop()
{
  Blynk.run();
  timer.run();
}


void readTemp()
{
  sensors.requestTemperatures();
  float floatTempC = sensors.getTempCByIndex(0);
  char t_buffer[15];
  dtostrf(floatTempC, 4, 2, t_buffer);
  Blynk.virtualWrite(5, t_buffer);
}
1 Like

@iclimb

Thank, so this is ESP library specific issue. Hope @vshymanskyy will fix it somehow =).

I traced through the code, and see the define for ARDUINO_ARCH_ESP8266 in BlynkParam.cpp, and see where the float/double is supposed to get converted into a string. It looks ok to me, so not sure what is wrong.

void BlynkParam::add(float value)
{
	len += sprintf(buff+len, "%2.3f", value)+1;
}

void BlynkParam::add(double value)
{
	len += sprintf(buff+len, "%2.3f", value)+1;
}

I tried this with the latest version, and still have the problem. Looked into it more and the problem is that the code is using printf(%f), but there isn’t actually support for %f. I have opened a ticket at https://github.com/blynkkk/blynk-library/issues/14

I also had trouble getting floats to work until I realized the analogRead must be recast.

BLYNK_READ(V0)
{
vbat = (float)analogRead(A0)/75;
Blynk.virtualWrite(0, vbat);
}

Hello,

I would like to send the value 35.1.
Unfortunately, this does not work as desired.
Also the version 0.5.2 does not work.

float results = 35.1;
Send to terminal:
Serial.println (results); OK

Send to App:
Blynk.virtualWrite (V4, “35.1”); OK
Blynk.virtualWrite (V4, results); NOK (value in the app -0.0)

What can I do?

Which hardware and software do I use:
TI Launchpad: CC3200-LAUNCHXL REV4.1
Energia: 1.6.10E18
Blynk: v0.5.4
Blynk IOS APP: 2.23.0 (4)

Regards Martin

@MGAT Basically - this is a bug in most TI boards. I suggest you to report it to TI.
There’s a workaround. Try adding #define BLYNK_USE_INTERNAL_DTOSTRF in Blynk/BlynkConfig.h

Hello vhymanskyy,

thank you for you reply.
Your solution works. :+1:

Thanks a lot.

Regards Martin