CPP   32
code1
Guest on 6th February 2023 01:45:24 PM


  1. #include <Adafruit_GFX.h>
  2. #include "Font4x7Fixed.h"
  3. #include <Adafruit_NeoMatrix.h>
  4. #include <Adafruit_NeoPixel.h>
  5. #include <ESP8266WiFi.h>
  6. #include <time.h>
  7.  
  8. #define PIN  3
  9. #define BRIGHTNESS 10
  10.  
  11. const char *ssid = "cammar";
  12. const char *password = "shadow10121976";
  13. const char* ntpServer = "ro.pool.ntp.org";
  14. const char* TZ_INFO    = "EET-2EEST,M3.5.0/3,M10.5.0/4";
  15. struct tm timeinfo;
  16. time_t now;
  17. long unsigned lastNTPtime;
  18. unsigned long lastEntryTime;
  19. unsigned long previousMillis = 0;
  20.  
  21. Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
  22.                             NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  23.                             NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
  24.                             NEO_GRB            + NEO_KHZ800);
  25.  
  26. const uint16_t colors[] = {
  27.   matrix.Color(128, 0, 0), matrix.Color(0, 128, 0), matrix.Color(0, 0, 128),
  28.   matrix.Color(128, 0, 128), matrix.Color(0, 128, 128), matrix.Color(128, 128, 0)
  29. };
  30.  
  31. int pass = 0;
  32. int x = matrix.width();
  33.  
  34. char daysOfTheWeek[7][12] = {"Duminica", "Luni", "Marti", "Miercuri", "Joi", "Vineri", "Sambata"};
  35.  
  36. bool getNTPtime(unsigned long sec) {
  37.   {
  38.     //    uint32_t start1 = millis();
  39.     unsigned long start1 = millis();
  40.     do
  41.     {
  42.       time(&now);
  43.       localtime_r(&now, &timeinfo);
  44.       delay(10);
  45.     } while (((millis() - start1) <= (1000 * sec)) && (timeinfo.tm_year < (2023 - 1900)));
  46.  
  47.     if (timeinfo.tm_year <= (2023 - 1900))
  48.       return false;  // the NTP call was not successful
  49.   }
  50.   return true;
  51. }
  52.  
  53. void showTime(tm *localTime) {
  54.   char time_output[50];
  55.   char time_output1[50];
  56.  
  57.   matrix.clear();
  58.   matrix.setCursor(0, 0);
  59.   sprintf(time_output, "%02d : %02d : %02d", localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
  60.   matrix.print(time_output);
  61.   pass++;
  62.   if (pass >= 6) pass = 0;
  63.   matrix.setTextColor(colors[pass]);
  64.   matrix.show();
  65.   delay(1000);
  66.  
  67.   const long interval = 30000;
  68.   unsigned long currentMillis = millis();
  69.   if (currentMillis - previousMillis >= interval) {
  70.     for (int i = 0; i < 2; i++) {
  71.       if (i >= 2) {
  72.         i = 0;
  73.       }
  74.       matrix.print(i);
  75.  
  76.       switch (i) {
  77.         case 0:
  78.           while (i == 0) {
  79.             matrix.clear();
  80.             matrix.setCursor(x, 0);
  81.             matrix.print(daysOfTheWeek[localTime->tm_wday]);
  82.             if (--x < -36) {
  83.               matrix.setTextColor(matrix.Color(128, 0, 0));
  84.             }
  85.             matrix.show();
  86.             delay(150);
  87.           }
  88.           break;
  89.         case 1:
  90.           while (i == 1) {
  91.             matrix.clear();
  92.             matrix.setCursor(x, 0);
  93.             sprintf(time_output1, "%02d-%02d-%02d", localTime->tm_mday, localTime->tm_mon + 1, localTime->tm_year - 100);
  94.             matrix.print(time_output1);
  95.             if (--x < -36) {
  96.               matrix.setTextColor(matrix.Color(0, 128, 0));
  97.             }
  98.             matrix.show();
  99.             delay(150);
  100.           }
  101.           break;
  102.       }
  103.     }
  104.     previousMillis = currentMillis;
  105.   }
  106. }
  107.  
  108. void setup() {
  109.   WiFi.begin(ssid, password);
  110.   while (WiFi.status() != WL_CONNECTED) {
  111.     delay(500);
  112.   }
  113.   configTime(0, 0, ntpServer);
  114.   setenv("TZ", TZ_INFO, 1);
  115.   getNTPtime(10);
  116.   showTime(&timeinfo);
  117.   lastNTPtime = time(&now);
  118.   lastEntryTime = millis();
  119.   matrix.begin();
  120.   matrix.setFont(&Font4x7Fixed);
  121.   matrix.setTextSize(1);
  122.   matrix.setTextWrap(false);
  123.   matrix.setBrightness(BRIGHTNESS);
  124.   matrix.setTextColor(colors[0]);
  125. }
  126.  
  127. void loop() {
  128.   showTime(&timeinfo);
  129.   getNTPtime(10);
  130. }

Raw Paste

Login or Register to edit or fork this paste. It's free.