1
0
mirror of https://github.com/peterantypas/maiana.git synced 2025-06-21 09:00:34 -07:00

Initial web server

This commit is contained in:
Peter Antypas 2022-12-03 19:13:16 -08:00
parent 680dcd0fcf
commit a0cc8084c9
13 changed files with 340 additions and 2 deletions

View File

@ -5,6 +5,6 @@
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
set(EXTRA_COMPONENT_DIRS bsp)
set(EXTRA_COMPONENT_DIRS bsp core html)
list (APPEND EXTRA_COMPONENT_DIRS components)
project(MaianaWiFiAdapter)

View File

@ -0,0 +1,13 @@
set (SOURCES
wifi.c
configuration.c
http_server.c
http_handlers.c
)
idf_component_register(SRCS ${SOURCES}
INCLUDE_DIRS .
REQUIRES
nvs_flash
esp_http_server
)

View File

@ -0,0 +1,68 @@
#include "configuration.h"
#include "nvs_flash.h"
#include "esp_mac.h"
static char __ssid[48] = {0};
static char __ap_mac[32] = {0};
static char __st_mac[32] = {0};
const char *config_ap_mac_address()
{
if ( __ap_mac[0] == 0 )
{
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP);
sprintf(__ap_mac, "%.2X%.2X%.2X%.2X%.2X%.2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
return __ap_mac;
}
const char *config_st_mac_address()
{
if ( __st_mac[0] == 0 )
{
uint8_t mac[6];
esp_read_mac(mac, ESP_MAC_WIFI_STA);
sprintf(__st_mac, "%.2X%.2X%.2X%.2X%.2X%.2X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
return __st_mac;
}
wifi_operation_mode_t config_get_wifi_operation_mode()
{
return WIFI_OPEN_AP;
}
const char *config_get_ssid()
{
switch(config_get_wifi_operation_mode())
{
case WIFI_OPEN_AP:
case WIFI_SECURE_AP:
if ( __ssid[0] == 0 )
{
const char *mac = config_ap_mac_address();
sprintf(__ssid, "MAIANA_%s", mac+6);
}
break;
case WIFI_STATION:
if ( __ssid[0] == 0 )
{
const char *mac = config_st_mac_address();
sprintf(__ssid, "MAIANA_%s", mac+6);
}
break;
default:
return NULL;
}
return __ssid;
}
const char *config_get_password()
{
return NULL;
}

View File

@ -0,0 +1,24 @@
#ifndef __CONFIGURATION_H__
#define __CONFIGURATION_H__
#include <string.h>
typedef enum
{
WIFI_OPEN_AP,
WIFI_SECURE_AP,
WIFI_STATION
} wifi_operation_mode_t;
wifi_operation_mode_t config_get_wifi_operation_mode();
const char *config_get_ssid();
const char *config_get_password();
const char *config_ap_mac_address();
const char *config_st_mac_address();
#endif

View File

@ -0,0 +1,62 @@
#include <stdlib.h>
#include <esp_http_server.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <sys/param.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "configuration.h"
extern const char index_html[] asm("index_html");
static const char *TAG = "httpd";
void send_std_headers(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Connection", "Close");
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
}
esp_err_t http_root_handler(httpd_req_t *req)
{
ESP_LOGI(TAG, "Request: %s", req->uri);
if ( strcmp(req->uri, "/") == 0 || strcmp(req->uri, "/index.html") == 0 )
{
send_std_headers(req);
esp_err_t err = httpd_resp_send(req, index_html, HTTPD_RESP_USE_STRLEN);
ESP_LOGI(TAG, "GET %s %d %d", req->uri, strlen(index_html), err);
}
else if ( strcmp(req->uri, "/favicon.ico") == 0 )
{
httpd_resp_send_404(req);
}
return ESP_OK;
}
/** URI definitions */
httpd_uri_t uri_root1 =
{
.uri = "*",
.method = HTTP_GET,
.handler = http_root_handler,
.user_ctx = NULL
};
httpd_uri_t uri_root2 =
{
.uri = "/index.html",
.method = HTTP_GET,
.handler = http_root_handler,
.user_ctx = NULL
};
/** Registration */
void register_http_handlers(httpd_handle_t handle)
{
httpd_register_uri_handler(handle, &uri_root1);
//httpd_register_uri_handler(handle, &uri_root2);
}

View File

@ -0,0 +1,47 @@
#include "http_server.h"
#include <stdlib.h>
#include <esp_http_server.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <sys/param.h>
#include "configuration.h"
static httpd_handle_t __handle = NULL;
static const char *TAG = "httpd";
extern void register_http_handlers(httpd_handle_t);
void start_httpd()
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.uri_match_fn = httpd_uri_match_wildcard;
config.max_uri_handlers = 20;
//config.max_open_sockets = 1;
if (httpd_start(&__handle, &config) == ESP_OK)
{
register_http_handlers(__handle);
ESP_LOGI(TAG, "Started HTTPD");
}
}
void stop_httpd()
{
if (__handle)
{
httpd_stop(__handle);
__handle = NULL;
ESP_LOGI(TAG, "Stopped HTTPD");
}
}
bool is_httpd_running()
{
return __handle != NULL;
}

View File

@ -0,0 +1,10 @@
#ifndef __HTTP_SERVER_H__
#define __HTTP_SERVER_H__
#include <stdbool.h>
void start_httpd();
void stop_httpd();
bool is_httpd_running();
#endif

View File

@ -0,0 +1,76 @@
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_mac.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "wifi.h"
#include "configuration.h"
static const char *TAG = "wifi";
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
}
void wifi_init_softap(void)
{
esp_netif_init();
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_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
wifi_operation_mode_t mode = config_get_wifi_operation_mode();
wifi_config_t wifi_config = {0};
const char *ssid = config_get_ssid();
switch(mode)
{
case WIFI_OPEN_AP:
{
memcpy(wifi_config.ap.ssid, ssid, strlen(ssid));
wifi_config.ap.ssid_len = strlen(ssid);
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
wifi_config.ap.max_connection = 8;
esp_wifi_set_mode(WIFI_MODE_AP);
esp_wifi_set_config(WIFI_IF_AP, &wifi_config);
}
break;
case WIFI_SECURE_AP:
{
}
break;
case WIFI_STATION:
{
}
break;
}
esp_wifi_start();
}
void wifi_start()
{
wifi_init_softap();
}

View File

@ -0,0 +1,8 @@
#ifndef __WIFI_H__
#define __WIFI_H__
void wifi_start();
#endif

View File

@ -0,0 +1,3 @@
idf_component_register(EMBED_TXTFILES
"index.html"
)

View File

@ -0,0 +1,8 @@
<html>
<head>
<title>MAIANA Configuration</title>
</head>
<body>
<h1>This is the content</h1>
</body>
</html>

View File

@ -2,5 +2,8 @@ idf_component_register(SRCS "MaianaWiFiAdapter.c"
INCLUDE_DIRS
.
../bsp
REQUIRES app_update
../core
REQUIRES
app_update
nvs_flash
)

View File

@ -3,6 +3,9 @@
#include "bsp.h"
#include <string.h>
#include "esp_log.h"
#include "nvs_flash.h"
#include "wifi.h"
#include "http_server.h"
void uart_rx_cb(char *s)
{
@ -27,9 +30,22 @@ void btn_press_handler(void *args, esp_event_base_t base, int32_t id, void *data
void app_main(void)
{
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());
ret = nvs_flash_init();
}
esp_event_loop_create_default();
bsp_set_uart_rx_cb(uart_rx_cb);
bsp_hw_init();
esp_event_handler_register(BSP_EVENT, BSP_TX_BTN_EVENT, btn_press_handler, NULL);
wifi_start();
//sleep(10);
start_httpd();
vTaskDelete(NULL);
}