Kaynağa Gözat

Add OLED display functions and DHT22 sensor integration

dacowars 2 hafta önce
ebeveyn
işleme
be7a475e57
1 değiştirilmiş dosya ile 116 ekleme ve 0 silme
  1. 116 0
      main/main.c

+ 116 - 0
main/main.c

@@ -27,6 +27,15 @@ const char *OLED_TAG = "SSD1306OLED";
 #define PIN_NUM_CLK   CONFIG_PIN_CLK
 #define PIN_NUM_CS    CONFIG_PIN_CS
 
+#define PIN_NUM_SDA CONFIG_PIN_SDA
+#define PIN_NUM_SCL CONFIG_PIN_SCL
+
+//definicion de tiempos de pulsacion
+#define PULSACION_LARGA_MS 2000
+#define DURACION_WATCHDOG_MS 10000
+
+#define MEASUREMENT_INTERVAL_S 1
+
 const int uart_buffer_size = (1024 * 2);
 int length = 0;
 
@@ -41,6 +50,113 @@ static esp_err_t s_example_write_file(const char *path, char *data);
 static i2c_master_bus_handle_t i2c_bus0_init(gpio_num_t sda, gpio_num_t scl, uint32_t hz);
 
 
+struct SensorData {
+  double latitude;
+  double longitude;
+  float altura;
+  char tiempo;
+  float velocidad;
+  float temperature;
+  float humidity;
+  float pressure;
+};
+
+struct SensorData latestData;
+struct SensorData datosAntiguos;
+
+SemaphoreHandle_t dataMutex; // Mutex para proteger el acceso a latestData
+SemaphoreHandle_t buttonSemaphore; // Semáforo para la tarea del botón
+
+bool grabando = false; //inicia apagado
+bool finalizado = true; //indica que no hay ninguna grabacion ni iniciada ni pausada
+TaskHandle_t medicionesHandle = NULL; //para suspend/resume
+int pantallaEstado_grab = -1; //maquina de estados cuando se graba ruta
+int pantallaEstado_menu = -1; //maquina de estados cuando no se esta grabando ruta
+float distancia_total = 0.0;
+volatile unsigned long ignore_isr_until = 0; //para debounce
+
+char filename[13] = "/panchas.gpx";
+
+ssd1306_handle_t d = NULL;
+
+void OLED_test(){
+
+    i2c_master_bus_handle_t i2c_bus =
+        i2c_bus0_init(PIN_NUM_SDA, PIN_NUM_SCL, 400000);
+
+    ssd1306_config_t cfg = {
+        .width  = 128,
+        .height = 64,
+        .fb     = NULL, // let driver allocate internally
+        .fb_len = 0,
+        .iface.i2c =
+            {
+                .port     = I2C_NUM_0,
+                .addr     = 0x3C,        // typical SSD1306 I2C address
+                .rst_gpio = GPIO_NUM_NC, // no reset pin
+            },
+    };
+
+    ESP_ERROR_CHECK(ssd1306_new_i2c(&cfg, &d));
+
+    ESP_ERROR_CHECK(ssd1306_clear(d));
+
+    ESP_ERROR_CHECK(ssd1306_draw_rect(d, cfg.width / 2 - 50, cfg.height / 2 - 10, 100, 20, false));
+    
+    ESP_ERROR_CHECK(ssd1306_display(d));
+
+    ESP_ERROR_CHECK(ssd1306_draw_circle(d, cfg.width / 2, cfg.height / 2, 20, true));
+
+    ESP_ERROR_CHECK(ssd1306_display(d));
+    
+    ESP_ERROR_CHECK(ssd1306_draw_text_scaled(d, 0, 0, "Iniciando...", true, ));
+
+    ESP_ERROR_CHECK(ssd1306_display(d));
+
+    vTaskDelay(pdMS_TO_TICKS(1000));
+
+}
+
+void OLED_print(const char& line1, const char& line2){
+    
+    ESP_ERROR_CHECK(ssd1306_clear(d));
+
+    ESP_ERROR_CHECK(ssd1306_draw_text_scaled(d, 0, 0, line1, true, 2));
+    ESP_ERROR_CHECK(ssd1306_draw_text_scaled(d, 0, 40, line2, true, 2));
+
+    ESP_ERROR_CHECK(ssd1306_display(d));
+
+}
+
+void DHT_test (){
+    float t;
+    float h;
+
+    static esp_err_t err = dht_attach_pin();
+
+    if (err != ESP_OK) {
+        OLED_print("DHT22", "Error");
+        ESP_LOGE(DHT_TAG, "Failed to attach DHT pin: %s", esp_err_to_name(err));
+        vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
+        DHT_test();  //Reintentar
+    }
+    
+    err = dht_read(&t, &h);
+
+    if (err != ESP_OK) {
+        OLED_print("DHT22", "Error");
+        ESP_LOGE(DHT_TAG, "Failed to read DHT : %s", esp_err_to_name(err));
+        vTaskDelay(pdMS_TO_TICKS(5000)); // Wait for 5 seconds before retrying
+        DHT_test();  //Reintentar
+    }
+
+    else OLED_print("DHT22", "Correcto");
+}
+
+void SD_test(){
+    
+}
+
 void app_main(void)
 {
     // Initialize DHT22 sensor