Skip to content

Commit

Permalink
Add a basic HTTP web server that says hello at GET /hello
Browse files Browse the repository at this point in the history
  • Loading branch information
fnune committed Feb 15, 2021
1 parent 2fc3b17 commit 6671fbf
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 61 deletions.
8 changes: 7 additions & 1 deletion NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Rebuilding with baud rate 115200 makes `screen /dev/ttyUSB0 115200` produce outp
GGq4O.���ɏ�ɏQ�0�~?�4�!�S{�O�:9�O�:9�COAa�$\���S��4�MS��F�����.P�r|Vz4E^V8���M�0�zx��#A�AVZ��#�pRZR�rRRZ�E��&��,CORRV��'K�EEZG�Ň�EERGG���MK�EERG��'K�EEZG��#�G�����Gq���4O.�ɏ�ɏ��ɏ�ɏ��ɏ�ɏ��ɏ�ɏ��ɏ�ɏQ�0�~?�4�!�S{�O�:9�O�:9�COAa�$L���S��4�MS��F�����.P�r|Vz4E^V8���M�0�zx��#A�AVZ��#�pRZR�rRRZ�E��&��,CORRV��'K�EEZG�Ň�EERGG���MK�EER��r�b�nn�lnnl��r�nn�||�bbl��r�b�nn�lnn��r�nn�||�bbl��r�b�nn�lnn��r�nn�||�bbl��r�b�nn�lnn��r�nn�||�bbl��r�b�nn�lnnl�r�nn�||�bbl��r�b�nn�lnn�r�nn�||�bbl
```

At this point I'm going to give up and just use `make monitor`, and figure this out later with the help of someone more experienced. The project runs and I can move on to writing some application code.
At this point I'm going to give up and just use `make monitor`, and figure this out later with the help of someone more experienced.

### A foray into Linux specifics

Expand Down Expand Up @@ -236,3 +236,9 @@ minicom -p /dev/pts/4
```

Type some stuff... and it appears on my newest terminal! Cool! Trying to type on `/dev/pts/4` produces the same characters on `/dev/pts/3`, but some get dropped. I'm curious to find out why, but let's get back to the main course.

## The Memfault SDK

Documentation link: https://docs.memfault.com/docs/embedded/esp8266-rtos-sdk-guide/

I'm starting by getting some application code in place. It's just going to be an HTTP server with simple routes. Then, I'll try to integrate the Memfault SDK on top.
6 changes: 0 additions & 6 deletions hello_world/CMakeLists.txt

This file was deleted.

9 changes: 0 additions & 9 deletions hello_world/Makefile

This file was deleted.

5 changes: 0 additions & 5 deletions hello_world/README.md

This file was deleted.

2 changes: 0 additions & 2 deletions hello_world/main/CMakeLists.txt

This file was deleted.

38 changes: 0 additions & 38 deletions hello_world/main/hello_world_main.c

This file was deleted.

10 changes: 10 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# The following lines of boilerplate have to be in your project's CMakeLists
# in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.5)

# (Not part of the boilerplate)
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(simple)
6 changes: 6 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PROJECT_NAME := toastboard

EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common

include $(IDF_PATH)/make/project.mk

4 changes: 4 additions & 0 deletions src/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
set(COMPONENT_SRCS "main.c")
set(COMPONENT_ADD_INCLUDEDIRS ".")

register_component()
File renamed without changes.
103 changes: 103 additions & 0 deletions src/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* Simple HTTP Server Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <sys/param.h>

#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"

#include <esp_http_server.h>

static const char *TAG = "APP";

esp_err_t hello_get_handler(httpd_req_t *req) {
/* Send response with custom headers and body set as the
* string passed in user context*/
const char *resp_str = (const char *)req->user_ctx;
httpd_resp_send(req, resp_str, strlen(resp_str));

/* After sending the HTTP response the old HTTP request
* headers are lost. Check if HTTP request headers can be read now. */
if (httpd_req_get_hdr_value_len(req, "Host") == 0) {
ESP_LOGI(TAG, "Request headers lost");
}

return ESP_OK;
}

httpd_uri_t hello = {.uri = "/hello",
.method = HTTP_GET,
.handler = hello_get_handler,
/* Let's pass response string in user
* context to demonstrate it's usage */
.user_ctx = "Hello World!"};

httpd_handle_t start_webserver(void) {
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();

// 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, &hello);
return server;
}

ESP_LOGI(TAG, "Error starting server!");
return NULL;
}

void stop_webserver(httpd_handle_t server) {
// Stop the httpd server
httpd_stop(server);
}

static httpd_handle_t server = 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(TAG, "Stopping webserver");
stop_webserver(*server);
*server = NULL;
}
}

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(TAG, "Starting webserver");
*server = start_webserver();
}
}

void app_main() {
ESP_ERROR_CHECK(nvs_flash_init());
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());

ESP_ERROR_CHECK(example_connect());

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));

server = start_webserver();
}
2 changes: 2 additions & 0 deletions src/sdkconfig.defaults
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_LWIP_NETIF_LOOPBACK=y
CONFIG_LWIP_LOOPBACK_MAX_PBUFS=1

0 comments on commit 6671fbf

Please sign in to comment.