#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "soc/soc_caps.h"
#include "esp_log.h"
#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"
#include "driver/gpio.h"
#include "esp_err.h"
#include "mqtt_client.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_netif.h"
static const char *TAG = "MQTT_APP";
static int adc_raw[2];
static int voltage[2];
static adc_cali_handle_t adc2_cali_handle;
static esp_mqtt_client_handle_t client; // Global MQTT client handle
// ===================================== MQTT Event Handler =====================================
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 = (esp_mqtt_event_handle_t) event_data;
client = event->client; // Assign client globally
switch ((esp_mqtt_event_id_t) event_id)
{
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT Connected");
esp_mqtt_client_subscribe(client, "abc", 2);
esp_mqtt_client_publish(client, "abc", "hello bobo", 0, 2, 0);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGW(TAG, "MQTT Disconnected!");
break;
case MQTT_EVENT_ERROR:
ESP_LOGE(TAG, "MQTT Error Occurred");
break;
default:
break;
}
}
// ===================================== ADC Calibration =====================================
static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle)
{
adc_cali_handle_t handle = NULL;
esp_err_t ret = adc_cali_create_scheme_line_fitting(&(adc_cali_line_fitting_config_t){
.unit_id = unit,
.atten = atten,
.bitwidth = ADC_BITWIDTH_DEFAULT,
}, &handle);
if (ret == ESP_OK) {
*out_handle = handle;
return true;
}
return false;
}
// ===================================== MQTT Initialization =====================================
static void mqtt_app_start(void)
{
const esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = "mqtts://b221e58079dc4a1dbad44fcd35bbd730.s1.eu.hivemq.cloud:8883",
.credentials.username = "hivemq.webclient.1743555792194", // REQUIRED for HiveMQ Cloud
.credentials.authentication.password = "0oCe.Bf8>wD!x%1J7pAR", // REQUIRED for HiveMQ Cloud
};
client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
esp_mqtt_client_start(client);
}
// ===================================== Main Function =====================================
void app_main()
{
// Start MQTT first
mqtt_app_start();
adc_oneshot_unit_handle_t adc2_handle;
adc_oneshot_unit_init_cfg_t init_config2 = {
.unit_id = ADC_UNIT_2,
.ulp_mode = ADC_ULP_MODE_DISABLE,
};
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config2, &adc2_handle));
adc_oneshot_chan_cfg_t config = {
.bitwidth = ADC_BITWIDTH_DEFAULT,
.atten = ADC_ATTEN_DB_12,
};
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc2_handle, ADC_CHANNEL_0, &config));
// Initialize ADC calibration
bool do_calibration = adc_calibration_init(ADC_UNIT_2, ADC_ATTEN_DB_12, &adc2_cali_handle);
gpio_set_direction(GPIO_NUM_32, GPIO_MODE_OUTPUT);
gpio_set_direction(GPIO_NUM_17, GPIO_MODE_OUTPUT); // SET HIGH FOR CLOCKWISE
while (true)
{
ESP_ERROR_CHECK(adc_oneshot_read(adc2_handle, ADC_CHANNEL_0, &adc_raw[0]));
if (do_calibration) {
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0], &voltage[0]));
} else {
voltage[0] = adc_raw[0]; // Use raw values if calibration fails
}
if (adc_raw[0] < 3000) {
gpio_set_level(GPIO_NUM_32, 1);
printf("Voltage: %d mV\n", voltage[0]);
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
/*
just no msg try add log
fait demende en ligne hoope macepte go demain w commission
//here va switch pmw qui suit la switch .. debug plus sans chat still was close just take more time
//supposedo sans improve just take time improve code
dont seem to work seul so try calculer direct au pwm
*/