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

LOLIN32 Deep Sleep mode - Not posting

I am acquiring temperature and posting to Blynk using LoLin32 board + Arduino code. Simple code but it does not seem to post as the Superchart does not update… Blynk is configured for Push Type. The builtIn LED lights up every 15 secs and so I know the code is firing after deep sleep. But why is it not posting the value ?

#include "RunningAverage.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define ADC_Ch3 39

#define uS_TO_S_FACTOR 1000000               /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  15                    /* Time ESP32 will go to sleep (in seconds) */


int adcRawBits = 0;
int adcMeanBits = 0;
int raSampleSize = 50;
double DegC ;
// Use Virtual pin 0 for temperature value in Blynk App
#define ambientTemp V0


byte flashLED  = 5;
bool ledState;

RunningAverage ra_Ch3(raSampleSize);

char auth[] = "eb8ade88cefa468f9f6974600e9a0dfa";
char ssid[] = "RR_DLINK_2.4";
char pass[] = "@act2.4ragu1957";

//#################################

void setup()
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("ESP32 LM35 Read and Post to Blynk.");

  Blynk.begin(auth, ssid, pass);

  pinMode( flashLED, OUTPUT);
  digitalWrite(flashLED, LOW);

  Blynk.run();

  readAdcPin();                   // Read the ADC pin and average it ...
  getDegCValue();
  Blynk.virtualWrite(ambientTemp, DegC);

  digitalWrite(flashLED, HIGH); 
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  esp_deep_sleep_start();
}

//#################################

void loop()
{
  // Nothing here !
}

//#################################

void readAdcPin()
{
  for (byte count = 0; count < 100; count++)
  {
    adcRawBits = analogRead(ADC_Ch3);
    ra_Ch3.addValue(adcRawBits);
    adcMeanBits = ra_Ch3.getAverage();
    delay(5);
  }
}

//*********************************

void getDegCValue()
{
  DegC = getVoltage(adcMeanBits) * 100;
}

//*********************************

double getVoltage(int count)
{
  double reading = count;          // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
  if (reading < 1 || reading > 4095) return 0;
  return -0.000000000000016 * pow(reading, 4) + 0.000000000118171 * pow(reading, 3) - 0.000000301211691 * pow(reading, 2) + 0.001109019271794 * reading + 0.034143524634089;
}

This won’t be processed until the code encounters the next Blynk.run(); command, but as there isn’t one before the deep sleep command it never happens.
I’d suggest adding a Blynk.run(); and a short delay before your sleep command.

Also, because you’re using Blynk.begin, which is a blocking command, if the device fails to connect to either Wi-Fi or the Blynk server it will never go back to sleep again.

Pete.

Thanks PeteKnight.

I did as you mentioned and with a delay of 500ms before going to sleep. OK its working.

But I am not clear about how to overcome the blocking Blynk.run()… what is the standard method to overcome that ?

It’s Blynk.begin that’s the blocking function.
Using Blynk.config and Blynk.connect, after establishing a WiFi connection is the best approach.

Take a look at this code:

Pete.

2 Likes

Thanks Pete.

All well now… its been running since yesterday night and working well … just to check if it tries to recover switched off the router for some time. It recovered nicely .

:star_struck:

1 Like

Just one point to clarify …Refer the code below …

  if (Blynk.connected())                          // If we manages to connect to Blynk then carry-on as normal, else go to sleep
  {
    Serial.println ("Connected to Blynk");
  }
  else
  {
    sleep_time_minutes = sleep_time_minutes * 0.5; // If you enable this line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal
    Serial.println("Blynk connection failed - going to sleep");
    deepSleepNow();
  }

And deepSleepNow() function …

void deepSleepNow() {
  digitalWrite(flashLED, HIGH);
  esp_sleep_enable_timer_wakeup((uint64_t)(TIME_TO_SLEEP) * uS_TO_S_FACTOR);
  esp_deep_sleep_start();
}

