// Esp Sensor Luz
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "esp_wifi.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
#include <stdlib.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "esp_log.h"
#include "mqtt_client.h"
#include "sdkconfig.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include <driver/adc.h>
#include "driver/ledc.h"
#include "driver/i2c.h"
#include "esp_err.h"
// Definir etiquetas de log
static const char *TAG = "i2c_example";
// Definir pines y puerto I2C
#define I2C_MASTER_SCL_IO 22 // Pin de SCL
#define I2C_MASTER_SDA_IO 21 // Pin de SDA
#define I2C_MASTER_NUM I2C_NUM_1 // Número del puerto I2C
#define I2C_MASTER_FREQ_HZ 100000 // Frecuencia I2C
#define I2C_MASTER_TX_BUF_DISABLE 0 // No necesitamos buffer de TX
#define I2C_MASTER_RX_BUF_DISABLE 0 // No necesitamos buffer de RX
#define MPU6050_SENSOR_ADDR 0x68 // Dirección del sensor MPU6050
#define MPU6050_CMD_START 0x41 // Registro de temperatura del MPU6050
#define MPU6050_PWR_MGMT_1 0x6B // Registro de gestión de energía
#define MPU6050_RESET 0x80 // Comando de reinicio
#define LED_RED_PIN 2
#define LED_GREEN_PIN 4
#define LED_BLUE_PIN 16
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} color_t;
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_LOW_SPEED_MODE,
.timer_num = LEDC_TIMER_0,
.duty_resolution = LEDC_TIMER_13_BIT,
.freq_hz = 1000,
.clk_cfg = LEDC_AUTO_CLK
};
ledc_channel_config_t ledc_channel[3];
float celsiusGlobal;
float celsiusAnterior = 0;
static esp_err_t i2c_master_init() {
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = I2C_MASTER_SCL_IO,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
esp_err_t err = i2c_param_config(I2C_MASTER_NUM, &conf);
if (err != ESP_OK) {
return err;
}
return i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}
static esp_err_t mpu6050_init() {
// Despertar MPU6050
uint8_t reset_cmd[] = {MPU6050_PWR_MGMT_1, 0x00};
return i2c_master_write_to_device(I2C_MASTER_NUM, MPU6050_SENSOR_ADDR, reset_cmd, sizeof(reset_cmd), 1000 / portTICK_RATE_MS);
}
static esp_err_t mpu6050_read_temperature(float *temperature) {
uint8_t cmd = MPU6050_CMD_START;
uint8_t temp_data[2];
esp_err_t err = i2c_master_write_read_device(I2C_MASTER_NUM, MPU6050_SENSOR_ADDR, &cmd, 1, temp_data, 2, 1000 / portTICK_RATE_MS);
if (err != ESP_OK) {
return err;
}
int16_t raw_temp = (int16_t)(temp_data[0] << 8 | temp_data[1]);
*temperature = (raw_temp / 340.0) + 36.53;
return ESP_OK;
}
static void init_leds(void) {
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
ledc_channel[0].channel = LEDC_CHANNEL_0;
ledc_channel[0].gpio_num = LED_RED_PIN;
ledc_channel[1].channel = LEDC_CHANNEL_1;
ledc_channel[1].gpio_num = LED_GREEN_PIN;
ledc_channel[2].channel = LEDC_CHANNEL_2;
ledc_channel[2].gpio_num = LED_BLUE_PIN;
for (int i = 0; i < 3; i++) {
ledc_channel[i].speed_mode = LEDC_LOW_SPEED_MODE;
ledc_channel[i].timer_sel = LEDC_TIMER_0;
ledc_channel[i].intr_type = LEDC_INTR_DISABLE;
ledc_channel[i].duty = 0;
ledc_channel[i].hpoint = 0;
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel[i]));
}
}
static void setColor(color_t color) {
uint32_t red_duty = 8191 * color.r / 255;
uint32_t green_duty = 8191 * color.g / 255;
uint32_t blue_duty = 8191 * color.b / 255;
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, red_duty));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1, green_duty));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_1));
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2, blue_duty));
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_2));
}
void app_main() {
ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully");
ESP_ERROR_CHECK(mpu6050_init());
ESP_LOGI(TAG, "MPU6050 initialized successfully");
init_leds();
const color_t red = { .r = 255, .g = 0, .b = 0 };
const color_t green = { .r = 0, .g = 255, .b = 0 };
const color_t blue = { .r = 0, .g = 0, .b = 255 };
const color_t yellow = { .r = 255, .g = 255, .b = 0 };
const color_t purple = { .r = 128, .g = 0, .b = 128 };
const color_t orange = { .r = 255, .g = 165, .b = 0 };
while (true) {
vTaskDelay(pdMS_TO_TICKS(2000));
float temperature;
esp_err_t err = mpu6050_read_temperature(&temperature);
if (err == ESP_OK) {
celsiusGlobal = temperature;
// Imprimir la temperatura actual y anterior en el terminal
printf("Temperatura actual: %f ℃, Temperatura anterior: %f ℃\n", celsiusGlobal, celsiusAnterior);
// Control de los LEDs basado en la temperatura
if (celsiusGlobal >= 60) {
setColor(red);
} else if (celsiusGlobal < 60 && celsiusGlobal >= 30) {
setColor(purple);
} else if (celsiusGlobal < 30 && celsiusGlobal >= 0) {
setColor(yellow);
} else if (celsiusGlobal < 0) {
setColor(blue);
}
// Control del segundo LED basado en el cambio de temperatura
if (celsiusGlobal > celsiusAnterior) {
setColor(orange); // Cambiar el color del segundo LED a naranja
} else if (celsiusGlobal < celsiusAnterior) {
setColor(blue); // Cambiar el color del segundo LED a azul
} else {
setColor(green); // Cambiar el color del segundo LED a verde
}
// Actualizar la temperatura anterior
celsiusAnterior = celsiusGlobal;
} else {
ESP_LOGE(TAG, "Failed to read temperature from MPU6050: %s", esp_err_to_name(err));
}
}
}