#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <RTClib.h>
#include <SPI.h>
#include <SD.h>
#define SCREEN_WIDTH 128 // Largura do display em pixels
#define SCREEN_HEIGHT 64 // Altura do display em pixels
#define OLED_RESET -1 // Pino de reset do OLED (-1 se não for usado)
#define LED_PIN 2 // Pino do LED integrado no ESP32
boolean state = false; // Variável global para o estado do LED
unsigned long previousMillis = 0; // Armazena o tempo da última mudança de estado
const long interval = 1000; // Intervalo de 1 segundo para piscar o LED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
RTC_DS1307 rtc;
File myFile;
const int CS = 5;
/*
SD Card pinout:
DO/MISO - pin 23
DI/MOSI - pin 19
SCK/CLK - pin 18
CS - pin 5
*/
void setup() {
Serial.begin(115200);
Serial.println("Iniciando...");
// Configura o LED como saída
pinMode(LED_PIN, OUTPUT);
// Inicialização do display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Endereço I2C 0x3C para display 128x64
Serial.println(F("Erro ao iniciar o display SSD1306"));
for (;;); // Loop infinito em caso de erro
}
Serial.println("Display ok!");
if (!rtc.begin()) {
Serial.println("Erro ao inicializar o RTC.");
display.println("Erro RTC");
display.display();
while (1);
}
Serial.begin(9600); // Set serial baud rate to 9600
delay(500);
while (!Serial) { ; } // wait for serial port to connect. Needed for native USB port only
Serial.println("Initializing SD card...");
if (!SD.begin(CS)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
WriteFile("/test.txt", "Ok!");
ReadFile("/test.txt");
}
void loop() {
DateTime now = rtc.now();
// Pisca o LED lentamente
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis; // Atualiza o tempo da última mudança
state = !state; // Alterna o estado do LED
digitalWrite(LED_PIN, state ? HIGH : LOW);
}
// Limpa o display
display.clearDisplay();
// Configura o texto a ser exibido
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
char dateStr[11]; // "dd/mm/yyyy" = 10 chars + 1 para o terminador null
sprintf(dateStr, "%02d/%02d/%04d", now.day(), now.month(), now.year());
display.println(dateStr);
display.setTextSize(2);
display.setCursor(0, 20);
char timeStr[9]; // "hh:mm:ss" = 8 chars + 1 para o terminador null
sprintf(timeStr, "%02d:%02d:%02d", now.hour(), now.minute(), now.second());
display.println(timeStr);
display.display(); // Atualiza o display para mostrar o texto
}
////////////////////////////////
void WriteFile(const char * path, const char * message){
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open(path, FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.printf("Writing to %s ", path);
myFile.println(message);
myFile.close(); // close the file:
Serial.println("completed.");
}
// if the file didn't open, print an error:
else {
Serial.println("error opening file ");
Serial.println(path);
}
}
void ReadFile(const char * path){
// open the file for reading:
myFile = SD.open(path);
if (myFile) {
Serial.printf("Reading file from %s\n", path);
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close(); // close the file:
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
Loading
ssd1306
ssd1306