Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement web server #61

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion code/rafting-button/components/espnow_utils/espnow_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define TRUE 1
#define FALSE 0

#define WIFI_PASS "eC9hvLZQdpTv8a"

static const char *TAG = "ESPNOW_UTILS";

void wifi_init(void)
Expand All @@ -27,7 +29,18 @@ void wifi_init(void)
// Set current WiFi power save type
ESP_ERROR_CHECK(esp_wifi_set_ps(WIFI_PS_NONE));

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA));

wifi_config_t wifi_config = {
.ap =
{
.password = WIFI_PASS,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.ssid_hidden = 1,
},
};
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));

ESP_ERROR_CHECK(esp_wifi_start());
// ESP_ERROR_CHECK(esp_wifi_set_channel())
}
Expand Down
2 changes: 1 addition & 1 deletion code/rafting-button/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
idf_component_register(SRCS "rafting-button.c"
INCLUDE_DIRS "."
REQUIRES espnow_utils peripheral)
REQUIRES espnow_utils peripheral esp_http_server)

# If some components are kept in a third git repository
# (not the project repository or ESP-IDF repository),
Expand Down
184 changes: 173 additions & 11 deletions code/rafting-button/main/rafting-button.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
#include <driver/gpio.h>
#include <esp_chip_info.h>
#include <esp_flash.h>
#include <esp_http_server.h>
#include <esp_intr_alloc.h>
#include <esp_log.h>
#include <esp_mac.h>
#include <esp_netif.h>
#include <esp_netif_ip_addr.h>
#include <esp_now.h>
#include <esp_random.h>
#include <esp_timer.h>
Expand All @@ -28,6 +31,7 @@
#define PRIORITY_HANDLE_DS_EVENT 2
#define PRIORITY_REQUEST_TASK 2
#define PRIORITY_HANDLE_ISR_EVENT 3
#define PRIORITY_WEB_SERVER 1

#define ESPNOW_QUEUE_SIZE 10
#define PRINT_QUEUE_SIZE 4
Expand All @@ -49,6 +53,9 @@
#define VOTE_TIMEOUT 1000000
#define MASTER_TIMEOUT 2000000

#define WIFI_SSID "Rafting button"
#define WIFI_PASS "12345678"

// #define IS_MASTER
#define IS_SLAVE

Expand Down Expand Up @@ -946,6 +953,139 @@ void handle_ds_event_task(void)
vTaskDelete(NULL);
}

static esp_err_t web_server_get_handler(httpd_req_t *req)
{
/* Send a simple response */
const char resp[] = "<h1>Hello World</h1>";
ESP_LOGI("WEB SERVER", "Get request");
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}

/* URI handler structure for GET /uri */
static httpd_uri_t uri_get = {.uri = "/hello",
.method = HTTP_GET,
.handler = web_server_get_handler,
.user_ctx = NULL};
static esp_err_t stop_webserver(httpd_handle_t server)
{
// Stop the httpd server
return httpd_stop(server);
}
static httpd_handle_t start_webserver(void)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.lru_purge_enable = true;

// Start the httpd server
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Set URI handlers
ESP_LOGI(TAG, "Registering URI handlers");
httpd_register_uri_handler(server, &uri_get);
return server;
}

ESP_LOGI(TAG, "Error starting server!");
return NULL;
}
static void disconnect_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
httpd_handle_t *server = (httpd_handle_t *)arg;
if (*server) {
ESP_LOGI("WEB SERVER", "Stopping webserver");
if (stop_webserver(*server) == ESP_OK) {
*server = NULL;
} else {
ESP_LOGE(TAG, "Failed to stop http server");
}
}
}

static void connect_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
httpd_handle_t *server = (httpd_handle_t *)arg;
if (*server == NULL) {
ESP_LOGI("WEB SERVER", "Starting webserver");
*server = start_webserver();
}
}
void web_server_task(void)
{
ESP_LOGI("WEB SERVER", "Task run");
/* Generate default configuration */
httpd_config_t config = HTTPD_DEFAULT_CONFIG();

/* Empty handle to esp_http_server */
httpd_handle_t server = NULL;
while (1) {
if (!gpio_get_level(GPIO_NUM_23)) {
if (node.is_web_server_running) {
// STOP SERVER
ESP_LOGI("WEB SERVER", "remove server");
ESP_ERROR_CHECK(httpd_stop(server));
// set default AP configuration
wifi_config_t wifi_config = {
.ap =
{
.password = WIFI_PASS,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.ssid_hidden = 1,
},
};
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));

ESP_ERROR_CHECK(gpio_set_level(GPIO_NUM_2, 0));
node.is_web_server_running = false;
} else {
// START SERVER
ESP_LOGI("WEB SERVER", "Starting server on port: '%d'",
config.server_port);
// Config web server
wifi_config_t wifi_config = {
.ap = {.ssid = WIFI_SSID,
.ssid_len = strlen(WIFI_SSID),
.password = WIFI_PASS,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.max_connection = 4},
};
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));

