#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 <string.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. ");
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 = {
.broker.address.uri = "mqtt://test.mosquitto.org",
};
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 = {
.sta = {
.ssid = "Wokwi-GUEST",
.password = "",
},
};
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;
}
}
//codificacion y envio de datos
void codificar(char *cad)
{
/*1. Remueve los espacios del texto.
2. Sea L la longitud del texto resultante del paso anterior.
3. Los caracteres se almacenan en una matriz cuyos renglones y columnas tienen las siguientes restricciones:
floor(√L) ≤ renglones ≤ columnas ≤ ceil(√L)
floor(x): Es la función suelo, se aplica a un número real x y devuelve el entero menor o igual que x.
ceil(x): Es la función techo, se aplica a un número real x y devuelve el mínimo número entero y no inferior a x.
4. El mensaje cifrado se obtiene de tomar los caracteres de cada columna, con un espacio entre cada columna.
Considere que los caracteres de los mensajes pertenecen únicamente al abecedario inglés.
Use las funciones sqrt, ceil y floor de la biblioteca de funciones math.h para obtener la cantidad de renglones y columnas.
float sq_root = sqrt(54);
int result1 = ceil(sq_root);
int result2 = floor(sq_root);
*/
float raiz;
int longitud = strlen(cad);
char cadena[longitud];
int pos = 0;
int i ,l, x,y,j,c;
for(i = 0 ; i < longitud ; i++)
{
if(cad[i] != ' ')
{
cadena[pos] = cad[i];
pos ++;
}
}
cadena[pos] = '\0';
l = strlen(cadena);
raiz = sqrt(l);
x = ceil(raiz);
y = floor(raiz);
char mtx[x][y];
c = 0;
for(i = 0; i < y ; i++)
{
for(j = 0; j < x ; j++)
if(cadena[c])
{
mtx[j][i] = cadena[c];
c++;
}
}
char mensaje[l+x];
c=0;
for(i = 0 ;i < x ; i ++)
{
for(j = 0 ; j < y ; j++)
{
mensaje[c] = mtx[i][j];
c++;
}
mensaje[c] = ' ';
c++;
}
mensaje[c] = '\0';
esp_mqtt_client_publish(client, "1258191/cliente2/chat1", mensaje, 0, 1, 0);
}
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");
esp_mqtt_client_subscribe(client, "1258191/cliente2/chat1", 0);
vTaskDelay(3000 / portTICK_PERIOD_MS);
codificar("If man was meant to stay on the ground god would have given roots");
}