NodeMCU battery voltage status

Hi, i drive my nodemcu with old gps battery (3.7v 1400 mAh) and i wanted to check my battery voltage via gauge.After some research i found a code and adapted to my sketch.Now it’s running like charm but it drains my battery so quickly.Can someone check my code what is wrong.I am not a coder that’s why i ask for your help guys.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SPI.h>

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
extern "C" {
  uint16 readvdd33(void);
}
SimpleTimer timer;

void myTimerEvent()
{
  Blynk.virtualWrite(V5, millis() / 1000);   // i really don't know what it is 
  Blynk.virtualWrite(V10,readvdd33());  // i read value from that virtual pin
}

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

   timer.setInterval(60000L, myTimerEvent); // i set that value for 60 sec interval  
}

void loop()
{
  Blynk.run();
  timer.run(); 
}

As far as I can see, you are checking your battery every minute BUT, your NodeMCU is ALWAYS connected to the server and your battery goes down quickly.

Try to use Deep Sleep, this way, the NodeMCU maintains its RTC but shuts everything else off.

You can find more info HERE

Kind regards and good luck!!

You get accurate readings with this? The NodeMCU has an LM1117-3.3 regulator which means I always get a reading of 2.7v even when feeding it 3-6v.

Thank you for the tip,today im gonna try deep sleep :slight_smile::+1:

Yes i do,i think my board is chinese clone.I set the gauge value to 3700,because my battery 3.7. And i get accurate readings.

I like your comment.

millis() is a function that provides the milliseconds since your sketch started, ergo this line sends your device uptime, in seconds, to widget V5 (normally a Value Display widget).

As far as i understand it’s a good thing for to check how long can my battery last right?:grinning:
But minutes work better instead of seconds i think.

Blynk.virtualWrite(V5, millis() / 60000);
or
Blynk.virtualWrite(V5, millis() / 1000) / 60;

is ok but not:

the 60 can’t be outside the virtualWrite function().

Ok got it thank you. And do you have any idea where do i have to put Blynk.notify("NodeMCU Started!"); in my sketch to get notification?:slight_smile:

it would need to go in setup() after Blynk.begin() but I notice you don’t wait for a connection to Blynk in setup(). I would recommend:

void setup()
{
  Serial.begin(115200);   // 9600 only needed for inferior hardware
  Blynk.begin(auth, ssid, pass);
  unsigned long maxMillis=millis() + 8000;
  while ((Blynk.connect() == false) && ( millis() <= maxMillis)) {
  }
  Blynk.notify("NodeMCU Started!"); // ensure you have Notification widget in the project   
  timer.setInterval(60000L, myTimerEvent); // i set that value for 60 sec interval  
}

Thank you for helping copy&paste coders like me :smile:
I updated my sketch with few changes.For to check my board visually about it’s running or not i added breathing led option(thanks google). And i learned bit about how to do setup and loop options.I think now i can merge two basic codes like that :grinning:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#define LED     D4   // On Board Blue Led     
#define BRIGHT    200  // 0-500   
#define INHALE    2000   
#define PULSE     INHALE*1000/BRIGHT
#define REST      1500  

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
extern "C" {
  uint16 readvdd33(void);
}
SimpleTimer timer;

void myTimerEvent()
{
  Blynk.virtualWrite(V5, millis() / 60000);
  Blynk.virtualWrite(V10,readvdd33());
}

//----- Setup function. ------------------------
void setup() {                
  pinMode(LED, OUTPUT);
  Serial.begin(115200); 
  Blynk.begin(auth, ssid, pass);
  unsigned long maxMillis=millis() + 8000;
  while ((Blynk.connect() == false) && ( millis() <= maxMillis)) {
  }
  Blynk.notify("NodeMCU Started!");
  timer.setInterval(60000L, myTimerEvent);    
}

//----- Loop routine. --------------------------
void loop() {
  Blynk.run();
  timer.run(); 
    for (int i=1;i<BRIGHT;i++){
    digitalWrite(LED, LOW);          
    delayMicroseconds(i*10);         
    digitalWrite(LED, HIGH);        
    delayMicroseconds(PULSE-i*10);   
    delay(0);                       
  }

  for (int i=BRIGHT-1;i>0;i--){
    digitalWrite(LED, LOW);          
    delayMicroseconds(i*10);          
    digitalWrite(LED, HIGH);         
    delayMicroseconds(PULSE-i*10);  
    i--;
    delay(0);                       
  }
  delay(REST);                       
}

I have the same board on another project! I’ll try it out with your code as I have not tried your method.

Do you get the same result with the ESP.getVcc() method?

No,i never tried that,let me know if you succeed.

It only gives me low readings with that method.
Im jsut trying your one now!

Tried a few different esps and I get 2.7v on all of them with you code.

It sucks because I’ve got 20 esps and none of them have been able to read correct vcc voltage.

Starting to think it’s the firmware loaded on it.

Are you getting these value from battery driven board or any other? I ask because i noticed that when i drive my board with battery value seems ok,when i connect my board to my computer i get lower values like 2.7.

This is driven via a TP4056 Lipo charging board. The voltage from the module matches the battery exactly.
When I plug in the USB to charge the lipo via the TP4056, the output voltage climbs to 3.7v, but the reading says the same as 2.7v.
I read that its because on these dev boards the internal VCC voltage is read on the ESP chip after the VCC is used for other things on the boards like the CH340 chip… which causes the VCC to drop (because its all the ESP is getting)

Putting the battery directly in to the VIN/GND & 3V3/GND both give me 2.7v again.

As you see below i have same TP4056 charging board.I don’t know to much about what’s going on in the board electrically but maybe firmware version of board can cause this. Can you share your code also? My firmware is “v1.3.0.2 AT Firmware.bin”
Hope it helps.

Could you point me to info on how you loaded it and where you got it from?
This might be hte source of my issues.

I don’t remember where i’ve found it.I flashed via esptool on ubuntu.If you want i can send the firmware via e-mail.