Trying to put esp8266 to sleep

hi,
im trying to make a simle sketch where im putting the esp board in sleep mode after sending some data from a senzor.
It seems to get stuck in the while loop and i don’t know how to fix it.
here’s the code:

#define BLYNK_TEMPLATE_ID "TMPLlrSk5bl7"
#define BLYNK_DEVICE_NAME "SICD"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#define USE_NODE_MCU_BOARD

#include "BlynkEdgent.h"


#define trigPin 4
#define echoPin 5

int Distance()
{
  long duration;
  int distanceMeasured;
  digitalWrite(trigPin, LOW);
  delay(1000);

  //send a signal for 10 seconds from trigPin
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  //measuring the distance from the senzor to the object
  duration = pulseIn(echoPin, HIGH);
  distanceMeasured = duration*0.034/2;

  return distanceMeasured;
}
    
void setup()
{
  delay(500);
  pinMode(13, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  delay(500);
  Serial.begin(9600);
  delay(100);
  
  BlynkEdgent.begin();
  BlynkEdgent.run();
  while(Blynk.connected() != true){
  Serial.println("x");
  }
  Blynk.virtualWrite(V0, Distance());
  //ESP.deepSleep(10* 1000000);
}

void loop() {

}
   ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP8266

[1442] --------------------------
[1478] Product:  SICD
[1502] Firmware: 0.1.0 (build Mar 21 2022 22:09:25)
[1557] Device:   ESP8266 @ 80MHz
[1593] MAC:      30:83:98:A6:E4:7F
[1630] Flash:    4096K
[1655] ESP core: 3.0.2
[1680] ESP SDK:  2.2.2-dev(38a443e)
[1719] Boot Ver: 31
[1741] Boot Mode:1
[1762] FW info:  468560/1626112, MD5:190753ecdad5e7f4361878528ebe64a4
[1974] Free mem: 31240
[1974] --------------------------
[1975] INIT => WAIT_CONFIG
[2607] AP SSID: Blynk SICD-87AC5
[2607] AP IP:   ***.***.***.***
[2608] AP URL:  blynk.setup
[22174] WAIT_CONFIG => CONFIGURING
[24693] Sending board info...
[35063] Applying configuration...
[35063] WiFi SSID: tenda Pass: ****
[35063] Blynk cloud: icTI_ZsnjLIwZwJEdOTZTmY_f_ApAfNd @ blynk.cloud:443
[35089] CONFIGURING => SWITCH_TO_STA
x
x
x
x
x
x

Why are you using the Edgent example sketch as the starting point for this project?

Pete.

What should I use then

Explain more about what it is that you are trying to achieve with this project and why you’re using deep sleep.

Pete.

I think you need a bit more here like the call to Blynk and the Timer.

I’m want to make a senzor that detects how full a container is, but right know I just want to send the data from the senzor(HC-SR04) over to blynk. I need to put it on deep sleep because it’ll be powered by batteries.

After esp.deepsleep() is called and the timer reaches the end it should restart from the beginning of the setup function. So I don’t think I need anything in the loop() function from what I understand

It won’t work if it is not in the void loop.

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

It should look like this if you are using edgent

or like this

void loop()
{
  if (Blynk.connected()) {
    Blynk.run();
  } else Blynk.connect(1000l);                // timeout 1 seconds
  timer.run();
}

If not

This is a good example to start with…

It’s written for Blynk Legacy, so the blynk_server would need to be changed to blynk.cloud and it would be a good idea to put the Template ID and Device Name at the very beginning of of the sketch.

Pete.

this is the updated version of the code

#define BLYNK_TEMPLATE_ID "TMPLlrSk5bl7"
#define BLYNK_DEVICE_NAME "SICD"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#define USE_NODE_MCU_BOARD

#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP 10

//#include <WiFi.h>
//#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>

#define trigPin 4
#define echoPin 5

int Distance()
{
  long duration;
  int distanceMeasured;
  digitalWrite(trigPin, LOW);
  delay(1000);

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distanceMeasured = duration*0.034/2;

  return distanceMeasured;
}

//RTC_DATA_ATTR int bootCount = 0;

const char auth[] =            "***";
const char blynk_server [] =   "blynk.cloud";    // new variable to hold the name of the Blynk server
const int blynk_port =         443;                      // new variable to hold the port used by the Blynk server

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

//int wifi_connect_count = 0;          // New variable to keep track of how manty times we've tried to connect to the Wi-Fi
//int wifi_connect_max_retries = 50;   // New variable to specify how many attempts we will have at connecting to the Wi-Fi
//
//float sleep_time_minutes =     1;   // New variable - how long (in minutes) we sleep for. As this is a float variable then it can be 0.5 for a 30 second test

void WiFi_Connect() // New functon to handle the connectuon to the Wi-Fi network
{
  Serial.println(F("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, pass); // connect to the network
  }

  while(WiFi.status() != WL_CONNECTED){
  //wait to connect
  Serial.println("connnecting to wifi");  
  }
  if (WiFi.status() == WL_CONNECTED)
  {
    WiFi.mode(WIFI_STA);
    Serial.println(F("Wi-Fi CONNECTED"));
    Serial.println();
  }
} // End of void WiFi_Connect

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // New section of code - stop using Blynk.begin, which is a blocking function, and instead do the following:
  //
  // 1) Attempt to connect to Wi-Fi a few times (how many times we try is specified by the 'wifi_connect_max_retries' variable)
  // 2) If we successfully connected to Wi-Fi then attempt to connect to Blynk in a non-blocking way. If we aren't connected to Wi-Fi then go to sleep
  // 3) If we connected to Blynk then run the rest of the code as normal. If we aren't connected to Blynk then go to sleep
  
  //  Blynk.begin(auth, ssid, pass);//starts wifi and Blynk - Not used in the new code as it's a blocking function
  WiFi_Connect(); // 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
  {
    Blynk.config(auth, blynk_server, blynk_port);  // Initialise the Blynk connection settings
    Blynk.connect();                               // Attempt to connect to Blynk
  }
  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();
  }
  
  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 * 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("Blynk connection failed - going to sleep");
    Deep_Sleep_Now();
  }
  delay(2000);
}