But I don’t see where the variable “sleep_time_minutes” used in the function deepSleepNow(). Is it part of the Blynk API ?

You’re right!
This was a bit of a collaboration project via email and it looks like some of my code didn’t make it into the final version.
My original version of deepSleepNow() looked like this…

void Deep_Sleep_Now()
{
  Serial.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Going to sleep for ");
  Serial.print(sleep_time_minutes);  
  Serial.println(" minutes");   
  Serial.println();
  ESP.deepSleep(sleep_time_minutes * 60000000);  // Deep Sleep time is specified in micros
  delay(2000);
}

and of course sleep_time_minutes = sleep_time_minutes * 2; is needed to make it sleep for twice as long if no connection, sleep_time_minutes = sleep_time_minutes * 0.5; to make it re-try again sooner than normal.

This was intended to give some flexibility based on whether your readings were more important than saving battery life. If you think the scenario is that that Wi-Fi or internet connection being down is something that will fix itself quite quickly then repeating after 0.5x the normal sleep time is probably a good idea. If you think manual intervention is required to get the fault fixed and you just want to save battery life in the meantime then 2x (or more) is probably a better setting.

Pete.

Excellent idea.

Thanks for the clarifications. And I am a bit curious about the "delay(2000) " after the ESP.deepSleep() … does it get executed at all ?

It’s recommended to have a delay after deepSleep. Without it, it is possible that code execution continues and the device doesn’t actually sleep.

Pete.

:+1:

1 Like

Hi Pete,
I have a code that runs perfectly with the Blynk.begin, on an ESP8266 taken from the example EDGENT_xx
I implemented all the functions that you supplied.
The problem that I am facing now is, I am getting a compiler error:
Error compiling for board NodeMCU 1.0 (ESP-12E Module).
I saw in the code the BlynkSimpleEsp32.h and replaced it with the BlynkSimpleEsp8266.h I downloaded from Github, but this did not help.
Before digging into it, any hint where I should look?

My target is to send once a day sensor data to the cloud then go to sleep for a battery supplied application (water tank SRT04 sensor)
Thanks for any links and direction
Y3G

Why are you using Edgent?

Pete.

Hi Pete,
You are right, took me some “readings” to understand. The Edgent was a evolution from the first example, now I have patched yours with my beginners one :wink:
BUT my DASHBOARD is not updating…everything works on the Serial

NOW, after this weekend I took over your
Deep_Sleep_Now(float sleep_time_minutes)
void WiFi_Connect(ssid, passw, wifi_connect_max_retries),
also the Case structure for the Wifi connect which is in the SETUP (all yours)

Within the Setup and the case structure you have
Blynk.config(auth); // Initialise the Blynk connection settings → works fine Blynk.config(auth," blynk,cloud", 443);–> does not connect
Blynk.connect(); → works only with first Blynk.config

See the BOLD in the serial ouput for the two cases:

