// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <TM1637.h>
TM1637 firstBlock, secondBlock;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
//define display interface data of the TM1637 displays
#define firstBlock_digits 4
#define firstBlock_clkPin 25
#define firstBlock_dataPin 26
#define secondBlock_digits 2
#define secondBlock_clkPin 32
#define secondBlock_dataPin 33
//define NTP server data
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0//TIMEZONE
#define UTC_OFFSET_DST 0//DAY SAVING TIME OFFSET
//function for generating "loading" animation
void spinner() {
static int8_t counter = 0;
const char* glyphs = "\xa1\xa5\xdb";
LCD.setCursor(15, 1);
LCD.print(glyphs[counter++]);
if (counter == strlen(glyphs)) {
counter = 0;
}
}
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
LCD.setCursor(0, 1);
LCD.println("Connection Err");
return;
}
LCD.setCursor(8, 0);
LCD.println(&timeinfo, "%H:%M:%S");
LCD.setCursor(0, 1);
LCD.println(&timeinfo, "%d/%m/%Y %Z");
}
void printlocalTimeOnSegments () {
struct tm timeinfoSegments;
if (getLocalTime(&timeinfoSegments)) {
firstBlock.displayTime(timeinfoSegments.tm_hour,timeinfoSegments.tm_min, false);
secondBlock.displayTime(timeinfoSegments.tm_sec,timeinfoSegments.tm_sec,false);
//secondBlock.displayPChar("00");
}
else {
}
}
void setup() {
Serial.begin(115200);
// SWAPPED
firstBlock.begin(secondBlock_clkPin, secondBlock_dataPin, secondBlock_digits);
secondBlock.begin(firstBlock_clkPin, firstBlock_dataPin, firstBlock_digits);
/*
firstBlock.begin(firstBlock_clkPin, firstBlock_dataPin, firstBlock_digits);
secondBlock.begin(secondBlock_clkPin, secondBlock_dataPin, secondBlock_digits);
*/
firstBlock.setBrightness(7);
secondBlock.setBrightness(7);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
spinner();
}
//Spinner demo
/*
while (true) {
spinner();
}
*/
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
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);
}
void loop() {
printLocalTime();
printlocalTimeOnSegments();
delay(250);
//firstBlock.displayPChar("----");
//secondBlock.displayPChar("00");
}