#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 "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"
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
#include "esp_log.h"
#include "mqtt_client.h"
#include "driver/gpio.h"
#include "driver/uart.h"
#include "math.h"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
#define COL 8
#define REN 7
esp_mqtt_client_handle_t client;
void wifi_connection(void);
static void wifi_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data);
static void mqtt_app_start(void);
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
esp_mqtt_event_handle_t event = event_data;
esp_mqtt_client_handle_t client = event->client;
int msg_id;
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
printf("Conectado. ");
msg_id = esp_mqtt_client_subscribe(client, "/<1273699>/cliente1/chat1", 0);
//ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "/<1273699>/cliente2/chat1", 1);
//ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_DISCONNECTED:
break;
case MQTT_EVENT_SUBSCRIBED:
printf("Suscrito exitosamente.\n");
//ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
msg_id = esp_mqtt_client_publish(client, "/<1273699>/cliente1/chat1", "data", 0, 0, 0);
//ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
break;
case MQTT_EVENT_PUBLISHED:
printf("Publicado exitosamente.\n");
break;
case MQTT_EVENT_DATA:
break;
case MQTT_EVENT_ERROR:
break;
default:
break;
}
}
static void mqtt_app_start(void)
{
esp_mqtt_client_config_t mqtt_cfg = {
/* Configure el broker aquí */
.broker.address.uri = "mqtt://test.mosquitto.org:1883",
};
client = esp_mqtt_client_init(&mqtt_cfg);
/* El último argumento se puede utilizar para pasar datos al controlador de eventos, en este ejemplo mqtt_event_handler */
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
printf("Iniciando MQTT... \n");
esp_mqtt_client_start(client);
}
void wifi_connection(void)
{
esp_netif_init();
esp_event_loop_create_default();
esp_netif_create_default_wifi_sta();
wifi_init_config_t wifi_initiation = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&wifi_initiation);
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_event_handler, NULL);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, wifi_event_handler, NULL);
wifi_config_t wifi_configuration = {
/* Configure la conexión WiFi aquí */
.sta = {
.ssid = WIFI_SSID,
.password = WIFI_PASS,
},
};
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_config(WIFI_IF_STA, &wifi_configuration);
esp_wifi_start();
esp_wifi_connect();
}
static void wifi_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
switch (event_id)
{
case WIFI_EVENT_STA_START:
printf("WiFi conectándose...\n");
break;
case WIFI_EVENT_STA_CONNECTED:
printf("WiFi conectado.\n");
break;
case WIFI_EVENT_STA_DISCONNECTED:
printf("WiFi desconectado.\n");
break;
case IP_EVENT_STA_GOT_IP:
printf("WiFi obtuvo IP.\n\n");
break;
default:
printf("ID del evento: %ld\n", event_id);
break;
}
}
void uartPuts(const char *str){
uart_write_bytes(0, str,strlen(str));
}
void uartPutchar(char c){
uart_write_bytes(0, &c,sizeof(c));
}
void uartKbnit(void){
int length = 0;
uart_get_buffered_data_len(0, (size_t*)&length);
//return (length > 0);
}
void uartGetchar(void){
/*char c;
while (uartKbnit == 0){
}*/
}
void printMatrix(){
}
void app_main(void)
{
char tecla;
const uart_port_t uart_num = UART_NUM_0;
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
.rx_flow_ctrl_thresh = 122,
};
// Configure UART parameters
ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
// Set UART pins(TX: IO4, RX: IO5, RTS: IO18, CTS: IO19)
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_0, 4, 5, 18, 19));
// Setup UART buffered IO with event queue
const int uart_buffer_size = (1024 * 2);
QueueHandle_t uart_queue;
// Install UART driver using an event queue here
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, uart_buffer_size, \
uart_buffer_size, 10, &uart_queue, 0));
// Write data to UART.
char* test_str = "This is a test string.\n";
uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));
// Write data to UART, end with a break signal.
uart_write_bytes_with_break(uart_num, "test break\n",strlen("test break\n"), 100);
nvs_flash_init();
wifi_connection();
vTaskDelay(3000 / portTICK_PERIOD_MS);
mqtt_app_start();
vTaskDelay(3000 / portTICK_PERIOD_MS);
printf("MQTT Iniciado.\n");
while (true){
//char tecla = uartGetchar();
}
}