ESP32 deepsleep unsure

Thomas_01
Posts: 5
Joined: Tue Dec 31, 2019 9:44 am

ESP32 deepsleep unsure

Postby Thomas_01 » Fri Jan 10, 2020 3:13 pm

Hallo,
I'm trying to use the esp32 with WIFI and deepsleep with a Battery 18650. It works very unsure. Sometimes it works 20minutes, sometime it works 4 hours.
I tried all and I have written a little testprogram. Now my question: Is in the program an error or what can I do.
I use the 18650 only. with no capacitor or other things. I can see, WIFI uses many power. But I believe the battery delivers power enough - or not?

Code: Select all

/*
 *  This sketch demonstrates how to scan WiFi networks.
 *  Then I connect my own WIFI and after success I disconnect
 *  now deepsleep
 *  ========================================================================
 *  
 */
#include "WiFi.h"

#define LED_YELLOW 22   // external LED
#define LED_BLUE 2 // internal blue LED
#define uS_TO_S_FACTOR 1000000  

int x;

IPAddress ip(192, 168, 2, 55);  // static IP  
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 2, 1);
const char* SSID = "wirelesskabel";
const char* PSK = "pwd";

void setup()
{
    Serial.begin(115200);

    // Set WiFi to station mode 
    WiFi.mode(WIFI_STA);
    delay(100);
    
    pinMode(LED_BLUE,OUTPUT);
    pinMode(LED_YELLOW,OUTPUT);
    
    Serial.println("Setup done");

}

// show short blinks(gpio, count, led_on,led_off in ms))
void blink_led (byte gpionr,byte count, int led_on ,int led_off) {
  
  for (byte x=0; x<count; x++) {
    digitalWrite(gpionr, HIGH); // set led on 
    delay(led_on);
    digitalWrite(gpionr, LOW); // set led off
    delay(led_off);
  }
}

void sleep_now(int sleeptime){
  esp_sleep_enable_timer_wakeup(sleeptime * uS_TO_S_FACTOR );   //(sec * uS_TO_S_FACTOR);
  esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON); // siehe https://esp32.com/viewtopic.php?t=7045                                                        
  delay(50);                                                                   
  esp_deep_sleep_start(); // sleep_now ESP32
}

void loop()
{
    Serial.println("scan start");

    // WiFi.scanNetworks will return the number of networks found
    int n = WiFi.scanNetworks();
    if (n == 0) {
        Serial.println("no networks found");
        blink_led(LED_YELLOW,3,100,100);
        delay(100);
        sleep_now(5); // deepsleep  ESP32 for 5 s  because no Wifi found        
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        for (int i = 0; i < n; ++i) {
            // Print SSID and RSSI for each network found
            Serial.print("                    ");Serial.print(i + 1);
            Serial.print(": ");
            Serial.print(WiFi.SSID(i));
            Serial.print(" (");
            Serial.print(WiFi.RSSI(i));
            Serial.print(")");
            Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
            delay(10);          
        }
    }
    Serial.println("");

    // connecting to network "wirelesskabel"
    WiFi.mode(WIFI_STA);
    WiFi.config(ip,  gateway, subnet,dns,dns);   // feste IP dem ESP32 geben   - statisch 
    Serial.print("Wifi via fixed IP  Connecting to ");
    Serial.print(SSID);
    Serial.print("   IP: ");Serial.println(ip); 

    WiFi.begin(SSID, PSK);
    
    delay(100);
    Serial.println("after Wifi.begin");
    
    x=0;  //counter of trying connections
    
    while (WiFi.status() != WL_CONNECTED)  
    {
       
        delay(50);  
        
        x++;
       
        blink_led(LED_BLUE,1,200,100);      // Blinkzyklus: erst ON, dann OFF 
      
        if (x>10)
        {
          // max numbers wifi-connect exceeded  - WIFI-ERROR
          Serial.print("Wifi-Connect Error...");
          WiFi.disconnect(); 
          delay(50);
          sleep_now(5); // deepsleep  ESP32 for 5 s          
       }
    }
    
    if (  WL_CONNECTED ) {
      Serial.println("");
      Serial.println("Wifi-Connect OK, now disconnect ...");
      delay(1000);
      blink_led(LED_YELLOW,1,1000,100);
      WiFi.disconnect(); 
      Serial.println("OK: Wifi-disconnected, deepsleep now for 20s ----------------------");
      Serial.println("");
      delay(100);
    }
    else
      // normally I come never to this point
      blink_led(LED_BLUE,5,600,400);  // Error Blinken
    
    sleep_now(20); // normal deepsleep ESP32 for 20s
    // end 
}
There are many delay() I believe we don't need. This are "anxiety"-delays.

My target is, running the ESP32 with a battery months and more (i believed in the low power consumption in deepsleep). Today I believe: the power consumption with wifi is to high (connecting to WIFI every 20 minutes) and the deepsleep is unsure, because it doesn't work really. Nowhere I can find an example with practical experiences for such a function.

what are your experiences ?

Many thanks
Thomas

Who is online

Users browsing this forum: No registered users and 47 guests