Adde OTA update code trigger from server input
This commit is contained in:
parent
ad862aafec
commit
4ed1f02cea
@ -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
|
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
|
INCLUDE_DIRS # optional, add here public include directories
|
||||||
PRIV_INCLUDE_DIRS # optional, add here private 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
|
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
|
PRIV_REQUIRES # optional, list the private requirements
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
219
main/main.c
219
main/main.c
@ -18,35 +18,84 @@
|
|||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_mac.h"
|
#include "esp_mac.h"
|
||||||
#include "mqtt_client.h"
|
#include "mqtt_client.h"
|
||||||
|
#include "esp_ota_ops.h"
|
||||||
|
|
||||||
#include "lwip/sockets.h"
|
#include "lwip/sockets.h"
|
||||||
#include "lwip/dns.h"
|
#include "lwip/dns.h"
|
||||||
#include "lwip/netdb.h"
|
#include "lwip/netdb.h"-
|
||||||
|
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
#include "freertos/queue.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 IMEI "353165803930522"
|
||||||
#define WEB_PATH "/hae/azuma/353165803930522/update/"
|
#define CONFIG_BROKER_URL "mqtt://broker.mqtt.cool"
|
||||||
#define MCU_CONFIG "/hae/azuma/353165803930522/mcu_config_download/"
|
#define MQTT_TOPIC_SUB "/topic/qos0"
|
||||||
#define MCU_PROG_VER "/hae/azuma/353165803930522/mcu_pgm_download/version/"
|
#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/"
|
||||||
|
#define MCU_UPDATE "/hae/azuma/353165803930522/update/"
|
||||||
|
|
||||||
|
#define BROCKER_URL_TEST "mqtt://broker.mqtt.cool:1883"
|
||||||
|
|
||||||
|
//#define BROCKER_URL_TEST "mqtt://azumamqtt1.cedalo.cloud:1883"
|
||||||
|
|
||||||
|
uint8_t comms_mode = DEFAULT_COMMS_MODE;
|
||||||
|
char MAC_ID[15];
|
||||||
|
char HTTP_GET_DATA[512];
|
||||||
|
|
||||||
const char *TAG = "main";
|
const char *TAG = "main";
|
||||||
|
|
||||||
///////////////////////////////////
|
|
||||||
|
|
||||||
|
|
||||||
|
void extract_json(const char *input, char *output) {
|
||||||
|
const char *start = strchr(input, '{'); // Find the first '{'
|
||||||
|
const char *end = strrchr(input, '}'); // Find the last '}'
|
||||||
|
|
||||||
|
if (start != NULL && end != NULL && end > start) {
|
||||||
|
size_t length = end - start + 1; // Include '}'
|
||||||
|
strncpy(output, start, length);
|
||||||
|
output[length] = '\0'; // Null-terminate the extracted string
|
||||||
|
} else {
|
||||||
|
strcpy(output, "Not Found"); // Handle case where {} are missing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void extract_mcu_update(const char *json_data, char *output) {
|
||||||
|
char *start = strstr(json_data, "\"McuUpdate\":");
|
||||||
|
|
||||||
|
if (start) {
|
||||||
|
int value;
|
||||||
|
sscanf(start, "\"McuUpdate\":%d", &value); // Extract the integer value
|
||||||
|
snprintf(output, 20, "McuUpdate%d", value); // Format as "McuUpdate1"
|
||||||
|
} else {
|
||||||
|
strcpy(output, "Not Found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int Comms_Parse_SP3(char *buf, int len){
|
||||||
|
|
||||||
|
char *strret;
|
||||||
|
char message_iccid[19];
|
||||||
|
char messageIMEI[15];
|
||||||
|
int retval;
|
||||||
|
int ret = -1;
|
||||||
|
|
||||||
|
strret = strstr(buf, "IMEI");
|
||||||
|
messageIMEI[15] = '\0';
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
void get_mac(void){
|
void get_mac(void){
|
||||||
|
|
||||||
@ -75,8 +124,9 @@ void get_mac(void){
|
|||||||
mac_15[15] = '\0'; // Null-terminate
|
mac_15[15] = '\0'; // Null-terminate
|
||||||
|
|
||||||
// Print the final 15-character decimal MAC address
|
// Print the final 15-character decimal MAC address
|
||||||
printf("Formatted MAC (15 Characters): %s\n", mac_15);
|
printf("Formatted MAC (15 Characters): %s\n", mac_15);
|
||||||
|
strncpy(MAC_ID, mac_15, 15);
|
||||||
|
printf("Formatted MAC (15 Characters): %s\n", MAC_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mqtt_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)
|
||||||
@ -85,57 +135,16 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
|||||||
esp_mqtt_event_handle_t event = event_data;
|
esp_mqtt_event_handle_t event = event_data;
|
||||||
esp_mqtt_client_handle_t client = event->client;
|
esp_mqtt_client_handle_t client = event->client;
|
||||||
|
|
||||||
/*
|
|
||||||
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;
|
|
||||||
|
|
||||||
|
|
||||||
}*/
|
|
||||||
switch ((esp_mqtt_event_id_t)event_id) {
|
switch ((esp_mqtt_event_id_t)event_id) {
|
||||||
case MQTT_EVENT_CONNECTED:
|
case MQTT_EVENT_CONNECTED:
|
||||||
ESP_LOGI(TAG, "MQTT Connected!");
|
ESP_LOGI(TAG, "MQTT Connected!");
|
||||||
esp_mqtt_client_subscribe(client, MQTT_TOPIC_SUB, 0); // Subscribe to topic
|
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
|
char data[100];
|
||||||
|
strcpy(data, "SP1 ");
|
||||||
|
strcat(data, MAC_ID);
|
||||||
|
strcat(data, " FW_VERSION DATE_TIME SENS_EN HEARTBEAT_TIME TEST_MODE RELAY_STAT EP");
|
||||||
|
|
||||||
|
esp_mqtt_client_publish(client, MQTT_TOPIC_PUB, data, 0, 0, 0); // Publish a message
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_DATA:
|
case MQTT_EVENT_DATA:
|
||||||
@ -145,22 +154,13 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
|
|||||||
strncpy(tmp, event->data, sizeof(tmp) - 1); // Copy message safely
|
strncpy(tmp, event->data, sizeof(tmp) - 1); // Copy message safely
|
||||||
ESP_LOGI(TAG, "string copy value : %s",tmp);
|
ESP_LOGI(TAG, "string copy value : %s",tmp);
|
||||||
ESP_LOGI(TAG, "string copy end");
|
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;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/****************************************/
|
|
||||||
|
|
||||||
void http_get_request(const char *path) {
|
void http_get_request(const char *path) {
|
||||||
char request[512];
|
char request[512];
|
||||||
snprintf(request, sizeof(request),
|
snprintf(request, sizeof(request),
|
||||||
@ -203,19 +203,13 @@ void http_get_request(const char *path) {
|
|||||||
if (received > 0) {
|
if (received > 0) {
|
||||||
buffer[received] = '\0';
|
buffer[received] = '\0';
|
||||||
ESP_LOGI(TAG, "Response:\n%s", buffer);
|
ESP_LOGI(TAG, "Response:\n%s", buffer);
|
||||||
|
strcpy(HTTP_GET_DATA, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
close(sock);
|
close(sock);
|
||||||
freeaddrinfo(res);
|
freeaddrinfo(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/****************************************/
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////
|
|
||||||
uint8_t comms_mode = DEFAULT_COMMS_MODE;
|
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -229,9 +223,11 @@ void app_main(void)
|
|||||||
/* Create the UART tasks for both UART0 and UART1 */
|
/* Create the UART tasks for both UART0 and UART1 */
|
||||||
uart_create_rx_tasks();
|
uart_create_rx_tasks();
|
||||||
get_mac();
|
get_mac();
|
||||||
|
|
||||||
|
// Get firmwae version
|
||||||
|
const esp_app_desc_t *app_desc = esp_app_get_description();
|
||||||
|
ESP_LOGI(TAG, "Firmware Version: %s", app_desc->version);
|
||||||
|
|
||||||
// ESP_ERROR_CHECK(example_connect());
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
uint8_t* test_str[] = {0xA5,0x05,0x4C,0x4C,0x42}; // Example command to request current value
|
uint8_t* test_str[] = {0xA5,0x05,0x4C,0x4C,0x42}; // Example command to request current value
|
||||||
@ -251,6 +247,7 @@ void app_main(void)
|
|||||||
ESP_LOGI(TAG,"*** data send ***");
|
ESP_LOGI(TAG,"*** data send ***");
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* read the comms-mode from the NVS (if any) */
|
/* read the comms-mode from the NVS (if any) */
|
||||||
comms_mode = nvm_read_comms_mode();
|
comms_mode = nvm_read_comms_mode();
|
||||||
|
|
||||||
@ -272,23 +269,47 @@ void app_main(void)
|
|||||||
port_blue_led_on();
|
port_blue_led_on();
|
||||||
wifi_first_init();
|
wifi_first_init();
|
||||||
Connect_wifi_sta(WIFI_MODE_STA);
|
Connect_wifi_sta(WIFI_MODE_STA);
|
||||||
|
|
||||||
ESP_LOGI(TAG," -------------> Wifi Connected ... :)");
|
ESP_LOGI(TAG," -------------> Wifi Connected ... :)");
|
||||||
|
|
||||||
http_get_request(MCU_CONFIG);
|
|
||||||
http_get_request(MCU_PROG_VER);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//Configure MQTT broker credentials
|
||||||
|
/* esp_mqtt_client_config_t mqtt_cfg = {
|
||||||
|
.broker.address.uri = CONFIG_BROKER_URL,
|
||||||
|
};
|
||||||
|
|
||||||
|
*/
|
||||||
|
esp_mqtt_client_config_t mqtt_cfg = {
|
||||||
|
.broker.address.uri = BROCKER_URL_TEST,
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
http_get_request(MCU_CONFIG);
|
||||||
|
//ESP_LOGI(TAG,"REcived data from main1 %s",HTTP_GET_DATA);
|
||||||
|
|
||||||
|
http_get_request(MCU_PROG_VER);
|
||||||
|
|
||||||
///////////////////////////////
|
http_get_request(MCU_UPDATE);
|
||||||
|
char extracted_json[256]; // Buffer to store extracted JSON
|
||||||
|
extract_json(HTTP_GET_DATA, extracted_json);
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Jason data %s",extracted_json);
|
||||||
|
extract_mcu_update(extracted_json, HTTP_GET_DATA);
|
||||||
|
ESP_LOGI(TAG,"REcived data from main1 %s",HTTP_GET_DATA);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ESP_LOGI(TAG, "[APP] Startup..");
|
ESP_LOGI(TAG, "[APP] Startup..");
|
||||||
ESP_LOGI(TAG, "[APP] Free memory: %" PRIu32 " bytes", esp_get_free_heap_size());
|
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_LOGI(TAG, "[APP] IDF version: %s", esp_get_idf_version());
|
||||||
|
|
||||||
|
/*
|
||||||
esp_log_level_set("*", ESP_LOG_INFO);
|
esp_log_level_set("*", ESP_LOG_INFO);
|
||||||
esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE);
|
esp_log_level_set("mqtt_client", ESP_LOG_VERBOSE);
|
||||||
esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE);
|
esp_log_level_set("mqtt_example", ESP_LOG_VERBOSE);
|
||||||
@ -296,19 +317,13 @@ void app_main(void)
|
|||||||
esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
|
esp_log_level_set("esp-tls", ESP_LOG_VERBOSE);
|
||||||
esp_log_level_set("transport", ESP_LOG_VERBOSE);
|
esp_log_level_set("transport", ESP_LOG_VERBOSE);
|
||||||
esp_log_level_set("outbox", ESP_LOG_VERBOSE);
|
esp_log_level_set("outbox", ESP_LOG_VERBOSE);
|
||||||
|
*/
|
||||||
|
|
||||||
esp_mqtt_client_config_t mqtt_cfg = {
|
if (strcmp(HTTP_GET_DATA ,"McuUpdate1" ) == 0)
|
||||||
.broker.address.uri = CONFIG_BROKER_URL,
|
{
|
||||||
};
|
ESP_LOGI(TAG,"OTA Start");
|
||||||
|
wifi_ota_start_firmware_update(IMEI);
|
||||||
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){
|
while(1){
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
// ESP_LOGI(TAG, "Loop......");
|
// ESP_LOGI(TAG, "Loop......");
|
||||||
@ -316,12 +331,6 @@ void app_main(void)
|
|||||||
|
|
||||||
// OTA start
|
// OTA start
|
||||||
wifi_ota_start_firmware_update(IMEI);
|
wifi_ota_start_firmware_update(IMEI);
|
||||||
|
|
||||||
|
|
||||||
while(1){
|
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
||||||
}
|
|
||||||
vTaskDelete(NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user