wifi reconnect policy improved, default uart settings restored
This commit is contained in:
parent
2547be508c
commit
e8e8876686
34
src/MQTT.c
34
src/MQTT.c
|
|
@ -161,42 +161,12 @@ esp_err_t ExternalServiceMQTTSend(char *servname, char *data, int len, int idx)
|
|||
}
|
||||
|
||||
#define MAX_ERROR_JSON 256
|
||||
/*
|
||||
static char resp[256];
|
||||
static char JSONMess[1024];
|
||||
mqtt_app_err_t PublicTestMQTT(int idx)
|
||||
{
|
||||
char tmp[10];
|
||||
char resp[256];
|
||||
char JSONMess[512];
|
||||
struct jWriteControl jwc;
|
||||
jwOpen(&jwc, JSONMess, MAX_ERROR_JSON, JW_OBJECT, JW_PRETTY);
|
||||
time_t now;
|
||||
time(&now);
|
||||
jwObj_int(&jwc, "time", (unsigned int) now);
|
||||
jwObj_string(&jwc, "event", "MQTT_TEST_MESSAGE)");
|
||||
strcpy(resp, "mqtt://");
|
||||
strcat(resp, GetSysConf()->mqttStation[idx].ServerAddr);
|
||||
itoa(GetSysConf()->mqttStation[idx].ServerPort, tmp, 10);
|
||||
strcat(resp, ":");
|
||||
strcat(resp, tmp);
|
||||
jwObj_string(&jwc, "url", resp);
|
||||
ComposeTopic(resp, idx, SERVICE_NAME, UPLINK_SUBTOPIC);
|
||||
jwObj_string(&jwc, "tx_topic", resp);
|
||||
ComposeTopic(resp, idx, SERVICE_NAME, DOWNLINK_SUBTOPIC);
|
||||
jwObj_string(&jwc, "rx_topic", resp);
|
||||
jwEnd(&jwc);
|
||||
jwClose(&jwc);
|
||||
mqtt_app_err_t merr = API_OK;
|
||||
if (SysServiceMQTTSend(JSONMess, strlen(JSONMess), idx) != ESP_OK)
|
||||
merr = API_INTERNAL_ERR;
|
||||
return merr;
|
||||
}
|
||||
*/
|
||||
|
||||
mqtt_app_err_t PublicTestMQTT(int idx)
|
||||
{
|
||||
char tmp[10];
|
||||
char resp[256];
|
||||
char JSONMess[1024];
|
||||
struct jWriteControl jwc;
|
||||
jwOpen(&jwc, JSONMess, 1024 - 64, JW_OBJECT, JW_COMPACT);
|
||||
jwObj_object(&jwc, "data");
|
||||
|
|
|
|||
|
|
@ -231,8 +231,8 @@ void InitSerialPort(void)
|
|||
{
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = GetSysConf()->serialSettings.BaudRate,
|
||||
.data_bits = UART_DATA_7_BITS,
|
||||
.parity = UART_PARITY_EVEN,
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
.source_clk = UART_SCLK_APB,
|
||||
|
|
|
|||
|
|
@ -87,9 +87,7 @@ void resonnectWithDelay(void *agr)
|
|||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id,
|
||||
void *event_data)
|
||||
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||
{
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
|
||||
{
|
||||
|
|
@ -116,7 +114,6 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
|||
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_BEACON_TIMEOUT)
|
||||
{
|
||||
ESP_LOGW(TAG, "STA beacon timeout");
|
||||
|
||||
}
|
||||
|
||||
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
||||
|
|
@ -165,17 +162,13 @@ static void event_handler(void *arg, esp_event_base_t event_base,
|
|||
static void wifi_init_softap(void *pvParameter)
|
||||
{
|
||||
char if_key_str[24];
|
||||
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
|
||||
;
|
||||
esp_netif_inherent_config_t esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
|
||||
|
||||
strcpy(if_key_str, "WIFI_AP_USER");
|
||||
esp_netif_conf.if_key = if_key_str;
|
||||
esp_netif_conf.route_prio = AP_PRIO;
|
||||
|
||||
esp_netif_config_t cfg_netif = {
|
||||
.base = &esp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
|
||||
};
|
||||
esp_netif_config_t cfg_netif = { .base = &esp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP };
|
||||
|
||||
ap_netif = esp_netif_new(&cfg_netif);
|
||||
assert(ap_netif);
|
||||
|
|
@ -196,15 +189,10 @@ static void wifi_init_softap(void *pvParameter)
|
|||
esp_netif_attach_wifi_ap(ap_netif);
|
||||
esp_wifi_set_default_wifi_ap_handlers();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
NULL));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, NULL));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.ap = {
|
||||
|
|
@ -220,8 +208,7 @@ static void wifi_init_softap(void *pvParameter)
|
|||
}
|
||||
|
||||
memcpy(wifi_config.ap.ssid, GetSysConf()->wifiSettings.ApSSID, strlen(GetSysConf()->wifiSettings.ApSSID));
|
||||
memcpy(wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
memcpy(wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey, strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
wifi_config.ap.ssid_len = strlen(GetSysConf()->wifiSettings.ApSSID);
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||||
|
|
@ -244,10 +231,7 @@ static void wifi_init_sta(void *pvParameter)
|
|||
strcpy(if_key_str, "WIFI_STA_USER");
|
||||
esp_netif_conf.if_key = if_key_str;
|
||||
esp_netif_conf.route_prio = STA_PRIO;
|
||||
esp_netif_config_t cfg_netif = {
|
||||
.base = &esp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
|
||||
};
|
||||
esp_netif_config_t cfg_netif = { .base = &esp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
|
||||
sta_netif = esp_netif_new(&cfg_netif);
|
||||
assert(sta_netif);
|
||||
|
||||
|
|
@ -273,22 +257,13 @@ static void wifi_init_sta(void *pvParameter)
|
|||
esp_netif_attach_wifi_station(sta_netif);
|
||||
esp_wifi_set_default_wifi_sta_handlers();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
|
|
@ -304,8 +279,7 @@ static void wifi_init_sta(void *pvParameter)
|
|||
},
|
||||
};
|
||||
memcpy(wifi_config.sta.ssid, GetSysConf()->wifiSettings.InfSSID, strlen(GetSysConf()->wifiSettings.InfSSID));
|
||||
memcpy(wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
memcpy(wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey, strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
|
||||
esp_netif_set_hostname(sta_netif, GetSysConf()->NetBIOSName);
|
||||
|
||||
|
|
@ -325,17 +299,13 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
{
|
||||
// BEGIN AP MODE IF
|
||||
char ap_if_key_str[24];
|
||||
esp_netif_inherent_config_t ap_esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP()
|
||||
;
|
||||
esp_netif_inherent_config_t ap_esp_netif_conf = ESP_NETIF_INHERENT_DEFAULT_WIFI_AP();
|
||||
|
||||
strcpy(ap_if_key_str, "WIFI_AP_USER");
|
||||
ap_esp_netif_conf.if_key = ap_if_key_str;
|
||||
ap_esp_netif_conf.route_prio = AP_PRIO;
|
||||
|
||||
esp_netif_config_t cfg_netif = {
|
||||
.base = &ap_esp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP
|
||||
};
|
||||
esp_netif_config_t cfg_netif = { .base = &ap_esp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_AP };
|
||||
|
||||
ap_netif = esp_netif_new(&cfg_netif);
|
||||
assert(ap_netif);
|
||||
|
|
@ -347,10 +317,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
strcpy(sta_if_key_str, "WIFI_STA_USER");
|
||||
staesp_netif_conf.if_key = sta_if_key_str;
|
||||
staesp_netif_conf.route_prio = STA_PRIO;
|
||||
esp_netif_config_t sta_cfg_netif = {
|
||||
.base = &staesp_netif_conf,
|
||||
.stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA
|
||||
};
|
||||
esp_netif_config_t sta_cfg_netif = { .base = &staesp_netif_conf, .stack = ESP_NETIF_NETSTACK_DEFAULT_WIFI_STA };
|
||||
sta_netif = esp_netif_new(&sta_cfg_netif);
|
||||
assert(sta_netif);
|
||||
// END STA MODE IF
|
||||
|
|
@ -372,8 +339,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
esp_netif_attach_wifi_ap(ap_netif);
|
||||
esp_wifi_set_default_wifi_ap_handlers();
|
||||
|
||||
wifi_init_config_t ap_cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
wifi_init_config_t ap_cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&ap_cfg));
|
||||
wifi_config_t ap_wifi_config = {
|
||||
|
|
@ -395,8 +361,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
}
|
||||
|
||||
memcpy(ap_wifi_config.ap.ssid, GetSysConf()->wifiSettings.ApSSID, strlen(GetSysConf()->wifiSettings.ApSSID));
|
||||
memcpy(ap_wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
memcpy(ap_wifi_config.ap.password, GetSysConf()->wifiSettings.ApSecurityKey, strlen(GetSysConf()->wifiSettings.ApSecurityKey));
|
||||
ap_wifi_config.ap.ssid_len = strlen(GetSysConf()->wifiSettings.ApSSID);
|
||||
// END AP MODE CONFIGURATION
|
||||
|
||||
|
|
@ -423,22 +388,13 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
esp_netif_attach_wifi_station(sta_netif);
|
||||
esp_wifi_set_default_wifi_sta_handlers();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT()
|
||||
;
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip));
|
||||
|
||||
wifi_config_t sta_wifi_config = {
|
||||
.sta = {
|
||||
|
|
@ -456,8 +412,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
},
|
||||
};
|
||||
memcpy(sta_wifi_config.sta.ssid, GetSysConf()->wifiSettings.InfSSID, strlen(GetSysConf()->wifiSettings.InfSSID));
|
||||
memcpy(sta_wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey,
|
||||
strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
memcpy(sta_wifi_config.sta.password, GetSysConf()->wifiSettings.InfSecurityKey, strlen(GetSysConf()->wifiSettings.InfSecurityKey));
|
||||
// END STA MODE CONFIGURATION
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_set_hostname(sta_netif, "test_TEST"));
|
||||
|
|
@ -479,8 +434,7 @@ static void wifi_init_apsta(void *pvParameter)
|
|||
|
||||
wifi_country_t CC;
|
||||
esp_wifi_get_country(&CC);
|
||||
ESP_LOGI(TAG, "Country code %.*s, start_ch=%d, total_ch=%d, max power %d", 3, CC.cc, CC.schan, CC.nchan,
|
||||
CC.max_tx_power);
|
||||
ESP_LOGI(TAG, "Country code %.*s, start_ch=%d, total_ch=%d, max power %d", 3, CC.cc, CC.schan, CC.nchan, CC.max_tx_power);
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_softap_sta finished");
|
||||
vTaskDelete(NULL);
|
||||
|
|
@ -496,16 +450,17 @@ void WiFiConnect(void)
|
|||
esp_wifi_connect();
|
||||
}
|
||||
|
||||
#define RECONNECT_INTERVAL_AP 60
|
||||
#define RECONNECT_INTERVAL_AP 30
|
||||
#define RECONNECT_INTERVAL_STA 30
|
||||
#define RECONNECT_FAST_ATTEMPTS 5
|
||||
#define WAITIP_INTERVAL 10
|
||||
|
||||
static void WiFiControlTask(void *arg)
|
||||
{
|
||||
// WiFi init and start block
|
||||
static int attempts = 0;
|
||||
static int reconnect_counter;
|
||||
reconnect_counter =
|
||||
(GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA) ? RECONNECT_INTERVAL_STA : RECONNECT_INTERVAL_AP;
|
||||
reconnect_counter = 5;
|
||||
static int waitip_counter = WAITIP_INTERVAL;
|
||||
// s_wifi_event_group = xEventGroupCreate();
|
||||
switch (GetSysConf()->wifiSettings.WiFiMode)
|
||||
|
|
@ -528,6 +483,7 @@ static void WiFiControlTask(void *arg)
|
|||
vTaskDelay(pdMS_TO_TICKS(1000));
|
||||
if (isWiFiConnected)
|
||||
{
|
||||
attempts = 0;
|
||||
reconnect_counter = RECONNECT_INTERVAL_STA;
|
||||
if (!isWiFiGotIp)
|
||||
{
|
||||
|
|
@ -540,16 +496,19 @@ static void WiFiControlTask(void *arg)
|
|||
}
|
||||
}
|
||||
if (isWiFiFail)
|
||||
|
||||
{
|
||||
if (--reconnect_counter <= 0 && !isScanExecuting)
|
||||
{
|
||||
ESP_LOGI(TAG, "WiFi STA started, reconnecting to AP...");
|
||||
esp_wifi_connect();
|
||||
reconnect_counter =
|
||||
(GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA) ?
|
||||
RECONNECT_INTERVAL_STA : RECONNECT_INTERVAL_AP;
|
||||
if (++attempts <= RECONNECT_FAST_ATTEMPTS)
|
||||
reconnect_counter = 5;
|
||||
else
|
||||
reconnect_counter = (GetSysConf()->wifiSettings.WiFiMode == WIFI_MODE_STA) ? RECONNECT_INTERVAL_STA : RECONNECT_INTERVAL_AP;
|
||||
}
|
||||
}
|
||||
|
||||
if (TempAPCounter > 0)
|
||||
{
|
||||
if (--TempAPCounter <= 0)
|
||||
|
|
@ -563,7 +522,6 @@ static void WiFiControlTask(void *arg)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (isWiFiConnected)
|
||||
|
|
@ -647,4 +605,3 @@ int GetAPClientsNumber()
|
|||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user