Battery status / level

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
fibigod
New user
Posts: 4
Joined: 03 Jun 2016, 21:06

Battery status / level

#1 Post by fibigod » 03 Jun 2016, 21:10

Dear people,

I have a setup with a esp-1 with a temperature sensor ds18b20. Connected with 2 aa batteries.

Is it possible to get a reading of the battery level? I use domoticz to receive the temp status.

Kind Regards,

Fabian

User avatar
lucaberta
Normal user
Posts: 59
Joined: 09 May 2016, 11:26
Location: Lausanne, Switzerland
Contact:

Re: Battery status / level

#2 Post by lucaberta » 03 Jun 2016, 21:54

fibigod wrote:I have a setup with a esp-1 with a temperature sensor ds18b20. Connected with 2 aa batteries.

Is it possible to get a reading of the battery level? I use domoticz to receive the temp status.
hi Fabian,

check the "Measure VCC" thread here:
http://www.esp8266.nu/forum/viewtopic.php?f=6&t=1357

Of course this is only a reading on the Vcc and gives the output in volt, this is not in any way a battery gauge to tell you 100% or 50% or 10% of battery left. But it's better than nothing, and it comes already with the R107 code, though it has to be specifically added by changing one line in the main ESP Easy source code.

See if it can help your needs.

Bye, Luca

CoolWombat
Normal user
Posts: 30
Joined: 31 Oct 2015, 08:00

Re: Battery status / level

#3 Post by CoolWombat » 11 Jun 2016, 15:00

Hi Fabian

It should be fairly easy to implement using a voltage divider and feeding the voltage to the ADC input of the ESP. I'm using an ESP-12E module and the ADC input is on pin 2. You need the voltage divider because the ADC input can only take a max of around 1V. Since you're using 3V, one option for the two resistors for the voltage divider are 4.7K and 10K. You should be able to pick other resistor values if you don't have those two on hand. 10K should be fairly common, for the other value, anything less than 4.7K should be fine. However you do not want to go too low or you will start to loose resolution. If you are not familiar, the way to connect it all up is to connect the resistors in series first ie: connect two end of the two resistors together, then connect the unconnected end of the 10K to the positive of the battery and then connect the unconnected end of the 4.7K to the negative/ground of the battery. You then connect the point where you connected the two resistors together to the ADC input. That should be it for the hardware.

For the software side, you will need to add a device and then make up a formula to calculate the real voltage. First you will need to create a new device type "analog input" and give it a name and IDX value. Once you have done that click on submit and look at the analog value in the table of devices. Use a multimeter and measure the real voltage of the battery. Take the voltage you measured with the multimeter and divide by the analog value. For example this might be 0.004571. You then go back to your analog device and enter the following formula into the "Formula Analog" box: %value%*0.004571

It should now send the correct voltage reading to Domoticz.

Good Luck!

fibigod
New user
Posts: 4
Joined: 03 Jun 2016, 21:06

Re: Battery status / level

#4 Post by fibigod » 19 Jul 2016, 22:52

Thank you for the solutions provided.

I've used the build suggested by lucaberta and changed the vcc line in the sourcecode.

User avatar
rajbadri
Normal user
Posts: 56
Joined: 21 Dec 2015, 21:38
Location: India

Re: Battery status / level

#5 Post by rajbadri » 20 Jul 2016, 07:08

voltage-divider.jpg
voltage-divider.jpg (211.79 KiB) Viewed 34066 times

is this schematic ok ?

simeonof
Normal user
Posts: 10
Joined: 20 Jan 2016, 06:38

Re: Battery status / level

#6 Post by simeonof » 18 Mar 2017, 11:32

Hi everyone!
D1mini already have voltage divider 220KOhm/100KOhm on ADC pin.
I'm using 3.3MOhm and 1MOhm for my battery powered sensors. so this voltage divider consumes under 1 micro amp. esp8266 have very large input impedance (over 12MOhm),
To have an idea about battery percentage left and RSSI, I've add these lines in C002 (I'm using domoticz MQTT):
In EspEasy.ino:

Code: Select all

 
int BatteryLevel = 0;
/*********************************************************************************************\
 * SETUP
\*********************************************************************************************/
In Webserver.ino:

Code: Select all

    if (WiFi.status() == WL_CONNECTED)
    {

        BatteryLevel = (analogRead(A0)-700)/2;
        if (BatteryLevel > 100) 
            {
              BatteryLevel = 100;
            }
        else if (BatteryLevel < 0)
            {
                BatteryLevel = 0;
            }
            
      reply += F("<TR><TD>Wifi RSSI:<TD>");
      reply += WiFi.RSSI();
      reply += F(" dB");
      reply += F("<TR><TD>Battery:<TD>");
      reply += BatteryLevel;
      reply += F(" %");
    }
In C002.ino just before

Code: Select all

 char json[256]; 

Code: Select all

        
        root["RSSI"] = (WiFi.RSSI()+100)/5;
        BatteryLevel = (analogRead(A0)-700)/2;
        if (BatteryLevel > 100) 
            {
              BatteryLevel = 100;
            }
        else if (BatteryLevel < 0)
            {
                BatteryLevel = 0;
            }
        root["Battery"] = BatteryLevel;
 <---------------- original code------------------------//             
       char json[256];
I know the code is awful, but I'm not a programmer after all.
You can adjust "(analogRead(A0) - 700)/2" according to your R1/R2 and the type of battery (lipo or 3AA) to show more correctly percentage.
This it the result:

Code: Select all

MQTT: Topic: domoticz/in, Message: {"idx":2,"nvalue":0,"svalue":"12.50","RSSI":8,"Battery":33}
Attachments
espbattery.jpg
espbattery.jpg (34.03 KiB) Viewed 31745 times