8:52:36.823 →
18:56:09.420 → Wi-Fi connection - attempt number 1
18:56:09.904 → Wi-Fi connection - attempt number 2
18:56:10.384 → Wi-Fi connection - attempt number 3
18:56:10.901 → Wi-Fi connection - attempt number 4
18:56:11.386 → Wi-Fi connection - attempt number 5
18:56:12.666 → Wi-Fi connection - attempt number 6
18:56:13.199 → Wi-Fi connection - attempt number 7
18:56:13.681 → Wi-Fi connection - attempt number 8
18:56:14.163 → Wi-Fi connection - attempt number 9
18:56:14.683 → Wi-Fi connection - attempt number 10
18:56:15.176 → Wi-Fi connection - attempt number 11
18:56:15.176 → Wi-Fi CONNECTED
18:56:15.176 →
18:56:15.176 → YS:If we managed to connect to Wi-Fi then try to connect to Blynk, else go to sleep
18:56:15.176 → [6440]
18:56:15.176 → ___ __ __
18:56:15.216 → / _ )/ /_ _____ / /__
18:56:15.216 → / _ / / // / _ / '/
18:56:15.216 → /
//_, /////_
18:56:15.216 → /
/ v1.0.0-beta.3 on NodeMCU
18:56:15.216 →
18:56:15.216 → [6454]Connecting to blynk.cloud:443
18:56:20.427 → [11698] Connecting to blynk.cloud:443
18:56:25.650 → [16916] Connecting to blynk.cloud:443
18:56:30.865 → [22133] Connecting to blynk.cloud:443
18:56:33.182 → YS:Succeed to return from Blynk.Connect()
18:56:33.222 → FALSE from Blynk.connected(), failed - going to sleep
18:56:33.222 → >>>>>>>>>>>>>>>>>>>>> Going to sleep for 1.00 minutes
18:56:33.222 →
18:57:48.632 → Wi-Fi connection - attempt number 1
18:57:49.117 → Wi-Fi connection - attempt number 2
18:57:49.614 → Wi-Fi connection - attempt number 3
18:57:50.139 → Wi-Fi connection - attempt number 4
18:57:50.622 → Wi-Fi connection - attempt number 5
18:57:51.904 → Wi-Fi connection - attempt number 6
18:57:52.397 → Wi-Fi connection - attempt number 7
18:57:52.917 → Wi-Fi connection - attempt number 8
18:57:53.411 → Wi-Fi connection - attempt number 9
18:57:53.902 → Wi-Fi connection - attempt number 10
18:57:54.418 → Wi-Fi connection - attempt number 11
18:57:54.418 → Wi-Fi CONNECTED
18:57:54.418 →
18:57:54.418 → YS:If we managed to connect to Wi-Fi then try to connect to Blynk, else go to sleep
18:57:54.418 → [6439]
18:57:54.418 → ___ __ __
18:57:54.418 → / _ )/ /
_____ / /
_
18:57:54.418 → / _ / / // / _ / '/
18:57:54.418 → /
//_, /////_
18:57:54.418 → /
__/ v1.0.0-beta.3 on NodeMCU
18:57:54.418 →
18:57:54.418 → [6453] Connecting to blynk.cloud:80
18:57:54.858 → [6910] Ready (ping: 220ms).
18:57:55.018 → YS:Succeed to return from Blynk.Connect()
18:57:55.018 → YS:On Setup(), TRUE from Blynk.connected()
18:57:55.018 → Boot number: 1
18:57:55.018 → Wake Time = 0.06 seconds
18:57:55.059 → A0 read → 13.63
18:57:55.139 → RSSI:-80.00
18:57:55.219 → 28.94ºC
18:57:55.219 → Begin SRF
18:57:55.259 → distance [cm]–>: 160
18:57:55.259 → end Iterations: 0
18:57:55.379 → >>>>>>>>>>>>>>>>>>>>> Going to sleep for 10.00 minutes

#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPLRwKDdSO8"
#define BLYNK_DEVICE_NAME "ESP8266"
#include <BlynkSimpleEsp8266.h>
//#define APP_DEBUG

// -----------------------------Global Variables ---------------------------------
#define TRIGGER 13 // NodeMCU Pin D7=io13 for SFR
#define ECHO    14 // NodeMCU Pin D5=io14  for SFR
//#define TEMP    12 // NodeMCU Pin D6  for TempSensor
const int TEMP = 12; 
#include <OneWire.h>           // for Temp Diode
#include <DallasTemperature.h>  // for Temp Diode

OneWire oneWire(TEMP); // TEMP=Temperature=io 12 Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature sensor 

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-";
char ssid[] ="ssid";
char passw[] ="passw";
float wake_time = (float)millis()/float(1000); // Find out how long since the ESP rebooted
int bootCount = 0;
float sleep_time_minutes =0.5;
int CounterIn = 0;
int CounterOut = 0;

