Updare rtc code time vale get from epoch

This commit is contained in:
Manticore 2025-03-07 17:58:16 +05:30
parent 4ed1f02cea
commit 1d457bb387
3 changed files with 99 additions and 28 deletions

View File

@ -30,6 +30,11 @@
#include "freertos/semphr.h"
#include "freertos/queue.h"
#include "esp_timer.h"
#include <sys/time.h>
#include "rtc.h"
@ -45,9 +50,9 @@
#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://broker.mqtt.cool:1883"
//#define BROCKER_URL_TEST "mqtt://azumamqtt1.cedalo.cloud:1883"
#define BROCKER_URL_TEST "mqtt://azumamqtt1.cedalo.cloud:1883"
uint8_t comms_mode = DEFAULT_COMMS_MODE;
char MAC_ID[15];
@ -55,8 +60,6 @@ char HTTP_GET_DATA[512];
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 '}'
@ -82,21 +85,6 @@ void extract_mcu_update(const char *json_data, char *output) {
}
}
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){
uint8_t mac[6]; // Buffer to store MAC address
@ -223,6 +211,7 @@ void app_main(void)
/* Create the UART tasks for both UART0 and UART1 */
uart_create_rx_tasks();
get_mac();
// Get firmwae version
const esp_app_desc_t *app_desc = esp_app_get_description();
@ -261,24 +250,31 @@ void app_main(void)
{
vTaskDelay(750/portTICK_PERIOD_MS);
port_red_led_toggle();
}
}
port_blue_led_on();
wifi_first_init();
Connect_wifi_sta(WIFI_MODE_STA);
ESP_LOGI(TAG," -------------> Wifi Connected ... :)");
obtain_time(); // Sync time with NTP
get_time();
while(1){
vTaskDelay(100 / portTICK_PERIOD_MS);
// get_time();
// ESP_LOGI("TIME", "Current time =====> %s", buff);
ESP_LOGI(TAG,"====>>> HIiiiiii <<<=========");
}
//Configure MQTT broker credentials
/* esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = CONFIG_BROKER_URL,
};
*/
// Configure Target brocker URL
esp_mqtt_client_config_t mqtt_cfg = {
.broker.address.uri = BROCKER_URL_TEST,
};
@ -290,7 +286,9 @@ void app_main(void)
esp_mqtt_client_start(client);
http_get_request(MCU_CONFIG);
//ESP_LOGI(TAG,"REcived data from main1 %s",HTTP_GET_DATA);
char* strret = NULL;
char* token = strtok(HTTP_GET_DATA, "SP4");
ESP_LOGI(TAG,"REcived data from main1 %s",token);
http_get_request(MCU_PROG_VER);

View File

@ -7,6 +7,7 @@
#include "rtc.h"
#include <time.h>
#include <sys/time.h>
#include "esp_sntp.h"
@ -27,6 +28,8 @@ static const char* TAG = "RTC";
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
#include "esp_log.h"
#include "esp_sntp.h"
static bool rtc_set = false;
int app_rtc_init(void)
@ -221,4 +224,69 @@ bool rtc_is_set(void)
///////////////////////////////////////////////////////
/*
void time_sync_notification_cb(struct timeval_1 *tv) {
ESP_LOGI(TAG, "Time synchronization event received");
}
*/
void obtain_time(void) {
ESP_LOGI(TAG, "Initializing SNTP");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
sntp_setservername(0, "pool.ntp.org"); // Set NTP server
// sntp_set_time_sync_notification_cb(time_sync_notification_cb);
sntp_init();
// Wait for time to be set
time_t now = 0;
struct tm timeinfo = { 0 };
int retry = 0;
const int max_retries = 10;
while (timeinfo.tm_year < (2020 - 1900) && ++retry < max_retries) {
ESP_LOGI(TAG, "Waiting for system time to be set... (%d/%d)", retry, max_retries);
vTaskDelay(2000 / portTICK_PERIOD_MS);
time(&now);
localtime_r(&now, &timeinfo);
}
if (retry >= max_retries) {
ESP_LOGE(TAG, "Failed to obtain time");
} else {
ESP_LOGI(TAG, "Time synchronized");
}
}
char* get_time(void) {
struct tm timeinfo;
long int now;
char time_str[50];
time(&now);
ESP_LOGI(TAG, "Time print start %ld",now);
localtime_r(&now, &timeinfo);
const char *weekdays[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
// Format as "Weekday YYYY/MM/DD_HH:MM:SS_w" and store in the buffer
snprintf(time_str, sizeof(time_str), "%04d/%02d/%02d_%02d:%02d:%02d_%s",
timeinfo.tm_year + 1900,
timeinfo.tm_mon + 1, timeinfo.tm_mday,
timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec,weekdays[timeinfo.tm_wday]);
ESP_LOGI(TAG, "Time and Date %s",time_str);
return time_str;
}
///////////////////////////////////////////////////////

View File

@ -13,6 +13,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "esp_sntp.h"
#define RTC_STATUS_OK 0
#define RTC_STATUS_ERROR -1
@ -75,4 +76,8 @@ uint32_t rtc_get_epoch(void);
*/
void rtc_set_epoch(uint32_t epoch);
char* get_time(void);
void obtain_time(void);
#endif /* MAIN_RTC_H_ */