Added azuma wifi switch backup code

This commit is contained in:
unknown 2025-04-03 12:21:41 +05:30
parent 23c75794c2
commit a3839f0942
21 changed files with 3245 additions and 0 deletions

View File

@ -1,18 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<<<<<<< HEAD
<cconfiguration id="org.eclipse.cdt.core.default.config.297119829">
<storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.297119829" moduleId="org.eclipse.cdt.core.settings" name="Configuration">
=======
<cconfiguration id="org.eclipse.cdt.core.default.config.503389380">
<storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.503389380" moduleId="org.eclipse.cdt.core.settings" name="Configuration">
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
<externalSettings/>
<extensions/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<<<<<<< HEAD
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets"/>
<storageModule moduleId="org.eclipse.cdt.core.pathentry">
<pathentry excluding="**/CMakeFiles/**" kind="out" path="build"/>
</storageModule>
=======
<storageModule moduleId="org.eclipse.cdt.core.pathentry">
<pathentry kind="src" path=""/>
<pathentry excluding="**/CMakeFiles/**" kind="out" path="build"/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
</cproject>

View File

@ -1,6 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<<<<<<< HEAD
<name>tempstick_cellular_esp32</name>
=======
<name>Azuma-wifi-switch</name>
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
<comment></comment>
<projects>
</projects>

View File

