Reading electron battery level

Is there a way to, in the device code, read out the battery level (ie raw input voltage) of the Electron’s onboard JST connector?

Would this work?
https://docs.particle.io/reference/firmware/electron/#fuelgauge
https://docs.particle.io/reference/firmware/electron/#pmic-power-managment-ic-

@taco, here’s what I did.

void loop() {
FuelGauge fuel;
float value;
value = fuel.getVCell();
//json string
String output = "{\"batt-value\": \"" + String(value) + "\"}";
Particle.publish("fuel-level1", output);
//NOTE: This block OTA updates... Use safe mode (hold both buttons, release reset) to reprogram
System.sleep(SLEEP_MODE_DEEP, 21600);   //6 hours = 21600
}

Thanks folks, can’t believe I missed that @Moors7

1 Like

Hi All,
I’m working with the fuel gauge for the first time and have a question, super basic to you guys, I’m sure, but I can’t figure it.

How do I get from the float reported with value = fuel.getSoc() to an integer percentage?

I currently get “68.300781”, but I want to display "68"
I know it’s got to be super simple, but I can’t find it.
Either converting from the string to an int, or from the float to an int is what I’m looking for.

FuelGauge fuel;
value = fuel.getSoC();
//json string
String output = “{“batt-SoC”: “” + String(value) + “”}”;
Particle.publish(“fuel-SoC1”, output);

OK, got it.

Found this thanks to @Moors7:

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;
}

1 Like

HI folks!

I just tried to use FuelGauge and it’s not listed in the Library search. Am I missing something? Is this library no longer supported?

THANKS!

You shouldn’t need a library as it should be part of the stock functionality included via Particle.h

Perhaps Nickbee means it’s missing from the docs. It doesn’t appear when “Core” is selected, but you see it if you select “Electron”.

Seeing as the Core, and Photon, don’t have that chip, it seems to make sense not to include that in the system firmware for those devices.

2 Likes

Hi Nick,
Here is some code that I got off the asset tracker library. It creates a function you can call to battery voltage and state of charge.

#include <Particle.h>
#include "SdCardLogHandlerRK.h"
#include <AssetTrackerRK.h>

// A FuelGauge named 'fuel' for checking on the battery state
FuelGauge fuel;

void setup() {
 Particle.function("batt", batteryStatus);
}

void loop() {

// Lets you remotely check the battery status by calling the function "batt"
// Triggers a publish with the info (so subscribe or watch the dashboard)
// and also returns a '1' if there's >10% battery left and a '0' if below
int batteryStatus(String command){
    // Publish the battery voltage and percentage of battery remaining
    // if you want to be really efficient, just report one of these
    // the String::format("%f.2") part gives us a string to publish,
    // but with only 2 decimal points to save space
    Particle.publish("B",
          "v:" + String::format("%.2f",fuel.getVCell()) +
          ",c:" + String::format("%.2f",fuel.getSoC()),
          60, PRIVATE
    );
    // if there's more than 10% of the battery left, then return 1
    if (fuel.getSoC()>10){ return 1;}
    // if you're running out of battery, return 0
    else { return 0;}

}

Note that I just copied and pasted parts of my code here for your reference. They guys above can help you format it correctly for your application. Hope it helps in some way.

Cheers,
Tom