/*
 * Board:   Wemos D1 R32
 * Sketch:  wemosd1r32_template.ino
 * Version: 1.0
 * Datum:   03.07.2025
 * 
 */
// Bibliotheken einbinden
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <LiquidCrystal_I2C.h>
#include "RTClib.h"
#include "DHTesp.h"
// Parameter für OLED SSD1306 festlegen
#define SCREEN_WIDTH 128 // OLED Breite
#define SCREEN_HEIGHT 64 // OLED Höhe
#define OLED_RESET    -1 // Reset
#define SCREEN_ADDRESS 0x3C
// Parameter für LCD1602 festlegen
#define I2C_ADDR    0x27
#define LCD_COLUMNS 16
#define LCD_LINES   2
const int POTI_PIN = 10;
const int DHT_PIN = 46;
// Taste nicht gedrückt:  Signal ist HIGH
// Taste gedrückt:        Signal ist LOW
//
// Der Widerstand hält die Pins 11, 12, 13 auf HIGH, wenn die Taste nicht gedrückt ist.
// Der Widerstand ist ein Pullup-Widerstand.
const int S1 = 13;
const int S2 = 12;
const int S3 = 11;
int ledPins[10] = {35, 36, 37, 38, 39, 40, 41, 42, 2, 1};
char daysOfTheWeek[7][12] = {"Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"};
// Objekte anlegen
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);               // Objekt 'lcd'
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // Objekt 'oled'
RTC_DS1307 rtc;                                                        // Objekt 'rtc'
DHTesp dhtSensor;                                                      // Objekt 'dht' 
void setup() {
  Serial.begin(115200);
  Wire.begin(21, 20); // SDA, SCL
  // DHT22 initialisieren
  dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
  
  // LCD1602 initialisieren
  lcd.init();
  lcd.backlight();
  
  // OLED initialisieren
  if(!oled.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 - Adresszuweisung fehlgeschlagen!"));
    for(;;); // wenn es nicht funktioniert, dann Endlosschleife ;)
  }
  // RTC-Modul initialisieren
  if (! rtc.begin()) {
    Serial.println("RTC-Modul nicht gefunden!");
    Serial.flush();
    while (1);
  }
  // Übernahme TimeStamp vom PC
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  // wenn manuelle Einstellung, dann so:
  // rtc.adjust(DateTime(2025, 7, 2, 15, 0, 0));
  //alle LED Pins als Ausgänge definieren
  for (int i = 0; i <= 9; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  // Taster Pins als Eingang definieren
  pinMode(S1, INPUT_PULLUP);
  pinMode(S2, INPUT_PULLUP);
  pinMode(S3, INPUT_PULLUP);
}
void loop() {
  DateTime now = rtc.now();
  Serial.print("Aktuelle Zeit: ");
  Serial.print(now.day(), DEC);
  Serial.print('.');
  Serial.print(now.month(), DEC);
  Serial.print('.');
  Serial.print(now.year(), DEC);
  Serial.print(" (");
  Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
  Serial.print(") ");
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
  Serial.println();
  delay(3000);
}