void loop()
{
  delay(2000);
  Blynk.virtualWrite(V0, Distance());

  Blynk.run(); // Needed to ensure that the Wake Time value is always uploaded to Blynk before going to sleep
  delay(100);
  
  Deep_Sleep_Now();
}

void Deep_Sleep_Now() // New function - moded code out of void loop so that the sleep function can be called if we fail to connect to Wi-Fi or Blynk
{
//  esp_sleep_enable_timer_wakeup((uint64_t)(TIME_TO_SLEEP) * uS_TO_S_FACTOR);
//  esp_deep_sleep_start();

  ESP.deepSleep((uint64_t)(TIME_TO_SLEEP) * uS_TO_S_FACTOR);
  delay(2000);
}

and this is displayed on the terminal:

[14326] connected
[14345] connected
[14365] connected
[19327] Connecting to domain blynk.cloud:443
[19361] connected
[19361] connected
[19361] connected
[19361] connected
[19362] connected
[19362] connected
[19362] connected
[19381] connected
[19401] connected
[19420] connected
Blynk connection failed - going to sleep

i cant figure out why it doesn’t connect to blynk

Seeing all of your serial monitor output, not just the last part, would be useful.

Pete.

connnecting to wifi
connnecting to wifi
Wi-Fi CONNECTED

[4031] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.1 on ESP8266

[4186] Connecting to domain blynk.cloud:443
[4273] <[1D|00|01|00] 0dq7yfqu7oLnbUFU5olGlwUbYpRMiszC
[4291] connected
[4310] connected
[4329] >[15|03|01|00|02]
[4356] >[02]F
[4372] Invalid header type: 21
[9292] Connecting to domain blynk.cloud:443
[9327] <[1D|00|01|00] 0dq7yfqu7oLnbUFU5olGlwUbYpRMiszC
[9328] connected
[9328] connected
[9328] connected
[9328] connected
[9340] connected
[9358] connected
[9377] >[15|03|01|00|02]
[9404] >[02]F
[9420] Invalid header type: 21
[14329] Connecting to domain blynk.cloud:443
[14367] <[1D|00|01|00] 0dq7yfqu7oLnbUFU5olGlwUbYpRMiszC
[14368] connected
[14368] connected
[14368] connected
[14368] connected
[14383] connected
[14403] connected
[14422] >[15|03|01|00|02]
[14450] >[02]F
[14467] Invalid header type: 21
[19369] Connecting to domain blynk.cloud:443
[19410] <[1D|00|01|00] 0dq7yfqu7oLnbUFU5olGlwUbYpRMiszC
[19410] connected
[19411] connected
[19411] connected
[19411] connected
[19423] connected
[19443] connected
[19463] >[15|03|01|00|02]
[19490] >[02]F
[19507] Invalid header type: 21
Blynk connection failed - going to sleep

Okay, if you’re going to use port 443 then you also need to use #include <BlynkSimpleEsp8266_SSL.h>

If you want to stick with #include <BlynkSimpleEsp8266.h> then switch to port 80

Pete.

That solved it. Thanks a lot Peter

1 Like