@ -1,3 +1,4 @@
<<<<<<< HEAD
ESP-IDF template app
====================
@ -9,3 +10,50 @@ Please check [ESP-IDF docs](https://docs.espressif.com/projects/esp-idf/en/lates
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.*
=======
**Edit a file, create a new file, and clone from Bitbucket in under 2 minutes**
When you're done, you can delete the content in this README and update the file with details for others getting started with your repository.
*We recommend that you open this README in another tab as you perform the tasks below. You can [watch our video](https://youtu.be/0ocf7u76WSo) for a full demo of all the steps in this tutorial. Open the video in a new tab to avoid leaving Bitbucket.*
---
## Edit a file
Youll start by editing this README file to learn how to edit a file in Bitbucket.
1. Click **Source** on the left side.
2. Click the README.md link from the list of files.
3. Click the **Edit** button.
4. Delete the following text: *Delete this line to make a change to the README from Bitbucket.*
5. After making your change, click **Commit** and then **Commit** again in the dialog. The commit page will open and youll see the change you just made.
6. Go back to the **Source** page.
---
## Create a file
Next, youll add a new file to this repository.
1. Click the **New file** button at the top of the **Source** page.
2. Give the file a filename of **contributors.txt**.
3. Enter your name in the empty file space.
4. Click **Commit** and then **Commit** again in the dialog.
5. Go back to the **Source** page.
Before you move on, go ahead and explore the repository. You've already seen the **Source** page, but check out the **Commits**, **Branches**, and **Settings** pages.
---
## Clone a repository
Use these steps to clone from SourceTree, our client for using the repository command-line free. Cloning allows you to work on your files locally. If you don't yet have SourceTree, [download and install first](https://www.sourcetreeapp.com/). If you prefer to clone from the command line, see [Clone a repository](https://confluence.atlassian.com/x/4whODQ).
1. Youll see the clone button under the **Source** heading. Click that button.
2. Now click **Check out in SourceTree**. You may need to create a SourceTree account or log in.
3. When you see the **Clone New** dialog in SourceTree, update the destination path and name if youd like to and then click **Clone**.
4. Open the directory you just created to see your repositorys files.
Now that you're more familiar with your Bitbucket repository, go ahead and add a new file locally. You can [push your change back to Bitbucket with SourceTree](https://confluence.atlassian.com/x/iqyBMg), or you can [add, commit,](https://confluence.atlassian.com/x/8QhODQ) and [push from the command line](https://confluence.atlassian.com/x/NQ0zDQ).
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)

View File

@ -2,8 +2,13 @@
# for more information about component CMakeLists.txt files.
idf_component_register(
<<<<<<< HEAD
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
=======
SRCS main.c uart_ifx.c port.c adc_ifx.c modem.c comms.c rtc.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
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
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
PRIV_REQUIRES # optional, list the private requirements

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,4 @@
<<<<<<< HEAD
/*
* comms.h
*
@ -17,6 +18,18 @@
*
* Copyright 2020 HAE Innovations
*
=======
#ifndef __COMMS_H_
#define __COMMS_H_
#include <stdint.h>
#include <stdbool.h>
/* comms.h
*
* Copyright 2020 HAE Innovations
*
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
* Celluar Communications process manager.
*
* Handles communication to/from server over cell modem,
@ -48,6 +61,7 @@
#define PARTHA_SERVER_BASE_URL "https://www.parthasarathimishra.com"
<<<<<<< HEAD
#define TESTING_SERVER_BASE_URL "http://54.204.230.201:8085"
@ -68,6 +82,9 @@ typedef enum
#if (TEMPSTICK_SERVER == 1)
=======
#define TESTING_SERVER_BASE_URL "http://54.204.230.201:8081"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/*HTTP POST header component for tempstick server*/
#define END_POINT_POSTING1 "POST / HTTP/1.1\r\n"
@ -90,9 +107,15 @@ typedef enum
#define MCU_POST_URL3 MCU_BASE_URL3
//URL to get configuration flags from servers 1, 2, and 3
<<<<<<< HEAD
#define MCU_GET_CONFIG_FLAGS_URL1 TESTING_SERVER_BASE_URL"/hae/azuma/%s/update/"//MCU_BASE_URL1//config/flag/server1
#define MCU_GET_CONFIG_FLAGS_URL2 TESTING_SERVER_BASE_URL"/hae/azuma/%s/update/"//MCU_BASE_URL2//config/flag/server2
#define MCU_GET_CONFIG_FLAGS_URL3 TESTING_SERVER_BASE_URL"/hae/azuma/%s/update/"//MCU_BASE_URL3//config/flag/server3
=======
#define MCU_GET_CONFIG_FLAGS_URL1 TESTING_SERVER_BASE_URL"/hae/azuma/%s/update/" //MCU_BASE_URL1//config/flag/server1
#define MCU_GET_CONFIG_FLAGS_URL2 TESTING_SERVER_BASE_URL"/hae/azuma/%s/update/" //MCU_BASE_URL2//config/flag/server2
#define MCU_GET_CONFIG_FLAGS_URL3 TESTING_SERVER_BASE_URL"/hae/azuma/%s/update/" //MCU_BASE_URL3//config/flag/server3
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/* this endpoint not implemented yet in the code (customer pending) */
// if the config_update flag is set
@ -122,6 +145,7 @@ typedef enum
#define MODEM_FW_BIN_FILE_URL3 TESTING_SERVER_BASE_URL"/hae/azuma/%s/modem_pgm_download/MOTA.bin"//MCU_BASE_URL3//modem/fw/bin/file/server3
<<<<<<< HEAD
#else
@ -236,6 +260,8 @@ typedef enum
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/* TEMPSTICK servers URL's */
typedef enum {
TEMPSTICK_SERVER1=1,
@ -243,6 +269,14 @@ typedef enum {
TEMPSTICK_SERVER3
}serverUrl_t;
<<<<<<< HEAD
=======
#define COMMS_STATUS_UPDATED 1
#define COMMS_STATUS_OK 0
#define COMMS_STATUS_ERROR -1
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/* Operation complete indication callback */
typedef void (*comms_op_cb_t)(int status);
@ -264,7 +298,11 @@ int comms_poll(void);
/* Check if the process needs to be polled. */
bool comms_needs_poll(void);
<<<<<<< HEAD
/* Connect to server once process has completed startup and is idle
=======
/* Connect to server once process has completed startup and is idle
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
* Optional callback is invoked when process completes.
*/
int comms_connect(comms_op_cb_t cb);
@ -281,7 +319,16 @@ bool comms_is_started(void);
*/
int comms_get_status(void);
<<<<<<< HEAD
// Get MAC id
char* get_mac(void);
#endif /* MAIN_COMMS_H_ */
=======
void SP1_Mqtt_Tx(int );
#endif /* __COMMS_H_ */
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)

View File

@ -24,10 +24,14 @@
#include "esp_task_wdt.h"
#include "port.h"
#include "adc_ifx.h"
<<<<<<< HEAD
#include "ulp_main.h"
#include "i2c_sensors.h"
#include "data_processing.h"
#include "ulp_main.h"
=======
#include "data_processing.h"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#include "main.h"
#include "hmi.h"
#include "comms.h"
@ -44,7 +48,10 @@ static const char* TAG = "DATA";
extern bool isCycleCompleted;
extern uint8_t comms_mode;
<<<<<<< HEAD
extern comms_medium_t comms_medium;
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
extern uint8_t checkin_cycle_counter;
extern uint8_t number_of_check_in_to_do;
@ -73,6 +80,7 @@ uint32_t g_McuUpdate;
uint32_t g_ModemUpdate;
<<<<<<< HEAD
/*RTC_DATA_ATTR static uint16_t g_triggersThatCanWakeTheHost;
RTC_DATA_ATTR static uint16_t g_strobedHumidityFlags;
@ -182,6 +190,9 @@ static bool isThermocouplePresent = false;
settings *esp_settings = (settings*)(&ulp_settings_structure);
RTC_DATA_ATTR historyLog_t _logs[NVM_MAX_NUMBER_OF_IN_ONE_SECTOR] = {};
=======
RTC_DATA_ATTR historyLog_t _logs[NVM_MAX_NUMBER_OF_IN_ONE_SECTOR] = {};
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
typedef struct
@ -214,6 +225,7 @@ typedef struct
RTC_DATA_ATTR rtcRamData_t _rtcRamData;
<<<<<<< HEAD
uint32_t data_get_send_interval(void)
{
if(esp_settings->sendInterval)
@ -227,6 +239,8 @@ bool data_is_usb_connected(void)
return g_usbConnected;
}
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
static esp_err_t data_parse_float_value(float *target_buffer, const char needle[], const char *http_resp)
{
esp_err_t retval = ESP_FAIL;
@ -456,6 +470,7 @@ static esp_err_t data_parse_String_value(char *target_buffer, const char needle[
}
<<<<<<< HEAD
static void setAccelerometerPowerState(void)
{
@ -2392,6 +2407,8 @@ void data_initialize_rtc_ram(void)
_rtcRamData.retriesAreInProgress = false;
}
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
void data_set_ap_mac_addr(uint8_t *bssid)
{
for(int i=0; i<6; i++)
@ -2471,7 +2488,10 @@ void data_clearWifiConnectionSettings(void)
<<<<<<< HEAD
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/*******************************************************************/
/* Buffer(Json Sample) */
/*{ "request": "settings", "time":1675859084, "id":10608508,
@ -2481,13 +2501,20 @@ void data_clearWifiConnectionSettings(void)
esp_err_t data_parsing_config(char *buff,int len){
esp_err_t retval = ESP_FAIL;
<<<<<<< HEAD
char *ret = NULL;
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
char server_request[11]={0};
// char setting_req[]="settings";
// ret = strstr(buff, (const char*)"settings");
// data_parse_String_value(server_request,"request",buff);
// if( ret != NULL ){
<<<<<<< HEAD
if(1){
=======
if(1){
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
//retval = data_parse_uint_value(&g_server_time,"time", buff);
retval = data_parse_uint_value(&g_ID,"id", buff);
@ -2495,6 +2522,7 @@ esp_err_t data_parsing_config(char *buff,int len){
retval = data_parse_uint_value(&g_ConfigUpdate,"ConfigUpdate", buff);
retval = data_parse_uint_value(&g_McuUpdate,"McuUpdate", buff);
retval = data_parse_uint_value(&g_ModemUpdate,"ModemUpdate", buff);
<<<<<<< HEAD
ESP_LOGI(TAG, "ConfigUpdate: %ld McuUpdate: %ld ModemUpdate: %ld", g_ConfigUpdate, g_McuUpdate, g_ModemUpdate);
if(g_McuUpdate == 1)
@ -2504,6 +2532,25 @@ esp_err_t data_parsing_config(char *buff,int len){
{
ESP_LOGI(TAG, "Invalid request :%s",server_request);
}
=======
ESP_LOGI(TAG, "ConfigUpdate: %luMcuUpdate: %lu ModemUpdate: %lu", g_ConfigUpdate, g_McuUpdate, g_ModemUpdate);
g_McuUpdate = 1;
//g_ConfigUpdate = 1;
if(g_ConfigUpdate == 1)
ESP_LOGI(TAG, "Configuration update = 1 ");
}
if(g_McuUpdate == 1){
ESP_LOGI(TAG, "g_McuUpdate update = 1 ");
}
else
{
ESP_LOGI(TAG, "Invalid request :%s",server_request);
}
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
return retval;
}
@ -2526,6 +2573,7 @@ esp_err_t check_in_attempts_count( int httpRetryCnt, int maxretrynum )
}
<<<<<<< HEAD
/* should use in main.c to initiate sendDetails var*/
void init_send_Details(void)
{
@ -2608,4 +2656,6 @@ void data_set_urlConnect(void)
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)

View File

@ -22,7 +22,10 @@
#include "comms.h"
#include "port.h"
#include "adc_ifx.h"
<<<<<<< HEAD
#include "i2c_sensors.h"
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#include "data_processing.h"
#include "hmi.h"
@ -44,6 +47,14 @@ static const char* TAG = "HMI";
#define BLUE_RED_FLASHING_200ms_DURARION 200 // Duration for BLUE_RED_FLASHING led_event
#define NORMAL_HMI_DURATION 1000 // Duration for NORMAL_HMI cycle (cycle without triggered events)
<<<<<<< HEAD
=======
/*LED definitions*/
#define RED_TRI_LED 0
#define GREEN_TRI_LED 1
#define BLUE_TRI_LED 2
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
static QueueHandle_t hmi_queue; // FIFO queue that stores the leds event
@ -555,6 +566,11 @@ void hmi_stop_ota_event(void)
}
bool leds_enabled = true;
<<<<<<< HEAD
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
void set_leds_enable(bool status)
{
leds_enabled = status;
@ -564,6 +580,68 @@ void set_leds_enable(bool status)
port_blue_led_off();
}
}
<<<<<<< HEAD
=======
/* State of the hmi process */
typedef enum
{
HMI_STATE_INIT,
HMI_STATE_RUN,
} hmi_state_t;
typedef struct
{
hmi_state_t state;
/* Timer */
bool poll_timer;
/* General 1 sec timer */
int sec_timer;
int active_led;
bool red_led_on;
bool green_led_on;
bool blue_led_on;
uint8_t led_state;
int** blink_pattern;
} hmi_t;
static hmi_t hmi;
int hmi_choose_led(int led_color,uint8_t op_mode)
{
// int retval=HMI_STATUS_OK;
hmi.led_state = op_mode; /*led state ( on,off or blinking).*/
switch (led_color)
{
case RED_TRI_LED:
hmi.red_led_on = 1;
hmi.green_led_on = 0;
hmi.blue_led_on = 0;
break;
case GREEN_TRI_LED:
hmi.red_led_on = 0;
hmi.green_led_on = 1;
hmi.blue_led_on = 0;
break;
case BLUE_TRI_LED:
hmi.red_led_on = 0;
hmi.green_led_on = 0;
hmi.blue_led_on = 1;
break;
default:
hmi.red_led_on = 0;
hmi.green_led_on = 0;
hmi.blue_led_on = 0;
break;
}
return 0;
}
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/* This is the hmi_task that will be executed periodically*/
void hmi_task(void *pvParameters)