void setup()
{
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(TEMP, INPUT); //Temp Sensor 
  pinMode(4,OUTPUT);  // Led Output switch
  Serial.begin(115200);
  sensors.begin();   // Start the DS18B20 sensor 
     
  WiFi_Connect(ssid,passw,50); // Attempt to connect to Wi-Fi

  if (WiFi.status() == WL_CONNECTED)               // If we managed to connect to Wi-Fi then try to connect to Blynk, else go to sleep
  {
    Serial.println("YS:If we managed to connect to Wi-Fi then try to connect to Blynk, else go to sleep");
    Blynk.config(auth);  // Initialise the Blynk connection settings
    //Blynk.config(auth, blynk_server, blynk_port);
    Blynk.connect();                               // Attempt to connect to Blynk
    Serial.println("YS:Succeed to return from Blynk.Connect()");
  }
  else
  {
    Serial.println ("Wi-Fi connection failed - going to sleep");
    sleep_time_minutes = sleep_time_minutes * 2; // If you enable this line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal
    Deep_Sleep_Now(sleep_time_minutes);
  }
if (Blynk.connected()) //= is Boolean TRUE, If we manages to connect to Blynk then carry-on as normal, else go to sleep
  {  
    Serial.println ("YS:On Setup(), TRUE from  Blynk.connected()");
  }
  else
  {  
    sleep_time_minutes = sleep_time_minutes * 2; // If you enable this line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal
    Serial.println("FALSE from Blynk.connected(), failed - going to sleep");
    Deep_Sleep_Now(sleep_time_minutes);
  }
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount)); 
 // the above is instead the Blynk.begin(auth,ssid ,passw );
}


void loop()
{
  Serial.print("Wake Time = ");
  Serial.print(wake_time);
  Serial.println(" seconds");
  BatteryLevel();
  SignalStrenght();
  TempValue();
  SensorValue();
    
  Blynk.run(); // Needed to ensure that the Wake Time value is always uploaded to Blynk before going to sleep
  delay(100);
  //digitalWrite (transistorPin, LOW);  // turn off the supply
  Deep_Sleep_Now(10);
  
}

BLYNK_CONNECTED() //sets the LED button to OFF at beginning
{
  Blynk.syncVirtual(V0);  // will cause BLYNK_WRITE(V0) to be executed
}
////-------------------------------------------------  LED Switch On/Off on GPIO 4/D2--------------------------------------------------------
BLYNK_WRITE(V0) // Executes when the value of virtual pin 0 changes
{
  if(param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    digitalWrite(4,HIGH);  // Set digital pin 2 HIGH
    Serial.println("Set digital pin D2 HIGH");
  }
  else
  {
    // execute this code if the switch widget is now OFF
    digitalWrite(4,LOW);  // Set digital pin 2 LOW
    Serial.println("Set digital pin D2 LOW");    
  }
}

//---------------------------------------------------- ANALOG READ Battery ---------------------------------------------

