// https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/
// https://lastminuteengineers.com/esp32-i2c-lcd-tutorial/
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "time.h"
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
// 0x27hex = default, je nach Chipsatz 0x3Fhex möglich
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 3600 // Zeitkorrektur mit Vielfachem von +/-3600
#define UTC_OFFSET_DST 3600 // Sommerzeit, sonst 0, Daylight Saving Time
void printLocalTime(void);
void i2c_scanner (void);
void special_char (void);
void setup() {
Serial.begin(115200);
LCD.init(); // initialisation - 1)
LCD.backlight(); // Make sure backlight is on - 2)
// LCD.clear(); // clears the display - 3) -> in setup
LCD.setCursor(0, 0); // x / y, x=0-15, y=0-1
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
delay (1000);
LCD.clear();
LCD.setCursor(0, 0);
LCD.println("Online");
LCD.setCursor(0, 1);
LCD.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
i2c_scanner();
}
/*
lcd.home(); position cursor in the upper-left of the LCD without clearing display.
lcd.blink(); displays a blinking block of 5×8 pixels at the position of cursor
lcd.cursor(); displays an underscore (line) at the position of cursor.
lcd.noBlink(); function turns off the blinking LCD cursor.
lcd.noCursor(); hides the LCD cursor.
*/
void loop() {
printLocalTime();
delay(250);
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Error");
return;
}
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");
Serial.println(&timeinfo, " %A %a %B %b month %d %Y year %H %M %S ");
special_char ();
}
/*
struct tm
---------
%A Full weekday name
%a abbreviated weekday name
%B Full month name
%b abbreviated month name
%d Day of the month
%Y Year
%H Hour in 24h format
%I Hour in 12h format
%M Minute
%S Second
store data into variable:
char timeHour[3]; // must be 3 char long, 2 char data & terminating char
strftime(timeHour,3, "%H", &timeinfo);
Serial.println(timeHour);
char timeWeekDay[10];
strftime(timeWeekDay,10, "%A", &timeinfo);
Serial.println(timeWeekDay);
*/
void i2c_scanner (void) {
byte error, address;
int nDevices = 0;
Serial.println("Scanning for i2c devices . . .");
// Wire.begin(); // ev. nicht nötig
for(address = 1; address < 127; address++ ) {
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0) {
Serial.print("I2C device found at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
nDevices++;
}
else if (error==4) {
Serial.print("Unknow error at address 0x");
if (address<16) {
Serial.print("0");
}
Serial.println(address,HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
}
else {
Serial.println("done\n");
}
delay(3000);
}
void special_char (void) {
byte Alien[8] = {
0b11111,
0b10101,
0b11111,
0b11111,
0b01110,
0b01010,
0b11011,
0b00000
};
// create a new characters, Skull[8] wird "0" zugeteilt -> ausgeschrieben
LCD.createChar(0, Alien);
LCD.setCursor(11, 1);
LCD.write(0);
}
/*
createChar();
each character is made up of a 5×8 pixel matrix
https://lastminuteengineers.com/esp32-i2c-lcd-tutorial/ --> für binär-Struktur-Generierung
byte Heart[8] = {
0b00000,
0b01010,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000,
0b00000
};
byte Bell[8] = {
0b00100,
0b01110,
0b01110,
0b01110,
0b11111,
0b00000,
0b00100,
0b00000
};
byte Alien[8] = {
0b11111,
0b10101,
0b11111,
0b11111,
0b01110,
0b01010,
0b11011,
0b00000
};
byte Check[8] = {
0b00000,
0b00001,
0b00011,
0b10110,
0b11100,
0b01000,
0b00000,
0b00000
};
byte Speaker[8] = {
0b00001,
0b00011,
0b01111,
0b01111,
0b01111,
0b00011,
0b00001,
0b00000
};
byte Sound[8] = {
0b00001,
0b00011,
0b00101,
0b01001,
0b01001,
0b01011,
0b11011,
0b11000
};
byte Skull[8] = {
0b00000,
0b01110,
0b10101,
0b11011,
0b01110,
0b01110,
0b00000,
0b00000
};
byte Lock[8] = {
0b01110,
0b10001,
0b10001,
0b11111,
0b11011,
0b11011,
0b11111,
0b00000
};
*/