NodeMCU + BLYNK + Deep Sleep

Hi all, here me again to ask help and suggestions. Last week i figured out lots of things about Blynk . But the final round has not come yet.Today i tried to handle deep sleep option but as usual i stucked.I just want to do put my battery powered NodeMCU deep sleep after i get some data like battery status,ip address,wifi strength and uptime. It connect to blynk but i can not get data .Can you please point me what is wrong? :slight_smile:

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

#define BlueLed     D4                              // NodeMCU on board blue led
#define STROBE      D8                                              // White led

const int sleepTime = 30;                                    // Sleep time cycle

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

extern "C" {
#include "user_interface.h"
uint16 readvdd33(void);
}

WidgetLCD lcd(V3);
long rssi;
IPAddress myip;
int Led1=D6;                                   
int Led2=D7;

SimpleTimer timer;

void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(BlueLed, OUTPUT);           // When i set this pin as output it starts 
  pinMode(STROBE, OUTPUT);           // fire instantly.
  unsigned long maxMillis=millis() + 8000;
  while ((Blynk.connect() == false) && ( millis() <= maxMillis)) {
  }
  myip = WiFi.localIP();
  Blynk.notify("NodeMCU Started!");
  timer.setInterval(1000L, DataUpload);
  blink();
  delay(30000);               // Delay to keep connection open with Blynk Server
  Serial.println("NodeMCU going to sleep...");
  system_deep_sleep(sleepTime * 1000000);                //Let's hypnotise begin 
  delay(1000);
}

void DataUpload()
{
  Blynk.virtualWrite(V11, millis() / 3600000);                   // Uptime Hours
  Blynk.virtualWrite(V12, millis() / 60000);                   // Uptime Minutes
  Blynk.virtualWrite(V13,readvdd33());                                // Voltage
}

void blink()                        // For indication blink while data uploading 

{
  digitalWrite(STROBE, HIGH);
  delay(200);
  digitalWrite(STROBE, LOW);
  delay(200);
  digitalWrite(STROBE, HIGH);
  delay(200);
  digitalWrite(STROBE, LOW);
  delay(3000);
  digitalWrite(STROBE, HIGH);
  delay(200);
  digitalWrite(STROBE, LOW);
  delay(200);
  digitalWrite(STROBE, HIGH);
  delay(200);
  digitalWrite(STROBE, LOW);
}

void loop()
{
  Blynk.run();
  timer.run();
  rssi=WiFi.RSSI();
  myip=WiFi.localIP();
}

BLYNK_WRITE(V6)                                                 // Wifi strength
{                                               // Some magic happens down there
 int pinData = param.asInt();
 if(pinData==1)
 {
   lcd.clear();
   lcd.print(0 ,0,"Wifi Strength");
   lcd.print(0 ,1,rssi);
 }else
 {
   lcd.clear();
 }
}

BLYNK_WRITE(V7)                                                    // IP Address
{
 int pinData = param.asInt();                        
 if(pinData==1)
 {
   lcd.clear();
   lcd.print(0 ,0,"IP ADDRESS:");
   String fullip = String(myip[0]) + "." + myip[1] + "." + myip[2] + "." + myip[3];
   lcd.print(0, 1, fullip);

 }else
 {
   lcd.clear();
 }
}

@elanozturk I am getting tired of saying it but don’t use delay and don’t put functions in your loop() other than the two “approved” functions.

delay(30000); after connecting to Blynk is an absolute no, no and tells your ESP you are not interested in the Blynk server, so Blynk server duly boots you off.

Repeat, lose all delays even for flashing LED’s. All you have to do is use Timers for everything e.g. LED high for 300ms, LED low for 300ms, no delays involved.

For deepSleep all you need to do is move:

  Serial.println("NodeMCU going to sleep...");
  system_deep_sleep(sleepTime * 1000000);                //Let's hypnotise begin 

from setup()

to the end of your DataUpload() function i.e send data to Blynk and then sleep. Note V6 and V7 will no longer function as you will be asleep for all but a few seconds per cycle.

