V1.ino 4.5 KB

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