#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 ""
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;
int msg_id2;
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);
msg_id2 = esp_mqtt_client_subscribe(client, "1273699/cliente2/chat1", 1);
break;
case MQTT_EVENT_DISCONNECTED:
break;
case MQTT_EVENT_SUBSCRIBED:
printf("Suscrito exitosamente.\n");
break;
case MQTT_EVENT_UNSUBSCRIBED:
break;
case MQTT_EVENT_PUBLISHED:
printf("Publicado exitosamente.\n");
break;
case MQTT_EVENT_DATA:
printf("TOPIC=%.*s\r\n", event->topic_len, event->topic);
printf("DATA=%.*s\r\n", event->data_len, 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 eliminar_espacio(const char* str, char* Newstr){
int aux = 0;
for(int i = 0; i < strlen(str);i++){
if(str[i] != ' '){
Newstr[aux] = str[i];
aux++;
}
}
Newstr[aux] = '\0'; // Asegurarse de terminar la cadena correctamente
}
void hacer_matriz(const char* input){
char text[100];
eliminar_espacio(input, text);
int Longitud = strlen(text);
int rows = floor(sqrt(Longitud));
int cols = ceil(sqrt(Longitud));
if (rows * cols < Longitud) {
rows = cols;
}
printf("Texto con matriz:\n");
for (int i = 0; i < Longitud; i++) {
printf("%c", text[i]);
if ((i + 1) % cols == 0) {
printf("\n");
}
}
printf("\n");
}
void cifrar(const char* input2){
char text[100];
eliminar_espacio(input2, text);
int Longitud = strlen(text);
int rows = floor(sqrt(Longitud));
int cols = ceil(sqrt(Longitud));
if (rows * cols < Longitud) {
rows = cols;
}
printf("Texto cifrado:\n");
// Inicializar la matriz
char matrix[rows][cols];
memset(matrix, 0, sizeof(matrix));
// Llenar la matriz con los caracteres del texto
for (int i = 0; i < Longitud; i++) {
matrix[i / cols][i % cols] = text[i];
}
// Leer columna por columna
for (int col = 0; col < cols; col++) {
for (int row = 0; row < rows; row++) {
if (matrix[row][col] != 0) {
printf("%c", matrix[row][col]);
}
}
if (col < cols - 1) {
printf(" ");
}
}
printf("\n");
}
void app_main(void)
{
nvs_flash_init();
wifi_connection();
vTaskDelay(3000 / portTICK_PERIOD_MS);
mqtt_app_start();
vTaskDelay(3000 / portTICK_PERIOD_MS);
printf("MQTT Iniciado.\n");
const char *message = "if man was meant to stay on the ground god would have given us roots";
hacer_matriz(message);
printf("\n Enviando cifrado...\n");
cifrar(message);
esp_mqtt_client_publish(client, "1273699/cliente2/chat1", message, 0, 1, 0);
}