📟 pmme-device: backporting - add wifi provision code

This commit is contained in:
Joseph Ferano 2026-05-17 07:31:26 +07:00
parent 49c123809a
commit a638e0293c

View File

@ -15,6 +15,12 @@
#include "driver/gpio.h"
#include "ssd1306/ssd1306.h"
#include "esp_wifi.h"
#include "esp_http_server.h"
#include "esp_netif.h"
#include "nvs_flash.h"
#include "esp_event.h"
static const char *TAG = "PMME-Device";
#define I2C_HOST 0
@ -261,6 +267,62 @@ void button_led_task(void *pvParameters) {
}
}
static esp_err_t provision_handler(httpd_req_t *req) {
const char *response = "<html><body>Hello world!</body></html>";
httpd_resp_send(req, response, strlen(response));
return ESP_OK;
}
void wifi_task(void *pvParameters) {
// Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ESP_ERROR_CHECK(nvs_flash_init());
}
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = "PMME-Wifi",
.password = "88888888",
.ssid_len = strlen("PMME-Wifi"),
.channel = 1,
.authmode = WIFI_AUTH_WPA2_PSK,
.max_connection = 2,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "WiFi AP started: SSID=PMME-Wifi");
httpd_handle_t server = NULL;
httpd_config_t server_config = HTTPD_DEFAULT_CONFIG();
ESP_ERROR_CHECK(httpd_start(&server, &server_config));
httpd_uri_t provision_uri = {
.uri = "/api/provision",
.method = HTTP_GET,
.handler = provision_handler,
.user_ctx = NULL,
};
ESP_ERROR_CHECK(httpd_register_uri_handler(server, &provision_uri));
ESP_LOGI(TAG, "HTTP server started on port %d", server_config.server_port);
while (1) {
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void app_main(void) {
sensor_data_mutex = xSemaphoreCreateMutex();
if (sensor_data_mutex == NULL) {
@ -270,4 +332,5 @@ void app_main(void) {
xTaskCreate(pm_sensor_task, "pm_sensor_task", 4096, NULL, 5, NULL);
xTaskCreate(oled_display_task, "oled_display_task", 4096, NULL, 5, NULL);
xTaskCreate(button_led_task, "button_led_task", 2048, NULL, 5, NULL);
xTaskCreate(wifi_task, "wifi_task", 8192, NULL, 5, NULL);
}