From a638e0293c68d7a6bc149dd3d4db68ef0b13391d Mon Sep 17 00:00:00 2001 From: Joseph Ferano Date: Sun, 17 May 2026 07:31:26 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9F=20pmme-device:=20backporting=20-?= =?UTF-8?q?=20add=20wifi=20provision=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmme-device/c-version/main/pmme.c | 63 +++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/pmme-device/c-version/main/pmme.c b/pmme-device/c-version/main/pmme.c index fc3ce48..7acdc68 100644 --- a/pmme-device/c-version/main/pmme.c +++ b/pmme-device/c-version/main/pmme.c @@ -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 = "Hello world!"; + 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); }