155 lines
5.0 KiB
C
155 lines
5.0 KiB
C
#include "http_metrics.h"
|
|
|
|
static EventGroupHandle_t s_wifi_event_group;
|
|
|
|
static const char *TAG = "esp32-env-exp-http_metrics";
|
|
|
|
char caHtmlResponse[HTML_RESPONSE_SIZE];
|
|
SemaphoreHandle_t xMutexAccessMetricResponse = NULL;
|
|
|
|
void vSetMetrics(sMetric* paMetrics, uint16_t u16Size) {
|
|
|
|
if( xSemaphoreTake( xMutexAccessMetricResponse, ( TickType_t ) 100 ) == pdTRUE )
|
|
{
|
|
memset(caHtmlResponse,0,strlen(caHtmlResponse));
|
|
for(uint16_t u16Index = 0U; u16Index < u16Size; u16Index++) {
|
|
char caValueBuffer[64];
|
|
sprintf(caValueBuffer, " %f", paMetrics[u16Index].fMetricValue);
|
|
//printf("%s\n", caValueBuffer);
|
|
strcat(caHtmlResponse, paMetrics[u16Index].caMetricName);
|
|
strcat(caHtmlResponse, caValueBuffer);
|
|
strcat(caHtmlResponse, "\n");
|
|
}
|
|
xSemaphoreGive( xMutexAccessMetricResponse );
|
|
} else {
|
|
ESP_LOGI(TAG, "[SET] Unable to obtain mutex for metric response");
|
|
}
|
|
}
|
|
|
|
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)
|
|
{
|
|
esp_wifi_connect();
|
|
}
|
|
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
|
|
{
|
|
esp_wifi_connect();
|
|
ESP_LOGI(TAG, "Retry to connect to the AP");
|
|
}
|
|
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
|
|
{
|
|
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
|
|
ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
void connect_wifi(void)
|
|
{
|
|
s_wifi_event_group = xEventGroupCreate();
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
esp_netif_t *my_sta = esp_netif_create_default_wifi_sta();
|
|
esp_netif_dhcpc_stop(my_sta);
|
|
esp_netif_ip_info_t ip_info;
|
|
ip_info.ip.addr = ipaddr_addr(CONFIG_STATIC_IP_ADDR);
|
|
ip_info.gw.addr = ipaddr_addr(CONFIG_STATIC_GATEWAY_IP_ADDR);
|
|
ip_info.netmask.addr = ipaddr_addr(CONFIG_STATIC_IP_NETMASK);
|
|
esp_netif_set_ip_info(my_sta, &ip_info);
|
|
|
|
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));
|
|
|
|
wifi_config_t wifi_config = {
|
|
.sta = {
|
|
.ssid = CONFIG_SSID,
|
|
.password = CONFIG_WIFI_PASSWORD,
|
|
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
|
},
|
|
};
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
|
|
|
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
|
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
|
pdFALSE,
|
|
pdFALSE,
|
|
portMAX_DELAY);
|
|
|
|
if (bits & WIFI_CONNECTED_BIT)
|
|
{
|
|
ESP_LOGI(TAG, "Connected to ap SSID:%s", CONFIG_SSID);
|
|
}
|
|
else if (bits & WIFI_FAIL_BIT)
|
|
{
|
|
ESP_LOGI(TAG, "Failed to connect to SSID:%s", CONFIG_SSID);
|
|
}
|
|
else
|
|
{
|
|
ESP_LOGE(TAG, "Unexpected event");
|
|
}
|
|
vEventGroupDelete(s_wifi_event_group);
|
|
}
|
|
|
|
esp_err_t get_metrics_handler(httpd_req_t *req)
|
|
{
|
|
if( xSemaphoreTake( xMutexAccessMetricResponse, ( TickType_t ) 100 ) == pdTRUE )
|
|
{
|
|
esp_err_t err = httpd_resp_send(req, caHtmlResponse, HTTPD_RESP_USE_STRLEN);
|
|
xSemaphoreGive( xMutexAccessMetricResponse );
|
|
return err;
|
|
} else
|
|
{
|
|
ESP_LOGI(TAG, "[GET] Unable to obtain mutex for metric response");
|
|
return httpd_resp_send(req, 0, 0);
|
|
}
|
|
}
|
|
|
|
httpd_uri_t uri_get = {
|
|
.uri = "/metrics",
|
|
.method = HTTP_GET,
|
|
.handler = get_metrics_handler,
|
|
.user_ctx = NULL
|
|
};
|
|
|
|
httpd_handle_t setup_server(void)
|
|
{
|
|
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
|
config.server_port = 9100;
|
|
httpd_handle_t server = NULL;
|
|
|
|
|
|
xMutexAccessMetricResponse = xSemaphoreCreateBinary();
|
|
if(xMutexAccessMetricResponse == NULL) {
|
|
ESP_LOGE(TAG, "Unable to create mutex for metric response");
|
|
vTaskDelay(pdMS_TO_TICKS(300*60)); //wait 5min before restart
|
|
esp_restart();
|
|
}
|
|
xSemaphoreGive( xMutexAccessMetricResponse );
|
|
|
|
if (httpd_start(&server, &config) == ESP_OK)
|
|
{
|
|
httpd_register_uri_handler(server, &uri_get);
|
|
}
|
|
|
|
return server;
|
|
}
|