System calls

powerSource()

Since 1.5.0:

Determines the power source, typically one of:

  • POWER_SOURCE_VIN Powered by VIN.
  • POWER_SOURCE_USB_HOST Powered by a computer that is acting as a USB host.
  • POWER_SOURCE_USB_ADAPTER Powered by a USB power adapter that supports DPDM but is not a USB host.
  • POWER_SOURCE_BATTERY Powered by battery connected to LiPo connector or Li+.
// PROTOTYPE
int powerSource() const;

// CONSTANTS
typedef enum {
    POWER_SOURCE_UNKNOWN = 0,
    POWER_SOURCE_VIN = 1,
    POWER_SOURCE_USB_HOST = 2,
    POWER_SOURCE_USB_ADAPTER = 3,
    POWER_SOURCE_USB_OTG = 4,
    POWER_SOURCE_BATTERY = 5
} power_source_t;

// EXAMPLE
int powerSource = System.powerSource();
if (powerSource == POWER_SOURCE_BATTERY) {
  Log.info("running off battery");
}

Note:

Power Management including power source detection is available on the Boron, B-Series SoM, Tracker SoM, E404X (Gen 3), Electron, and E-Series (Gen 2).

It is not available on the P2, Photon 2, Argon, Photon, or P1.