clock tft con ora legale

Home Forum Schede esp8266 clock tft con ora legale

  • Questo topic ha 2 risposte, 2 partecipanti ed è stato aggiornato l'ultima volta 4 anni fa da Aldo.
Visualizzazione 2 filoni di risposte
  • Autore
    Post
    • #16374
      ALDO BERTULETTI

        come posso fare per visualizzare su display la stessa ora che stampo su seriale??

        grazie!!

        /**************************************************************************
        *
        * WiFi Internet clock (NTP) with ESP8266 NodeMCU (ESP-12E) board and
        * ST7735 TFT display.
        * This is a free software with NO WARRANTY.
        * https://simple-circuit.com/
        * by Renzo Mischianti <www.mischianti.org>
        * modificato in data 31 marzo 2021 con aggiunta libreria Timezone
        * lettura regole da file Renzo Mischianti by Aldo
        * IMPOSTARE COME SCHEDA NODEMCU 1.0 (ESP12E MODULE)..NOVEMBRE 2021
        *************************************************************************/

        #include <ESP8266WiFi.h>
        #include <WiFiUdp.h>
        #include <NTPClient.h>
        #include <TimeLib.h>
        #include <Adafruit_GFX.h>
        #include <Adafruit_ST7735.h> // include Adafruit ST7735 TFT library
        #include <Timezone.h> // https://github.com/JChristensen/Timezone
        #include <time.h> //questa libreria converte il timestamp Unix (Unix epoch) in: secondi, minuti, ore, giorno della settimana, giorno, mese e anno.
        //L’epoca Unix è il numero di secondi trascorsi dal 1 gennaio 1970 (mezzanotte UTC/GMT).
        //Fondamentalmente il time server invia l’ora in formato Unix epoch che deve essere convertito, questa libreria fa tutto il lavoro
        // ST7735 TFT module connections
        #define TFT_RST D4 // TFT RST NodeMCU pin D4 (GPIO2)
        #define TFT_CS D3 // TFT CS NodeMCU pin D3 (GPIO0)
        #define TFT_DC D2 // TFT DC NodeMCU pin D2 (GPIO4)
        // initialize ST7735 TFT library with hardware SPI module
        // SCK (CLK) —> NodeMCU pin D5 (GPIO14)
        // MOSI(DIN) —> NodeMCU pin D7 (GPIO13)
        Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

        // set WiFi network SSID and password
        const char *ssid = “retemiaenontua”;
        const char *password = “vadaviailculo”;
        //—————————————————————————–

        //Immettere l’ora nel formato epoch e restituire il formato dell’ora tm
        static tm getDateTimeByParams(long time){
        struct tm *newtime;
        const time_t tim = time;
        newtime = localtime(&tim);
        return *newtime;
        }

        // Immettere il formato dell’ora tm e restituire la stringa con il modello di formato
        static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)”%d/%m/%Y %H:%M:%S”){
        char buffer[30];
        strftime(buffer, 30, pattern, newtime);
        return buffer;
        }

        // Inserisci l’ora in formato epoch e restituisci Stringa con modello di formato
        static String getEpochStringByParams(long time, char* pattern = (char *)”%d/%m/%Y %H:%M:%S”){
        //struct tm *newtime;
        tm newtime;
        newtime = getDateTimeByParams(time);
        return getDateTimeStringByParams(&newtime, pattern);
        }
        WiFiUDP ntpUDP;

        // Per impostazione predefinita, “pool.ntp.org” viene utilizzato con un intervallo
        // di aggiornamento di 60 secondi e nessun offset

        int GTMOffset = 0; // SET TO UTC TIME
        NTPClient timeClient(ntpUDP, “europe.pool.ntp.org”, GTMOffset*60*60, 60*60*1000);
        TimeChangeRule CEST = {“CEST”, Last, Sun, Mar, 2, 120}; // Central European Summer Time
        TimeChangeRule CET = {“CET “, Last, Sun, Oct, 3, 60}; // Central European Standard Time
        Timezone CE(CEST, CET);
        unsigned long epoch;

        //collegamento–http://mischianti.org/it/protocollo-ntp-fuso-orario-e-ora-legale-dst-con-esp8266-esp32-o-arduino/
        //——————————————————————————————–//
        void setup(void)
        {
        Serial.begin(115200);

        tft.initR(INITR_BLACKTAB); // inizializza un chip ST7735S, sfondo nero
        tft.fillScreen(ST7735_BLACK); // riempire lo schermo con il colore nero
        tft.setRotation(5);
        tft.setTextColor(ST7735_CYAN, ST7735_BLACK); // imposta il colore del testo su sfondo nero
        tft.setTextSize(1);
        tft.setCursor(12, 14);
        tft.print(“Wi-Fi Internet Clock”);

        WiFi.begin(ssid, password);

        while ( WiFi.status() != WL_CONNECTED ) {
        delay(500);
        tft.setCursor(15, 54);
        tft.print(“CONNETTING…”);
        }
        timeClient.begin();
        tft.setCursor(15, 84);
        delay(500);
        //tft.print(“CONNESSO”);
        if (timeClient.update()){
        tft.print ( “Adjust local clock” );
        unsigned long epoch = timeClient.getEpochTime();
        setTime(epoch);
        }
        else{
        tft.print ( “NTP Update not WORK!!” );
        }
        delay ( 500 );
        tft.fillScreen(ST7735_BLACK);
        tft.setTextSize(1);
        tft.setCursor(10, 6);
        tft.print(“Wi-Fi Internet Clock”);
        tft.setTextSize(2);
        //timeClient.begin();
        delay ( 500 );

        }
        //—————————————————————————//
        void RTC_display()

        {
        char dow_matrix[7][10] = {“DOMENICA”, “LUNEDI”, “MARTEDI”, “MERCOLEDI”, “GIOVEDI”, “VENERDI”, “SABATO”};
        byte x_pos[7] = {14, 25, 24, 13, 24, 24, 25};
        static byte previous_dow = 0;

        if( previous_dow != weekday(epoch) ) // stampa il giorno della settimana
        {
        previous_dow = weekday(epoch);
        tft.fillRect(9, 70, 112, 25, ST7735_BLACK); // disegnare un rettangolo (cancella il giorno dal display)
        tft.setCursor(x_pos[previous_dow-1], 77); // COORDINATA ALTEZZA SCRITTA GIORNO SETTIMANA
        tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); // imposta il colore del testo su sfondo nero
        tft.print( dow_matrix[previous_dow-1] );
        tft.setTextSize(2);
        }

        // stampa la data
        tft.setTextSize(2);
        tft.setCursor(5, 105);
        tft.setTextColor(ST7735_MAGENTA, ST7735_BLACK);
        tft.printf( “%02u-%02u-%04u”, day(epoch), month(epoch), year(epoch) );
        // stampa il tempo con punti lampeggianti dei secondi
        tft.setTextSize(4);
        tft.setCursor(8, 30);
        tft.setTextColor(ST7735_GREEN, ST7735_BLACK);
        /* controlla che il resto della divisione per 2 del secondo corrente,
        richiesto mediante il metodo getSeconds() all’ntp library sia 0,
        questo comporta che il secondo è uno di quelli pari: 0,2,4,6,8,10….
        quando il secondo corrente è dispari, la stringa composta è priva del “:”
        */
        if ( (timeClient.getSeconds()%2) == 0 ) {
        tft.printf( “%02u:%02u”, hour(epoch), minute(epoch), second(epoch) );
        } else {
        tft.printf( “%02u %02u”, hour(epoch), minute(epoch), second(epoch) );
        }
        tft.setTextSize(2);
        }

        //———————————————————————–//
        void loop()
        {
        timeClient.update();
        epoch = timeClient.getEpochTime(); // ottenere il tempo Epoch

        Serial.print(“lettura… : “);
        Serial.println(epoch);
        Serial.println(getEpochStringByParams(CE.toLocal(now())));
        RTC_display();
        delay(300);
        }

      • #16411
        Renzo Mischianti
        Amministratore del forum

          Ciao Aldo,
          credo che la via più breve sia usare il formattatore che il C mette a disposizione, prendi spunto dalla funzione

          
          // Immettere il formato dell’ora tm e restituire la stringa con il modello di formato
          static String getDateTimeStringByParams(tm *newtime, char* pattern = (char *)”%d/%m/%Y %H:%M:%S”){
          char buffer[30];
          strftime(buffer, 30, pattern, newtime);
          return buffer;
          }
          

          del tipo

          
          char ora[2];
          strftime(ora, 2, "%H", newtime); 
          

          Ciao Renzo

        • #16418
          Aldo
          Partecipante

            unsigned long epoch = CE.toLocal(timeClient.getEpochTime());

            setTime(epoch);

            <hr />

            void RTC_display()

            {
            char dow_matrix[7][10] = {“DOMENICA”, “LUNEDI”, “MARTEDI”, “MERCOLEDI”, “GIOVEDI”, “VENERDI”, “SABATO”};
            byte x_pos[7] = {14, 25, 24, 13, 24, 24, 25};
            static byte previous_dow = 0;

            if( previous_dow != weekday(epoch) ) // stampa il giorno della settimana
            {
            previous_dow = weekday(epoch);
            tft.fillRect(9, 70, 112, 25, ST7735_BLACK); // disegnare un rettangolo (cancella il giorno dal display)
            tft.setCursor(x_pos[previous_dow-1], 77); // COORDINATA ALTEZZA SCRITTA GIORNO SETTIMANA
            tft.setTextColor(ST7735_YELLOW, ST7735_BLACK); // imposta il colore del testo su sfondo nero
            tft.print( dow_matrix[previous_dow-1] );
            tft.setTextSize(2);
            }

            // stampa la data
            tft.setTextSize(2);
            tft.setCursor(5, 105);
            tft.setTextColor(ST7735_MAGENTA, ST7735_BLACK);
            tft.printf( “%02u-%02u-%04u”, day(epoch), month(epoch), year(epoch) );
            // stampa il tempo con punti lampeggianti dei secondi
            tft.setTextSize(4);
            tft.setCursor(8, 30);
            tft.setTextColor(ST7735_GREEN, ST7735_BLACK);
            /* controlla che il resto della divisione per 2 del secondo corrente,
            richiesto mediante il metodo getSeconds() all’ntp library sia 0,
            questo comporta che il secondo è uno di quelli pari: 0,2,4,6,8,10….
            quando il secondo corrente è dispari, la stringa composta è priva del “:”
            */
            if ( (timeClient.getSeconds()%2) == 0 ) {
            tft.printf( “%02u:%02u”, hour(epoch), minute(epoch), second(epoch) );
            } else {
            tft.printf( “%02u %02u”, hour(epoch), minute(epoch), second(epoch) );
            }
            tft.setTextSize(2);
            }

            //———————————————————————–//
            void loop()
            {
            timeClient.update();
            epoch = CE.toLocal(timeClient.getEpochTime()); // ottenere il tempo Epoch

            //Serial.println(getEpochStringByParams(CE.toLocal(now())));
            Serial.println(getEpochStringByParams(now(), “%H:%M:%S %d.%m.%Y e’ un bel giorno %A di %B”));
            Serial.print(“Mio codice:”);
            RTC_display();
            delay(300);
            }

            grazie!!!!!!!!!!….cosi va bene!!!!

        Visualizzazione 2 filoni di risposte
        • Devi essere connesso per rispondere a questo topic.
        Exit mobile version