New System BatteryCharge API deviceOS@1.5.0

Has anyone been able to get System.batteryCharge() feature to work? I am running this simple code:

// Program Variables                      
char batteryString[64];

void setup() { 
  takeMeasurement();                                              // Test the battery state of charge
  Particle.publish("State",batteryString,PRIVATE);
}

void loop() {
}

void takeMeasurement() {
  const char* batteryContext[7] ={"Unknown","Not Charging","Charging","Charged","Discharging","Fault","Diconnected"};
  // Battery conect information - https://docs.particle.io/reference/device-os/firmware/boron/#batterystate-
  snprintf(batteryString, sizeof(batteryString),"Battery SOC: %.1f and State: %s", System.batteryCharge(), batteryContext[System.batteryState()]);
}

When I run this, on a Bare Boron running deviceOS@1.5.0 with a fully charged LiPO battery attached - and nothing else. I get the following result:

Screen Shot 2020-05-05 at 1.49.12 PM

How is it possible to get a -1 State of Charge? Any idea what I am doing wrong?

Thanks,

Chip

1 Like

@All,

Thanks to Chris from the support team, I think we have a fix for this issue. Here is what happened in case this helps someone else.

  1. I have been playing with settings for Solar Power and the Boron device. I had set too high a charge current for my 3.5W / 6V solar panel and the current theory that this caused the PMIC to go into the state where battery was reported as “-1” as you see above.

  2. To bring the PMIC back to its factory defaults, Chris provided the following command. This command, and the correct setPowerConfiguration API commands resolved this issue.

System.setPowerConfiguration(SystemPowerConfiguration());  // To restore the default configuration
  1. With this reset command and the correct settings for solar power, the code was rewritten. Please see amended code below which works:
// Program Variables                      
char batteryString[64] = "Null";

// setup() runs once, when the device is first turned on.
void setup() { 

  System.setPowerConfiguration(SystemPowerConfiguration());  // To restore the default configuration

  takeMeasurement();                                              // Test the battery state of charge
  Particle.publish("State",batteryString,PRIVATE);
    SystemPowerConfiguration conf;
    conf.powerSourceMaxCurrent(550) // Set maximum current the power source can provide (applies only when powered through VIN)
        .powerSourceMinVoltage(5080) // Set minimum voltage the power source can provide (applies only when powered through VIN)
        .batteryChargeCurrent(512) // Set battery charge current
        .batteryChargeVoltage(4210) // Set battery termination voltage
        .feature(SystemPowerFeature::USE_VIN_SETTINGS_WITH_USB_HOST); // For the cases where the device is powered through VIN
                                                                     // but the USB cable is connected to a USB host, this feature flag
                                                                     // enforces the voltage/current limits specified in the configuration
                                                                     // (where by default the device would be thinking that it's powered by the USB Host)
  int res = System.setPowerConfiguration(conf); // returns SYSTEM_ERROR_NONE (0) in case of success
}

void loop() {
}

// Utility Functions Area

void takeMeasurement() {
  const char* batteryContext[7] ={"Unknown","Not Charging","Charging","Charged","Discharging","Fault","Diconnected"};
  // Battery conect information - https://docs.particle.io/reference/device-os/firmware/boron/#batterystate-
  snprintf(batteryString, sizeof(batteryString),"Battery SOC: %.1f and State: %s", System.batteryCharge(), batteryContext[System.batteryState()]);
}

Now, I have a way to reset devices which are mis-behaving. Next step is to test with sleep and deploy to a solar powered device to confirm this fix.

Thanks, Chip

4 Likes

Does anyone know why I get this error when running this code?

‘class SystemClass’ has no member named ‘setPowerConfiguration’
‘SystemPowerConfiguration’ was not declared in this scope

Is there some library I didn’t include. I am running it on a v1.5.1 Argon.

The Argon has no PMIC as the Boron has.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.