new meter

This commit is contained in:
2025-06-14 10:27:29 +01:00
parent 4892718736
commit 6f95c7ba59
228 changed files with 3178 additions and 3115 deletions

View File

@@ -0,0 +1,143 @@
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_mac.h"
#include "nvs.h"
#include "mdns.h"
#include "wifi.h"
#include "nvs_flash.h"
#include <string.h>
#define WIFI_STORAGE_NAMESPACE "wifi_config"
#define TAG "wifi"
#define AP_SSID "plx-%02x%02x%02x"
#define MDNS_HOSTNAME "plx%02x"
#define NVS_NAMESPACE "wifi"
static nvs_handle_t nvs;
static esp_netif_t *ap_netif;
EventGroupHandle_t wifi_event_group;
//
// Event handler para modo AP
//
static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT) {
switch (event_id) {
case WIFI_EVENT_AP_STACONNECTED: {
wifi_event_ap_staconnected_t *event = event_data;
ESP_LOGI(TAG, "STA " MACSTR " conectou, AID=%d", MAC2STR(event->mac), event->aid);
xEventGroupSetBits(wifi_event_group, WIFI_AP_CONNECTED_BIT);
break;
}
case WIFI_EVENT_AP_STADISCONNECTED: {
wifi_event_ap_stadisconnected_t *event = event_data;
ESP_LOGI(TAG, "STA " MACSTR " desconectou, AID=%d", MAC2STR(event->mac), event->aid);
xEventGroupClearBits(wifi_event_group, WIFI_AP_CONNECTED_BIT);
break;
}
}
}
}
//
// Iniciar o AP com SSID baseado no MAC
//
void wifi_ap_start(void)
{
ESP_LOGI(TAG, "Iniciando AP");
ESP_ERROR_CHECK(esp_wifi_stop());
wifi_config_t ap_config = {
.ap = {
.ssid = "",
.ssid_len = 0,
.channel = 1,
.password = "",
.max_connection = 4,
.authmode = WIFI_AUTH_OPEN
}
};
uint8_t mac[6];
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP));
snprintf((char *)ap_config.ap.ssid, sizeof(ap_config.ap.ssid), AP_SSID, mac[3], mac[4], mac[5]);
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
ESP_ERROR_CHECK(esp_wifi_start());
xEventGroupSetBits(wifi_event_group, WIFI_AP_MODE_BIT);
}
//
// Inicializar Wi-Fi em modo AP
//
void wifi_ini(void)
{
ESP_LOGI(TAG, "Inicializando Wi-Fi (modo AP)");
ESP_ERROR_CHECK(nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs));
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
/*
if (!esp_event_loop_is_running()) {
ESP_ERROR_CHECK(esp_event_loop_create_default());
}*/
ap_netif = esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
uint8_t mac[6];
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP));
char hostname[16];
snprintf(hostname, sizeof(hostname), MDNS_HOSTNAME, mac[5]);
ESP_ERROR_CHECK(mdns_init());
ESP_ERROR_CHECK(mdns_hostname_set(hostname));
ESP_ERROR_CHECK(mdns_instance_name_set("EVSE Controller"));
wifi_ap_start();
}
esp_netif_t *wifi_get_ap_netif(void)
{
return ap_netif;
}
esp_err_t wifi_set_config(bool enabled, const char *ssid, const char *password) {
return ESP_OK;
}
void wifi_get_ssid(char *value) {
// Your implementation here
}
void wifi_get_password(char *value) {
// Your implementation here
}
bool wifi_get_enabled(void)
{
return true;
}