slowtraveler
New user
Posts: 1
Joined: 08 Apr 2017, 16:33

Re: Battery status / level

#7 Post by slowtraveler » 08 Apr 2017, 22:16

I've tried voltage reporting, using several power supplies (3x1.5V, 2х1.2V etc)... Unfortunately when using 2x1.2V rechargeable batteries, Domoticz logically reports LOW BATTERY.
Simeonof's idea was great, but needs some update, to solve such issues....
So i upgraded his idea with:
1. Working on HTTP domoticz communication too
2. Voltage calibration index can be changed using CONFIG screen

So changes are not too big (including/alternating simeonof's code), but it's not easy to explain step by step, because small changes are fragmented file by file...
Anyway... Required changes are remarked (ADD LINE(S) BELOW / ABOVE)
ESPEasy.ino

Code: Select all

// ADD LINE(S) BELOW
int BatteryLevel = 0;
// ADD LINE(S) ABOVE
/*********************************************************************************************\
 * SETUP
\*********************************************************************************************/

....

Code: Select all

struct SettingsStruct
{
..
  int16_t       TimeZone;
// ADD LINE(S) BELOW
  int16_t       BatteryVoltageIndex; 
// ADD LINE(S) ABOVE
} Settings;
In Webserver.ino

Code: Select all

//********************************************************************************
// Web Interface root page
//********************************************************************************
void handle_root() {
// ADD LINE(S) BELOW
  String BatteryVoltageIndex=WebServer.arg("batteryvoltageindex"); // BATTERY VOLTAGE INDEX INITIALIZATION
// ADD LINE(S) ABOVE
....

Code: Select all

   if (WiFi.status() == WL_CONNECTED)
    {
// ADD LINE(S) BELOW

//****** START BATTERY LEVEL VIEW
        BatteryVoltageIndex=Settings.BatteryVoltageIndex;
        BatteryLevel = (analogRead(A0)- BatteryVoltageIndex.toInt())/2;
        if (BatteryLevel > 100) 
            {
              BatteryLevel = 100;
            }
        else if (BatteryLevel < 0)
            {
                BatteryLevel = 0;
            }
            
      reply += F("<TR><TD>Wifi RSSI:<TD>");
      reply += WiFi.RSSI();
      reply += F(" dB");
      reply += F("<TR><TD>Battery:<TD>");
      reply += BatteryLevel;
      reply += F(" %");
    }
//******* END BATTERY LEVEL VIEW
// ADD LINE(S) ABOVE
...

Code: Select all

//********************************************************************************
// Web Interface config page
//********************************************************************************
void handle_config() {
  ..........
  String apkey = WebServer.arg("apkey");
// ADD LINE(S) BELOW
  String BatteryVoltageIndex = WebServer.arg("batteryvoltageindex"); // Battery reporting voltage index - BATTERY LEVEL = ( ANALOG INPUT VALUE - VOLTAGE INDEX) / 2
// ADD LINE(S) ABOVE
  ....
    Settings.Unit = unit.toInt();
// ADD LINE(S) BELOW
    Settings.BatteryVoltageIndex = BatteryVoltageIndex.toInt(); // Battery voltage index reading
// ADD LINE(S) ABOVE
    SaveSettings();
  }
  
....

Code: Select all

reply += F("'><TR><TD>Unit nr:<TD><input type='text' name='unit' value='");
  reply += Settings.Unit;
// ADD LINE(S) BELOW
// BATTERY VOLTAGE INDEX CELL
  reply += F("'><TR><TD>Battery Index (700 for 3x1.5V batteries) :<TD><input type='text' name='batteryvoltageindex' value='");
  reply += Settings.BatteryVoltageIndex;
// END OF BATTERY
// ADD LINE(S) ABOVE  

  reply += F("'><TR><TD>Protocol:");

In _C001.ino (http requests)

Code: Select all

boolean CPlugin_001(byte function, struct EventStruct *event, String& string)
{
 ........
          case SENSOR_TYPE_DIMMER:
          ......
            break;
        }

// ADD LINE(S) BELOW
//************** START BATTERY REPORTING
int BatteryVoltageIndex=Settings.BatteryVoltageIndex;
        if (BatteryVoltageIndex>0) {

        BatteryLevel = (analogRead(A0)-BatteryVoltageIndex)/2;
        if (BatteryLevel > 100) 
            {
              BatteryLevel = 100;
            }
        else if (BatteryLevel < 0)
            {
                BatteryLevel = 0;
            }
         url += F("&rssi=");
         url += toString((WiFi.RSSI()+100)/5,0);
         url += F("&battery=");
         url += toString(BatteryLevel,0);
         url += ";";
        }

//************ END BATTERY REPORTING
// ADD LINE(S) ABOVE

        url.toCharArray(log, 80);
        addLog(LOG_LEVEL_DEBUG_MORE, log);

        // This will send the request to the server
In _C002

Code: Select all

// ADD LINE(S) BELOW
//************** START BATTERY REPORTING
int BatteryVoltageIndex=Settings.BatteryVoltageIndex;     
        root["RSSI"] = (WiFi.RSSI()+100)/5;
        BatteryLevel = (analogRead(A0)-BatteryVoltageIndex)/2;
        if (BatteryLevel > 100) 
            {
              BatteryLevel = 100;
            }
        else if (BatteryLevel < 0)
            {
                BatteryLevel = 0;
            }
        root["Battery"] = BatteryLevel;

//************ END BATTERY REPORTING
// ADD LINE(S) ABOVE
        char json[256];

Post Reply

Who is online

Users browsing this forum: No registered users and 27 guests