| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- #include "TinyGPSPlus.h"
- #include "DHT.h"
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include "FS.h"
- #include "SD.h"
- #include "SPI.h"
- /*
- Uncomment and set up if you want to use custom pins for the SPI communication
- #define REASSIGN_PINS
- int sck = -1;
- int miso = -1;
- int mosi = -1;
- int cs = -1;
- */
- #define SCREEN_WIDTH 128 // Ancho píxeles
- #define SCREEN_HEIGHT 64 // Alto píxeles
- #define OLED_RESET -1 // Reset pin (no usado)
- // Pines para UART2 (Serial2)
- #define RX_PIN 16 // RX del ESP32 conectado a TX del GPS
- #define TX_PIN 17 // TX del ESP32 conectado a RX del GPS
- #define GPS_BAUD 115200
- #define DHTPIN 4 // Digital pin connected to the DHT sensor
- #define DHTTYPE DHT22
- // Objeto TinyGPS++
- TinyGPSPlus gps;
- // Serial para GPS
- HardwareSerial gpsSerial(2);
- // Configuracion del DHT
- DHT dht(DHTPIN, DHTTYPE);
- // Bandera volátil para interrupción
- volatile bool gpsFlag = false;
- void ARDUINO_ISR_ATTR onTimer(void) {
- gpsFlag = true; // Activa bandera para procesar GPS en el loop
- }
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- void DHT_test() {
- float h = dht.readHumidity();
- float t = dht.readTemperature();
- if (isnan(h) || isnan(t)) {
- Serial.println("Failed to read from DHT sensor!");
- return;
- }
- Serial.print("Humidity: ");
- Serial.print(h);
- Serial.print(" %\t");
- Serial.print("Temperature: ");
- Serial.print(t);
- Serial.println(" *C");
- }
- void OLED_test() { //pantallazo a blanco y luego iniciando
- display.clearDisplay();
- display.fillScreen(SSD1306_WHITE); // Pantalla blanca
- display.display();
- display.clearDisplay();
- display.setTextSize(2); // Tamaño texto
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0); // Posición
- display.println("¡Hola ESP32!");
- display.println("OLED OK!");
- display.display(); // Muestra
- delay(1000);
- }
- void setup() {
- Serial.begin(115200);
- Serial.println(F("DHTxx test!"));
- dht.begin();
- Serial.println("Iniciando prueba de GPS con temporizador (API 3.x)...");
- // Iniciar Serial2 para GPS
- gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RX_PIN, TX_PIN);
- // Configurar temporizador hardware con frecuencia de 1 MHz (tick cada 1 us)
- hw_timer_t *timer = timerBegin(1000000); // Frecuencia en Hz
-
- // Adjuntar interrupción al temporizador
- timerAttachInterrupt(timer, &onTimer);
-
- // Configurar alarma para 1 segundo (1,000,000 ticks), autoreload activado, unlimited
- timerAlarm(timer, 1000000, true, 0);
- // Inicia I2C en pines default ESP32 (21 SDA, 22 SCL)
- if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Dirección común: 0x3C
- Serial.println(F("Error: OLED no encontrado!"));
- for (;;); // Para siempre
- }
- display.clearDisplay();
- display.setTextSize(2); // Tamaño texto
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0, 0); // Posición
- display.println("¡Hola ESP32!");
- display.println("OLED OK!");
- display.display(); // Muestra
- delay(1000);
- // SD Card initialization
- if (!SD.begin()) {
- Serial.println("SD Card Mount Failed");
- } else {
- Serial.println("SD Card Mounted");
- }
- OLED_test();
- // DHT check
- DHT_test();
- }
- //revisar toda esta parte, generada por copilot##########################################################
- void loop() {
- // GPS check
- if (gpsFlag) {
- gpsFlag = false;
- while (gpsSerial.available() > 0) {
- gps.encode(gpsSerial.read());
- }
- if (gps.location.isValid()) {
- Serial.print("Latitude: ");
- Serial.println(gps.location.lat(), 6);
- Serial.print("Longitude: ");
- Serial.println(gps.location.lng(), 6);
- } else {
- Serial.println("GPS: Waiting for fix...");
- }
- }
- // SD Card check (write and read once)
- static bool sdChecked = false;
- if (!sdChecked) {
- sdChecked = true;
- File file = SD.open("test.txt", FILE_WRITE);
- if (file) {
- file.printf("Temp: %.1f C, Hum: %.1f %%\n", t, h);
- if (gps.location.isValid()) {
- file.printf("Lat: %.4f, Lon: %.4f\n", gps.location.lat(), gps.location.lng());
- } else {
- file.println("GPS: No fix");
- }
- file.close();
- Serial.println("Data written to SD card");
- } else {
- Serial.println("Failed to open file for writing on SD");
- }
- // Read back
- file = SD.open("test.txt", FILE_READ);
- if (file) {
- Serial.println("Reading from SD card:");
- while (file.available()) {
- Serial.write(file.read());
- }
- file.close();
- } else {
- Serial.println("Failed to open file for reading on SD");
- }
- }
- }
|