Deepsleep only works sometimes
Posted: 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.
This is my code, i'm kinda new, so any tips are helpful.
Thanks.
- #include <Adafruit_MPU6050.h>
- #include <Adafruit_Sensor.h>
- #include <Wire.h>
- #include <WiFi.h>
- #include "FirebaseESP32.h"
- #define WIFI_SSID "jacaranda 2 g"
- #define WIFI_PASSWORD ""
- #define TIME_TO_SLEEP 60 /* Time ESP32 will go to sleep (in seconds) 3600 seconds = 1 hour */
- #define TIME_TO_COLLECT 30
- #define ASSET_ID "cxs09"
- RTC_DATA_ATTR int bootCount = 0;
- FirebaseData firebaseData;
- Adafruit_MPU6050 mpu;
- const char* ntpServer = "pool.ntp.org";
- const long gmtOffset_sec = 0;
- const int daylightOffset_sec = 3600;
- unsigned long startTime;
- uint64_t loopi = 0;
- uint64_t xx_time_get_time() {
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return (tv.tv_sec * 1000LL + (tv.tv_usec / 1000LL));
- }
- void print_wakeup_reason(){
- esp_sleep_wakeup_cause_t wakeup_reason;
- wakeup_reason = esp_sleep_get_wakeup_cause();
- switch(wakeup_reason)
- {
- case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
- case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
- case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
- case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
- case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
- default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
- }
- }
- char * uintToStr( const uint64_t num, char *str )
- {
- uint8_t i = 0;
- uint64_t n = num;
- do
- i++;
- while ( n /= 10 );
- str[i] = '\0';
- n = num;
- do
- str[--i] = ( n % 10 ) + '0';
- while ( n /= 10 );
- return str;
- }
- void setup(void) {
- Serial.begin(115200);
- while (!Serial)
- delay(100); // will pause until serial console opens
- //Increment boot number and print it every reboot
- ++bootCount;
- Serial.println("Boot number: " + String(bootCount));
- //Print the wakeup reason for ESP32
- print_wakeup_reason();
- esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000);
- Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
- esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
- Serial.println("Configured all RTC Peripherals to be powered down in sleep");
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(300);
- }
- Serial.println();
- Serial.print("Connected with IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- Firebase.begin("cloud-iot-68406.firebaseio.com", "MDkvUMYa1WCdOjej3kegTEWroWV3gSXkZMto1O3P");
- Firebase.reconnectWiFi(true);
- // Try to initialize!
- if (!mpu.begin()) {
- Serial.println("Failed to find MPU6050 chip");
- while (1) {
- delay(10);
- }
- }
- Serial.println("MPU6050 Found!");
- mpu.setAccelerometerRange(MPU6050_RANGE_4_G); // Ate 20m/s^2
- mpu.setFilterBandwidth(MPU6050_BAND_21_HZ); //Delay 8.5 ms
- Serial.println("");
- configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
- Serial.flush();
- startTime = millis();
- delay(1000);
- }
- void loop() {
- char str[21];
- FirebaseJson json;
- FirebaseJson json2;
- /* Get new sensor events with the readings */
- sensors_event_t a, g, temp;
- mpu.getEvent(&a, &g, &temp);
- double ax = a.acceleration.x;
- double ay = a.acceleration.y;
- double az = a.acceleration.z;
- double at = temp.temperature;
- uint64_t epoch = xx_time_get_time();
- json2.set("x", ax);
- json2.set("y", ay);
- json2.set("z", az);
- json2.set("temp", at);
- json.set(uintToStr(epoch, str), json2);
- Firebase.updateNode(firebaseData, ASSET_ID, json);
- /* Print out the values */
- Serial.print("X: ");
- Serial.print(ax);
- Serial.print(", Y: ");
- Serial.print(ay);
- Serial.print(", Z: ");
- Serial.print(az);
- Serial.println(" m/s^2");
- Serial.print("Temp: ");
- Serial.print(at);
- Serial.println(" degC");
- Serial.println("");
- delay(200);
- if (millis() - startTime >= TIME_TO_COLLECT * 1000) {
- esp_deep_sleep_start();
- }
- }
Thanks.