Quellcode durchsuchen

Contenido generado de checkeo inicial

dacowars vor 2 Wochen
Ursprung
Commit
09fe1cec6e
1 geänderte Dateien mit 117 neuen und 2 gelöschten Zeilen
  1. 117 2
      V1/V1.ino

+ 117 - 2
V1/V1.ino

@@ -1,5 +1,24 @@
 #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
@@ -25,6 +44,8 @@ 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 setup() {
   Serial.begin(115200);
 
@@ -45,8 +66,102 @@ void setup() {
   // 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");
+  }
 
+}
+//revisar toda esta parte, generada por copilot##########################################################
 void loop() {
-  
+  // DHT check
+  float h = dht.readHumidity();
+  float t = dht.readTemperature();
+  if (isnan(h) || isnan(t)) {
+    Serial.println("Failed to read from DHT sensor!");
+  } else {
+    Serial.print("Humidity: ");
+    Serial.print(h);
+    Serial.print(" %\t");
+    Serial.print("Temperature: ");
+    Serial.print(t);
+    Serial.println(" *C");
+  }
+
+  // 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");
+    }
+  }
+
+  // Update OLED
+  display.clearDisplay();
+  display.setCursor(0, 0);
+  display.setTextSize(1); // Smaller text for more info
+  display.printf("Temp: %.1f C\nHum: %.1f %%\n", t, h);
+  if (gps.location.isValid()) {
+    display.printf("Lat: %.4f\nLon: %.4f", gps.location.lat(), gps.location.lng());
+  } else {
+    display.println("GPS: No fix");
+  }
+  display.display();
+  delay(1000);
 }