Skip to content

Instantly share code, notes, and snippets.

@wybiral
Created February 25, 2020 00:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wybiral/ff410907b9c79864c2499a0d238207b2 to your computer and use it in GitHub Desktop.
Save wybiral/ff410907b9c79864c2499a0d238207b2 to your computer and use it in GitHub Desktop.
#include <esp32cam.h>
#include "FS.h"
#include "SD_MMC.h"
#include "WiFi.h"
// Current image number (per folder)
uint32_t image;
// Current folder number
uint32_t folder;
void setup() {
Serial.begin(9600);
// Stop WiFi/BLE
WiFi.mode(WIFI_OFF);
btStop();
// Setup Cam
auto res = esp32cam::Resolution::find(1024, 768);
esp32cam::Config cfg;
cfg.setPins(esp32cam::pins::AiThinker);
cfg.setResolution(res);
cfg.setJpeg(80);
esp32cam::Camera.begin(cfg);
// Setup SD
Serial.println("Starting SD Card");
if (!SD_MMC.begin()) {
Serial.println("SD Card Mount Failed");
}
uint8_t cardType = SD_MMC.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD Card attached");
}
// Find next folder number by scanning root
image = 0;
folder = 0;
File root = SD_MMC.open("/");
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
String name = String(file.name());
name = name.substring(1);
long n = name.toInt();
if (n > folder) {
folder = n;
}
}
file = root.openNextFile();
}
folder++;
SD_MMC.mkdir("/" + String(folder));
Serial.println(folder);
}
void loop() {
// Capture
Serial.println("Capturing...");
auto img = esp32cam::capture();
if (img == nullptr) {
Serial.println("Image capture failed");
return;
}
// Write to SD
String name = "/" + String(folder) + "/" + String(image) + ".jpg";
File file = SD_MMC.open(name, FILE_WRITE);
if (!file) {
Serial.println("Failed to open file");
} else {
img->writeTo(file);
}
image++;
file.close();
Serial.println("Done");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment