1075 lines
29 KiB
C
1075 lines
29 KiB
C
|
|
<<<<<<< HEAD
|
|
=======
|
|
#include <stdint.h>
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdbool.h>
|
|
#include <unistd.h>
|
|
#include "esp_log.h"
|
|
#include "nvm.h"
|
|
#include "port.h"
|
|
#include "uart_ifx.h"
|
|
#include "wifi_webServer.h"
|
|
#include "comms.h"
|
|
#include "main.h"
|
|
#include "ota.h"
|
|
#include "MCP39F501.h"
|
|
#include "wifi_OTA.h"
|
|
#include "wifi_Init.h"
|
|
#include "MCP39F501.h"
|
|
#include "esp_system.h"
|
|
#include "esp_mac.h"
|
|
#include "mqtt_client.h"
|
|
#include "esp_ota_ops.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
#include "lwip/dns.h"
|
|
<<<<<<< HEAD
|
|
#include "lwip/netdb.h"-
|
|
=======
|
|
#include "lwip/netdb.h"
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "freertos/semphr.h"
|
|
#include "freertos/queue.h"
|
|
|
|
#include "esp_timer.h"
|
|
#include <sys/time.h>
|
|
#include "rtc.h"
|
|
<<<<<<< HEAD
|
|
|
|
#include "data_processing.h"
|
|
#include "wifi_Client.h"
|
|
|
|
=======
|
|
#include "wifi_Client.h"
|
|
#include "data_processing.h"
|
|
#include "driver/gpio.h"
|
|
#include "comms.h"
|
|
#include "driver/timer.h"
|
|
|
|
#include "port.h"
|
|
|
|
#define HEARTBEAT_TIME_SEC_TEST 2
|
|
#define HEARTBEAT_TIME_SEC 2 //14400
|
|
|
|
/* Unique timer ID for checking the value of isTestMode variable (should not be true for more than 1hr) */
|
|
#define COMMS_TESTMODE_ID 3
|
|
#define TESTMODE_DURATION 3600000
|
|
|
|
//#define RESTART_THRESHOLD 10 // Threshold in seconds
|
|
|
|
//communication init flag
|
|
extern bool deviceCfged;
|
|
extern int config_heartbeat_time;
|
|
bool isCommInit = false;
|
|
bool isTestMode = false;
|
|
bool isFirstRun = true;
|
|
bool isTestModeTimerOff = true;
|
|
|
|
int heartbeat_time = HEARTBEAT_TIME_SEC;
|
|
bool testmode_timeout = false;
|
|
static bool heartbeat_event_flag = false;
|
|
|
|
// Global variable to store the initialization timestamp
|
|
uint32_t init_timestamp = 0;
|
|
// Global variable to store the last check timestamp
|
|
uint32_t last_check_timestamp = 0;
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
|
|
|
|
|
|
|
|
#define IMEI "353165803930522"
|
|
#define CONFIG_BROKER_URL "mqtt://broker.mqtt.cool"
|
|
#define MQTT_TOPIC_SUB "/topic/qos0"
|
|
#define MQTT_TOPIC_PUB "/topic/qos0"
|
|
|
|
<<<<<<< HEAD
|
|
|
|
#define WEB_SERVER "54.204.230.201"
|
|
#define WEB_PORT "8085"
|
|
=======
|
|
#define WEB_SERVER "54.204.230.201"
|
|
#define WEB_PORT "8081"
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
#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"
|
|
|
|
<<<<<<< HEAD
|
|
#define SP3_URL "http://54.204.230.201:8085/hae/azuma/353165803930522/update/"
|
|
|
|
//#define BROCKER_URL_TEST "mqtt://azumamqtt1.cedalo.cloud:1883"
|
|
=======
|
|
// #define BROCKER_URL_TEST "mqtt://azumamqtt1.cedalo.cloud:1883"
|
|
|
|
char MAC_ID[16] ="353165803930522\0";
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
|
|
uint8_t comms_mode = DEFAULT_COMMS_MODE;
|
|
char HTTP_GET_DATA[512];
|
|
|
|
<<<<<<< HEAD
|
|
const char *TAG = "main";
|
|
|
|
char* SP1_data(void) {
|
|
static char sp1[] = "SP1 F/W TIME_DATA";
|
|
return sp1;
|
|
=======
|
|
|
|
bool RelayState_1 = false;
|
|
bool PreviousRelayState_1 = false;
|
|
|
|
#define RELAY_PIN 42 // Define GPIO14
|
|
#define RELAY_ON 1
|
|
#define RELAY_OFF 0
|
|
#define HOURS_TO_SECONDS_COEFF 3600
|
|
|
|
|
|
#define TIMER_DIVIDER 16 // Prescaler (80MHz / 16 = 5MHz)
|
|
#define TIMER_SCALE (APB_CLK_FREQ / TIMER_DIVIDER) // 5,000,000 ticks per second
|
|
#define TIMER_INTERVAL_SEC 5 // 1-minute interval
|
|
bool timer_on = false;
|
|
|
|
const char *TAG = "main";
|
|
|
|
/* This timer triggers server connections based on a fundamental
|
|
* timer (e.g. heartbeat timer). It never resets.
|
|
*/
|
|
static uint32_t comms_timer = 0;
|
|
uint32_t extern_comms_timer = 0;
|
|
/* flag associated with comm process intervals signaling that a
|
|
* connection should be made.
|
|
*/
|
|
bool initialize_completed_event_flag = false;
|
|
|
|
/*Relay-Controlling*/
|
|
void board_relay_on(void)
|
|
{
|
|
gpio_set_level(RELAY_PIN, RELAY_ON); // Set GPIO14 HIGH
|
|
ESP_LOGI(TAG,"Relay ON");
|
|
}
|
|
|
|
void board_relay_off(void)
|
|
{
|
|
gpio_set_level(RELAY_PIN, RELAY_OFF); // Set GPIO14 LOW
|
|
ESP_LOGI(TAG,"Relay OFF");
|
|
}
|
|
|
|
static void comms_started_cb(int status)
|
|
{
|
|
/* Comms process has started and configuration has been updated. */
|
|
initialize_completed_event_flag = true;
|
|
}
|
|
static void comms_connect_cb(int status)
|
|
{
|
|
isCommInit = true;
|
|
}
|
|
|
|
bool main_hearbeat = 0;
|
|
|
|
int app_tick()
|
|
{
|
|
static int retval;
|
|
|
|
/* This heartbeat is for timing communication cycles and other main system functions. */
|
|
if (heartbeat_event_flag)
|
|
{
|
|
heartbeat_event_flag = false;
|
|
|
|
comms_timer++;
|
|
extern_comms_timer++;
|
|
|
|
ESP_LOGI(TAG, "Comms timer value = %d",(uint8_t)comms_timer);
|
|
|
|
if(comms_timer >= heartbeat_time) //2 mins
|
|
{
|
|
comms_timer = 0;
|
|
extern_comms_timer = 0;
|
|
main_hearbeat = 1;
|
|
}
|
|
|
|
if(main_hearbeat)
|
|
{
|
|
main_hearbeat = 0;
|
|
|
|
if(isCommInit == true)
|
|
{
|
|
isCommInit = false ;
|
|
isFirstRun = false;
|
|
|
|
comms_start(comms_started_cb, comms_connect_cb);
|
|
}
|
|
}
|
|
|
|
if(isTestMode)
|
|
{
|
|
heartbeat_time = HEARTBEAT_TIME_SEC_TEST;
|
|
|
|
if(isTestModeTimerOff)
|
|
{
|
|
isTestModeTimerOff = false;
|
|
/*Start COMMS_TESTMODE_TIMER to prevent the "TESTMODE" to last more than 1hr*/
|
|
// timer_start(COMMS_TESTMODE_ID,TESTMODE_DURATION);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
//Seraj
|
|
// if(isFirstRun == false){
|
|
if(deviceCfged == true) {
|
|
//don't set hbt as default value
|
|
heartbeat_time = config_heartbeat_time;
|
|
//heartbeat_time = 180;
|
|
}
|
|
else{
|
|
heartbeat_time = HEARTBEAT_TIME_SEC;
|
|
//heartbeat_time = 180;
|
|
}
|
|
// }
|
|
}
|
|
|
|
/* Heartbeat event is driven by watchdog. Servicing is automatic */
|
|
// watchdog_service();
|
|
|
|
/* comms process */
|
|
if (comms_needs_poll())
|
|
{
|
|
int retval = comms_poll();
|
|
if (retval != 0)
|
|
{
|
|
ESP_LOGE(TAG, "comms poll error: %d\r\n", retval);
|
|
}
|
|
}
|
|
}
|
|
return retval;
|
|
}
|
|
|
|
int sum_digits(int digit)
|
|
{
|
|
int sum = 0;
|
|
while (digit > 0)
|
|
{
|
|
sum = sum + digit % 10;
|
|
digit = digit / 10;
|
|
}
|
|
return sum;
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
}
|
|
|
|
void get_mac_add(void) { // The function modifies the passed buffer
|
|
uint8_t mac[6];
|
|
esp_efuse_mac_get_default(mac);
|
|
|
|
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];
|
|
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
|
|
if (len < 15) {
|
|
memset(MAC_ID, '0', 15 - len);
|
|
strcpy(MAC_ID + (15 - len), full_mac_decimal);
|
|
} else {
|
|
strncpy(MAC_ID, full_mac_decimal, 15);
|
|
}
|
|
// MAC_ID[15] = '\0';
|
|
printf("Formatted MAC (15 Characters): %s\n", MAC_ID);
|
|
}
|
|
|
|
<<<<<<< HEAD
|
|
typedef enum {
|
|
MQTT_STATE_INIT,
|
|
MQTT_STATE_CONNECT,
|
|
MQTT_STATE_PUBLISH,
|
|
MQTT_STATE_DISCONNECT,
|
|
MQTT_STATE_ERROR
|
|
} mqtt_state_t;
|
|
|
|
static mqtt_state_t state = MQTT_STATE_INIT;
|
|
static esp_mqtt_client_handle_t client;
|
|
|
|
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 = event_data;
|
|
esp_mqtt_client_handle_t client = event->client;
|
|
|
|
switch (event_id) {
|
|
case MQTT_EVENT_CONNECTED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_CONNECTED");
|
|
state = MQTT_STATE_PUBLISH;
|
|
break;
|
|
case MQTT_EVENT_DISCONNECTED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
|
|
state = MQTT_STATE_ERROR;
|
|
break;
|
|
case MQTT_EVENT_PUBLISHED:
|
|
ESP_LOGI(TAG, "MQTT_EVENT_PUBLISHED");
|
|
state = MQTT_STATE_DISCONNECT;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void mqtt_task(void *param) {
|
|
|
|
|
|
esp_mqtt_client_config_t mqtt_cfg = {
|
|
.broker.address.uri = BROCKER_URL_TEST,
|
|
};
|
|
|
|
client = esp_mqtt_client_init(&mqtt_cfg);
|
|
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
|
|
|
|
while (1) {
|
|
switch (state) {
|
|
case MQTT_STATE_INIT:
|
|
ESP_LOGI(TAG, "Initializing MQTT");
|
|
esp_mqtt_client_start(client);
|
|
state = MQTT_STATE_CONNECT;
|
|
break;
|
|
case MQTT_STATE_CONNECT:
|
|
ESP_LOGI(TAG, "Connecting to broker...");
|
|
vTaskDelay(pdMS_TO_TICKS(2000));
|
|
break;
|
|
case MQTT_STATE_PUBLISH:
|
|
ESP_LOGI(TAG, "Publishing message");
|
|
SP1_data();
|
|
// esp_mqtt_client_publish(client, "/topic/qos0", "Hello ESP32", 0, 1, 0);
|
|
// esp_mqtt_client_publish(client, "/topic/qos0", sp1(), 0, 1, 0);
|
|
esp_mqtt_client_publish(client, "/topic/qos0", SP1_data(), 0, 1, 0);
|
|
|
|
state = MQTT_STATE_DISCONNECT;
|
|
break;
|
|
case MQTT_STATE_DISCONNECT:
|
|
ESP_LOGI(TAG, "Disconnecting...");
|
|
esp_mqtt_client_stop(client);
|
|
state = MQTT_STATE_INIT;
|
|
vTaskDelay(pdMS_TO_TICKS(7200000)); // 2 hours delay
|
|
break;
|
|
case MQTT_STATE_ERROR:
|
|
ESP_LOGE(TAG, "Error encountered, retrying...");
|
|
state = MQTT_STATE_INIT;
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
|
break;
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
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");
|
|
}
|
|
}
|
|
|
|
/*
|
|
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;
|
|
|
|
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
|
|
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;
|
|
|
|
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");
|
|
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);
|
|
strcpy(HTTP_GET_DATA, buffer);
|
|
}
|
|
|
|
close(sock);
|
|
freeaddrinfo(res);
|
|
=======
|
|
|
|
static bool mac_check_valid(char *message_mac)
|
|
{
|
|
//find the length of message imei
|
|
int len = strlen(message_mac);
|
|
int sum = 0;
|
|
ESP_LOGI(TAG,"MESSAGE MAC_ID : %s\r\n",message_mac);
|
|
ESP_LOGI(TAG,"BOARD MAC_ID : %s\r\n",MAC_ID);
|
|
|
|
ESP_LOGI(TAG,"SIZE OF MAC ID : %d\r\n",len);
|
|
|
|
//check length of imei field
|
|
if (len != 15){
|
|
//invalid IMEI
|
|
ESP_LOGW(TAG,"Invalid MAC length");
|
|
return false;
|
|
}
|
|
if(!strcmp(message_mac,MAC_ID)){
|
|
|
|
for(int i = len-1 ; i >= 0; i--)
|
|
{
|
|
//check if message imei fields contain numbers (0-9)
|
|
if((message_mac[i] > 47) && (message_mac[i] < 58)){
|
|
int digit = (message_mac[i] - 48);
|
|
// Doubling every alternate beginning at [13] digit
|
|
//ignore last number [14] and first number [0] as well
|
|
if (i % 2 != 0)
|
|
digit = 2 * digit;
|
|
// Finding sum of the digits
|
|
// if digit*2 is a two digit number add the two digits
|
|
sum += sum_digits(digit);
|
|
|
|
}
|
|
else{
|
|
ESP_LOGW(TAG,"Invalid MAC (not numerical values)\r\n");
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
ESP_LOGI(TAG,"Message MAC doesn't match board MAC ID\r\n");
|
|
return false;
|
|
}
|
|
|
|
return (sum % 10 == 0);
|
|
}
|
|
|
|
static int Comms_Parse_SP4(char *buf, int len){
|
|
|
|
char messageheader[4] = {0};
|
|
char date_time[18] = {'0'};
|
|
char messageIMEI[15] = {'0'};
|
|
char testMode[3] = {'0'};
|
|
int sp4_ret = -1;
|
|
char* strret = NULL;
|
|
char hbeat[5] = {'0'};
|
|
char epcheck[2] = {0};
|
|
|
|
strret = strtok(buf," ");
|
|
strcpy(messageheader, strret);
|
|
ESP_LOGI(TAG,"%s message is received \r\n",messageheader);
|
|
while(strret != NULL){
|
|
|
|
|
|
strret = strtok (NULL," ");
|
|
strcpy(messageIMEI, strret);
|
|
//messageIMEI[15] = '\0';
|
|
|
|
sp4_ret = mac_check_valid(messageIMEI);
|
|
if(sp4_ret != false){
|
|
//ok IMEI
|
|
ESP_LOGI(TAG,"Fetched IMEI matches board.\r\n");
|
|
}
|
|
else{
|
|
ESP_LOGI(TAG,"INVALID IMEI!!\r\n");
|
|
// comms.poll_timer = true;
|
|
// transition(COMMS_STATE_GOING_IDLE);
|
|
return sp4_ret;
|
|
}
|
|
|
|
/*Parse string for date/time*/
|
|
strret = strtok (NULL," ");
|
|
strcpy(date_time, strret);
|
|
|
|
|
|
|
|
ESP_LOGI(TAG,"Date Time : %s\r\n",date_time);
|
|
|
|
strret = strtok (NULL," ");
|
|
if(strret[0] == '1'){
|
|
RelayState_1 = true;
|
|
}else
|
|
{
|
|
RelayState_1 = false;
|
|
}
|
|
|
|
ESP_LOGI(TAG,"Relay State : %d\r\n",RelayState_1);
|
|
// //heartbeat_time = (buf[43] - 48) * HOURS_TO_SECONDS_COEFF ;
|
|
// heartbeat_time = (buf[44] - 48) * HOURS_TO_SECONDS_COEFF ;
|
|
//
|
|
// LOG_INFO("Heartbeat time in hrs : %d\r\n", (heartbeat_time/HOURS_TO_SECONDS_COEFF));
|
|
|
|
strret = strtok (NULL," ");
|
|
strcpy(hbeat, strret+2);
|
|
|
|
ESP_LOGI(TAG,"fetched Heartbeat time value %s \r\n", hbeat);
|
|
ESP_LOGI(TAG,"previous Heartbeat time value %d \r\n", config_heartbeat_time);
|
|
|
|
|
|
/*moved changing heartbeat so that it comes after looking for test mode field and configuring it*/
|
|
config_heartbeat_time = atoi(hbeat);
|
|
strret = strtok(NULL," ");
|
|
strcpy(testMode, strret);
|
|
if(testMode[2] == '1')
|
|
{
|
|
if(testmode_timeout){
|
|
isTestMode = false;
|
|
testmode_timeout = false;
|
|
}
|
|
else{
|
|
isTestMode = true;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
isTestMode = false;
|
|
}
|
|
//=====================================
|
|
ESP_LOGI(TAG,"Test Mode: %d\r\n",isTestMode);
|
|
|
|
//maximum heartbeat time is 4 days (96 hours)
|
|
if((config_heartbeat_time != 0) && (config_heartbeat_time < 97 && config_heartbeat_time > 0)){
|
|
ESP_LOGI(TAG,"Valid fetched Heartbeat time value\r\n");
|
|
config_heartbeat_time *= HOURS_TO_SECONDS_COEFF;
|
|
deviceCfged = true;
|
|
}
|
|
else{
|
|
config_heartbeat_time = HEARTBEAT_TIME_SEC;
|
|
deviceCfged = true;
|
|
ESP_LOGI(TAG,"Invalid fetched Heartbeat time value\r\n");
|
|
}
|
|
|
|
ESP_LOGI(TAG,"Heartbeat time in Sec : %d\r\n", config_heartbeat_time);
|
|
ESP_LOGI(TAG,"Heartbeat time in Hrs : %d\r\n", (config_heartbeat_time/HOURS_TO_SECONDS_COEFF));
|
|
//___________________________
|
|
|
|
strret = strtok (NULL," ");
|
|
strcpy(epcheck, strret);
|
|
|
|
if(!strcmp(epcheck,"EP\"") || !strcmp(epcheck,"EP")){
|
|
ESP_LOGI(TAG,"(%s) %s message is terminated\r\n", epcheck,messageheader);
|
|
break;
|
|
}
|
|
else{
|
|
ESP_LOGI(TAG,"(%s) %s message is not terminated!! \r\n", epcheck,messageheader);
|
|
}
|
|
|
|
}
|
|
/* ============================
|
|
temp[0] = date_time[0];
|
|
temp[1] = date_time[1];
|
|
temp[2] = date_time[2];
|
|
temp[3] = date_time[3];
|
|
|
|
RTC_DateTime.Year = atoi(temp);
|
|
|
|
==============TEST==============
|
|
LOG_INFO("Year: %d\r\n",RTC_DateTime.Year);
|
|
============================
|
|
|
|
temp[0] = date_time[5];
|
|
temp[1] = date_time[6];
|
|
temp[2] = ' ';
|
|
temp[3] = ' ';
|
|
RTC_DateTime.Month = atoi(temp); //mm value to integer
|
|
|
|
==============TEST==============
|
|
LOG_INFO("Month: %d\r\n",RTC_DateTime.Month);
|
|
============================
|
|
|
|
temp[0] = date_time[8];
|
|
temp[1] = date_time[9];
|
|
temp[2] = ' ';
|
|
temp[3] =' ';
|
|
RTC_DateTime.MonthDay = atoi(temp); //dd value to integer
|
|
|
|
==============TEST==============
|
|
LOG_INFO("MonthDay: %d\r\n",RTC_DateTime.MonthDay);
|
|
============================
|
|
temp[0] = date_time[11];
|
|
temp[1] = date_time[12];
|
|
temp[2] = ' ';
|
|
temp[3] =' ';
|
|
RTC_DateTime.Hour = atoi(temp); //HH value to integer
|
|
|
|
==============TEST==============
|
|
LOG_INFO("Hour: %d\r\n",RTC_DateTime.Hour);
|
|
============================
|
|
temp[0] = date_time[14];
|
|
temp[1] = date_time[15];
|
|
temp[2] = ' ';
|
|
temp[3] = ' ';
|
|
RTC_DateTime.Minute = atoi(temp); //hh value t integer
|
|
|
|
==============TEST==============
|
|
LOG_INFO("Minute: %d\r\n",RTC_DateTime.Minute);
|
|
============================
|
|
temp[0] = date_time[17];
|
|
temp[1] = ' ';
|
|
temp[2] = ' ';
|
|
temp[3] = ' ';
|
|
RTC_DateTime.WeekDay = atoi(temp);
|
|
|
|
==============TEST==============
|
|
LOG_INFO("WeekDay: %d\r\n",RTC_DateTime.WeekDay);
|
|
============================
|
|
//rtc_set_timestamp(&rtcData);
|
|
RTC_SetTimeDate(&RTC_DateTime);*/
|
|
|
|
// update relay state if changed
|
|
if(RelayState_1 != PreviousRelayState_1)
|
|
{
|
|
|
|
|
|
ESP_LOGI(TAG,"Previous Relay State :%d \n",PreviousRelayState_1);
|
|
PreviousRelayState_1 = RelayState_1; /* update PreviousRelayState */
|
|
|
|
if(RelayState_1 == true)
|
|
{
|
|
/*============================*/
|
|
board_relay_on();
|
|
//board_led_all_off();
|
|
//board_led_green_on();
|
|
//hmi_choose_led(HMI_LED_GREEN, HMI_LED_ON);
|
|
|
|
ESP_LOGI(TAG,"Board relay state : %d\n",RelayState_1);
|
|
}
|
|
else
|
|
{
|
|
/*============================*/
|
|
board_relay_off();
|
|
//board_led_all_off();
|
|
//board_led_red_on();
|
|
// hmi_choose_led(HMI_LED_RED, HMI_LED_ON);
|
|
|
|
ESP_LOGI(TAG,"Board relay state : %d\n ",RelayState_1);
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(TAG,"Previous Relay State :%d \n",PreviousRelayState_1);
|
|
ESP_LOGI(TAG,"No changes in Relay State\r\n");
|
|
/*Transition to idle state*/
|
|
//comms.poll_timer = true; //polling the comms_timer
|
|
//Comms_Check_TestMode();
|
|
}
|
|
|
|
|
|
int retval = COMMS_STATUS_OK;
|
|
//creating sp1 message
|
|
SP1_Mqtt_Tx(config_heartbeat_time);
|
|
|
|
/* Run the MQTT publish sequence if nothing else pending */
|
|
// retval = start_mqtt_publish_sequence();
|
|
if(retval == 1)
|
|
{
|
|
ESP_LOGI(TAG,"MQTT Publish SP1 message success");
|
|
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGI(TAG,"MQTT Publish SP1 message failed");
|
|
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// Interrupt Service Routine (ISR)
|
|
void IRAM_ATTR timer_isr_callback(void *param) {
|
|
int timer_idx = (int)param;
|
|
|
|
// Clear the interrupt flag
|
|
timer_group_clr_intr_status_in_isr(TIMER_GROUP_0, timer_idx);
|
|
|
|
// Re-enable the timer alarm
|
|
timer_group_enable_alarm_in_isr(TIMER_GROUP_0, timer_idx);
|
|
|
|
heartbeat_event_flag = true;
|
|
}
|
|
|
|
// Timer Initialization
|
|
void my_timer_init() {
|
|
// Disable existing interrupts before registering a new one
|
|
timer_deinit(TIMER_GROUP_0, TIMER_0);
|
|
|
|
timer_config_t config = {
|
|
.divider = TIMER_DIVIDER,
|
|
.counter_dir = TIMER_COUNT_UP,
|
|
.alarm_en = TIMER_ALARM_EN,
|
|
.auto_reload = true, // Restart timer after reaching the alarm value
|
|
.intr_type = TIMER_INTR_LEVEL
|
|
};
|
|
|
|
timer_init(TIMER_GROUP_0, TIMER_0, &config);
|
|
timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0);
|
|
timer_set_alarm_value(TIMER_GROUP_0, TIMER_0, TIMER_SCALE * TIMER_INTERVAL_SEC);
|
|
timer_enable_intr(TIMER_GROUP_0, TIMER_0);
|
|
|
|
// Register the ISR with SHARED interrupt flag
|
|
esp_err_t err = timer_isr_callback_add(TIMER_GROUP_0, TIMER_0, timer_isr_callback, (void*)TIMER_0, ESP_INTR_FLAG_SHARED | ESP_INTR_FLAG_IRAM);
|
|
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to register timer ISR: %s", esp_err_to_name(err));
|
|
return;
|
|
}
|
|
|
|
// Start the timer
|
|
timer_start(TIMER_GROUP_0, TIMER_0);
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
}
|
|
|
|
void app_main(void)
|
|
{
|
|
|
|
ESP_LOGI(TAG,"*** Starting app_main ***");
|
|
|
|
/* Initialize the dedicated NVS partition */
|
|
nvm_init();
|
|
port_init();
|
|
uart_ifx_init();
|
|
|
|
/* Create the UART tasks for both UART0 and UART1 */
|
|
uart_create_rx_tasks();
|
|
<<<<<<< HEAD
|
|
|
|
|
|
// Get firmwae version
|
|
// const esp_app_desc_t *app_desc = esp_app_get_description();
|
|
// ESP_LOGI(TAG, "Firmware Version: %s", app_desc->version);
|
|
|
|
// while(1){
|
|
// vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
// get_time();
|
|
// ESP_LOGI("TIME", "Current time =====> %s", buff);
|
|
|
|
// ESP_LOGI(TAG,"====>>> Test data <<<=========");
|
|
// }
|
|
/*
|
|
|
|
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 ***");
|
|
}
|
|
*/
|
|
/* Start On-Boarding process */
|
|
webserver_start();
|
|
|
|
/* Wait till the onboarding process is completed */
|
|
while(!webserver_get_status())
|
|
{
|
|
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 ... :)");
|
|
|
|
// xTaskCreate(mqtt_task, "mqtt_task", 4096, NULL, 5, NULL);
|
|
|
|
char buf[512];
|
|
|
|
// get SP3 update
|
|
http_client_get(SP3_URL, buf);
|
|
get_mac_add();
|
|
|
|
while(1){
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
// get_time();
|
|
// ESP_LOGI("TIME", "Current time =====> %s", buff);
|
|
|
|
ESP_LOGI(TAG,"====>>> Test data <<<=========");
|
|
}
|
|
|
|
http_client_do_get_request(0, IMEI, buf);
|
|
http_client_do_get_request(1, IMEI, buf);
|
|
http_client_do_get_request(2, IMEI, buf);
|
|
|
|
while(1){
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
// get_time();
|
|
// ESP_LOGI("TIME", "Current time =====> %s", buff);
|
|
|
|
ESP_LOGI(TAG,"====>>> Test data <<<=========");
|
|
}
|
|
|
|
http_get_request(MCU_CONFIG);
|
|
|
|
|
|
|
|
|
|
while(1){
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
// get_time();
|
|
// ESP_LOGI("TIME", "Current time =====> %s", buff);
|
|
|
|
// ESP_LOGI(TAG,"====>>> Test data <<<=========");
|
|
}
|
|
|
|
|
|
/*
|
|
|
|
obtain_time(); // Sync time with NTP
|
|
|
|
get_time();
|
|
|
|
|
|
// Configure Target brocker 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);
|
|
|
|
http_get_request(MCU_UPDATE);
|
|
|
|
data_parsing_config(&HTTP_GET_DATA, sizeof(HTTP_GET_DATA));
|
|
|
|
while(1){
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
// get_time();
|
|
// ESP_LOGI("TIME", "Current time =====> %s", buff);
|
|
|
|
ESP_LOGI(TAG,"====>>> Test 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);
|
|
|
|
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] 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);
|
|
*/
|
|
|
|
if (strcmp(HTTP_GET_DATA ,"McuUpdate1" ) == 0)
|
|
{
|
|
ESP_LOGI(TAG,"OTA Start");
|
|
wifi_ota_start_firmware_update(IMEI);
|
|
}
|
|
while(1){
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
// ESP_LOGI(TAG, "Loop......");
|
|
}
|
|
|
|
// OTA start
|
|
wifi_ota_start_firmware_update(IMEI);
|
|
=======
|
|
/*
|
|
while(1) {
|
|
|
|
port_red_led_on();
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
port_red_led_off();
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
port_blue_led_on();
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
port_blue_led_off();
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
port_green_led_on();
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
port_green_led_off();
|
|
|
|
// app_tick();
|
|
ESP_LOGI(TAG, "LED testing running ....");
|
|
}*/
|
|
|
|
|
|
// get_mac(mac_address);
|
|
// printf("MAC Address in main: %s\n", mac_address);
|
|
|
|
/*while(1){
|
|
|
|
|
|
port_vbatt_sense_enable(0);
|
|
ESP_LOGI(TAG, "Realy ON");
|
|
vTaskDelay(1500/portTICK_PERIOD_MS);
|
|
|
|
port_accel_pwr_enable(1);
|
|
ESP_LOGI(TAG, "Realy OFF");
|
|
vTaskDelay(1500/portTICK_PERIOD_MS);
|
|
|
|
}*/
|
|
|
|
|
|
/* Start On-Boarding process */
|
|
webserver_start();
|
|
|
|
get_mac_add();
|
|
|
|
/* Wait till the onboarding process is completed */
|
|
while(!webserver_get_status())
|
|
{
|
|
vTaskDelay(750/portTICK_PERIOD_MS);
|
|
ESP_LOGI(TAG, "Waiting from wifi connect .....");
|
|
port_blue_led_toggle();
|
|
}
|
|
port_red_led_off();
|
|
port_green_led_on();
|
|
wifi_first_init();
|
|
|
|
// Connect_wifi_sta(WIFI_MODE_STA);
|
|
|
|
uint8_t ret = comms_init();
|
|
if ( ret == COMMS_STATUS_OK )
|
|
{
|
|
ESP_LOGI(TAG, "Comms init Successfully :) ");
|
|
comms_start(comms_started_cb, comms_connect_cb);
|
|
} else
|
|
{
|
|
ESP_LOGW(TAG, "Comms init faild :(");
|
|
}
|
|
|
|
my_timer_init();
|
|
|
|
// char url_sp4[]="http://54.204.230.201:8081/hae/azuma/128101153943920/mcu_config_download/";
|
|
// char res[512];
|
|
// http_client_get(url_sp4, res);
|
|
// ESP_LOGI(TAG, "SP4 ===> messgage \n %s",res);
|
|
//
|
|
// Comms_Parse_SP4(res, sizeof(res));
|
|
|
|
while(1) {
|
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
|
app_tick();
|
|
ESP_LOGI(TAG, "App tick running ....");
|
|
}
|
|
|
|
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
|
|
}
|
|
|
|
|
|
|