Here is the final Code:
//#include <Wire.h>
#include <DS3232.h>
DS3232 clock;
const unsigned int DISPLAY_TIMEOUT = 8000;
bool displayIsOn;
unsigned long displayTimeout;
#define DS3231_I2C_ADDRESS 0x68
float temp3231;
#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3);
// BTconnected will = false when not connected and true when connected
boolean BTconnected = false;
// connect the STATE pin to Arduino pin D4
const byte BTpin = 4;
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
// BLK an 3,3V VCC
#define TFT_CLK 13
#define TFT_MISO 12
#define TFT_MOSI 11
#define TFT_DC 5
#define TFT_CS -1 //erforderlich!
#define TFT_RST 8
#define TFT_BLK 9
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#include <TimeLib.h>
#include <EtherCard.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// NTP Server
const char timeServer[] PROGMEM = "pool.ntp.org";
const uint8_t utcOffset = 1; // Central European Time
// Packet buffer, must be big enough to packet and payload
#define BUFFER_SIZE 340
byte Ethernet::buffer[BUFFER_SIZE];
const unsigned int remotePort = 123;
boolean IPconnected = true;
time_t prevDisplay = 0; // when the digital clock was displayed
boolean timeset = false;
byte trigger = 7; //Trigger-Pin des Ultraschallsensors an Pin7 des Arduino-Boards
byte echo = 6; // Echo-Pim des Ultraschallsensors an Pin6 des Arduino-Boards
long dauer = 0; // Das Wort dauer ist jetzt eine Variable, unter der die Zeit gespeichert wird, die eine Schallwelle bis zur Reflektion und zurück benötigt. Startwert ist hier 0.
long entfernung = 0; // Das Wort „entfernung“ ist jetzt die variable, unter der die berechnete Entfernung gespeichert wird. Info: Anstelle von „int“ steht hier vor den beiden Variablen „long“. Das hat den Vorteil, dass eine größere Zahl gespeichert werden kann. Nachteil: Die Variable benötigt mehr Platz im Speicher.
byte zl1 = 0;
byte zl2 = 31;
byte zl3 = 62;
byte zl4 = 93;
byte zl5 = 124;
byte zl6 = 155;
byte zl7 = 186;
byte zl8 = 217;
char buffer[9];
void setup() {
Wire.begin();
clock.begin();
//Serial.begin(9600);
tft.begin();
tft.setRotation(1);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(3); //TextSize(3) is 18 pixels wide by 24 pixels high.
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0, zl3);
tft.print(F("Balkon:"));
tft.setCursor(0, zl5);
tft.print(F("Box:"));
tft.setCursor(0, zl6);
tft.print(F("IP:"));
analogWrite(TFT_BLK, 200);
displayIsOn = true;
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
tft.setCursor(72, zl6);
tft.print(F("Warte auf IP"));
if (ether.begin(BUFFER_SIZE, mac) == 0) {
// no point in carrying on, so do nothing forevermore:
IPconnected = false;
}
if (!ether.dhcpSetup()) {
// no point in carrying on, so do nothing forevermore:
IPconnected = false;
}
if (IPconnected == true) {
char resultip[16] = {};
sprintf_P(resultip, PSTR("%d:%d:%d:%d"), ether.myip[0], ether.myip[1], ether.myip[2], ether.myip[3]);
tft.setTextColor(ILI9341_DARKCYAN, ILI9341_BLACK);
tft.setCursor(72, zl6);
tft.print(resultip);
} else {
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
tft.setCursor(72, zl6);
tft.print(F("Keine IP "));
}
BTserial.begin(38400);
pinMode(trigger, OUTPUT); // Trigger-Pin ist ein Ausgang
pinMode(echo, INPUT); // Echo-Pin ist ein Eingang
}
void loop() {
digitalWrite(trigger, LOW);
delay(5);
digitalWrite(trigger, HIGH); //Jetzt sendet man eine Ultraschallwelle los.
delay(10); //Dieser „Ton“ erklingt für 10 Millisekunden.
digitalWrite(trigger, LOW); //Dann wird der „Ton“ abgeschaltet.
dauer = pulseIn(echo, HIGH); //Mit dem Befehl „pulseIn“ zählt der Mikrokontroller die Zeit in Mikrosekunden, bis der Schall zum Ultraschallsensor zurückkehrt.
entfernung = (dauer / 2) * 0.03432; //Nun berechnet man die Entfernung in Zentimetern. Man teilt zunächst die Zeit durch zwei (Weil man ja nur eine Strecke berechnen möchte und nicht die Strecke hin- und zurück). Den Wert multipliziert man mit der Schallgeschwindigkeit in der Einheit Zentimeter/Mikrosekunde und erhält dann den Wert in Zentimetern.
displayBacklight();
tft.setTextColor(ILI9341_PINK, ILI9341_BLACK);
clock.read();
sprintf(buffer, "%02u.%02u.%02u", clock.day(), clock.month(), clock.year());
tft.setCursor(0, zl1);
tft.print(buffer);
tft.print(F(" "));
sprintf(buffer, "%02u:%02u:%02u", clock.hours(), clock.minutes(), clock.seconds());
tft.print(buffer);
tft.setCursor(0, zl2);
switch (clock.weekDay()) {
case 1: tft.print(F("Montag ")); break;
case 2: tft.print(F("Dienstag ")); break;
case 3: tft.print(F("Mittwoch ")); break;
case 4: tft.print(F("Donnerstag ")); break;
case 5: tft.print(F("Freitag ")); break;
case 6: tft.print(F("Samstag ")); break;
case 7: tft.print(F("Sonntag ")); break;
default: tft.print(F("Wochentag ?"));
}
tft.setTextColor(ILI9341_CYAN, ILI9341_BLACK);
tft.setCursor(90, zl5);
tft.print(getDS3231temp());
tft.print(F("C "));
if ((IPconnected == true) && (clock.weekDay() == 1) && (timeset == false)) {
setSyncProvider(getDstCorrectedTime); // Use this for local, DST-corrected time
clock.setSeconds(second());
clock.setMinutes(minute());
clock.setHours(hour());
clock.setDay(day());
clock.setMonth(month());
clock.setYear(year() - 2000);
//Serial.println(year() - 2000);
clock.write();
timeset = true;
digitalClockDisplay();
}
if (clock.weekDay() != 1) { timeset = false; }
if (digitalRead(BTpin) == HIGH) {
if (BTserial.available() > 0) {
delay(100);
String readString = BTserial.readString();
int firstClosingBracket = readString.indexOf('<');
int secondClosingBracket = readString.indexOf('>', firstClosingBracket + 1);
String readString1 = readString.substring(firstClosingBracket + 1, secondClosingBracket);
if (readString1.length() > 17) { readString1.remove(17); }
tft.setTextColor(ILI9341_GREEN, ILI9341_BLACK);
tft.setCursor(0, zl4);
tft.print(readString1);
readString = "";
readString1 = "";
}
} else {
tft.setTextColor(ILI9341_RED, ILI9341_BLACK);
tft.setCursor(0, zl4);
tft.print(F("Kein Signal "));
tft.setCursor(120, zl7);
}
}
void displayBacklight() {
if (displayIsOn && (millis() - displayTimeout <= DISPLAY_TIMEOUT)) {
tft.setTextColor(ILI9341_ORANGE, ILI9341_BLACK);
tft.setCursor(120, zl7);
unsigned long count1 = ((millis() - displayTimeout) / 1000) + 1;
unsigned long count2 = (DISPLAY_TIMEOUT / 1000) + 1;
tft.print(String(abs(int(count1) - int(count2))) + " Sek.");
}
if (entfernung > 0 && entfernung <= 30) {
displayTimeout = millis();
if (!displayIsOn) {
displayIsOn = true;
analogWrite(TFT_BLK, 200);
}
return;
}
if (displayIsOn && (millis() - displayTimeout > DISPLAY_TIMEOUT)) {
displayIsOn = false;
tft.setCursor(120, zl7);
tft.print(F(" "));
analogWrite(TFT_BLK, 0);
}
}
void digitalClockDisplay() {
tft.setTextColor(ILI9341_LIGHTGREY, ILI9341_BLACK);
//tft.setCursor(0, zl8);
sprintf(buffer, "%02u.%02u.%02u", day(), month(), year() - 2000);
tft.setCursor(0, zl8);
tft.print(buffer);
tft.print(F(" "));
sprintf(buffer, "%02u:%02u:%02u", hour(), minute(), second());
tft.print(buffer);
}
/*-------- NTP code ----------*/
// SyncProvider that returns UTC time
time_t getNtpTime() {
// Send request
tft.setTextColor(ILI9341_WHITE, ILI9341_BLACK);
tft.setCursor(0, zl8);
tft.print(F("NTP Request"));
if (!ether.dnsLookup(timeServer)) {
tft.print(F(" failed"));
return 0; // return 0 if unable to get the time
} else {
ether.ntpRequest(ether.hisip, remotePort);
// Wait for reply
uint32_t beginWait = millis();
while (millis() - beginWait < 1500) {
word len = ether.packetReceive();
ether.packetLoop(len);
unsigned long secsSince1900 = 0L;
if (len > 0 && ether.ntpProcessAnswer(&secsSince1900, remotePort)) {
tft.setCursor(0, zl8);
tft.print(F("Receive NTP"));
return secsSince1900 - 2208988800UL;
}
}
tft.setCursor(0, zl8);
tft.print(F("No NTP Response"));
return 0;
}
}
/* Alternative SyncProvider that automatically handles Daylight Saving Time (DST) periods,
* at least in Europe, see below.
*/
time_t getDstCorrectedTime(void) {
time_t t = getNtpTime();
if (t > 0) {
TimeElements tm;
breakTime(t, tm);
t += (utcOffset + dstOffset(tm.Day, tm.Month, tm.Year + 1970, tm.Hour)) * SECS_PER_HOUR;
}
return t;
}
/* This function returns the DST offset for the current UTC time.
* This is valid for the EU, for other places see
* http://www.webexhibits.org/daylightsaving/i.html
*
* Results have been checked for 2012-2030 (but should work since
* 1996 to 2099) against the following references:
* - http://www.uniquevisitor.it/magazine/ora-legale-italia.php
* - http://www.calendario-365.it/ora-legale-orario-invernale.html
*/
byte dstOffset(byte d, byte m, unsigned int y, byte h) {
// Day in March that DST starts on, at 1 am
byte dstOn = (31 - (5 * y / 4 + 4) % 7);
// Day in October that DST ends on, at 2 am
byte dstOff = (31 - (5 * y / 4 + 1) % 7);
if ((m > 3 && m < 10) || (m == 3 && (d > dstOn || (d == dstOn && h >= 1))) || (m == 10 && (d < dstOff || (d == dstOff && h <= 1))))
return 1;
else
return 0;
}
float getDS3231temp() {
float temp3231 = 0;
byte tMSB, tLSB;
//temp registers (11h-12h) get updated automatically every 64s
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0x11);
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 2);
if (Wire.available()) {
tMSB = Wire.read(); //2's complement int portion
tLSB = Wire.read(); //fraction portion
temp3231 = (tMSB & B01111111); //do 2's math on Tmsb
temp3231 += ((tLSB >> 6) * 0.25); //only care about bits 7 & 8
} else {
//oh noes, no data!
}
return temp3231;
}