Connecting ESP32 Cam with ESP32 Dev Module

esoesp32
Posts: 1
Joined: Tue Jul 02, 2024 1:29 pm

Connecting ESP32 Cam with ESP32 Dev Module

Postby esoesp32 » Tue Jul 02, 2024 1:37 pm

Hello everyone, I'm having trouble connecting my modules together. What should I consider when connecting these modules? Are the camera settings correct?

Code: Select all

#include "esp_camera.h"
#include "soc/soc.h"
#include "soc/rtc_cntl_reg.h"
#include "quirc.h"
#include <Wire.h>
#include <RTClib.h>

RTC_DS3231 rtc; // Instanz der RTC_DS3231 Klasse

TaskHandle_t QRCodeReader_Task;

/* Kamera Einstellung */
#define CAMERA_MODEL_AI_THINKER

#if defined(CAMERA_MODEL_AI_THINKER)
  #define PWDN_GPIO_NUM     32
  #define RESET_GPIO_NUM    -1
  #define XCLK_GPIO_NUM      0
  #define SIOD_GPIO_NUM     26
  #define SIOC_GPIO_NUM     27
  
  #define Y9_GPIO_NUM       35
  #define Y8_GPIO_NUM       34
  #define Y7_GPIO_NUM       39
  #define Y6_GPIO_NUM       36
  #define Y5_GPIO_NUM       21
  #define Y4_GPIO_NUM       19
  #define Y3_GPIO_NUM       18
  #define Y2_GPIO_NUM        5
  #define VSYNC_GPIO_NUM    25
  #define HREF_GPIO_NUM     23
  #define PCLK_GPIO_NUM     22
#else
  #error "Camera model not selected"
#endif


/* Variablen Deklaration QR */
struct QRCodeData
{
  bool valid;
  int dataType;
  uint8_t payload[1024];
  int payloadLen;
};

struct quirc *q = NULL;
uint8_t *image = NULL;
camera_fb_t *fb = NULL;
struct quirc_code code;
struct quirc_data data;
quirc_decode_error_t err;
struct QRCodeData qrCodeData;
String QRCodeResult = "";

struct Tablettendaten
{
  String einnahmezeit;
  String ablaufdatum;
  String identifikationsnummer;
};

Tablettendaten tbd[] = {
    {"07:30", "2024-06-17", "12345678"},
    {"12:45", "2024-06-18", "87654321"},
    {"19:15", "2024-06-19", "11223344"},
    {"08:00", "2024-06-20", "56781234"},
    {"10:30", "2024-06-21", "87651234"},
    {"14:00", "2024-06-22", "98765432"},
    {"17:45", "2024-06-23", "23456123"},
    {"21:30", "2024-06-24", "34561234"},
    {"07:00", "2024-06-25", "45612345"},
    {"09:30", "2024-06-26", "56123456"},
    {"12:15", "2024-06-27", "61234567"},
    {"15:45", "2024-06-28", "12345678"},
    {"18:30", "2024-06-29", "23456789"},
    {"22:00", "2024-06-30", "34567891"},
    {"08:30", "2024-07-01", "45678912"},
    {"11:00", "2024-07-02", "56789123"},
    {"14:45", "2024-07-03", "67891234"},
    {"17:00", "2024-07-04", "78912345"},
    {"20:15", "2024-07-05", "89123456"},
    {"23:00", "2024-07-06", "91234567"},
    {"07:45", "2024-07-07", "12345678"},
    {"10:15", "2024-07-08", "23456789"},
    {"13:30", "2024-07-09", "34567891"},
    {"16:45", "2024-07-10", "45678912"},
    {"19:30", "2024-07-11", "56789123"},
    {"22:45", "2024-07-12", "67891234"},
    {"09:00", "2024-07-13", "78912345"}};

/* Struktur für einen Wecker */
struct Alarm
{
  uint8_t id;
  uint8_t hour;
  uint8_t minute;
  String action;
};

Alarm alarms[4]; // Maximal 4 Wecker

void setup()
{
  Serial.begin(115200); // Kommunikation der verbundenen Hardware
  Wire.begin();         // Überprüfen, ob das RTC-Modul gefunden wurde

  if (!rtc.begin())
  {
    Serial.println("RTC nicht gefunden");
    while (1)
      ; // Endlosschleife, falls das RTC-Modul nicht gefunden wurde
  }

  // Hier Wecker einstellen, WeckerID, Stunde, Minute
  setAlarm(0, 11, 5, "Nehme Medikament ein (Mittags)"); // Morgens um 08:30 Uhr
  setAlarm(1, 13, 44, "Nehme Medikament ein (Mittags)"); // Mittags um 13:44 Uhr
  setAlarm(2, 18, 30, "Nehme Medikament ein (Nachts)"); // Abends um 18:30 Uhr
  setAlarm(3, 22, 0, "Nehme Medikament ein (Nachts)");  // Nachts um 22:00 Uhr

  // Disable brownout detector.
  WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);

  Serial.setDebugOutput(true);
  Serial.println();

  /* Kamera Konfiguration */
 Serial.println("Start configuring and initializing the camera...");
  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 10000000;
  config.pixel_format = PIXFORMAT_GRAYSCALE;
  config.frame_size = FRAMESIZE_QVGA;
  config.jpeg_quality = 15;
  config.fb_count = 1;