void BatteryLevel()
{
  int sensorPin = A0;    // select the input pin for the potentiometer
  double sensorValue = 0;  // variable to store the value coming from the sensor
  double VoltageRead = 0;

  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  
  //  MCU has 10 bits precision: 2^10= 1024 --> 1-1024 values where 3.3 V ~ 1024 (board!) (chip only to 1V). 
  VoltageRead= sensorValue/1024 *13.63;
  Blynk.virtualWrite(V4,VoltageRead);
  Serial.print("A0 read --> ");
  Serial.println(VoltageRead);
}
//------------------------------------------------- Sensor  Function ------------------------------------------------------------  
// Function that does the SFR Sensing
void SensorValue(){
  long duration;
  int distance;
  Serial.println("Begin SRF");
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(5); 

  // Trigger the sensor by setting the trigPin high for 10 microseconds:
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10); 
  digitalWrite(TRIGGER, LOW);
  
  // Read the echoPin. pulseIn() returns the duration (length of the pulse) in microseconds:
   duration = pulseIn(ECHO, HIGH);
  
  // Calculate the distance:
  distance = duration*0.034/2;
  //distance = (duration/2) / 29.1;
  if (distance>0)
  {
    Blynk.virtualWrite(V3,distance); // sends the "distance" value to virtualPin V3
    Serial.print("distance [cm]-->: ");
    Serial.println(distance);
  }
  // Print the distance on the Serial Monitor (Ctrl+Shift+M):
 
  //Serial.print("duration is the ECHO high :");
 // Serial.println(duration);
  
  
  Serial.print("end Iterations: ");
  Serial.println(CounterIn);
  CounterIn=CounterIn+1;
  
}
//------------------------------------------------- Temperature Function ------------------------------------------------------------  
void TempValue()
{
  sensors.requestTemperatures(); 
  float temperatureC = sensors.getTempCByIndex(0);
  if (temperatureC > -127) 
  {
    Blynk.virtualWrite(V1,temperatureC);
  }
 Serial.print(temperatureC);
 Serial.println("ºC");
}
//------------------------------------------------------ RSSI-----------------------------------------
void SignalStrenght()
 {
  float rssi = WiFi.RSSI();
  Blynk.virtualWrite(V2,rssi);
  Serial.print("RSSI:");
  Serial.println(rssi);
}
/*---------------------------------DEEP SLEEP -----------------------------
 * Function that performs deep sleep
 * INPUT: Time to sleep in min
 * 
 * ----------------------------------------------------------------
 */


void Deep_Sleep_Now(float sleep_time_minutes)
{
  
  Serial.print(">>>>>>>>>>>>>>>>>>>>> Going to sleep for ");
  Serial.print(sleep_time_minutes);  
  Serial.println(" minutes");   
  Serial.println();
  ESP.deepSleep(sleep_time_minutes *60e6);  // Deep Sleep time is specified in micros
 
  delay(2000);
}

/*--------------------------------------------------------------
 * Function that performs Wifi access tries
 * INPUT: SSID - name in string
 *        PASSWORD in String
 *        Time of retries in INT
 *        
 * GLOBAL PARAMETERS : Boolean WiFi.status() != WL_CONNECTED)
 * need to #include <BlynkSimpleEsp8266.h>           
 * ----------------------------------------------------------------
 */

void WiFi_Connect(char ssid[],char passw[],int wifi_connect_max_retries) // New functon to handle the connectuon to the Wi-Fi network
{

  int wifi_connect_count = 0;          // New variable to keep track of how manty times we've tried to connect to the Wi-Fi

  
  Serial.println(F("Printout from function Wifi_Connect(): Connecting to Wi-Fi"));
  //WiFi.config(device_ip, dns, gateway, subnet); // Not needed if you just want to have a DHCP assigned IP address. If you diont use this then delete the device_ip, dns, gateway & subnet declarations
    
  if (WiFi.status() != WL_CONNECTED)
  {
      WiFi.begin(ssid, passw); // connect to the network
  }
  while (WiFi.status() != WL_CONNECTED  && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attemps allowed
  {
    delay(500);
    wifi_connect_count++;   
    Serial.print(F("Wi-Fi connection - attempt number "));
    Serial.println(wifi_connect_count);
  }
  
  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.mode(WIFI_STA);
    Serial.println(F("Wi-Fi CONNECTED"));
    Serial.println();
  }
} //------------------------------------------ End of void WiFi_Connect trial




Web console or app dashboard?

You have a comma rather than a decimal point. Should be blynk.cloud but it seems you had it correct in your code?

You probably need to specify the and of your server, as described here…
https://docs.blynk.io/en/blynk.cloud/troubleshooting

This is a very old library, you should be using 1.0.1

Pete.

1 Like

I’m played with such code and found that it is not reliable. Sometimes data were lost. I’m guess the reason can be some period that blynk server need to work with data - they does not processes immediately. Plus ping here is also not optimal. So, I’m added few seconds to let the server to do it’s job properly. Now everything seems to be OK for 200 minutes cycle. This is my template for battery powered ESP32 device with deep sleep function for future experiments. Watchdog timer is also involved to prevent WiFi connection endless loop (got it few times).

Here I’m just sending counter value to Blynk and on the next cycle checking is that value at server is correspondig to the value on device. If some data is missed, I’ll get error message. Error value normally should be equal to 1 due to the first cycle, or just a piece of code can be added to avoid the very first boot error.

#define BLYNK_PRINT Serial

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID "TMPL***"
#define BLYNK_DEVICE_NAME "ESP32"
char auth[] = "***";

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Lair";
char pass[] = "***";

#include <esp_task_wdt.h>
//90 seconds WDT
#define WDT_TIMEOUT 90

BlynkTimer timer;


//deep sleep

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  60        /* Time ESP32 will go to sleep (in seconds) */

RTC_DATA_ATTR int bootCount = 0;
RTC_DATA_ATTR int errors = 0;



BLYNK_CONNECTED() 
{
  Blynk.syncVirtual(V2); //get counter data back from server
  Serial.println("Synchro");
}

BLYNK_WRITE(V2)
{
  int oldValue = param.asInt(); // assigning incoming value from pin V2 to a variable
  if (oldValue == bootCount - 1 ) Serial.println("Server data OK"); //checking data at server and device
  else 
  {
  Serial.println("Server data missed");
  ++errors;
  Blynk.virtualWrite(V3, errors); //fill label with int data
  }
}


void send_data() //send sensor and other data here
{
Blynk.virtualWrite(V1,"Boot number: " + String(bootCount)); //fill label with string data
Blynk.virtualWrite(V2, bootCount); //fill chart with int data
Serial.println("Data sent to blynk");
}

void goto_sleep() //deep sleep procedure
{
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
delay(2000);
Serial.println("This will never be printed");  
}

void setup()
{
  esp_task_wdt_init(WDT_TIMEOUT, true); //enable panic so ESP32 restarts
  esp_task_wdt_add(NULL); //add current thread to WDT watch
  
  // Debug console
  Serial.begin(115200);
  delay(1000);
    
  Blynk.begin(auth, ssid, pass);
 
  ++bootCount;
  Serial.println("Boot number: " + String(bootCount));

  /*
  First we configure the wake up source
  We set our ESP32 to wake up every 60 seconds
  */
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
  " Seconds");
 
//timer values can be optimized  
 timer.setTimeout(2000, send_data); //send data in 2 seconds after start
 timer.setTimeout(5000, goto_sleep); //involve Blynk.run() wizard and goto sleep in 5 secods after start
}

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

So, the result is:

[1028] Connecting to Lair
[4065] Connected to WiFi
[4065] IP: 192.168.1.110
[4066] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP32

[4072] Connecting to blynk.cloud:80
[4523] Ready (ping: 203ms).
Synchro
Boot number: 2
Setup ESP32 to sleep for every 60 Seconds
Server data OK
Data sent to blynk
Going to sleep now

Hi Pete,
Thanks. The change of the version did the change.
I will run it for several days and let this forum know about the outcome of data to Blynk-Cloud.

1 Like

Hi Mogaraghu,
I think we have the same project going on.
Can yo tell me how you’d switched the the power supply on an external sensor?
I have to supply it with 3.3 V and it draws 110 mA. When going into sleep mode I can see the LED is still flashing and my sleep mode stays at 4mA.
When removing the sensor, the sleep mode shows the 8 uA.
Thanks for any hints

Ok I know that cutting of supply to sensor when MCU goes to sleep saves lots of power. Many options with MOSFET switches are there but the one I have used in some projects is this : Polulu Power Switch Check out the options and you will find one to suiit !