Deepsleep only works sometimes

LMolenkamp
Posts: 2
Joined: Thu Feb 20, 2020 6:43 pm

Deepsleep only works sometimes

Postby LMolenkamp » Wed Feb 26, 2020 12:29 am

I'm trying to program a ESP32 to run with batteries, and collect accelerometer data with MPU-6050, but my deepsleep only works on short periods of time, i was trying to make him function during 5 minutes, and deepsleep for 55 minutes.
  1. #include <Adafruit_MPU6050.h>
  2. #include <Adafruit_Sensor.h>
  3.  
  4. #include <Wire.h>
  5. #include <WiFi.h>
  6.  
  7. #include "FirebaseESP32.h"
  8.  
  9. #define WIFI_SSID "jacaranda 2 g"
  10. #define WIFI_PASSWORD ""
  11.  
  12. #define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */
  13. #define TIME_TO_COLLECT 30
  14.  
  15. #define ASSET_ID "cxs09"
  16.  
  17. RTC_DATA_ATTR int bootCount = 0;
  18.  
  19. FirebaseData firebaseData;
  20.  
  21. Adafruit_MPU6050 mpu;
  22.  
  23. const char* ntpServer = "pool.ntp.org";
  24. const long gmtOffset_sec = 0;
  25. const int daylightOffset_sec = 3600;
  26.  
  27. unsigned long startTime;
  28.  
  29. uint64_t loopi = 0;
  30.  
  31. uint64_t xx_time_get_time() {
  32.   struct timeval tv;
  33.   gettimeofday(&tv, NULL);
  34.   return (tv.tv_sec * 1000LL + (tv.tv_usec / 1000LL));
  35. }
  36.  
  37. void print_wakeup_reason(){
  38.   esp_sleep_wakeup_cause_t wakeup_reason;
  39.  
  40.   wakeup_reason = esp_sleep_get_wakeup_cause();
  41.  
  42.   switch(wakeup_reason)
  43.   {
  44.     case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
  45.     case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
  46.     case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
  47.     case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
  48.     case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
  49.     default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
  50.   }
  51. }
  52.  
  53. char * uintToStr( const uint64_t num, char *str )
  54. {
  55.   uint8_t i = 0;
  56.   uint64_t n = num;
  57.  
  58.   do
  59.     i++;
  60.   while ( n /= 10 );
  61.  
  62.   str[i] = '\0';
  63.   n = num;
  64.  
  65.   do
  66.     str[--i] = ( n % 10 ) + '0';
  67.   while ( n /= 10 );
  68.  
  69.   return str;
  70. }
  71.  
  72. void setup(void) {
  73.  
  74.   Serial.begin(115200);
  75.   while (!Serial)
  76.     delay(100); // will pause until serial console opens
  77.  
  78.   //Increment boot number and print it every reboot
  79.   ++bootCount;
  80.   Serial.println("Boot number: " + String(bootCount));
  81.  
  82.   //Print the wakeup reason for ESP32
  83.   print_wakeup_reason();
  84.  
  85.   esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000);
  86.   Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
  87.  
  88.   esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
  89.   Serial.println("Configured all RTC Peripherals to be powered down in sleep");
  90.  
  91.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  92.   Serial.print("Connecting to Wi-Fi");
  93.   while (WiFi.status() != WL_CONNECTED)
  94.     {
  95.       Serial.print(".");
  96.       delay(300);
  97.   }
  98.   Serial.println();
  99.   Serial.print("Connected with IP: ");
  100.   Serial.println(WiFi.localIP());
  101.   Serial.println();
  102.  
  103.   Firebase.begin("cloud-iot-68406.firebaseio.com", "MDkvUMYa1WCdOjej3kegTEWroWV3gSXkZMto1O3P");
  104.   Firebase.reconnectWiFi(true);
  105.  
  106.   // Try to initialize!
  107.   if (!mpu.begin()) {
  108.     Serial.println("Failed to find MPU6050 chip");
  109.     while (1) {
  110.       delay(10);
  111.     }
  112.   }
  113.   Serial.println("MPU6050 Found!");
  114.  
  115.   mpu.setAccelerometerRange(MPU6050_RANGE_4_G); // Ate 20m/s^2
  116.  
  117.   mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); //Delay 8.5 ms
  118.  
  119.   Serial.println("");
  120.  
  121.   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  122.  
  123.   Serial.flush();
  124.  
  125.   startTime = millis();
  126.  
  127.   delay(1000);
  128. }
  129.  
  130. void loop() {
  131.  
  132.   char str[21];
  133.   FirebaseJson json;
  134.   FirebaseJson json2;
  135.  
  136.   /* Get new sensor events with the readings */
  137.   sensors_event_t a, g, temp;
  138.   mpu.getEvent(&a, &g, &temp);
  139.  
  140.   double ax = a.acceleration.x;
  141.   double ay = a.acceleration.y;
  142.   double az = a.acceleration.z;
  143.   double at = temp.temperature;
  144.  
  145.   uint64_t epoch = xx_time_get_time();
  146.  
  147.   json2.set("x", ax);
  148.   json2.set("y", ay);
  149.   json2.set("z", az);
  150.   json2.set("temp", at);
  151.   json.set(uintToStr(epoch, str), json2);
  152.   Firebase.updateNode(firebaseData, ASSET_ID, json);
  153.  
  154.   /* Print out the values */
  155.   Serial.print("X: ");
  156.   Serial.print(ax);
  157.   Serial.print(", Y: ");
  158.   Serial.print(ay);
  159.   Serial.print(", Z: ");
  160.   Serial.print(az);
  161.   Serial.println(" m/s^2");
  162.   Serial.print("Temp: ");
  163.   Serial.print(at);
  164.   Serial.println(" degC");
  165.  
  166.   Serial.println("");
  167.  
  168.   delay(200);
  169.  
  170.   if (millis() - startTime >= TIME_TO_COLLECT * 1000) {
  171.     esp_deep_sleep_start();
  172.   }
  173.  
  174. }
This is my code, i'm kinda new, so any tips are helpful.

Thanks.

boarchuz
Posts: 605
Joined: Tue Aug 21, 2018 5:28 am

Re: Deepsleep only works sometimes

Postby boarchuz » Wed Feb 26, 2020 2:42 am

Try
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000ULL);

LMolenkamp
Posts: 2
Joined: Thu Feb 20, 2020 6:43 pm

Re: Deepsleep only works sometimes

Postby LMolenkamp » Wed Feb 26, 2020 5:09 pm

It didn't work, i tried the new code, but after 10 minutes collecting, he still doesn't sleep

Who is online

Users browsing this forum: Masoud123456 and 115 guests