Using get to receive decimal case

Hello,

I have a question with respect to variables and hope someone can help me.

I have the variable data1

double date1 = 20.5;

but when I use the get I get only 20.5 how do I receive also the decimal value?

{
   "cmd", "VarReturn"
   "name": "result"
   "result": "{\" data1 \ ": 20.}"
   "Coreinfo": {
     "last_app": "",
     "last_heard": "2015-03-03T22: 08: 03.673Z"
     "connected": true,
     "last_handshake_at": "2015-03-03T22: 07: 59.110Z"
     "deviceID": "mydevice"
   }
}

? your spark variable looks like this:

Spark.variable("temp", &tempC, DOUBLE);

outputs like this:

Thank you for answer, yes now I can see:
result": 5.567611162256132e-144,

but the correct value is 0.0

the line current = map(current/5, 0, 4095, 0, 3300); shows 0,000

but the line sprintf(resultstr, “{“data1”:%d,}”, current); show 5.567611162256132e-144

How can I convert 5.567611162256132e-144 to 0.000

FULL CODE

void loop()
{
Serial.print(“ACS712@A2:”);Serial.print(readCurrent(ACSPin),3);Serial.println(" mA");
delay(150);
}

int determineVQ(int PIN) {
Serial.print(“estimating avg. quiscent voltage:”);
long VQ = 0;
//read 5000 samples to stabilise value
for (int i=0; i<5000; i++) { // esttava 5000
VQ += analogRead(PIN);
delay(1);//depends on sampling (on filter capacitor), can be 1/80000 (80kHz) max.
}
VQ /= 5000;
Serial.print(map(VQ, 0, 4095, 0, 3300));Serial.println(" mV");
return int(VQ);
}

float readCurrent(int PIN) {
int current = 0;
int sensitivity = 100.0;//change this to 100 for ACS712-20A or to 66 for ACS712-30A
//read 5 samples to stabilise value
for (int i=0; i<5; i++) {
current += analogRead(PIN) - VQ;
delay(1);
}

current = map(current/5, 0, 4095, 0, 3300);

sprintf(resultstr, “{“data1”:%d,}”, current);

return float(current)/sensitivity;

}

If you want to display it on you Serial monitor, you want to do it like this example:


void setup()
{
  Serial.begin(9600);
  delay(6000);// let your spark get ready while you set up your terminal
  char buffer[20];
  double val = 5.567611162256132e-114;
  sprintf(buffer,"my number is: %.4f", val);
  Serial.println(buffer);
}
void loop()
{
}

so something like this (untested)

void loop()
{
  char buffer[25];
  //Serial.print("ACS712@A2:");Serial.print(readCurrent(ACSPin),3);Serial.println(" mA");
  double val = readCurrent(ACSPin);
  sprintf(buffer, "ACS712@A2:%.4f mA", val);
  Serial.println(buffer);
  delay(150);
}

If your double reports an odd number that’s the value it realky has even if you expect another one :wink:

Some background about floating point numbers you can watch here

So if you need a certain human readable representation, you need to take further steps.
You could either do the formatting on your client side.
You could round the value on the Core and send this value, still possible to have the problems outlined in the video.
You could multiply the value by powers of 10 to get an integral value with the desired precision and make your Spark.variable an INT.
You could build a STRING for the `SPARK.variable.

thank you, wortks fine.

1 Like