Skip to content

Instantly share code, notes, and snippets.

@IgorDePaula
Created October 2, 2025 02:46
Show Gist options
  • Select an option

  • Save IgorDePaula/eb208cd97ac6a8c9547b33a6b8751ce2 to your computer and use it in GitHub Desktop.

Select an option

Save IgorDePaula/eb208cd97ac6a8c9547b33a6b8751ce2 to your computer and use it in GitHub Desktop.

Revisions

  1. IgorDePaula created this gist Oct 2, 2025.
    197 changes: 197 additions & 0 deletions show_hour.ino
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,197 @@
    #include <Wire.h>
    #include <RTClib.h>
    #include <U8g2lib.h>

    // SOLUÇÃO: Display usa Software I2C (bit-bang)
    // RTC usa Hardware I2C (Wire)

    // Display OLED com Software I2C (não conflita!)
    U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0,
    /* clock=*/ 15,
    /* data=*/ 4,
    /* reset=*/ 16);

    // RTC DS3231 - Hardware I2C
    RTC_DS3231 rtc;

    // ========================================
    // CONFIGURE A HORA AQUI (se precisar)
    // ========================================
    bool FORCAR_AJUSTE = false;
    int ANO = 2025;
    int MES = 10;
    int DIA = 2;
    int HORA = 0;
    int MINUTO = 0;
    int SEGUNDO = 0;
    // ========================================

    bool rtcFuncionando = false;

    void setup() {
    Serial.begin(115200);
    delay(2000);

    Serial.println("\n=== HELTEC V2 + DS3231 (Software I2C Display) ===\n");

    // IMPORTANTE: Inicializa RTC PRIMEIRO
    Serial.println("Inicializando RTC DS3231...");
    Wire.begin(21, 22);
    Wire.setClock(100000);
    delay(200);

    // Scanner I2C
    Serial.println("Scanner I2C:");
    int dispositivos = 0;
    for (byte addr = 1; addr < 127; addr++) {
    Wire.beginTransmission(addr);
    byte erro = Wire.endTransmission();

    if (erro == 0) {
    Serial.print(" Encontrado: 0x");
    if (addr < 16) Serial.print("0");
    Serial.println(addr, HEX);
    dispositivos++;
    }
    }

    if (dispositivos == 0) {
    Serial.println(" NENHUM dispositivo I2C encontrado!");
    Serial.println("\n*** PROBLEMA DE HARDWARE ***");
    Serial.println("Verifique:");
    Serial.println("1. Conexoes: SDA=21, SCL=22, VCC=3.3V, GND=GND");
    Serial.println("2. Resistores pull-up (4.7k) no SDA e SCL");
    Serial.println("3. Alimentacao do modulo DS3231");
    Serial.println("4. Bateria CR2032 instalada");
    while(1) delay(1000);
    }

    Serial.print(" Total: ");
    Serial.print(dispositivos);
    Serial.println(" dispositivo(s)\n");

    // Inicializa RTC
    if (!rtc.begin()) {
    Serial.println("ERRO: DS3231 nao responde!");
    Serial.println("\nPossivel causa:");
    Serial.println("- Resistores pull-up ausentes ou muito fracos");
    Serial.println("- Modulo DS3231 defeituoso");
    Serial.println("- Bateria ausente");
    while(1) delay(1000);
    }

    Serial.println("RTC inicializado com sucesso!\n");
    rtcFuncionando = true;

    // Verifica se precisa ajustar
    if (rtc.lostPower() || FORCAR_AJUSTE) {
    Serial.println("Ajustando horario...");

    if (FORCAR_AJUSTE) {
    rtc.adjust(DateTime(ANO, MES, DIA, HORA, MINUTO, SEGUNDO));
    Serial.printf("Horario configurado: %02d/%02d/%04d %02d:%02d:%02d\n",
    DIA, MES, ANO, HORA, MINUTO, SEGUNDO);
    } else {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    Serial.println("Horario ajustado para hora da compilacao");
    }

    delay(1000);
    }

    // Mostra status inicial
    DateTime now = rtc.now();
    Serial.println("Status do RTC:");
    Serial.printf("Data/Hora: %02d/%02d/%04d %02d:%02d:%02d\n",
    now.day(), now.month(), now.year(),
    now.hour(), now.minute(), now.second());
    Serial.print("Temperatura: ");
    Serial.print(rtc.getTemperature());
    Serial.println(" C\n");

    // AGORA inicializa o display (Software I2C - não conflita!)
    Serial.println("Inicializando display OLED...");
    u8g2.begin();
    Serial.println("Display inicializado!\n");

    Serial.println("=== Sistema funcionando! ===\n");
    exibirMensagem("Sistema", "Pronto!", "", "");
    delay(1500);
    }

    void loop() {
    if (!rtcFuncionando) {
    exibirMensagem("ERRO", "RTC OFF", "", "");
    delay(1000);
    return;
    }

    DateTime now = rtc.now();

    // Verifica se leitura é válida
    if (now.year() < 2020 || now.year() > 2100) {
    Serial.println("AVISO: Leitura invalida do RTC!");
    exibirMensagem("AVISO!", "Leitura", "invalida", "");
    delay(2000);
    return;
    }

    // Formata strings para o display
    char linha1[20];
    char linha2[20];
    char linha3[20];
    char linha4[20];

    sprintf(linha1, "Heltec V2");
    sprintf(linha2, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
    sprintf(linha3, "%02d/%02d/%04d", now.day(), now.month(), now.year());
    sprintf(linha4, "%.1fC", rtc.getTemperature());

    exibirMensagem(linha1, linha2, linha3, linha4);

    // Debug no serial a cada 5 segundos
    static unsigned long ultimo = 0;
    if (millis() - ultimo > 5000) {
    ultimo = millis();
    Serial.printf("%02d/%02d/%04d %02d:%02d:%02d | %.1fC\n",
    now.day(), now.month(), now.year(),
    now.hour(), now.minute(), now.second(),
    rtc.getTemperature());
    }

    delay(500);
    }

    // ===== FUNÇÃO DE EXIBIÇÃO NO DISPLAY =====
    void exibirMensagem(const char* linha1, const char* linha2,
    const char* linha3, const char* linha4) {
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_9x15_tf);

    int y = 16;

    if (strlen(linha1) > 0) {
    int w = u8g2.getStrWidth(linha1);
    u8g2.drawStr((128 - w) / 2, y, linha1);
    }
    y += 16;

    if (strlen(linha2) > 0) {
    int w = u8g2.getStrWidth(linha2);
    u8g2.drawStr((128 - w) / 2, y, linha2);
    }
    y += 16;

    if (strlen(linha3) > 0) {
    int w = u8g2.getStrWidth(linha3);
    u8g2.drawStr((128 - w) / 2, y, linha3);
    }
    y += 16;

    if (strlen(linha4) > 0) {
    int w = u8g2.getStrWidth(linha4);
    u8g2.drawStr((128 - w) / 2, y, linha4);
    }

    u8g2.sendBuffer();
    }