View File

@ -8,6 +8,12 @@
#ifndef MAIN_HMI_H_
#define MAIN_HMI_H_
<<<<<<< HEAD
=======
#include <stdbool.h>
#include <stdbool.h>
#include <stdint.h>
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
typedef enum{
BLUE_LED_FLASHING, // Connecting to LTE Network
BLUE_LED_FLASHING_OTA, // OTA Update Firmware
@ -21,6 +27,26 @@ typedef enum{
BLUE_RED_LEDS_OFF
}hmi_leds_state_t;
<<<<<<< HEAD
=======
typedef enum
{
HMI_LED_RED,
HMI_LED_GREEN,
HMI_LED_BLUE,
HMI_LED__MAX__
} hmi_led_type_t;
typedef enum
{
HMI_LED_OFF = 0,
HMI_LED_ON,
HMI_LED_BLINK,
} hmi_led_state_t;
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
void hmi_init(void);
void hmi_set_leds_state(hmi_leds_state_t status);
void hmi_stop_continued_led_state(hmi_leds_state_t led_event);
@ -31,5 +57,9 @@ void hmi_task(void *pvParameters);
void set_leds_enable(bool status);
void hmi_do_usb_event_inside_hmi(bool decision);
void hmi_exception_usb_event(void);
<<<<<<< HEAD
=======
int hmi_choose_led(int led_color,uint8_t op_mode);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#endif /* MAIN_HMI_H_ */

View File

@ -1,4 +1,8 @@
<<<<<<< HEAD
=======
#include <stdint.h>
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
@ -22,7 +26,11 @@
#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"
@ -33,10 +41,46 @@
#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)
@ -46,9 +90,14 @@
#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/"
@ -56,18 +105,166 @@
#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
@ -93,6 +290,7 @@ void get_mac_add(void) { // The function modifies the passed buffer
printf("Formatted MAC (15 Characters): %s\n", MAC_ID);
}
<<<<<<< HEAD
typedef enum {
MQTT_STATE_INIT,
MQTT_STATE_CONNECT,
@ -283,6 +481,328 @@ void http_get_request(const char *path) {
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)
@ -297,6 +817,7 @@ void app_main(void)
/* Create the UART tasks for both UART0 and UART1 */
uart_create_rx_tasks();
<<<<<<< HEAD
// Get firmwae version
@ -464,6 +985,89 @@ void app_main(void)
// 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)
}

View File

@ -8,7 +8,12 @@
#ifndef MAIN_MAIN_H_
#define MAIN_MAIN_H_
<<<<<<< HEAD
extern char MAC_ID[15];
=======
#include <stdint.h>
extern char MAC_ID[16];
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#define SENS_INT_WAKEUP 0
#define SENS_INT_WAKEUP0 1
@ -25,6 +30,11 @@ extern char MAC_ID[15];
#define DEFAULT_COMMS_MODE 1
<<<<<<< HEAD
=======
#define BUILD_VERSION_STRING "LRSM0009C_Azuma_v0.5"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
void update_checkin_timer(uint64_t checkin_interval);
#if (SENS_INT_WAKEUP == 1)
void set_periodic_sleep_mode();

View File

@ -24,11 +24,18 @@ static const char* TAG = "PORT";
#define ESP_INTR_FLAG_DEFAULT 0
<<<<<<< HEAD
#define LED_BLUE_PIN 12
#define LED_RED_PIN 13
#define UD_RAD_PWRKEY_PIN 20
#define UD_RAD_RESET_PIN 21
=======
#define LED_BLUE_PIN 26
#define LED_RED_PIN 20
#define LED_GREEN_PIN 21
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#define MODEM_LDO_EN_PIN 33
#define RAD_STATUS_PIN 19
@ -41,7 +48,11 @@ static const char* TAG = "PORT";
#define ADC_BATT_MON_PIN 1
#define ADC_LIGHT_SEN_PIN 2
<<<<<<< HEAD
#define VBAT_SENSE_EN_PIN 14
=======
#define VBAT_SENSE_EN_PIN 42
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#define LOWV_DETECT_PIN 38
@ -54,6 +65,10 @@ extern void IRAM_ATTR push_button_isr(void *args);
static bool red_led_status = 0;
static bool blue_led_status = 0;
<<<<<<< HEAD
=======
static bool green_led_status = 0;
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
static void init_outputs(void)
{
@ -63,11 +78,15 @@ static void init_outputs(void)
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = PIN_MASK(LED_RED_PIN) | \
PIN_MASK(LED_BLUE_PIN) | \
<<<<<<< HEAD
PIN_MASK(UD_RAD_PWRKEY_PIN) | \
PIN_MASK(UD_RAD_RESET_PIN) | \
PIN_MASK(MODEM_LDO_EN_PIN) | \
PIN_MASK(ACCEL_PWN_PIN) | \
/* PIN_MASK(LIGHT_SENSOR_EN_PIN) |*/
=======
PIN_MASK(LED_GREEN_PIN) | \
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
PIN_MASK(VBAT_SENSE_EN_PIN);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
@ -75,6 +94,7 @@ static void init_outputs(void)
ESP_ERROR_CHECK(gpio_config(&io_conf));
//Initialize all Outputs to LOW
<<<<<<< HEAD
gpio_set_level(LED_RED_PIN, 0);
gpio_set_level(LED_BLUE_PIN, 0);
gpio_set_level(UD_RAD_PWRKEY_PIN, 0);
@ -82,6 +102,11 @@ static void init_outputs(void)
gpio_set_level(MODEM_LDO_EN_PIN, 0);
gpio_set_level(ACCEL_PWN_PIN, 0);
gpio_set_level(LIGHT_SENSOR_EN_PIN, 0);
=======
gpio_set_level(LED_RED_PIN, 1);
gpio_set_level(LED_BLUE_PIN, 1);
gpio_set_level(LED_GREEN_PIN, 1);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
gpio_set_level(VBAT_SENSE_EN_PIN, 0);
}
@ -106,7 +131,11 @@ static void init_inputs(void)
static void init_isrs(void)
{
<<<<<<< HEAD
gpio_config_t io_conf = {0};
=======
/*gpio_config_t io_conf = {0};
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
io_conf.intr_type = GPIO_INTR_ANYEDGE;
io_conf.mode = GPIO_MODE_INPUT;
@ -122,16 +151,27 @@ static void init_isrs(void)
gpio_isr_handler_add(ACCEL_INT1_PIN, accel_int1_isr, (void *)ACCEL_INT1_PIN);
gpio_isr_handler_add(VUSB_DETECT_PIN, vusb_detect_isr, (void *)VUSB_DETECT_PIN);
gpio_isr_handler_add(LOWV_DETECT_PIN, lowv_detect_isr, (void *)LOWV_DETECT_PIN);
<<<<<<< HEAD
gpio_isr_handler_add(SWITCH_INPUT_PIN, push_button_isr, (void *)SWITCH_INPUT_PIN);
=======
gpio_isr_handler_add(SWITCH_INPUT_PIN, push_button_isr, (void *)SWITCH_INPUT_PIN);*/
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
}
void port_init(void)
{
init_outputs();
<<<<<<< HEAD
init_inputs();
init_isrs();
gpio_deep_sleep_hold_dis();
=======
// init_inputs();
// init_isrs();
// gpio_deep_sleep_hold_dis();
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/*rtc_gpio_init(LED_BLUE_PIN);
rtc_gpio_set_direction(LED_BLUE_PIN, RTC_GPIO_MODE_OUTPUT_ONLY);
rtc_gpio_pulldown_dis(LED_BLUE_PIN);
@ -141,15 +181,38 @@ void port_init(void)
void port_red_led_on(void)
{
<<<<<<< HEAD
gpio_set_level(LED_RED_PIN, 1);
=======
gpio_set_level(LED_RED_PIN, 0);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
red_led_status = 1;
}
void port_red_led_off(void)
{
<<<<<<< HEAD
gpio_set_level(LED_RED_PIN, 0);
red_led_status = 0;
}
=======
gpio_set_level(LED_RED_PIN, 1);
red_led_status = 0;
}
void port_green_led_off(void)
{
gpio_set_level(LED_GREEN_PIN, 1);
green_led_status = 0;
}
void port_green_led_on(void)
{
gpio_set_level(LED_GREEN_PIN, 0);
green_led_status = 1;
}
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
void port_red_led_toggle(void)
{
if(red_led_status)
@ -169,12 +232,20 @@ bool port_red_led_is_on(void)
void port_blue_led_on(void)
{
<<<<<<< HEAD
gpio_set_level(LED_BLUE_PIN, 1);
=======
gpio_set_level(LED_BLUE_PIN, 0);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
blue_led_status = 1;
}
void port_blue_led_off(void)
{
<<<<<<< HEAD
gpio_set_level(LED_BLUE_PIN, 0);
=======
gpio_set_level(LED_BLUE_PIN, 1);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
blue_led_status = 0;
}
@ -200,7 +271,11 @@ bool port_blue_led_is_on(void)
return blue_led_status;
}
<<<<<<< HEAD
void port_modem_assert_pwrkey(void)
=======
/*void port_modem_assert_pwrkey(void)
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
{
gpio_set_level(UD_RAD_PWRKEY_PIN, 1);
}
@ -208,7 +283,11 @@ void port_modem_assert_pwrkey(void)
void port_modem_deassert_pwrkey(void)
{
gpio_set_level(UD_RAD_PWRKEY_PIN, 0);
<<<<<<< HEAD
}
=======
}*/
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
int port_modem_is_on(void)
{

View File

@ -36,6 +36,12 @@ void port_blue_led_off(void);
void port_blue_led_toggle(void);
bool port_blue_led_is_on(void);
<<<<<<< HEAD
=======
void port_green_led_on(void);
void port_green_led_off(void);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
void port_modem_assert_pwrkey(void);
void port_modem_deassert_pwrkey(void);
int port_is_usb_connected(void);

View File

@ -8,6 +8,22 @@
#include <time.h>
#include <sys/time.h>
#include "esp_sntp.h"
<<<<<<< HEAD
=======
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lwip/sockets.h"
#include "lwip/err.h"
#include "lwip/netdb.h"
#include "lwip/icmp.h"
#include "esp_system.h"
#include "esp_log.h"
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "nvm.h"
#include "nvs_flash.h"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
@ -32,6 +48,98 @@ static const char* TAG = "RTC";
static bool rtc_set = false;
<<<<<<< HEAD
=======
#define PING_HOST "8.8.8.8" // Google DNS
#define CHECK_INTERVAL 1 //60000 // Check every 60 seconds
#define MAX_DISCONNECT_TIME 5 //86400 // 24 hours in seconds
#define SEC 3600
static time_t last_successful_ping = 0;
/// Internet monitor //
time_t get_sntp_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec; // Return UNIX timestamp
}
bool ping_test()
{
struct sockaddr_in target_addr;
target_addr.sin_addr.s_addr = inet_addr(PING_HOST);
target_addr.sin_family = AF_INET;
target_addr.sin_port = htons(0);
int sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sock < 0)
{
ESP_LOGE(TAG, "Socket creation failed");
return false;
}
struct icmp_echo_hdr echo_req;
memset(&echo_req, 0, sizeof(echo_req));
echo_req.id = htons(1);
echo_req.seqno = htons(1);
echo_req.type = ICMP_ECHO;
if (sendto(sock, &echo_req, sizeof(echo_req), 0, (struct sockaddr *)&target_addr, sizeof(target_addr)) < 0)
{
ESP_LOGE(TAG, "Ping send failed");
close(sock);
return false;
}
fd_set read_fds;
struct timeval timeout = {1, 0}; // 1 second timeout
FD_ZERO(&read_fds);
FD_SET(sock, &read_fds);
if (select(sock + 1, &read_fds, NULL, NULL, &timeout) > 0)
{
ESP_LOGI(TAG, "Internet is available");
close(sock);
return true;
}
ESP_LOGW(TAG, "Ping timeout, no internet");
close(sock);
return false;
}
void internet_monitor_task(void *pvParameter)
{
while (1)
{
time_t current_time = get_sntp_time();
if (ping_test())
{
last_successful_ping = current_time;
}
if (last_successful_ping > 0 && (current_time - last_successful_ping) > MAX_DISCONNECT_TIME)
{
ESP_LOGE(TAG, "No internet for %d hours, restarting...", (MAX_DISCONNECT_TIME/SEC));
/* erase the NVS partition */
ESP_ERROR_CHECK(nvs_flash_erase());
esp_restart();
}
vTaskDelay(pdMS_TO_TICKS(CHECK_INTERVAL)); // Wait before next check
}
}
// END ///
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
int app_rtc_init(void)
{
int retval = RTC_STATUS_OK;
@ -231,6 +339,7 @@ void time_sync_notification_cb(struct timeval_1 *tv) {
}
*/
<<<<<<< HEAD
void obtain_time(void) {
ESP_LOGI(TAG, "Initializing SNTP");
@ -238,6 +347,64 @@ void obtain_time(void) {
sntp_setservername(0, "pool.ntp.org"); // Set NTP server
// sntp_set_time_sync_notification_cb(time_sync_notification_cb);
sntp_init();
=======
void obtain_time(void) {
ESP_LOGI(TAG, "Initializing SNTP");
const char *ntp_servers[] = {
"pool.ntp.org",
"time.google.com",
"1.pool.ntp.org"
};
int server_index = 0;
int num_servers = sizeof(ntp_servers) / sizeof(ntp_servers[0]);
time_t now = 0;
struct tm timeinfo = { 0 };
while (server_index < num_servers) {
ESP_LOGI(TAG, "Using NTP server: %s", ntp_servers[server_index]);
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
esp_sntp_setservername(0, ntp_servers[server_index]);
esp_sntp_init();
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 + 1, max_retries);
vTaskDelay(2000 / portTICK_PERIOD_MS);
time(&now);
localtime_r(&now, &timeinfo);
retry++;
}
if (timeinfo.tm_year >= (2020 - 1900)) {
ESP_LOGI(TAG, "Time synchronized: %s", asctime(&timeinfo));
// create internet monitor task
xTaskCreate(&internet_monitor_task, "internet_monitor_task", 4096, NULL, 5, NULL);
return; // Exit the function since time is successfully obtained
}
ESP_LOGE(TAG, "Failed to sync time with %s, switching to next server...", ntp_servers[server_index]);
esp_sntp_stop(); // Stop SNTP before switching
server_index++; // Move to the next server
}
ESP_LOGE(TAG, "All NTP servers failed! Check network settings.");
}
/*void obtain_time(void) {
ESP_LOGI(TAG, "Initializing SNTP");
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
esp_sntp_setservername(0, "pool.ntp.org"); // Set NTP server
// sntp_set_time_sync_notification_cb(time_sync_notification_cb);
esp_sntp_init();
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
// Wait for time to be set
time_t now = 0;
@ -257,6 +424,7 @@ void obtain_time(void) {
} else {
ESP_LOGI(TAG, "Time synchronized");
}
<<<<<<< HEAD
}
char* get_time(void) {
@ -290,3 +458,32 @@ char* get_time(void) {
=======
}*/
void get_time(char *time_str, size_t size) { // Buffer is provided by the caller
time_t now;
struct tm timeinfo;
time(&now);
// ESP_LOGI(TAG, "Time print start %ld", now);
localtime_r(&now, &timeinfo);
const char *weekdays[] = {"0", "1", "2", "3",
"4", "5", "6"};
// Format time string
snprintf(time_str, size, "%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, "Formatted Time: %s", time_str);
}
///////////////////////////////////////////////////////
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)

View File

@ -77,7 +77,11 @@ uint32_t rtc_get_epoch(void);
void rtc_set_epoch(uint32_t epoch);
<<<<<<< HEAD
char* get_time(void);
=======
void get_time(char *, size_t );
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
void obtain_time(void);
#endif /* MAIN_RTC_H_ */

View File

@ -6,9 +6,20 @@
*/
<<<<<<< HEAD
#include <string.h>
#include <stdlib.h>
#include "esp_log.h"
=======
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_netif_types.h"
#include "main.h"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#include "nvs_flash.h"
#include "esp_event.h"
#include "esp_netif.h"
@ -26,6 +37,10 @@
#include "esp_mac.h"
#include "data_processing.h"
#include "hmi.h"
<<<<<<< HEAD
=======
#include "comms.h"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#if (WIFI_NEEDED == 1)
static const char* TAG = "CLIENT";
@ -34,6 +49,11 @@ static const char* TAG = "CLIENT";
extern uint32_t g_version;
extern uint32_t g_McuUpdate;
<<<<<<< HEAD
=======
char http_resp[512];
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
bool isDataNeeded = false;
bool getBinFile;
@ -110,8 +130,14 @@ esp_err_t http_event_handler(esp_http_client_event_t *evt)
//output_buffer = (char *)pvPortMalloc(evt->data_len);
memcpy(output_buffer, evt->data, evt->data_len);
output_buffer[evt->data_len] = 0;
<<<<<<< HEAD
isDataNeeded = false;
//ESP_LOGI(TAG, "%s\n", output_buffer);
=======
ESP_LOGI(TAG, "%s\n", output_buffer);
strcpy(http_resp, output_buffer);
ESP_LOGI(TAG, "http_resp ==> %s\n", http_resp);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
}
break;
@ -147,8 +173,14 @@ esp_err_t http_event_handler(esp_http_client_event_t *evt)
break;
case HTTP_EVENT_REDIRECT:
<<<<<<< HEAD
ESP_LOGI(TAG, "HTTP_EVENT_REDIRECTED");
break;
=======
ESP_LOGI(TAG, "HTTP_EVENT_REDIRECT");
esp_http_client_set_redirection(evt->client);
break;
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
}
return ESP_OK;
}
@ -164,21 +196,62 @@ static int sum_digits(int digit)
return sum;
}
<<<<<<< HEAD
=======
static bool mac_check_valid(char *message_imei)
{
//find the length of message imei
int len = strlen(message_imei);
ESP_LOGI(TAG,"Received MAC ID : %s",message_imei);
ESP_LOGI(TAG,"Board MAC ID : %s",MAC_ID);
//check length of imei field
if (len != 15){
//invalid IMEI
ESP_LOGW(TAG,"Invalid MAC length");
return false;
}
if(!strcmp(message_imei,MAC_ID)){
ESP_LOGW(TAG,"Valid MAC ");
return true;
}
else{
ESP_LOGI(TAG,"Message IMEI doesn't match board IMEI");
return false;
}
}
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
static bool imei_check_valid(char *message_imei)
{
//find the length of message imei
int len = strlen(message_imei);
int sum = 0;
ESP_LOGI(TAG,"IMEI : %s",message_imei);
<<<<<<< HEAD
=======
ESP_LOGI(TAG,"Board IMEI : %s",MAC_ID);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
//check length of imei field
if (len != 15){
//invalid IMEI
<<<<<<< HEAD
ESP_LOGW(TAG,"Invalid IMEI length");
return false;
}
if(!strcmp(message_imei,modem_get_imei())){
=======
ESP_LOGW(TAG,"Invalid MAC length");
return false;
}
if(!strcmp(message_imei,MAC_ID)){
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
for(int i = len-1 ; i >= 0; i--)
{
//check if message imei fields contain numbers (0-9)
@ -191,6 +264,12 @@ static bool imei_check_valid(char *message_imei)
// Finding sum of the digits
// if digit*2 is a two digit number add the two digits
sum += sum_digits(digit);
<<<<<<< HEAD
=======
ESP_LOGW(TAG,"Sum value = %d",sum);
ESP_LOGW(TAG,"Sum mod value = %d",sum %10);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
}
else{
@ -229,7 +308,11 @@ static void wifi_Parse_SP5(char *buf, int len)
/*IMEI*/
strret = strtok (NULL," ");
strcpy(messageIMEI, strret);
<<<<<<< HEAD
ret_sp5 = imei_check_valid(messageIMEI);
=======
ret_sp5 = mac_check_valid(messageIMEI);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
if(ret_sp5 != false){
//ok IMEI
ESP_LOGI(TAG,"Fetched IMEI matches board");
@ -251,9 +334,19 @@ static void wifi_Parse_SP5(char *buf, int len)
strret = strtok(NULL, "FW_");
strcpy(firmware_version,strret);
new_fw_version = (uint32_t)atoi(firmware_version);
<<<<<<< HEAD
ESP_LOGI(TAG,"Firmware version from SP5: %ld\nBoard Firmware version: %ld",new_fw_version,g_version); //logging/printing FR_version message for tracing
/**********************************************************************************************************/
=======
ESP_LOGI(TAG,"Firmware version from SP5: %lu\nBoard Firmware version: %lu",new_fw_version,g_version); //logging/printing FR_version message for tracing
/**********************************************************************************************************/
// test version
new_fw_version = 1;
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/*check if the new version is greater than current version*/
if(!(new_fw_version > g_version))
{
@ -268,6 +361,10 @@ static void wifi_Parse_SP5(char *buf, int len)
getBinFile = true;
/*reading OTA files*/
}
<<<<<<< HEAD
=======
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
}
@ -363,6 +460,10 @@ static void http_client_get_task(void *pvParameters)
.url = (const char*)url, // Replace with your desired URL
.method = HTTP_METHOD_GET,
.cert_pem = NULL,
<<<<<<< HEAD
=======
.user_data = http_resp,
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
.event_handler = http_event_handler
};
@ -429,10 +530,37 @@ esp_err_t http_client_get(const char *url1, char *response)
{
vTaskDelay(10 / portTICK_PERIOD_MS);
}
<<<<<<< HEAD
return http_client_get_err;
}
=======
isDataNeeded = false;
return http_client_get_err;
}
esp_err_t http_get_response_buf(char* bufptr, int len)
{
if (bufptr && len)
{
uint16_t buf_len = strlen(http_resp);
ESP_LOGI(TAG, "output_buffer => : %s\n",output_buffer);
ESP_LOGI(TAG, "http_resp => : %s\n",http_resp);
ESP_LOGI(TAG, " buf len %d", buf_len);
if (http_resp && buf_len > 0) {
strcpy(bufptr, http_resp);
// *bufptr = output_buffer;
len = buf_len;
ESP_LOGI(TAG, "BUF: %s\nLEN : %d", bufptr, len);
return ESP_OK;
}
bufptr = 0;
len = 0;
}
return ESP_FAIL;
}
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
esp_err_t http_client_do_get_request(get_req_type_t target_url, char* device_imei, char *response_buf)
{
@ -461,8 +589,12 @@ esp_err_t http_client_do_get_request(get_req_type_t target_url, char* device_ime
if(selected_server == SERVER_TWO)
{
sprintf(server_url, MCU_GET_CONFIG_FLAGS_URL2, device_imei);
<<<<<<< HEAD
retval = http_client_get((const char*)MCU_GET_CONFIG_FLAGS_URL2,response_buf);
=======
http_client_get((const char*)server_url,response_buf);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
if(retval != WIFI_OK)
{
selected_server = SERVER_THREE;
@ -474,8 +606,12 @@ esp_err_t http_client_do_get_request(get_req_type_t target_url, char* device_ime
if(selected_server == SERVER_THREE)
{
sprintf(server_url, MCU_GET_CONFIG_FLAGS_URL3, device_imei);
<<<<<<< HEAD
// http_client_get((const char*)server_url,response_buf);
retval = http_client_get((const char*)MCU_GET_CONFIG_FLAGS_URL3,response_buf);
=======
http_client_get((const char*)server_url,response_buf);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
if(retval != WIFI_OK)
{
selected_server = SERVER_ONE;
@ -563,6 +699,10 @@ esp_err_t http_client_do_get_request(get_req_type_t target_url, char* device_ime
break;
default:
<<<<<<< HEAD
=======
break;
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
}
return retval;

View File

@ -38,6 +38,11 @@ esp_err_t http_client_post(char *url, char *params, char *response);
void http_client_post_stop(void);
esp_err_t http_client_get(const char *url1, char *response);
<<<<<<< HEAD
=======
esp_err_t http_get_response_buf(char* bufptr, int len);
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
esp_err_t http_client_do_get_request(get_req_type_t target_url, char* device_imei, char *response_buf);
esp_err_t http_client_do_post_request(char *post_params, char *response_buf);
void wifi_set_post_str_type(deviceToServer_msgType_t str);;

View File

@ -20,6 +20,10 @@
#include "main.h"
#include "nvm.h"
#include "data_processing.h"
<<<<<<< HEAD
=======
#include "port.h"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#if (WIFI_NEEDED == 1)
static const char* TAG = "WIFI";
@ -35,7 +39,11 @@ static const char* TAG = "WIFI";
#define WIFI_AP_DISCONNECTED_BIT BIT4
#define WIFI_DISCONNECTED_BIT BIT5
<<<<<<< HEAD
#define WIFI_AP_SSID "Sensor Setup "
=======
#define WIFI_AP_SSID "Azuma Wifi Setup "
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
static char g_pWifiSsidA[MAX_SSID_LEN];
static char g_pWifiPasswordA[MAX_PASSPHRASE_LEN];
@ -66,6 +74,14 @@ static bool set_wifi_bits;
static int s_retry_num = 0;
static bool turnOffStation;
<<<<<<< HEAD
=======
int qmtopen_retries = 0;
bool qmtopen_fail = false;
bool qmtclose_fail = false;
bool cops_fail = false;
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
/* This structure holds the WIFI configurations (modes, ip's, ssid&pwd ...etc )*/
esp_netif_t *sta_netif;
esp_netif_t *ap_netif;
@ -302,6 +318,10 @@ esp_err_t Connect_wifi_sta(uint8_t mode)
void Wifi_Init_SoftAp(void)
{
<<<<<<< HEAD
=======
port_red_led_on();
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
char ap_ssid[32];
char mac_str[15];
uint8_t mac_base[6] = {0};

View File

@ -21,6 +21,13 @@ typedef enum
#define STORING_TO_NVM
<<<<<<< HEAD
=======
#define WIFI_STATUS_OK 0
#define WIFI_STATUS_ERROR -1
#define WIFI_STATUS_TIMEOUT -2
#define WIFI_STATUS_MQTT_PUB_OVERFLOW -10
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#define WIFI_CLIENT_MODE 1
#define WIFI_AP_MODE 2

View File

@ -23,6 +23,10 @@
#include "main.h"
#include "comms.h"
#include "wifi_OTA.h"
<<<<<<< HEAD
=======
#include "comms.h"
>>>>>>> 4ea13f8 (Added azuma wifi switch backup code)
#define BUFFSIZE 1024
#define HASH_LEN 32 /* SHA-256 digest length */

File diff suppressed because one or more lines are too long