#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;
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 remove_spaces(char *str) {
char *i = str;
char *j = str;
while(*j != '\0') {
*i = *j++;
if(*i != ' ')
i++;
}
*i = '\0';
}
void process_text(const char *input) {
char text[100];
strncpy(text, input, sizeof(text) - 1);
text[sizeof(text) - 1] = '\0';
remove_spaces(text);
int L = strlen(text);
int rows = floor(sqrt(L));
int cols = ceil(sqrt(L));
if (rows * cols < L) {
rows = cols;
}
printf("Processed text:\n");
for (int i = 0; i < L; i++) {
printf("%c", text[i]);
if ((i + 1) % cols == 0) {
printf("\n");
}
}
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 = "el celular de alain martinez y tambien de iris es el mismo";
process_text(message);
//remove_spaces(message),
}