#if defined(CAMERA_MODEL_ESP_EYE)
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
#endif

  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK)
  {
    Serial.printf("Camera init failed with error 0x%x", err);
    ESP.restart();
  }

  sensor_t *s = esp_camera_sensor_get();
  s->set_framesize(s, FRAMESIZE_QVGA);

  Serial.println("Configure and initialize the camera successfully.");
  Serial.println();

  /* QRCodeReader_Task erstellen */
  xTaskCreatePinnedToCore(
      QRCodeReader,        /* Aufgabe Funktion */
      "QRCodeReader_Task", /* Name */
      10000,               /* Stapelgröße der Aufgabe */
      NULL,                /* Parameter der Aufgabe */
      1,                   /* Priorität der Aufgabe */
      &QRCodeReader_Task,  /* Task handle */
      0);                  /* pin task to core 0 */
}

void loop()
{
  // Überprüfen, ob ein Wecker ausgelöst wurde
  for (int i = 0; i < 4; i++)
  {
    if (alarmTriggered(alarms[i]))
    {
      Serial.print(getAlarmName(alarms[i].id));
      Serial.print(" wurde ausgelöst: ");
      Serial.println(alarms[i].action);
      // Hier können Sie den Code einfügen, der ausgeführt wird, wenn ein Wecker ausgelöst wird
    }
  }

  // lesen des aktuellen Datums- und Uhrzeitinformationen von der RTC
  DateTime now = rtc.now();

  // Ausgabe der Zeit und des Datums in der selben zeile, 2 digits für 2stellige Zahlen
  Serial.print("Datum: ");
  Serial.print(now.year(), DEC);
  Serial.print("/");
  printTwoDigits(now.month());
  Serial.print("/");
  printTwoDigits(now.day());
  Serial.print("  Uhrzeit: ");
  printTwoDigits(now.hour());
  Serial.print(":");
  printTwoDigits(now.minute());
  Serial.print(":");
  printTwoDigits(now.second()); // 2 stellige Zahlen
  Serial.println();

  // Ausgabe des QR-Code-Ergebnisses, wenn ein QR-Code erkannt wurde
  if (!QRCodeResult.isEmpty())
  {
    Serial.print("QR-Code erkannt: ");
    Serial.println(QRCodeResult);
    // Hier können Sie den Code einfügen, der nach dem QR-Code-Scan ausgeführt werden soll
    QRCodeResult = ""; // Zurücksetzen des QR-Code-Ergebnisses für den nächsten Scan
  }

  delay(1000); // Update jede Sekunde
}

void QRCodeReader(void *pvParameters)
{
  Serial.println("QRCodeReader is ready.");
  Serial.print("QRCodeReader running on core ");
  Serial.println(xPortGetCoreID());
  Serial.println();

  while (1)
  {
    q = quirc_new();
    if (q == NULL)
    {
      Serial.println("can't create quirc object");
      continue;
    }

    fb = esp_camera_fb_get();
    if (!fb)
    {
      Serial.println("Camera capture failed");
      continue;
    }

    quirc_resize(q, fb->width, fb->height);
    image = quirc_begin(q, NULL, NULL);
    memcpy(image, fb->buf, fb->len);
    quirc_end(q);

    int count = quirc_count(q);
    if (count > 0)
    {
      quirc_extract(q, 0, &code);
      err = quirc_decode(&code, &data);

      if (err)
      {
        Serial.println("Decoding FAILED");
        QRCodeResult = "Decoding FAILED";
      }
      else
      {
        Serial.println("Decoding successful:");
        dumpData(&data);
      }
      Serial.println();
    }

    esp_camera_fb_return(fb);
    fb = NULL;
    image = NULL;
    quirc_destroy(q);
  }
}

void setAlarm(uint8_t id, uint8_t hour, uint8_t minute, String action)
{
  if (id >= 4)
  {
    Serial.println("Maximal 4 Wecker sind erlaubt.");
    return;
  }
  alarms[id].id = id;
  alarms[id].hour = hour;
  alarms[id].minute = minute;
  alarms[id].action = action;
}

bool alarmTriggered(Alarm alarm)
{
  DateTime now = rtc.now();
  if (now.hour() == alarm.hour && now.minute() == alarm.minute && now.second() == 0)
  {
    return true;
  }
  return false;
}

void printTwoDigits(int number)
{
  if (number < 10)
  {
    Serial.print("0");
  }
  Serial.print(number);
}

String getAlarmName(uint8_t id)
{
  switch (id)
  {
  case 0:
    return "Wecker Morgens";
  case 1:
    return "Wecker Mittags";
  case 2:
    return "Wecker Abends";
  case 3:
    return "Wecker Nachts";
  default:
    return "Unbekannt";
  }
}

void dumpData(const struct quirc_data *data)
{
  Serial.printf("Version: %d\n", data->version);
  Serial.printf("ECC level: %c\n", "MLHQ"[data->ecc_level]);
  Serial.printf("Mask: %d\n", data->mask);
  Serial.printf("Length: %d\n", data->payload_len);
  Serial.printf("Payload: %s\n", data->payload);

  QRCodeResult = (const char *)data->payload;
}

Who is online

Users browsing this forum: No registered users and 47 guests