ESPhome custom sensor (BQ27441)

I have a BQ27441 Lipo fuel gauge (Same as on SparkFun Battery Babysitter - LiPo Battery Manager - PRT-13777 - SparkFun Electronics) that I would like to get working with ESPHome.

There’s some documentation about creating a custom senor here: Custom Sensor Component — ESPHome
I tried to follow it along, but I get stuck when I want to use the Adafruit library: PlatformIO Registry

According to the documentation, I should add something like this to my config in ESPHome:

esphome:
  includes:
    - my_custom_sensor.h
  libraries:
    - "Adafruit BMP085 Library"

I guess I should replace the library with “SparkFun BQ27441 LiPo Fuel Gauge Arduino Library”, but the documentation is not really clear on that point. When I try to compile, I see:

Library Manager: Installing SparkFun BQ27441 LiPo Fuel Gauge Arduino Library

but it seems to mention that, whatever you specify:

Library Manager: Installing some non existing library :thinking:

However, when I add this to my “my_custom_sensor.h” :

#include "esphome.h" 
#include "SparkFunBQ27441.h"

I get this error:

In file included from src/main.cpp:26:0:
src/my_custom_sensor.h:2:29: fatal error: SparkFunBQ27441.h: No such file or directory

Any idea what I should do to fix this error?

You might try, at the command line, manually installing that library, and see if it reports any problems.
e.g.,

pio pkg install "SparkFun BQ27441 LiPo Fuel Gauge Arduino Library"

and see if what happens provides any clues.

thanks for the advice, I was able to get to the command line of the host i’m running HA on. I’m running home assistant OS on a NUC. I then went into the container running ESPHome with:

docker exec -it addon_a0d7b954_esphome bash

When I ran pio pkg install "SparkFun BQ27441 LiPo Fuel Gauge Arduino Library" I got: Error: No such command "pkg

Apparently, the container is not running the latest version of platform.io. I read on pio pkg install — PlatformIO latest documentation that pio pkg is only available in version 6.

I then tried pio platform install "SparkFun BQ27441 LiPo Fuel Gauge Arduino Library", that gave this error:

Platform Manager: Installing SparkFun BQ27441 LiPo Fuel Gauge Arduino Libraryge Arduino Library"
Error: Please upgrade to the PlatformIO Core 6

Upon closer inspection of the ESPHome log, I saw this warning:

Library Manager: Installing Installing SparkFun BQ27441 LiPo Fuel Gauge Arduino Library
Warning! Please upgrade to the PlatformIO Core 6

So on the command line, I see it as a error, but in the ESPHome log, it’s just a warning :thinking:

I though I was using the latest version of ESPHome, because I use it as a add-on and always update when there is a update available.Turned out I was on version 2022.3, while the latest version is 2022.10

I didn’t had a update button in the add-on, so I eventually uninstalled the plugin and re-installed it. That worked, and now my problem seems to have been solved!

1 Like

For anyone interested in using the BQ27441 fuel gauge, My config now looks like:

esphome:
  name: test
  includes:
  - BQ27441.h
  libraries:
  - Wire
  - "SparkFun BQ27441 LiPo Fuel Gauge Arduino Library"

sensor:
- platform: custom
  lambda: |-
    auto BQ27441 = new My_BQ27441();
    App.register_component(BQ27441);
    return {BQ27441};

  sensors:
    name: "battery level"

and my BQ27441.h looks like:

#include "esphome.h"
#include "SparkFunBQ27441.h"

class My_BQ27441 : public PollingComponent, public Sensor {
 public:
  My_BQ27441() : PollingComponent(15000) {}

  float get_setup_priority() const override { return esphome::setup_priority::HARDWARE; }

  void setup() override {
    lipo.begin()
 	lipo.setCapacity(2000);
  }
  void update() override {
    unsigned int soc = lipo.soc();
    publish_state(soc);
  }
};

I am just trying to implement the BQ27441 into my first EPSHome project, and I get the following error:

[C][spi:023]: Setting up SPI bus...
E (16330) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (16330) task_wdt:  - loopTask (CPU 1)
E (16330) task_wdt: Tasks currently running:
E (16330) task_wdt: CPU 0: IDLE0
E (16330) task_wdt: CPU 1: IDLE1
E (16330) task_wdt: Aborting.
abort() was called at PC 0x4015920c on core 0

ELF file SHA256: 0000000000000000

Backtrace: 0x4008920c:0x3ffbf8b0 0x40089489:0x3ffbf8d0 0x4015920c:0x3ffbf8f0 0x40087885:0x3ffbf910 0x40170f13:0x3ffbc160 0x4015ab17:0x3ffbc180 0x4008bc59:0x3ffbc1a0 0x4008a49a:0x3ffbc1c0

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
[I][logger:258]: Log initialized
[C][ota:469]: There have been 1 suspected unsuccessful boot attempts.
[D][esp32.preferences:113]: Saving 1 preferences to flash...
[D][esp32.preferences:142]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[I][app:029]: Running through setup()...
[I][i2c.arduino:175]: Performing I2C bus recovery

I have my SparkFun connected to GPIO 21 and 22. Below is the code I am using:

esphome:
  name: "weather-frame-01"
  includes:
  - BQ27441.h
  libraries:
  - Wire
  - "SparkFun BQ27441 LiPo Fuel Gauge Arduino Library"

...

# Measure points for Lipo charger
i2c:
  sda: 21
  scl: 22
  scan: true
  id: bus_a

...

sensor:
  - platform: custom
    lambda: |-
      auto BQ27441 = new My_BQ27441();
      App.register_component(BQ27441);
      return {BQ27441};
    sensors:
      name: "Battery Level"

I am using the WaveShare e-paper board to then display weather info on an e-ink display. I use the SparkFun to charge the LiPo battery and also wanted to show the battery status on the display. But as soon as I uncomment the sensor part it crashes with the error above. I am using the same BQ27441.h file as above.

Anybody who can help or guide me in the right direction?