#include <WiFi.h>
#include <FTPClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <RTClib.h> // Für Datum und Uhrzeit
// LCD initialisieren (I2C Adresse 0x27, 16x2 Display)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Keypad-Konfiguration
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {14, 27, 26, 25};
byte colPins[COLS] = {33, 32, 35, 34};
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// WLAN-Zugangsdaten
const char* ssid = "DeinWLANSSID";
const char* password = "DeinWLANPasswort";
// Fritzbox FTP-Server-Daten
const char* ftp_server = "192.168.178.1"; // Fritzbox IP-Adresse
const char* ftp_user = "fritzbox_user";
const char* ftp_pass = "fritzbox_pass";
// Zeitkonfiguration
RTC_DS3231 rtc;
// FTPClient Objekt
FTPClient ftp;
// Vordefinierte Nummernschilder
String nummernschilder[] = {"ABC123", "XYZ789", "LMN456", "DEF234", "GHI567", "JKL890", "MNO123", "PQR456", "STU789"};
int numCount = sizeof(nummernschilder) / sizeof(nummernschilder[0]);
// Variablen für Benutzereingaben
String selectedLicensePlate = "";
String kilometerStand = "";
String literGetankt = "";
void setup() {
Serial.begin(115200);
// LCD starten
lcd.begin();
lcd.backlight();
// WLAN-Verbindung aufbauen
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Verbinde mit WLAN...");
}
Serial.println("WLAN verbunden!");
// FTP-Verbindung initialisieren
ftp.begin(ftp_server, ftp_user, ftp_pass, 21, 5000, 2);
// RTC initialisieren
if (!rtc.begin()) {
Serial.println("RTC konnte nicht gestartet werden!");
}
lcd.setCursor(0, 0);
lcd.print("Waehle Nr.schild:");
}
void loop() {
// Nummernschilder-Auswahl
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Waehle Nr.schild:");
lcd.setCursor(0, 1);
lcd.print("[1-9]");
char key = keypad.getKey();
if (key >= '1' && key <= '9') {
int index = key - '1'; // Index aus dem Tastenwert berechnen
if (index < numCount) {
selectedLicensePlate = nummernschilder[index];
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ausgewählt:");
lcd.setCursor(0, 1);
lcd.print(selectedLicensePlate);
delay(2000); // Kurze Pause, um die Auswahl zu bestätigen
// Kilometerstand eingeben
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Eingabe KM:");
kilometerStand = getKeypadInput();
// Getankte Liter eingeben
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Eingabe Liter:");
literGetankt = getKeypadInput();
// Datum und Uhrzeit holen
DateTime now = rtc.now();
String timestamp = String(now.day()) + "/" + String(now.month()) + "/" + String(now.year()) + " " + String(now.hour()) + ":" + String(now.minute());
// Daten auf der Fritzbox speichern
saveDataToFritzbox(selectedLicensePlate, kilometerStand, literGetankt, timestamp);
}
}
}
String getKeypadInput() {
String input = "";
char key;
while (true) {
key = keypad.getKey();
if (key == '#') break; // Bestätigung der Eingabe
if (key) {
input += key;
lcd.print(key); // Zeige die Eingabe auf dem LCD an
}
}
return input;
}
void saveDataToFritzbox(String licensePlate, String kilometer, String liters, String timestamp) {
if (WiFi.status() == WL_CONNECTED) {
// Datei öffnen/erstellen auf der Fritzbox
ftp.OpenConnection();
ftp.ChangeWorkDir("/"); // Standardverzeichnis
String filename = "Tankdaten.txt";
ftp.InitFile("Type A");
if (!ftp.NewFile(filename.c_str())) {
Serial.println("Fehler beim Erstellen der Datei");
return;
}
// Daten im CSV-Format speichern
String data = licensePlate + "," + kilometer + "," + liters + "," + timestamp + "\n";
ftp.WriteData((unsigned char *)data.c_str(), data.length());
ftp.CloseFile();
ftp.CloseConnection();
Serial.println("Daten erfolgreich auf der Fritzbox gespeichert.");
}
}