| [ Web Proxy ] |
| Viewing: https://raw.githubusercontent.com/SysAdmin74/esp32_https_server/develop/README.md | [Back] [Original] |
... from your ESP32!
"); res->println(""); res->println(""); } ``` As you can see, the function gets references to the [`HTTPRequest`](https://fhessel.github.io/esp32_https_server/classhttpsserver_1_1HTTPRequest.html) and [`HTTPResponse`](https://fhessel.github.io/esp32_https_server/classhttpsserver_1_1HTTPResponse.html). You can use the request to read headers, parameters, authentication information etc. The response can be used to send data to the client, set headers or HTTP status codes. Now we need to tell the server which URL should be served by this function. This can be done by creating a [`ResourceNode`](https://fhessel.github.io/esp32_https_server/classhttpsserver_1_1ResourceNode.html) (usually in your `setup()` function). ```C++ ResourceNode * nodeRoot = new ResourceNode("/", "GET", &handleRoot); ``` The first parameter defines the route. It should always start with a slash, and using just a slash like in this example means that the function will be called for requests to the server's root (like https://10.0.x.x/). The second parameter is the HTTP method, `"GET"` in this case. Finally, you pass a reference to the request handler function to link it to the route and method. Now you just need to register the created [`ResourceNode`](https://fhessel.github.io/esp32_https_server/classhttpsserver_1_1ResourceNode.html) at your server: ```C++ myServer.registerNode(nodeRoot); ``` That's everything you need to do for a single web page on your server. Note that you can define a single [`ResourceNode`](https://fhessel.github.io/esp32_https_server/classhttpsserver_1_1ResourceNode.html) via `HTTPServer::setDefaultNode()`, which will be called if no other node on the server matches. Method and route are ignored in this case. Most examples use this to define a 404-handler, which might be a good idea for most scenarios. In case no default node is specified, the server will return with a small error page if no matching route is found. ### Start the Server A call to [`HTTPServer::start()`](https://fhessel.github.io/esp32_https_server/classhttpsserver_1_1HTTPServer.html#a1b1b6bce0b52348ca5b5664cf497e039) will start the server so that it is listening on the previously specified port: ```C++ myServer.start(); ``` This code usually goes into your `setup()` function. You can use `HTTPServer::isRunning()` to check whether the server started successfully. By default, you need to pass control to the server explicitly. This is done by calling the [`HTTPServer::loop()`](https://fhessel.github.io/esp32_https_server/classhttpsserver_1_1HTTPServer.html#af8f68f5ff6ad101827bcc52217249fe2) function, which you usually will put into your Arduino sketch's `loop()` function. Once called, the server will first check for incoming connection (up to the maximum connection count that has been defined in the constructor), and then handle every open connection if it has new data on the socket. So your request handler functions will be called during the call to `loop()`. Note that if one of your handler functions is blocking, it will block all other connections as well. ### Running the Server asynchronously If you want to have the server running in the background (and not calling `loop()` by yourself every few milliseconds), you can make use of the ESP32's task feature and put the whole server in a separate task. See the [Async-Server example](https://github.com/fhessel/esp32_https_server/tree/master/examples/Async-Server) to see how this can be done. ## Advanced Configuration This section covers some advanced configuration options that allow you, for example, to customize the build process, but which might require more advanced programming skills and a more sophisticated IDE that just the default Arduino IDE. ### Saving Space by Reducing Functionality To save program space on the microcontroller, there are some parts of the library that can be disabled during compilation and will then not be a part of your program. The following flags are currently available: | Flag | Effect | ------------------------- | --------------------------- | HTTPS_DISABLE_SELFSIGNING | Removes the code for generating a self-signed certificate at runtime. You will need to provide certificate and private key data from another data source to use the `HTTPSServer`. Setting these flags requires a build environment that gives you some control of the compiler, as libraries are usually compiled separately, so just doing a `#define HTTPS_SOMETHING` in your sketch will not work. **Example: Configuration with PlatformIO** To set these flags in PlatformIO, you can modify your `platformio.ini`. To disable for example the self-signed-certificates part of the library, the file could look like this: ```ini [env:wroom] platform = espressif32 board = esp32dev framework = arduino lib_deps = esp32_https_server build_flags = -DHTTPS_DISABLE_SELFSIGNING ``` Note the `-D` in front of the actual flag name, that passes this flag as a definition to the preprocessor. Multiple flags can be added one per line. ### Configure Logging The server provides some internal logging, which is activated on level `INFO` by default. This will look like this on your serial console: ``` [HTTPS:I] New connection. SocketFID=55 [HTTPS:I] Request: GET / (FID=55) [HTTPS:I] Connection closed. Socket FID=55 ``` Logging output can also be controlled by using compiler flags. This requires an advanced development environment like explained in [Saving Space by Reducing Functionality](#saving-space-by-reducing-functionality). There are two parameters that can be configured: - `HTTPS_LOGLEVEL` defines the log level to use - `HTTPS_LOGTIMESTAMP` adds a timestamp (based on uptime) to each log entry | Value of `HTTPS_LOGLEVEL` | Error | Warning | Info | Debug | | ------------------------- | ----- | ------- | ---- | ----- | | 0 | | | | | | 1 | | | | | | 2 | | | | | | 3 | | | | | | 4 | | | | | **Example: Configuration with PlatformIO** To set these flags in PlatformIO, you can modify your `platformio.ini`. The following entries set the minimum log level to warning and enable timestamps ```ini [env:wroom] platform = espressif32 board = esp32dev framework = arduino lib_deps = esp32_https_server build_flags = -DHTTPS_LOGLEVEL=2 -DHTTPS_LOGTIMESTAMP ```| Web Proxy Viewer | New URL | Original Page |