diff --git a/client/ESPcam/src/main.cpp b/client/ESPcam/src/main.cpp index f7215759f2a52f6f454eca307b49df17b75200ef..788dbc9cad37b748096cd04c8b8d4732b8e8a085 100644 --- a/client/ESPcam/src/main.cpp +++ b/client/ESPcam/src/main.cpp @@ -61,6 +61,14 @@ over-the-air updates for the camera at one point. // LED control #define LEDpin 4 +// Deep Sleep Settings +#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */ +#define TIME_TO_SLEEP 10 /* Time ESP32 will go to sleep (in seconds) */ + + +// Counting files on sd card for saving in the name of each image +int fileCountOnSD = 0; // for counting files + // string for saving the time char time_string[20]; @@ -237,21 +245,55 @@ void takeNewPhoto(String path) esp_camera_fb_return(fb); } -void readRTC() -{ - // This is used for a picture timestamp (name of the image) - rtc.begin(); - DateTime now = rtc.now(); - sprintf(time_string, "%04d-%02d-%02dT%02d-%02d-%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), - now.second()); - Serial.println(time_string); +// Count files on SD card to give a meaningful count +void listDir(fs::FS &fs, const char * dirname, uint8_t levels){ + Serial.printf("Listing directory: %s\n", dirname); + + File root = fs.open(dirname); + if(!root){ + Serial.println("Failed to open directory"); + return; + } + if(!root.isDirectory()){ + Serial.println("Not a directory"); + return; + } + + File file = root.openNextFile(); + while(file){ + fileCountOnSD += 1; + + //Serial.print(" FILE: "); + //Serial.print(file.name()); + //Serial.print(" SIZE: "); + //Serial.println(file.size()); + + file = root.openNextFile(); + } + Serial.println("File Count on SD: "); + Serial.println(fileCountOnSD); } + void readRTC() + { + // This is used for a picture timestamp (name of the image) + if (! rtc.begin()) { + Serial.println("Couldnt find RTC!"); + } else { + Serial.println("Setting the time"); + } + DateTime now = rtc.now(); + sprintf(time_string, "%04d-%02d-%02dT%02d-%02d-%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), + now.second()); + Serial.println(time_string); + + + } + Sht85 sht85Sensor; void setup() -{ - // control of the LED pin +{ // control of the LED pin pinMode(LEDpin, OUTPUT); // Disable brownout detector WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); @@ -259,19 +301,24 @@ void setup() // Start Serial Serial.begin(115200); + Serial.println("Wake Up!"); + delay(1000); + + // Initialize the camera Serial.print("Initializing the camera module..."); camera_configESPCamera(); Serial.println("Camera OK!"); espnow_setup(); sht85Sensor.setup(); -} -void loop() -{ + // before in loop // initiate I2C, read SHT+RTC, end I2C + readRTC(); + + try { // out_data_drs26 data= dr26.readData(); // Serial.printf("data circumfrence"); @@ -299,8 +346,12 @@ void loop() Serial.print("Initializing the MicroSD card module... "); initMicroSDCard(); + // Count files on SD card + listDir(SD_MMC, "/", 0); + + // Path where new image will be saved in MicroSD card - String path = "/" + String(time_string) + ".jpg"; + String path = "/Picture_" + String(fileCountOnSD) + ".jpg"; Serial.printf("Picture file name: %s\n", path.c_str()); // Take and save a picture @@ -311,4 +362,17 @@ void loop() // Delay for specified period delay(1000); + + // Deep sleep + esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); + Serial.println("Setup ESP32 to sleep for " + String(TIME_TO_SLEEP) + " Seconds"); + Serial.println("Going to sleep now"); + delay(1000); + Serial.flush(); + gpio_hold_en((gpio_num_t)LEDpin); + esp_deep_sleep_start(); +} + +void loop() +{ } diff --git a/client/libs/sht85/Sht85.hpp b/client/libs/sht85/Sht85.hpp index e1528c240be6229c7e6b2b13d91be84187f4cc9e..00c7114f27a7ab0382bc09e765d2728fd7d1b219 100644 --- a/client/libs/sht85/Sht85.hpp +++ b/client/libs/sht85/Sht85.hpp @@ -19,8 +19,8 @@ // Pin definitions for I2C (SHT85, RTC) // This is different from the pins on the ESP32-C3-DevKit boards! -#define SDA 12 -#define SCL 13 +#define SDA 13 +#define SCL 12 struct out_data_sht85 { float temperature = 0.0;