AP control based on number of connected clients

This commit is contained in:
Bogdan Pilyugin 2023-07-03 09:01:22 +02:00
parent 1bf82fd734
commit 32c90af2c1
5 changed files with 92 additions and 3 deletions

View File

@ -35,5 +35,6 @@ void bin_to_hex_str(const uint8_t *buf, int len, char *hex);
void UnencodeURL(char* URL); void UnencodeURL(char* URL);
esp_err_t SHA256Hash(unsigned char *data, int datalen, esp_err_t SHA256Hash(unsigned char *data, int datalen,
unsigned char *res); unsigned char *res);
void vTaskGetRunTimeStatsCustom( char *pcWriteBuffer );
#endif /* MAIN_INCLUDE_HELPERS_H_ */ #endif /* MAIN_INCLUDE_HELPERS_H_ */

View File

@ -65,6 +65,7 @@ void WiFiStop();
void WiFiStopAP(); void WiFiStopAP();
void WiFiStartAP(); void WiFiStartAP();
void WiFiStartAPTemp(int seconds); void WiFiStartAPTemp(int seconds);
int GetAPClientsNumber();
void EthStart(void); void EthStart(void);
void WiFiTransportTask(void *prm); void WiFiTransportTask(void *prm);

View File

@ -544,7 +544,7 @@ static HTTP_IO_RESULT HTTPPostSystemSettings(httpd_req_t *req, char *PostData)
{ {
if (!strcmp(tmp, (const char*) "prs")) if (!strcmp(tmp, (const char*) "prs"))
{ {
vTaskGetRunTimeStats(rtstat); vTaskGetRunTimeStatsCustom(rtstat);
httpd_resp_sendstr(req, rtstat); httpd_resp_sendstr(req, rtstat);
return HTTP_IO_DONE_API; return HTTP_IO_DONE_API;
} }

View File

@ -22,6 +22,8 @@
#include "esp_mac.h" #include "esp_mac.h"
#include "esp_rom_crc.h" #include "esp_rom_crc.h"
#include "mbedtls/md.h" #include "mbedtls/md.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
uint32_t crc32(uint32_t crc, uint8_t const *buf, uint32_t len) uint32_t crc32(uint32_t crc, uint8_t const *buf, uint32_t len)
{ {
@ -189,4 +191,69 @@ esp_err_t SHA256Hash(unsigned char *data, int datalen,
return ESP_OK; return ESP_OK;
} }
void vTaskGetRunTimeStatsCustom( char *pcWriteBuffer )
{
TaskStatus_t *pxTaskStatusArray;
volatile UBaseType_t uxArraySize, x;
unsigned long ulTotalRunTime, ulStatsAsPercentage;
/* Make sure the write buffer does not contain a string. */
*pcWriteBuffer = 0x00;
/* Take a snapshot of the number of tasks in case it changes while this
function is executing. */
uxArraySize = uxTaskGetNumberOfTasks();
/* Allocate a TaskStatus_t structure for each task. An array could be
allocated statically at compile time. */
pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) );
if( pxTaskStatusArray != NULL )
{
/* Generate raw status information about each task. */
uxArraySize = uxTaskGetSystemState( pxTaskStatusArray,
uxArraySize,
&ulTotalRunTime );
/* For percentage calculations. */
ulTotalRunTime /= 100UL;
/* Avoid divide by zero errors. */
if( ulTotalRunTime > 0 )
{
/* For each populated position in the pxTaskStatusArray array,
format the raw data as human readable ASCII data. */
for( x = 0; x < uxArraySize; x++ )
{
/* What percentage of the total run time has the task used?
This will always be rounded down to the nearest integer.
ulTotalRunTimeDiv100 has already been divided by 100. */
ulStatsAsPercentage =
pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalRunTime;
if( ulStatsAsPercentage > 0UL )
{
sprintf( pcWriteBuffer, "%s----%llu----%lu%%----%d\r\n",
pxTaskStatusArray[ x ].pcTaskName,
(uint64_t)pxTaskStatusArray[ x ].ulRunTimeCounter,
ulStatsAsPercentage,
(int)pxTaskStatusArray[ x ].usStackHighWaterMark);
}
else
{
/* If the percentage is zero here then the task has
consumed less than 1% of the total run time. */
sprintf( pcWriteBuffer, "%s----%llu----<1%%----%d\r\n",
pxTaskStatusArray[ x ].pcTaskName,
(uint64_t)pxTaskStatusArray[ x ].ulRunTimeCounter,
(int)pxTaskStatusArray[ x ].usStackHighWaterMark);
}
pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
}
}
/* The array is no longer needed, free the memory it consumes. */
vPortFree( pxTaskStatusArray );
}
}

View File

@ -536,11 +536,16 @@ static void WiFiControlTask(void *arg)
if (TempAPCounter > 0) if (TempAPCounter > 0)
{ {
if (--TempAPCounter <= 0) if (--TempAPCounter <= 0)
{
if (GetAPClientsNumber() > 0)
TempAPCounter = WIFI_AP_ONBOOT_TIME;
else
{ {
WiFiStopAP(); WiFiStopAP();
ESP_LOGI(TAG, "WiFi AP stopped after temporarily activity"); ESP_LOGI(TAG, "WiFi AP stopped after temporarily activity");
} }
} }
}
} }
@ -610,3 +615,18 @@ void WiFiScan(void)
isScanExecuting = true; isScanExecuting = true;
xTaskCreate(wifi_scan, "ScanWiFiTask", 1024 * 4, (void*) 0, 3, NULL); xTaskCreate(wifi_scan, "ScanWiFiTask", 1024 * 4, (void*) 0, 3, NULL);
} }
int GetAPClientsNumber()
{
wifi_sta_list_t clients;
wifi_mode_t mode;
esp_wifi_get_mode(&mode);
if (mode == WIFI_MODE_NULL || mode == WIFI_MODE_STA)
return -1;
if (esp_wifi_ap_get_sta_list(&clients) == ESP_OK)
{
return clients.num;
}
return -1;
}