V1.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. dht.begin();
  40. float h = dht.readHumidity();
  41. float t = dht.readTemperature();
  42. if (isnan(h) || isnan(t)) {
  43. Serial.println("Failed to read from DHT sensor!");
  44. OLED_print("DHT22", "Error");
  45. while(1); // Detener si hay error
  46. }
  47. else OLED_print("DHT22", "Correcto");
  48. }
  49. void OLED_test() { //pantallazo a blanco y luego iniciando
  50. // Inicia I2C en pines default ESP32 (21 SDA, 22 SCL)
  51. if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Dirección común: 0x3C
  52. Serial.println(F("Error: OLED no encontrado!"));
  53. for (;;); // Para siempre
  54. }
  55. display.clearDisplay();
  56. display.fillScreen(SSD1306_WHITE); // Pantalla blanca
  57. delay(500);
  58. display.display();
  59. display.clearDisplay();
  60. display.setTextSize(2); // Tamaño texto
  61. display.setTextColor(SSD1306_WHITE);
  62. display.setCursor(0, 0); // Posición
  63. display.println("Iniciando...");
  64. display.display(); // Muestra
  65. delay(1000);
  66. }
  67. void OLED_print(const char* line1, const char* line2) {
  68. display.clearDisplay();
  69. display.setTextSize(1);
  70. display.setTextColor(SSD1306_WHITE);
  71. display.setCursor(0, 0);
  72. display.println(line1);
  73. display.println(line2);
  74. display.display();
  75. }
  76. void SD_test() {
  77. if (!SD.begin()) {
  78. Serial.println("SD Card Mount Failed");
  79. OLED_print("SD Card", "Error");
  80. while(1); // Detener si hay error
  81. }
  82. else OLED_print("SD Card", "Correcto");
  83. }
  84. void GPS_test_wait() {
  85. // Iniciar Serial2 para GPS
  86. gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RX_PIN, TX_PIN);
  87. while ((gpsSerial.available() > 0) && !gps.location.isValid()) {
  88. gps.encode(gpsSerial.read());
  89. delay(10);
  90. OLED_print("GPS", "Esperando.");
  91. delay(100);
  92. OLED_print("GPS", "Esperando..");
  93. delay(100);
  94. OLED_print("GPS", "Esperando...");
  95. }
  96. OLED_print("GPS", "Encontrado");
  97. }
  98. void setup() {
  99. Serial.begin(115200);
  100. // Configurar temporizador hardware con frecuencia de 1 MHz (tick cada 1 us)
  101. hw_timer_t *timer = timerBegin(1000000); // Frecuencia en Hz
  102. // Adjuntar interrupción al temporizador
  103. timerAttachInterrupt(timer, &onTimer);
  104. // Configurar alarma para 1 segundo (1,000,000 ticks), autoreload activado, unlimited
  105. timerAlarm(timer, 1000000, true, 0);
  106. // OLED check
  107. OLED_test();
  108. delay(500);
  109. // DHT check
  110. DHT_test();
  111. delay(500);
  112. // SD Card check
  113. SD_test();
  114. delay(500);
  115. // GPS check
  116. GPS_test_wait();
  117. delay(2000);
  118. }
  119. void loop() {
  120. }