#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 <math.h>
#include <driver/adc.h>
const float BETA = 3950;
float celsiusGlobal;
#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 = {
/* Configure el broker aquí */
.broker.address.uri = "mqtt://mqtt.thingsboard.cloud",
.credentials = {
.username = "bHQTjychYvFEw6vsBhmi"
},
};
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 cifrar(char* input)
{
int Longitud = strlen(input);
int c = 0;
char aux[Longitud];
for(int i = 0; i < Longitud; i++)
{
if(input[i] != ' '){
aux[c] = input[i];
c++;
}
}
aux[c] = '\0';
printf("%s \n",aux);
Longitud = strlen(aux);
float raiz = sqrt(Longitud);
int rows = floor(raiz);
int cols = ceil(raiz);
char matriz[rows][cols];
c = 0;
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
matriz[j][i] = aux[c];
c++;
}
}
c = 0;
char mensaje[Longitud+cols+1];
for(int i = 0; i < cols; i++){
for(int j = 0; j < rows; j++){
mensaje[c] = matriz[i][j];
c++;
}
mensaje[c] = ' ';
c++;
}
mensaje[c] = '\0';
esp_mqtt_client_publish(client, "/1273699/cliente2/chat1", mensaje, 0, 1, 0);
}
void configurar_adc()
{
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_DB_11);
}
void app_main(void)
{
configurar_adc();
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, "/1273699/cliente2/chat1", 0);
cifrar("if man was meant to stay on the ground god would have given us roots");
*/
char mensaje[35];
while(true){
esp_mqtt_client_publish(client, "v1/devices/me/telemetry", mensaje, 0, 1, 0);
int analogValue = adc1_get_raw(ADC1_CHANNEL_6);
float celsius = 1 / ((log(1 / (4095.0 / analogValue - 1)) / BETA) + 1.0 / 298.15) - 273.15;
printf("Temperatura actual: %f ℃\n", celsius);
sprintf(mensaje,"{temperature:%f}",celsius);
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
}