RTC DS3231 ora UTC e ora locale

Salve a tutti, ho bisogno di aiuto. Mi sto costruendo un orologio che abbia sia l'ora UTC che l'ora locale, con un arduino nano, un DS3231 e oled 1306. Ho trovato uno sketch e ho cercato di adattarlo alle mie esigenze, ma sto avendo problemi con l'orario locale. Premetto che sono un novellino. Ora vi posto il mio sketch.




#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "RTClib.h"
#include <Timezone.h>    // https://github.com/JChristensen/Timezone


// UTC
TimeChangeRule utcRule = {"UTC", Last, Sun, Mar, 1, 0};     // UTC
Timezone UTC(utcRule);

// Central European Time (Rome)
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);
Timezone myTZ(CEST, CET);

// If TimeChangeRules are already stored in EEPROM, comment out the three
// lines above and uncomment the line below.
//Timezone myTZ(100);       //assumes rules stored at EEPROM address 100

TimeChangeRule *tcr;        //pointer to the time change rule, use to get TZ abbrev

RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Domenica", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato"};;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
  Serial.begin(9600);
  delay(3000); // wait for console opening
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2023, 6, 12, 21, 19, 0));
  }
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3C (for the 128x32)// Check your I2C address and enter it here, in Our case address is 0x3C
  display.clearDisplay();
  display.display(); // this command will display all the data which is in buffer
  display.setTextColor(WHITE, BLACK);
  draw_text(0, 25, "Ora Loc.", 1);
}

void loop() {

  time_t utc = now();
  time_t local = myTZ.toLocal(utc, &tcr);
    
  DateTime now = rtc.now();
  /*============Display Date=================*/
display.setTextSize(1);
display.setCursor(0,0);
display.print(daysOfTheWeek[now.dayOfTheWeek()]);
char currentDate [16];
uint8_t thisDay, thisMonth ;
thisDay = now.day();
thisMonth = now.month();
sprintf (currentDate, "%02d/%02d/", thisDay, thisMonth); //add leading zeros to the day and month
display.setTextSize(1);
display.setCursor(62,0);
display.print(currentDate);
display.setTextSize(1);
display.setCursor(102,0);
display.print(now.year(), DEC);
/*================ORA UTC================*/ 
char buffer [16];
uint8_t thisSec, thisMin, thisHour;
thisSec = now.second();
thisMin = now.minute();
thisHour = now.hour();
sprintf (buffer, "%02d:%02d:%02d", thisHour, thisMin, thisSec);
display.setTextSize(2);
display.setCursor(15,9);
display.print(buffer);
/*=============Ora Locale=====================*/

display.setTextSize(1);
display.setCursor(70,25);
display.print(local, tcr -> abbrev);
display.display(); 
}

void draw_text(byte x_pos, byte y_pos, char *text, byte text_size) {
  display.setCursor(x_pos, y_pos);
  display.setTextSize(text_size);
  display.print(text);
  display.display();

}

Non riesco ad implementare la libreria Timezone con il mio sketch.

Scusa, ma "abbrev" è un puntatore, ossia un indirizzo, ad una stringa (che tra l'altro è l'abbreviazione del timezone, non l'ora), come pretendi di visualizzare l'ora in quel modo?
Vedi gli esempi della libreria, ma anche se non ho attualmente sottomano modo di provare, penso che tu debba semplicemente fare una cosa di questo tipo:

  // display.print(local, tcr -> abbrev);
  sprintf(buffer, "%.2d:%.2d:%.2d", hour(local), minute(local), second(local));
  display.print(buffer);

Cercando con Google "Arduino timezone rtc", questo è il primo risultato:

Scusa, ma hai capito quale sia il problema dell'OP (non riesce a stampare il "local" time, al suo posto vede un numero "31251"), oltre alla mia risposta?

No, non avevo capito che è solo un problema di visualizzazione. Ecco perché non capivo la tua risposta! :smile:
D'altra parte, ha scritto solamente:

(...) sto avendo problemi con l'orario locale.
Non riesco a implementare la libreria Timezone con il mio sketch.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.