ESP8266 baterry indicator in Hassio

Hello to all,

my setup is ESP8266 - 02 Wroom lipo (16850 battery charged) + DS18B20 temperature sensor. I would like to see, how much battery is still available in % if this is possible with my setup. I did try some other solutions from other topics, but with no luck for me.

In ESPhome am using this code for temperature reading:

esphome:
  name: temperatura
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "xxxx"
  password: "xxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Temperatura Fallback Hotspot"
    password: "q7vN22bPvGD4"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

# Example configuration entry
dallas:
  - pin: GPIO4

# Individual sensors
sensor:
  - platform: dallas
    address: 0x0E00449B00000328
    name: "temperatura"
    

In Hassio I have temperature card, where I can see temperature, but like I said, I would like to observe also battery charge… To complicate things a little more, I would like to see temperature and battery charge in the same card, like this:

battery

Can anyone help, but please be aware, that am newbie in Hassio system and coding.

you need to setup another sensor :slight_smile:
havent done t myself yet (will do soon ™ )

Use the analogue input to measure the battery voltage. You will need a voltage divider to do this as it exceeds 3.3V.

16K For R1 and 47K for R2 would allow you to measure up to 4.42V and use a scaling factor in ESPHome of A0*1.34 to get the true battery voltage. You want to aim for a total series resistance somewhere in the ballpark of 100K to not draw too much current from the battery but still allow a reasonable input current into the ADC.

Then use the following lookup chart to convert to (a very rough) percentage in ESPhome:

V, %
4.2, 100
4.13, 90
4.06, 80
3.99, 70
3.92, 60
3.85, 50
3.78, 40
3.71, 30
3.64, 20
3.57, 10
3.5, 0

Note that this is highly dependant on load current.

See if you can get that working first then we’ll work on the card.

1 Like

Sorry for my late response…

I will try both proposals, but I must say, that second proposal is a little bit science fiction for me and my knowledge at the moment.

Before I open new topic,… I would like to put my ESP8266 to deep sleep. I uploaded code to ESP and seems, that code works well, but…ESP doesnt wake up by himself - I need to press RST button.

I googled, that GPIO16 and RST pin on ESP should be wired, on my Wemos ESP8266 - 02 wroom this is already factory done.

In ESP home am using this code:

esphome:
  name: temperatura
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "xxxx"
  password: "xxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Temperatura Fallback Hotspot"
    password: "q7vN22bPvGD4"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

# Example configuration entry
dallas:
  - pin: GPIO4

# Individual sensors
sensor:
  - platform: dallas
    address: 0x0E00449B00000328
    name: "temperatura"
    
  
deep_sleep:
  run_duration: 120s
  sleep_duration: 30min

ESP goes to deep sleep, but after 30 min, nothing happens (blue light on ESP starts to glow). After pressing the RST button, the ESP wakes up, take a measurement and go back to deep sleep.

I guess i’m missing something in the code…

Any help?

OK, back to original topic - battery level.

I used code from ESPhome (thanks lilbuh), the code is:

esphome:
  name: napetost
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: "xxxxx"
  password: "xxxxx"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Napetost Fallback Hotspot"
    password: "Im1Adzk9zPPG"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

# Example configuration entry
dallas:
  - pin: GPIO17

# Individual sensors
sensor:
  - platform: adc
    pin: A0
    name: "napetost"
    update_interval: 60s

In log ESPhome log files, i get this:

It appears, that ESP reading the voltage, which in my case is 0.00391V.

In Hassio, the result is 0.00V, probably because I need to do some multiplication code:

nao

What is next step, to get proper voltage reading?

Thanks!

Disconnect it. Now.

If you did not follow my post you will have more than 3.3V on the ADC input and risk damaging it.

Ok, I disconnected it.

What am doing wrong?

Again, please be aware, that am really begginer in all this.

Don’t get me wrong, but I really don’t know, where to put 16 and 47 K resistor… I thought that the ESP8266 could read the voltage on itself without any additional wiring…?

It can read up to 3.3v. Your battery can be as high as 4.2v if properly charged.

If you only read the internal 3.3V supply (Vcc) this will tell you nothing about the state of the battery as it is held at a constant voltage by a voltage regulator.

OK, let say, that I managed to connect everything as shown on voltage divider (am using 4,7K and 10K ) and am getting 2.26V.

By voltage divider I should get 2.857, but battery is not fully charged.

Now I assume, that I need to connect purple and yellow wire on some pins on ESP board…?

You are still not measuring the battery voltage. You are connected to the 3.3v rail.

Sorry, but now I don’t understand anything more :slight_smile:

Is my wiring is not the same as on the voltage divider site? On which ESP pin I need to connect red and black, to get battery voltage?

The voltage divider goes directly across the battery.

Ok, I understand (hope so) now, I need to connect + / - on battery via 4,7K and 10K resistor. Then I need to conect pin AD with exit from the devider between the two resistors.

Thanks for patience with me!

Ok, now I have properly wired directly across the battery and I am measuring the voltage in Hassio. The shown voltage is too small and needs to be multiplied…

a

Am using this code:


# Example configuration entry
dallas:
  - pin: GPIO17

# Individual sensors
sensor:
  - platform: adc
    pin: A0
    name: "napetost"
    update_interval: 60s

I did try some coding with multiply (scaling factor )…

# Example configuration entry
- platform: adc
  # ...
  filters:
    - offset: 2.0
    - multiply: 1.2

…but I really dont know, how to do it correctly, for everything I try I constantly get an error in the code.

I think, that code is right now…

# Individual sensors
sensor:
  - platform: adc
    pin: A0
    name: "napetost"
    update_interval: 60s
    filters:
    - multiply: 4.61

Am getting real battery voltage!

ab

Now I would liketo put battery voltage in %.

This is where you use the table I posted above and the calibrate linear filter, https://esphome.io/components/sensor/index.html#calibrate-linear

Then all you have to do is set the unit to %.

I think, that we have success!

So, my code is now:

# Example configuration entry
dallas:
  - pin: GPIO4

# Individual sensors
sensor:
  - platform: dallas
    address: 0x0E00449B00000328
    name: "temperatura"
    
  
  - platform: adc
    pin: A0
    name: "napetost"
    unit_of_measurement: "%"
    accuracy_decimals: 0
    update_interval: 60s
    filters:
    - multiply: 4.611111111111
    - calibrate_linear:
          - 4.2 -> 100
          - 4.13 -> 90
          - 4.06 -> 80
          - 3.99 -> 70
          - 3.92 -> 60
          - 3.85 -> 50
          - 3.78 -> 40
          - 3.71 -> 30
          - 3.64 -> 20
          - 3.57 -> 10
          - 3.50 -> 0


deep_sleep:
  run_duration: 120s
  sleep_duration: 30min

And this is the result:

ff

Thank you tom_I for your patience with me and your help!

3 Likes

I have one more question related to this topic. I have another same ESP8266 and I would like to connect it with soil humidity sensor which have analog output (I assume, that sensor in this case must be connected to A0 pin). In this case, I cannot measure battery level, because analog A0 pin is taken by sensor…Is there any workaround for this?