V1.ino 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 setup() {
  39. Serial.begin(115200);
  40. Serial.println(F("DHTxx test!"));
  41. dht.begin();
  42. Serial.println("Iniciando prueba de GPS con temporizador (API 3.x)...");
  43. // Iniciar Serial2 para GPS
  44. gpsSerial.begin(GPS_BAUD, SERIAL_8N1, RX_PIN, TX_PIN);
  45. // Configurar temporizador hardware con frecuencia de 1 MHz (tick cada 1 us)
  46. hw_timer_t *timer = timerBegin(1000000); // Frecuencia en Hz
  47. // Adjuntar interrupción al temporizador
  48. timerAttachInterrupt(timer, &onTimer);
  49. // Configurar alarma para 1 segundo (1,000,000 ticks), autoreload activado, unlimited
  50. timerAlarm(timer, 1000000, true, 0);
  51. // Inicia I2C en pines default ESP32 (21 SDA, 22 SCL)
  52. if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Dirección común: 0x3C
  53. Serial.println(F("Error: OLED no encontrado!"));
  54. for (;;); // Para siempre
  55. }
  56. display.clearDisplay();
  57. display.setTextSize(2); // Tamaño texto
  58. display.setTextColor(SSD1306_WHITE);
  59. display.setCursor(0, 0); // Posición
  60. display.println("¡Hola ESP32!");
  61. display.println("OLED OK!");
  62. display.display(); // Muestra
  63. delay(1000);
  64. // SD Card initialization
  65. if (!SD.begin()) {
  66. Serial.println("SD Card Mount Failed");
  67. } else {
  68. Serial.println("SD Card Mounted");
  69. }
  70. }
  71. //revisar toda esta parte, generada por copilot##########################################################
  72. void loop() {
  73. // DHT check
  74. float h = dht.readHumidity();
  75. float t = dht.readTemperature();
  76. if (isnan(h) || isnan(t)) {
  77. Serial.println("Failed to read from DHT sensor!");
  78. } else {
  79. Serial.print("Humidity: ");
  80. Serial.print(h);
  81. Serial.print(" %\t");
  82. Serial.print("Temperature: ");
  83. Serial.print(t);
  84. Serial.println(" *C");
  85. }
  86. // GPS check
  87. if (gpsFlag) {
  88. gpsFlag = false;
  89. while (gpsSerial.available() > 0) {
  90. gps.encode(gpsSerial.read());
  91. }
  92. if (gps.location.isValid()) {
  93. Serial.print("Latitude: ");
  94. Serial.println(gps.location.lat(), 6);
  95. Serial.print("Longitude: ");
  96. Serial.println(gps.location.lng(), 6);
  97. } else {
  98. Serial.println("GPS: Waiting for fix...");
  99. }
  100. }
  101. // SD Card check (write and read once)
  102. static bool sdChecked = false;
  103. if (!sdChecked) {
  104. sdChecked = true;
  105. File file = SD.open("test.txt", FILE_WRITE);
  106. if (file) {
  107. file.printf("Temp: %.1f C, Hum: %.1f %%\n", t, h);
  108. if (gps.location.isValid()) {
  109. file.printf("Lat: %.4f, Lon: %.4f\n", gps.location.lat(), gps.location.lng());
  110. } else {
  111. file.println("GPS: No fix");
  112. }
  113. file.close();
  114. Serial.println("Data written to SD card");
  115. } else {
  116. Serial.println("Failed to open file for writing on SD");
  117. }
  118. // Read back
  119. file = SD.open("test.txt", FILE_READ);
  120. if (file) {
  121. Serial.println("Reading from SD card:");
  122. while (file.available()) {
  123. Serial.write(file.read());
  124. }
  125. file.close();
  126. } else {
  127. Serial.println("Failed to open file for reading on SD");
  128. }
  129. }
  130. // Update OLED
  131. display.clearDisplay();
  132. display.setCursor(0, 0);
  133. display.setTextSize(1); // Smaller text for more info
  134. display.printf("Temp: %.1f C\nHum: %.1f %%\n", t, h);
  135. if (gps.location.isValid()) {
  136. display.printf("Lat: %.4f\nLon: %.4f", gps.location.lat(), gps.location.lng());
  137. } else {
  138. display.println("GPS: No fix");
  139. }
  140. display.display();
  141. delay(1000);
  142. }