Skip to content

Merge many PRs #162

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

Closed
wants to merge 5 commits into from
Closed
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -14,6 +14,18 @@ Breaking changes:


## [v1.1.0](https://github.com/fhessel/esp32_https_server/releases/tag/v1.0.0)

New functionality:

* Add examples to support WT32_ETH01 using LAN8720

Bug fixes:

* Fix compile error for using `hwcrypto/sha.h`

=========================================================

## [v1.0.0](https://github.com/fhessel/esp32_https_server/releases/tag/v1.0.0)

New functionality:
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# ESP32 HTTPS Server

![GitHub tag (latest SemVer)](https://img.shields.io/github/v/tag/fhessel/esp32_https_server?label=Version&sort=semver) ![Build Examples](https://github.com/fhessel/esp32_https_server/workflows/Build%20Examples/badge.svg?branch=master)

This repository contains an HTTPS server library that can be used with the [ESP32 Arduino Core](https://github.com/espressif/arduino-esp32). It supports HTTP as well.

## Features
@@ -73,19 +71,18 @@ git clone https://github.com/fhessel/esp32_https_server.git

> **Note:** To run the examples (except for the _Self-Signed-Certificates_ example), you need to execute the script extras/create_cert.sh first (see [Issue #26](https://github.com/fhessel/esp32_https_server/issues/26) for Windows). This script will create a simple CA to sign certificates that are used with the examples. Some notes on the usage can be found in the extras/README.md file.

You will find several examples showing how you can use the library (roughly ordered by complexity):
You will find several examples showing how you can use the library:

- [Static-Page](examples/Static-Page/Static-Page.ino): Short example showing how to serve some static resources with the server. You should start with this sketch and get familiar with it before having a look at the more complex examples.
- [Parameters](examples/Parameters/Parameters.ino): Shows how you can access request parameters (the part after the question mark in the URL) or parameters in dynamic URLs (like /led/1, /led/2, ...)
- [Parameter-Validation](examples/Parameter-Validation/Parameter-Validation.ino): Shows how you can integrate validator functions to do formal checks on parameters in your URL.
- [Put-Post-Echo](examples/Put-Post-Echo/Put-Post-Echo.ino): Implements a simple echo service for PUT and POST requests that returns the request body as response body. Also shows how to differentiate between multiple HTTP methods for the same URL.
- [HTTPS-and-HTTP](examples/HTTPS-and-HTTP/HTTPS-and-HTTP.ino): Shows how to serve resources via HTTP and HTTPS in parallel and how to check if the user is using a secure connection during request handling
- [HTML-Forms](examples/HTML-Forms/HTML-Forms.ino): Shows how to use body parsers to handle requests created from HTML forms (access text field contents, handle file upload, etc.).
- [Async-Server](examples/Async-Server/Async-Server.ino): Like the Static-Page example, but the server runs in a separate task on the ESP32, so you do not need to call the loop() function in your main sketch.
- [Self-Signed-Certificate](examples/Self-Signed-Certificate/Self-Signed-Certificate.ino): Shows how to generate a self-signed certificate on the fly on the ESP when the sketch starts. You do not need to run `create_cert.sh` to use this example.
- [Middleware](examples/Middleware/Middleware.ino): Shows how to use the middleware API for logging. Middleware functions are defined very similar to webservers like Express.
- [Authentication](examples/Authentication/Authentication.ino): Implements a chain of two middleware functions to handle authentication and authorization using HTTP Basic Auth.
- [Async-Server](examples/Async-Server/Async-Server.ino): Like the Static-Page example, but the server runs in a separate task on the ESP32, so you do not need to call the loop() function in your main sketch.
- [Websocket-Chat](examples/Websocket-Chat/Websocket-Chat.ino): Provides a browser-based chat built on top of websockets. **Note:** Websockets are still under development!
- [Parameter-Validation](examples/Parameter-Validation/Parameter-Validation.ino): Shows how you can integrate validator functions to do formal checks on parameters in your URL.
- [Self-Signed-Certificate](examples/Self-Signed-Certificate/Self-Signed-Certificate.ino): Shows how to generate a self-signed certificate on the fly on the ESP when the sketch starts. You do not need to run `create_cert.sh` to use this example.
- [REST-API](examples/REST-API/REST-API.ino): Uses [ArduinoJSON](https://arduinojson.org/) and [SPIFFS file upload](https://github.com/me-no-dev/arduino-esp32fs-plugin) to serve a small web interface that provides a REST API.

If you encounter error messages that cert.h or private\_key.h are missing when running an example, make sure to run create\_cert.sh first (see Setup Instructions).
146 changes: 77 additions & 69 deletions examples/Async-Server/Async-Server.ino
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/**
* Example for the ESP32 HTTP(S) Webserver
*
* IMPORTANT NOTE:
* To run this script, your need to
* 1) Enter your WiFi SSID and PSK below this comment
* 2) Make sure to have certificate data available. You will find a
* shell script and instructions to do so in the library folder
* under extras/
*
* This script will install an HTTPS Server on your ESP32 with the following
* functionalities:
* - Show simple page on web server root
* - 404 for everything else
* The server will be run in a separate task, so that you can do your own stuff
* in the loop() function.
* Everything else is just like the Static-Page example
*/
Example for the ESP32 HTTP(S) Webserver

IMPORTANT NOTE:
To run this script, your need to
1) Enter your WiFi SSID and PSK below this comment
2) Make sure to have certificate data available. You will find a
shell script and instructions to do so in the library folder
under extras/

This script will install an HTTPS Server on your ESP32 with the following
functionalities:
- Show simple page on web server root
- 404 for everything else
The server will be run in a separate task, so that you can do your own stuff
in the loop() function.
Everything else is just like the Static-Page example
*/

// TODO: Configure your WiFi here
#define WIFI_SSID "<your ssid goes here>"
#define WIFI_PSK "<your pre-shared key goes here>"
#define WIFI_SSID "your_ssid"
#define WIFI_PSK "12345678"

/** Check if we have multiple cores */
#if CONFIG_FREERTOS_UNICORE
#define ARDUINO_RUNNING_CORE 0
#define ARDUINO_RUNNING_CORE 0
#else
#define ARDUINO_RUNNING_CORE 1
#define ARDUINO_RUNNING_CORE 1
#endif

// Include certificate data (see note above)
@@ -46,53 +46,15 @@ using namespace httpsserver;

// Create an SSL certificate object from the files included above
SSLCert cert = SSLCert(
example_crt_DER, example_crt_DER_len,
example_key_DER, example_key_DER_len
);
example_crt_DER, example_crt_DER_len,
example_key_DER, example_key_DER_len
);

// Create an SSL-enabled server that uses the certificate
HTTPSServer secureServer = HTTPSServer(&cert);

// Declare some handler functions for the various URLs on the server
void handleRoot(HTTPRequest * req, HTTPResponse * res);
void handle404(HTTPRequest * req, HTTPResponse * res);

// We declare a function that will be the entry-point for the task that is going to be
// created.
void serverTask(void *params);

void setup() {
// For logging
Serial.begin(115200);

// Connect to WiFi
Serial.println("Setting up WiFi");
WiFi.begin(WIFI_SSID, WIFI_PSK);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("Connected. IP=");
Serial.println(WiFi.localIP());

// Setup the server as a separate task.
Serial.println("Creating server task... ");
// We pass:
// serverTask - the function that should be run as separate task
// "https443" - a name for the task (mainly used for logging)
// 6144 - stack size in byte. If you want up to four clients, you should
// not go below 6kB. If your stack is too small, you will encounter
// Panic and stack canary exceptions, usually during the call to
// SSL_accept.
xTaskCreatePinnedToCore(serverTask, "https443", 6144, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
}

void loop() {
Serial.println("loop()");
delay(5000);
}

void serverTask(void *params) {
void serverTask(void *params)
{
// In the separate task we first do everything that we would have done in the
// setup() function, if we would run the server synchronously.

@@ -112,11 +74,14 @@ void serverTask(void *params) {

Serial.println("Starting server...");
secureServer.start();
if (secureServer.isRunning()) {

if (secureServer.isRunning())
{
Serial.println("Server ready.");

// "loop()" function of the separate task
while(true) {
while (true)
{
// This call will let the server do its work
secureServer.loop();

@@ -126,7 +91,8 @@ void serverTask(void *params) {
}
}

void handleRoot(HTTPRequest * req, HTTPResponse * res) {
void handleRoot(HTTPRequest * req, HTTPResponse * res)
{
// Status code is 200 OK by default.
// We want to deliver a simple HTML page, so we send a corresponding content type:
res->setHeader("Content-Type", "text/html");
@@ -140,13 +106,14 @@ void handleRoot(HTTPRequest * req, HTTPResponse * res) {
res->println("<h1>Hello World!</h1>");
res->print("<p>Your server is running for ");
// A bit of dynamic data: Show the uptime
res->print((int)(millis()/1000), DEC);
res->print((int)(millis() / 1000), DEC);
res->println(" seconds.</p>");
res->println("</body>");
res->println("</html>");
}

void handle404(HTTPRequest * req, HTTPResponse * res) {
void handle404(HTTPRequest * req, HTTPResponse * res)
{
// Discard request body, if we received any
// We do this, as this is the default node and may also server POST/PUT requests
req->discardRequestBody();
@@ -165,3 +132,44 @@ void handle404(HTTPRequest * req, HTTPResponse * res) {
res->println("<body><h1>404 Not Found</h1><p>The requested resource was not found on this server.</p></body>");
res->println("</html>");
}

void setup()
{
// For logging
Serial.begin(115200);
while (!Serial && millis() < 5000);

///////////////////////////////////////////////

Serial.print("\nStarting Async_Server on "); Serial.println(ARDUINO_BOARD);

// Connect to WiFi
Serial.println("Setting up WiFi");
WiFi.begin(WIFI_SSID, WIFI_PSK);

while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}

Serial.print("Connected. IP=");
Serial.println(WiFi.localIP());

// Setup the server as a separate task.
Serial.println("Creating server task... ");
// We pass:
// serverTask - the function that should be run as separate task
// "https443" - a name for the task (mainly used for logging)
// 6144 - stack size in byte. If you want up to four clients, you should
// not go below 6kB. If your stack is too small, you will encounter
// Panic and stack canary exceptions, usually during the call to
// SSL_accept.
xTaskCreatePinnedToCore(serverTask, "https443", 6144, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
}

void loop()
{
Serial.println("loop()");
delay(5000);
}
4 changes: 4 additions & 0 deletions examples/Async-Server/cert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef CERT_H_
#define CERT_H_
#error You have to run the srcipt extras/create_cert.sh to recreate these files
#endif
4 changes: 4 additions & 0 deletions examples/Async-Server/private_key.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef PRIVATE_KEY_H_
#define PRIVATE_KEY_H_
#error You have to run the srcipt extras/create_cert.sh to recreate these files
#endif
Loading
Oops, something went wrong.