Thank you @Costas for your help and guidance. i’ll work on it today :slight_smile:

I updated my sketch and everything is fine except one thing; deep sleep resets up time counter,when the device wake up it starts from 1. Have any idea to solve this?

#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";

const int SleepTime = 30;

const int ledPin =  D4;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 500;

extern "C" {
#include "user_interface.h"
uint16 readvdd33(void);
}

WidgetLCD lcd(V3);
long rssi;
IPAddress myip;
int Led1=D6;
int Led2=D7;

SimpleTimer timer;

void DataUpload()
{
  Blynk.virtualWrite(V11, millis() / 3600000);                   // Uptime Hours
  Blynk.virtualWrite(V12, millis() / 60000);                   // Uptime Minutes
  Blynk.virtualWrite(V13,readvdd33());                                // Voltage
  Serial.println("NodeMCU going to sleep...");
  system_deep_sleep(SleepTime * 1000000);                //Let's hypnotise begin
  delay(1000);
}

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  pinMode(ledPin, OUTPUT);
  unsigned long maxMillis=millis() + 8000;
  while ((Blynk.connect() == false) && ( millis() <= maxMillis)) {
  }
  myip = WiFi.localIP();
  Blynk.notify("NodeMCU Started!");
  timer.setInterval(60000L, DataUpload);
}

void loop()
{
  Blynk.run();
  timer.run();
  rssi=WiFi.RSSI();
  myip=WiFi.localIP();
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (ledState == LOW) {
      ledState = HIGH;
    } else {
      ledState = LOW;
    }
  digitalWrite(ledPin, ledState);
  }
}

BLYNK_WRITE(V6)                                                 // Wifi strength
{
 int pinData = param.asInt();
 if(pinData==1)
 {
   lcd.clear();
   lcd.print(0 ,0,"Wifi Strength");
   lcd.print(0 ,1,rssi);
 }else
 {
   lcd.clear();
 }
}

BLYNK_WRITE(V7)                                                    // IP Address
{
 int pinData = param.asInt();
 if(pinData==1)
 {
   lcd.clear();
   lcd.print(0 ,0,"IP ADDRESS:");
   String fullip = String(myip[0]) + "." + myip[1] + "." + myip[2] + "." + myip[3];
   lcd.print(0, 1, fullip);

 }else
 {
   lcd.clear();
 }
}

Today,i saw this video
https://www.youtube.com/shared?ci=72kIb1SPB-U
I think this is great idea for buttons &battery operated devices.instead of deep sleep.deep sleep consume more power than this idea.
I hope this will be helpful.

1 Like

According to my knowledge esp(nodemcu) doesn’t do any work while deep sleep.

Blynk has this well covered. Just save any variable you need to the server and sync it back on reboot.
You can do the same with EEPROM (RTC area of ESP’s) and the Filesystem (up to 15MB) of some ESP’s but sync is by far the easiest way.

Thank you @Costas, dealing with the eeprom is too much for me right now,i’ll check the sync option.

thanks for sharing this!

what do you think, how long has the button to be pressed before the relay pin goes high?

I haven’t tried yet.
I think esp will boots up very fast & it will turn on relay within fraction of seconds. Declare gpio ON on the start of setup.
Let me know the results.

thanks. i will keep you updated when i will try.

If you have pins avaliable you could always turn on a led as your second action after turning on the transistor for Power, then switch of the led after half a second, that will give you a proper indication that it is powered without your button press. At a minimum battery cost

true.
but i’m asking this, because in my application, the wemos has to turn the pin high,
in just the time sequence as the magnet on the wheel will pass by the reed relay (very short time i think).

i do not want to have a separate button just to turn on the system, it should be automatic.

Then you need to work in a big enough capacitor to give you power when the magnet is in range.

Check this video for ideas. I’m not that good in electronics so I can only simply point you in the right direction I suppose

Replace his manual contact with your magnetic circuit. Might do the trick

hello @saurabh47 !

here is the solution i came up, if interested:

2 Likes