ESP_ERROR_CHECK(esp_event_handler_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
ESP_ERROR_CHECK(esp_event_handler_register(
WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler,
&server));

// /* Start the httpd server */
// if (httpd_start(&server, &config) == ESP_OK) {
// /* Register URI handlers */
// ESP_ERROR_CHECK(httpd_register_uri_handler(server, &uri_get));
// } else {
// ESP_LOGE("WEB SERVER", "Server can not start");
// }
server = start_webserver();

esp_netif_t *p_netif = esp_netif_create_default_wifi_ap();
esp_netif_ip_info_t if_info;
ESP_ERROR_CHECK(esp_netif_get_ip_info(p_netif, &if_info));
ESP_LOGI(TAG, "ESP32 IP:" IPSTR, IP2STR(&if_info.ip));

// Turn on build in LED
ESP_ERROR_CHECK(gpio_set_level(GPIO_NUM_2, 1));
node.is_web_server_running = true;
}
while (!gpio_get_level(GPIO_NUM_23))
vTaskDelay(50 / portTICK_PERIOD_MS);
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
vTaskDelete(NULL);
}

void app_main(void)
{
// Init NVS
Expand All @@ -961,25 +1101,41 @@ void app_main(void)
// Reset pint
ESP_ERROR_CHECK(gpio_reset_pin(GPIO_NUM_21));
// Config GPIO
gpio_config_t cfg = {
.pin_bit_mask = BIT64(GPIO_NUM_21),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_ENABLE,
.intr_type = GPIO_INTR_POSEDGE,
};
gpio_config(&cfg);
{
gpio_config_t cfg = {
.pin_bit_mask = BIT64(GPIO_NUM_21),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_ENABLE,
.intr_type = GPIO_INTR_POSEDGE,
};
gpio_config(&cfg);
}
// Install isr service
ESP_ERROR_CHECK(gpio_install_isr_service(0));
// Add isr handler
ESP_ERROR_CHECK(gpio_isr_handler_add(GPIO_NUM_21, gpio_handler_isr, NULL));

// Config GPIOs for web server
// input
ESP_ERROR_CHECK(gpio_reset_pin(GPIO_NUM_23));
{
gpio_config_t cfg = {
.pin_bit_mask = BIT64(GPIO_NUM_23),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&cfg);
}
// buildin led
gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_2, 0);

// Print device MAC address
print_mac_address();

// Set up GPIO_NUM_23 for LED blink
config_led(GPIO_NUM_23);

// Init Wifi
wifi_init();
// Init ESP-NOW
Expand All @@ -992,6 +1148,7 @@ void app_main(void)
node.rtt_balancer_index = 0;
node.epoch_id = 0;
node.is_time_synced = 0;
node.is_web_server_running = false;
node.title = SLAVE;
for (uint8_t i = 0; i < NEIGHBOURS_COUNT; ++i) {
node.neighbour[i].status = NOT_INITIALIZED;
Expand Down Expand Up @@ -1040,6 +1197,11 @@ void app_main(void)
"handle_isr_event_task", STACK_SIZE,
NULL, PRIORITY_HANDLE_ISR_EVENT, NULL);

BaseType_t web_server_task_v;
web_server_task_v =
xTaskCreate((TaskFunction_t)web_server_task, "web_server_task",
STACK_SIZE, NULL, PRIORITY_WEB_SERVER, NULL);

send_hello_ds_message();

vTaskDelay(1000 / portTICK_PERIOD_MS);
Expand Down
1 change: 1 addition & 0 deletions code/rafting-button/main/rafting-button.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ typedef struct node_info {
uint16_t rtt_balancer_index;
bool is_firts_setup_rtt;
bool is_time_synced;
bool is_web_server_running;
int32_t deviation_avg;
neighbour_t neighbour[NEIGHBOURS_COUNT];
uint8_t neighbour_error_count[NEIGHBOURS_COUNT];
Expand Down