Added MQTT support code

This commit is contained in:
Manticore 2025-03-05 18:16:32 +05:30
parent c6cf251b9b
commit f7da94aab9
2 changed files with 127 additions and 10 deletions

View File

@ -5,7 +5,7 @@ idf_component_register(
SRCS main.c uart_ifx.c port.c adc_ifx.c modem.c comms.c rtc.c i2c_sensors.c data_processing.c ota.c hmi.c wifi_Init.c wifi_Client.c wifi_OTA.c wifi_webServer.c nvm.c# list the source files of this component
INCLUDE_DIRS # optional, add here public include directories
PRIV_INCLUDE_DIRS # optional, add here private include directories
REQUIRES driver esp_adc nvs_flash app_update esp_timer esp_event esp-tls esp_http_client esp_https_server ulp soc esp_wifi lwip # optional, list the component requirements
REQUIRES driver esp_adc nvs_flash app_update esp_timer esp_event esp-tls esp_http_client esp_https_server ulp soc esp_wifi lwip mqtt # optional, list the component requirements
PRIV_REQUIRES # optional, list the private requirements
)

View File

@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include "esp_log.h"
@ -14,11 +15,87 @@
#include "wifi_OTA.h"
#include "wifi_Init.h"
#include "MCP39F501.h"
#include "esp_system.h"
#include "mqtt_client.h"
#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
#define IMEI "353165803930522"
#define CONFIG_BROKER_URL "mqtt://broker.mqtt.cool"
static const char* TAG = "MAIN";
const char *TAG = "main";
///////////////////////////////////
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%" PRIi32 "", base, event_id);
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:
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
msg_id = esp_mqtt_client_publish(client, "/topic/qos1", "data_3", 0, 1, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos0", 0);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_subscribe(client, "/topic/qos1", 1);
ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);
msg_id = esp_mqtt_client_unsubscribe(client, "/topic/qos1");
ESP_LOGI(TAG, "sent unsubscribe successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_DISCONNECTED:
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
break;
case MQTT_EVENT_SUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_SUBSCRIBED, msg_id=%d", event->msg_id);
msg_id = esp_mqtt_client_publish(client, "/topic/qos0", "Test data from ESP32", 0, 0, 0);
ESP_LOGI(TAG, "sent publish successful, msg_id=%d", msg_id);
break;
case MQTT_EVENT_UNSUBSCRIBED:
ESP_LOGI(TAG, "MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_PUBLISHED:
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED, msg_id=%d", event->msg_id);
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "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:
ESP_LOGI(TAG, "MQTT_EVENT_ERROR");
break;
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
}
////////////////////////////////////
//static const char* TAG = "MAIN";
uint8_t comms_mode = DEFAULT_COMMS_MODE;
void app_main(void)
{
@ -30,25 +107,28 @@ void app_main(void)
port_init();
uart_ifx_init();
/* Create the UART tasks for both UART0 and UART1 */
uart_create_rx_tasks();
// uint8_t* test_str[2] = { 0x01, 0x00 };
// char* test_str = MCP39F501_HEADER;
// ESP_ERROR_CHECK(example_connect());
/*
/* while(1)
uint8_t* test_str[] = {0xA5,0x05,0x4C,0x4C,0x42}; // Example command to request current value
while(1)
{
uart_ifx_uart1_send_bytes(&test_str, strlen(test_str));
// uart_write_bytes(1, (const char*)test_str, strlen(test_str));
vTaskDelay(100 / portTICK_PERIOD_MS);
ESP_LOGI(TAG,"*** data send ***");
}
*/
@ -74,6 +154,40 @@ void app_main(void)
wifi_first_init();
Connect_wifi_sta(WIFI_MODE_STA);
ESP_LOGI(TAG," -------------> Wifi Connected ... :)");
///////////////////////////////
ESP_LOGI(TAG, "[APP] Startup..");
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
ESP_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE);
esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE);
esp_log_level_set("transport_base", ESP_LOG_VERBOSE);
esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
esp_log_level_set("transport", ESP_LOG_VERBOSE);
esp_log_level_set("outbox", ESP_LOG_VERBOSE);
esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = CONFIG_BROKER_URL,
};
esp_mqtt_client_handle_t 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);
///////////////////////////////
while(1){
vTaskDelay(100 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Loop......");
}
// OTA start
wifi_ota_start_firmware_update(IMEI);
@ -84,3 +198,6 @@ void app_main(void)
}
vTaskDelete(NULL);
}