BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Battery is discharged


#1

The project is based on Nodemcu esp8266, WiFi . Powered by battery, to save using sleep. Problem: if esp8266 cannot communicate with the Blynk server, the device does not go to sleep and the battery is discharged. How to cancel constant attempts to contact the server?


#2

If you are talking about the battery that powers the ESP8266, rather than the Smartphone battery, there is no way that the ESP8266 will try to contact any server when in deepSleep().

Presumably you are using deepSleep() incorrectly.


#3

in time of sleep the minimum requirement. the problem is that if the device cannot communicate with the server, then it constantly tries to establish a connection and cannot go to sleep - the battery is discharged. how to limit the number of attempts to communicate with


#4

Which battery are you referring to?


#5

battery powering nodemcu esp8266


#6

Post a minimum sketch which shows how you are trying to use deepSleep().


#7
#define BLYNK_PRINT Serial


#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#include <DHT.h>
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO 
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)
#define DHTPIN 0 // The pin you connect to, 0 = D3
#define DHTTYPE DHT22   // DHT 11 Change this if you have a DHT22
DHT dht(DHTPIN, DHTTYPE,16); // Change this to 
SimpleTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "********";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ASUS_80_2G";
//char ssid[] = "ZyXEL_KEENETIC_4G_FEA6C8";
char pass[] = "******";

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8442);
    timer.setInterval(30000, sendDHT);
}
void sendDHT()
{
//Read the Temp and Humidity from DHT
  //delay(2000);
  pinMode(5, OUTPUT); 
  digitalWrite(5, HIGH);
  delay(1000);
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  digitalWrite(5, LOW);
  
  pinMode(12,OUTPUT);//Подача напряжения на датчик почвы
  digitalWrite(12, HIGH);
  //analogWriteFreq(75000);
  //analogWrite (12,1023);
  delay(100);
  float hs = analogRead(A0);
  digitalWrite (12,0);
  
  int hum = (int) h;
  int tem = (int) t;
  int mst = (int) hs;
//Write values to V04 and V05, blocking 2147483647 value
if (hum == 2147483647) {
  Serial.print(" false hum reading ");
} else {
  Blynk.virtualWrite(4, hum);
}
if (tem == 2147483647) {
  Serial.print(" false humtemp reading ");
} else {
  Blynk.virtualWrite(5, tem);
}

  Serial.print(hum);
  Serial.print(" percent  \n\r");
  Serial.print(tem);
  Serial.print(" celcius  \n\r");

  Serial.print("Moisture Sensor Value:");
  Serial.println(mst); 
  Blynk.virtualWrite(6, mst);
  
  ESP.deepSleep(600000000); //600000000 = 10 min

  
}
void loop()
{
  Blynk.run();
  // You can inject your own code or combine it with other sketches.
  // Check other examples on how to communicate with Blynk. Remember
  // to avoid delay() function!
    timer.run();
    
}

#8

For your deep sleep command to work, it probably needs to have a short delay after it (100ms should do the trick).

However, your code structure isn’t really optimised for deep sleep, so battery life won’t be as good as it could be.
When the deep sleep period ends, the ESP device will reboot. You then want to perform a temperature/humidity reading as quickly as possible, then do another deep sleep.
Waiting 30 seconds before the sendDHT function is called serves no purpose. Call it manually as soon as you have a connection to Blynk.

Setting a static IP address for your ESP (probably easiest done in your router) is also a good idea, as it speeds-up the connection to your Wi-Fi when the ESP re-awakens.
You’ll find that when using DHCP most routers remember the IP address for your device for a short while, then the router’s routing table is flushed and an IP has to be allocated each time the ESP connects, which takes time (this will almost certainly be the same IPaddress as before, but the negotiation process to re-allocate this IP address takes awhile to complete). As a result, power saving looks good when doing tests on short sleep periods, but you don’t get the expected power savings on longer sleep times if you don’t have a static IP for your ESP device.

Pete.


#9

@vgataullin here is a minimal working sketch for deepSleep() with a connection between D0 and RST.

Disconnect all your DHT stuff and just run it through Serial Monitor at 74880 baud.

Start with the sleep as the 5s then extend as required. With this there will be minimal battery drain during deepSleep(). As you add back DHT you might find where your battery drain is.

// Sleepy.ino
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer;

char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
char serv[] = "xxx";

void setup()
{
  Serial.begin(74880);
  Blynk.begin(auth, ssid, pass, serv);
  timer.setInterval(3333, gettingSleepy);  // sleep after 3.333S
}
void gettingSleepy()
{
  Serial.println(F("I feel sleepy"));
  ESP.deepSleep(5000000); //5000000 = sleep for approx 5 secs
}
void loop()
{
  Blynk.run();
  timer.run(); 
}

#10

Thanks for the advice. But sleep works well.
The main problem is that if my device cannot communicate with the blink server, then it tries to establish a connection an infinite number of times through the cycle and as a result, it discharges the battery. How to limit the number of attempts to establish communication?


#11

It DOES NOT. It is asleep and the only thing running is the internal clock of the ESP8266.


#12

My device sleeps well. I have no problem with this.
After waking up, if the device cannot establish connection with the blink server, it tries to establish a connection through the loop an infinite number of times. Do you agree?


#13

Yes I understand now.

Blynk.begin() is a blocking routine and if you have a problem connecting it will try forever.

Look up connection management in the docs and switch to Blynk.Config() with Blynk.connect() etc.

You can set a timeout with Blynk.connect() so that it will go into deepSleep() without trying to connect to Blynk. You would need to investigate why the connection is failing though or you will be permanently asleep.