Added get_mac ID firmware code

This commit is contained in:
Manticore 2025-03-06 16:26:04 +05:30
parent f7da94aab9
commit ad862aafec
2 changed files with 138 additions and 13 deletions

View File

@ -2,7 +2,7 @@
# for more information about component CMakeLists.txt files.
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
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 mqtt # optional, list the component requirements

View File

@ -16,6 +16,7 @@
#include "wifi_Init.h"
#include "MCP39F501.h"
#include "esp_system.h"
#include "esp_mac.h"
#include "mqtt_client.h"
#include "lwip/sockets.h"
@ -27,21 +28,64 @@
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"
//#include "mqtt.h"
#define IMEI "353165803930522"
#define CONFIG_BROKER_URL "mqtt://broker.mqtt.cool"
#define MQTT_TOPIC_SUB "/topic/qos0"
#define MQTT_TOPIC_PUB "/topic/qos0"
#define WEB_SERVER "54.204.230.201"
#define WEB_PORT "8085"
#define WEB_PATH "/hae/azuma/353165803930522/update/"
#define MCU_CONFIG "/hae/azuma/353165803930522/mcu_config_download/"
#define MCU_PROG_VER "/hae/azuma/353165803930522/mcu_pgm_download/version/"
const char *TAG = "main";
///////////////////////////////////
void get_mac(void){
uint8_t mac[6]; // Buffer to store MAC address
esp_efuse_mac_get_default(mac);
// Print original MAC in hex format
printf("MAC Address (Hex): %02X:%02X:%02X:%02X:%02X:%02X\n",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
// Convert MAC address to a decimal string
char full_mac_decimal[20]; // Buffer for full decimal MAC
int len = snprintf(full_mac_decimal, sizeof(full_mac_decimal), "%d%d%d%d%d%d",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
// Ensure exactly 15 characters by padding with leading zeros if necessary
char mac_15[16]; // 15 characters + null terminator
if (len < 15) {
// Fill with leading zeros
memset(mac_15, '0', 15 - len);
strcpy(mac_15 + (15 - len), full_mac_decimal);
} else {
// Truncate if necessary
strncpy(mac_15, full_mac_decimal, 15);
}
mac_15[15] = '\0'; // Null-terminate
// Print the final 15-character decimal MAC address
printf("Formatted MAC (15 Characters): %s\n", mac_15);
}
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");
@ -84,19 +128,94 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
default:
ESP_LOGI(TAG, "Other event id:%d", event->event_id);
break;
}
}*/
switch ((esp_mqtt_event_id_t)event_id) {
case MQTT_EVENT_CONNECTED:
ESP_LOGI(TAG, "MQTT Connected!");
esp_mqtt_client_subscribe(client, MQTT_TOPIC_SUB, 0); // Subscribe to topic
esp_mqtt_client_publish(client, MQTT_TOPIC_PUB, "Hello from ESP32!", 0, 0, 0); // Publish a message
break;
case MQTT_EVENT_DATA:
ESP_LOGI(TAG, "Received on topic: %.*s, Message: %.*s", event->topic_len, event->topic, event->data_len, event->data);
ESP_LOGI(TAG, "string copy start");
char tmp[20] = {0}; // Ensure it's properly initialized
strncpy(tmp, event->data, sizeof(tmp) - 1); // Copy message safely
ESP_LOGI(TAG, "string copy value : %s",tmp);
ESP_LOGI(TAG, "string copy end");
if (strcmp(tmp, "McuUpdate=1") == 0) { // Correct comparison
wifi_ota_start_firmware_update(IMEI);
}
else
ESP_LOGI(TAG, "string compare faild");
break;
default:
break;
}
}
/****************************************/
void http_get_request(const char *path) {
char request[512];
snprintf(request, sizeof(request),
"GET %s HTTP/1.1\r\n"
"Host: " WEB_SERVER ":" WEB_PORT "\r\n"
"User-Agent: esp32\r\n"
"Connection: close\r\n\r\n",
path);
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
int err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res);
if (err != 0 || res == NULL) {
ESP_LOGE(TAG, "DNS lookup failed");
return;
}
int sock = socket(res->ai_family, res->ai_socktype, 0);
if (sock < 0) {
ESP_LOGE(TAG, "Socket creation failed");
freeaddrinfo(res);
return;
}
if (connect(sock, res->ai_addr, res->ai_addrlen) != 0) {
ESP_LOGE(TAG, "Socket connection failed");
close(sock);
freeaddrinfo(res);
return;
}
send(sock, request, strlen(request), 0);
char buffer[512];
int received = recv(sock, buffer, sizeof(buffer) - 1, 0);
if (received > 0) {
buffer[received] = '\0';
ESP_LOGI(TAG, "Response:\n%s", buffer);
}
close(sock);
freeaddrinfo(res);
}
/****************************************/
////////////////////////////////////
//static const char* TAG = "MAIN";
uint8_t comms_mode = DEFAULT_COMMS_MODE;
void app_main(void)
{
@ -109,8 +228,8 @@ void app_main(void)
/* Create the UART tasks for both UART0 and UART1 */
uart_create_rx_tasks();
get_mac();
// ESP_ERROR_CHECK(example_connect());
/*
@ -155,6 +274,11 @@ void app_main(void)
Connect_wifi_sta(WIFI_MODE_STA);
ESP_LOGI(TAG," -------------> Wifi Connected ... :)");
http_get_request(MCU_CONFIG);
http_get_request(MCU_PROG_VER);
@ -176,9 +300,10 @@ void app_main(void)
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);
///////////////////////////////
@ -186,7 +311,7 @@ void app_main(void)
while(1){
vTaskDelay(100 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "Loop......");
// ESP_LOGI(TAG, "Loop......");
}
// OTA start