Electron FuelGauge Value Question?

This may be simple question to answer, but I seem to be struggling to find an answer, so I apologize if this is a newb question. I am attempting to retrieve the FuelGauge fuel.getSoC() in my iOS App. The iOS app is retrieving the value no problem, but as I am converting it from a Float to an Int or a Double, on the Electron, before it is being sent to the iPhone, it is coming across looking like this
When Converted to a Double:
Current Device Battery Life: -2.32462795913192e+51

How can I convert this value to read as a percentage? I have done some reading about Floating point values, but to be honest, I don’t really understand them very well, so any guidance here would be very much appreciated. Thank you very much!!

How do you do the conversion?
Show your code.

This is the line of code where the conversion is being done.

double batteryLife = 0;
FuelGauge fuel;

void setup(){
//get battery life on startup
    Particle.variable("batteryLife", double(fuel.getSoC()));
}
//function get battery life upon request
int getBatteryLife(String command){
    
     //Set Battery Life Variable with current battery life value
    Particle.variable( "batteryLife", double(fuel.getSoC()));

    return 0;
}

You can’t do it that way.
You need to read the data into a global double variable and expose that variable to the cloud.

Also Particle.variable()s are only registered once any update happens solely on the underlying variable.

double batteryLife = 0;
FuelGauge fuel;

void setup() { 
  //get battery life on startup
  batteryLife = fuel.getSoC();
  Particle.variable("batteryLife", batteryLife);
  Particle.function("getSoC", getBatteryLife);
}

//function get battery life upon request
int getBatteryLife(String command) {
  //Set Battery Life Variable with current battery life value
  batteryLife = fuel.getSoC();

  return (int)batteryLife;
}
2 Likes

Thank you very much for the fast and extremely accurate response. That solved the issue.:slight_smile:

1 Like

I have a followup question. The battery life percentage value is working fine and showing up on in my iOS app just fine, however, it seems to toping out at 84% charge while plugged into usb overnight. Is there any additional logic that needs to be added on either the Electron or in my iOS code? The only difference between the code I have now, and what you pasted in your last post is that I set the batteryLIfe variable to an Int instead of a double, and removed the int before the return batteryLife; , but this doesn’t appear to be the issue as I have tried it both ways? Any thoughts? Thanks again.

That is normal. The SoC is calculated from the cell voltage and for that Particle has set a safety margin to avoid overcharging the batteries. So with aprox. 85% you can consider the battery full.

You can tweak some things to cram more charge into the battery but 100% will never be reached.
There are some threads that deal with this.