V1.ino 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "TinyGPSPlus.h"
  2. #include "DHT.h"
  3. #include <Wire.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6. #include "FS.h"
  7. #include "SD.h"
  8. #include "SPI.h"
  9. /*
  10. Uncomment and set up if you want to use custom pins for the SPI communication
  11. #define REASSIGN_PINS
  12. int sck = -1;
  13. int miso = -1;
  14. int mosi = -1;
  15. int cs = -1;
  16. */
  17. #define SCREEN_WIDTH 128 // Ancho píxeles
  18. #define SCREEN_HEIGHT 64 // Alto píxeles
  19. #define OLED_RESET -1 // Reset pin (no usado)
  20. // Pines para UART2 (Serial2)
  21. #define RX_PIN 16 // RX del ESP32 conectado a TX del GPS
  22. #define TX_PIN 17 // TX del ESP32 conectado a RX del GPS
  23. #define GPS_BAUD 115200
  24. #define DHTPIN 4 // Digital pin connected to the DHT sensor
  25. #define DHTTYPE DHT22
  26. // Objeto TinyGPS++
  27. TinyGPSPlus gps;
  28. // Serial para GPS
  29. HardwareSerial gpsSerial(2);
  30. // Configuracion del DHT
  31. DHT dht(DHTPIN, DHTTYPE);
  32. // Bandera volátil para interrupción
  33. volatile bool gpsFlag = false;
  34. void ARDUINO_ISR_ATTR onTimer(void) {
  35. gpsFlag = true; // Activa bandera para procesar GPS en el loop
  36. }
  37. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  38. void DHT_test() {
  39. float h = dht.readHumidity();
  40. float t = dht.readTemperature();
  41. if (isnan(h) || isnan(t)) {
  42. Serial.println("Failed to read from DHT sensor!");
  43. return;
  44. }
  45. Serial.print("Humidity: ");
  46. Serial.print(h);
  47. Serial.print(" %\t");
  48. Serial.print("Temperature: ");
  49. Serial.print(t);
  50. Serial.println(" *C");
  51. }
  52. void OLED_test() { //pantallazo a blanco y luego iniciando
  53. display.clearDisplay();
  54. display.fillScreen(SSD1306_WHITE); // Pantalla blanca
  55. display.display();
  56. delay(5000);
  57. display.clearDisplay();
  58. display.setTextSize(2); // Tamaño texto
  59. display.setTextColor(SSD1306_WHITE);
  60. display.setCursor(0, 0); // Posición
  61. display.println("¡Hola ESP32!");
  62. display.println("OLED OK!");
  63. display.display(); // Muestra
  64. delay(1000);
  65. }
  66. void setup() {
  67. Serial.begin(115200);
  68. Serial.println(F("DHTxx test!"));
  69. dht.begin();
  70. Serial.println("Iniciando prueba de GPS con temporizador (API 3.x)...");
  71. // Iniciar Serial2 para GPS
  72. gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RX_PIN, TX_PIN);
  73. // Configurar temporizador hardware con frecuencia de 1 MHz (tick cada 1 us)
  74. hw_timer_t *timer = timerBegin(1000000); // Frecuencia en Hz
  75. // Adjuntar interrupción al temporizador
  76. timerAttachInterrupt(timer, &onTimer);
  77. // Configurar alarma para 1 segundo (1,000,000 ticks), autoreload activado, unlimited
  78. timerAlarm(timer, 1000000, true, 0);
  79. // Inicia I2C en pines default ESP32 (21 SDA, 22 SCL)
  80. if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Dirección común: 0x3C
  81. Serial.println(F("Error: OLED no encontrado!"));
  82. for (;;); // Para siempre
  83. }
  84. display.clearDisplay();
  85. display.setTextSize(2); // Tamaño texto
  86. display.setTextColor(SSD1306_WHITE);
  87. display.setCursor(0, 0); // Posición
  88. display.println("¡Hola ESP32!");
  89. display.println("OLED OK!");
  90. display.display(); // Muestra
  91. delay(1000);
  92. // SD Card initialization
  93. if (!SD.begin()) {
  94. Serial.println("SD Card Mount Failed");
  95. } else {
  96. Serial.println("SD Card Mounted");
  97. }
  98. OLED_test();
  99. // DHT check
  100. DHT_test();
  101. }
  102. //revisar toda esta parte, generada por copilot##########################################################
  103. void loop() {
  104. }