- #include <Adafruit_GFX.h>
- #include "Font4x7Fixed.h"
- #include <Adafruit_NeoMatrix.h>
- #include <Adafruit_NeoPixel.h>
- #include <ESP8266WiFi.h>
- #include <time.h>
- #define PIN 3
- #define BRIGHTNESS 10
- const char *ssid = "cammar";
- const char *password = "shadow10121976";
- const char* ntpServer = "ro.pool.ntp.org";
- const char* TZ_INFO = "EET-2EEST,M3.5.0/3,M10.5.0/4";
- struct tm timeinfo;
- time_t now;
- long unsigned lastNTPtime;
- unsigned long lastEntryTime;
- unsigned long previousMillis = 0;
- String dash = String(':');
- String minus = String('-');
- String space = String(' ');
- Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
- NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
- NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
- NEO_GRB + NEO_KHZ800);
- const uint16_t colors[] = {
- matrix.Color(128, 0, 0), matrix.Color(0, 128, 0), matrix.Color(0, 0, 128),
- matrix.Color(128, 0, 128), matrix.Color(0, 128, 128), matrix.Color(128, 128, 0)
- };
- int pass = 0;
- int x = matrix.width();
- char daysOfTheWeek[7][12] = {"Duminica", "Luni", "Marti", "Miercuri", "Joi", "Vineri", "Sambata"};
- bool getNTPtime(unsigned long sec) {
- {
- unsigned long start1 = millis();
- do
- {
- time(&now);
- localtime_r(&now, &timeinfo);
- delay(10);
- } while (((millis() - start1) <= (1000 * sec)) && (timeinfo.tm_year < (2023 - 1900)));
- if (timeinfo.tm_year <= (2023 - 1900))
- return false;
- }
- return true;
- }
- void setup() {
- Serial.begin(9600);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- }
- configTime(0, 0, ntpServer);
- setenv("TZ", TZ_INFO, 1);
- getNTPtime(10);
- showTime(&timeinfo);
- lastNTPtime = time(&now);
- lastEntryTime = millis();
- matrix.begin();
- matrix.setFont(&Font4x7Fixed);
- matrix.setTextSize(1);
- matrix.setTextWrap(false);
- matrix.setBrightness(BRIGHTNESS);
- matrix.setTextColor(colors[0]);
- }
- void loop() {
- showTime(&timeinfo);
- getNTPtime(10);
- }
- bool doneOnce=false;
- void showTime(tm *localTime) {
- String timeHour = String(localTime->tm_hour);
- Serial.print("timeHour = "); Serial.println(String(timeHour));
- String timeMinute = String(localTime->tm_min);
- Serial.print("timeMinute = "); Serial.println(String(timeMinute));
- String timeSeconds = String(localTime->tm_sec);
- Serial.print("timeSeconds = "); Serial.println(String(timeSeconds));
- String timeWeekDay = String(daysOfTheWeek[localTime->tm_wday]);
- Serial.print("timeWeekDay = "); Serial.println(String(timeWeekDay));
- String timeMonth = String(localTime->tm_mon + 1);
- Serial.print("timeMonth = "); Serial.println(String(timeMonth));
- String timeDay = String(localTime->tm_mday);
- Serial.print("timeDay = "); Serial.println(String(timeDay));
- String timeYear = String(localTime->tm_year - 100);
- Serial.print("timeYear = "); Serial.println(String(timeYear));
- String timeShow = String(timeHour) + String(space) + String(dash) + String(space) + String(timeMinute) + String(space) + String(dash) + String(space) + String(timeSeconds);
- String dateShow = String(timeWeekDay) + String(space) + String(space) + String(timeDay) + String(space) + String(minus) + String(space) + String(timeMonth) + String(space) + String(minus) + String(space) + String(timeYear);
- Serial.print("timeShow = "); Serial.println(timeShow);
- Serial.print("dateShow = "); Serial.println(dateShow);
- const long interval = 30000;
- const long interval2 = 50000;
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- if(doneOnce==false){x=0; doneOnce=true;}
- if (currentMillis - previousMillis >= interval2) {
- previousMillis = currentMillis;
- }
- matrix.fillScreen(0);
- matrix.setCursor(x, 0);
- matrix.print(dateShow);
- if (--x < -40) {
- matrix.setTextColor(matrix.Color(128, 0, 0));
- }
- matrix.show();
- delay(150);
- } else {
- doneOnce=false;
- matrix.clear();
- matrix.setCursor(0, 0);
- matrix.print(timeShow);
- pass++;
- if (pass >= 6) pass = 0;
- matrix.setTextColor(colors[pass]);
- matrix.show();
- delay(1000);
- }
- }
Raw Paste