Switch HTTP Server

This commit is contained in:
iranl
2024-08-26 21:47:10 +02:00
parent d3c3589233
commit ca9c2feebc
234 changed files with 20090 additions and 8061 deletions

View File

@@ -0,0 +1,39 @@
.pioenvs
.clang_complete
.gcc-flags.json
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# Visual Studio/VisualMicro stuff
Visual\ Micro
*.sdf
*.opensdf
*.suo
.pioenvs
.piolibdeps
.pio
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/settings.json
.vscode/.browse.c_cpp.db*
.vscode/ipch

View File

@@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in a an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View File

@@ -0,0 +1,64 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[common]
lib_deps = ArduinoMongoose
monitor_speed = 115200
monitor_port = /dev/ttyUSB2
build_flags =
-DENABLE_DEBUG
# -DCS_ENABLE_STDIO
build_flags_secure =
-DSIMPLE_SERVER_SECURE
-DMG_ENABLE_SSL=1
# -DMG_SSL_IF=MG_SSL_IF_OPENSSL
# -DKR_VERSION
-DMG_SSL_MBED_DUMMY_RANDOM=1
-DMG_SSL_IF=MG_SSL_IF_MBEDTLS
-DMG_SSL_IF_MBEDTLS_FREE_CERTS=1
-DMG_SSL_IF_MBEDTLS_MAX_FRAG_LEN=2048
[env:huzzah]
platform = espressif8266
board = huzzah
framework = arduino
monitor_speed = ${common.monitor_speed}
monitor_port = ${common.monitor_port}
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags}
[env:esp-wrover-kit]
platform = espressif32
framework = arduino
board = esp-wrover-kit
monitor_speed = ${common.monitor_speed}
monitor_port = ${common.monitor_port}
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags}
[env:esp-wrover-kit-secure]
platform = espressif32
framework = arduino
board = esp-wrover-kit
monitor_speed = ${common.monitor_speed}
monitor_port = ${common.monitor_port}
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags} ${common.build_flags_secure}
#[env:linux_x86_64]
#platform = linux_x86_64
#framework = arduino
#board = generic
#lib_deps = ${common.lib_deps}
#build_flags = ${common.build_flags}
#build_flags = -DSERIAL_TO_CONSOLE

View File

@@ -0,0 +1,235 @@
//
// A simple server implementation showing how to:
// * serve static messages
// * read GET and POST parameters
// * handle missing pages / 404s
//
#include <Arduino.h>
#include <MongooseCore.h>
#include <MongooseHttpServer.h>
#ifdef ESP32
#include <WiFi.h>
#define START_ESP_WIFI
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#define START_ESP_WIFI
#else
#error Platform not supported
#endif
MongooseHttpServer server;
const char *ssid = "wifi";
const char *password = "password";
const char *server_pem =
"-----BEGIN CERTIFICATE-----\r\n"
"MIIDDjCCAfagAwIBAgIBBDANBgkqhkiG9w0BAQsFADA/MRkwFwYDVQQDDBB0ZXN0\r\n"
"LmNlc2FudGEuY29tMRAwDgYDVQQKDAdDZXNhbnRhMRAwDgYDVQQLDAd0ZXN0aW5n\r\n"
"MB4XDTE2MTExMzEzMTgwMVoXDTI2MDgxMzEzMTgwMVowFDESMBAGA1UEAwwJbG9j\r\n"
"YWxob3N0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAro8CW1X0xaGm\r\n"
"GkDaMxKbXWA5Lw+seA61tioGrSIQzuqLYeJoFnwVgF0jB5PTj+3EiGMBcA/mh73V\r\n"
"AthTFmJBxj+agIp7/cvUBpgfLClmSYL2fZi6Fodz+f9mcry3XRw7O6vlamtWfTX8\r\n"
"TAmMSR6PXVBHLgjs5pDOFFmrNAsM5sLYU1/1MFvE2Z9InTI5G437IE1WchRSbpYd\r\n"
"HchC39XzpDGoInZB1a3OhcHm+xUtLpMJ0G0oE5VFEynZreZoEIY4JxspQ7LPsay9\r\n"
"fx3Tlk09gEMQgVCeCNiQwUxZdtLau2x61LNcdZCKN7FbFLJszv1U2uguELsTmi7E\r\n"
"6pHrTziosQIDAQABo0AwPjAJBgNVHRMEAjAAMAsGA1UdDwQEAwIDqDATBgNVHSUE\r\n"
"DDAKBggrBgEFBQcDATAPBgN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B5W84SxUKvM7DVEomy1QPB57ZpyQaoBq7adOz0pWJXfp7qo4950ZOhBGH1\r\n"
"hKbZ6c4ggwVJy2j49EgMok5NGCKvPAtabbR6H8Mz8DW9aXURxhWJvij+Qw1fWK4b\r\n"
"7G/qUI9iE5iUU7MkIcLIbTf/\r\n"
"-----END PRIVATE KEY-----\r\n";
const char *index_page =
"<!DOCTYPE html>\n"
"<html lang=\"en\">\n"
"<head>\n"
" <meta charset=\"utf-8\" />\n"
" <title>WebSocket Test</title>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n"
" <style type=\"text/css\">\n"
" body {\n"
" background-color: #789; margin: 0;\n"
" padding: 0; font: 14px Helvetica, Arial, sans-serif;\n"
" }\n"
" div.content {\n"
" width: 800px; margin: 2em auto; padding: 20px 50px;\n"
" background-color: #fff; border-radius: 1em;\n"
" }\n"
" #messages {\n"
" border: 2px solid #fec; border-radius: 1em;\n"
" height: 10em; overflow: scroll; padding: 0.5em 1em;\n"
" }\n"
" a:link, a:visited { color: #69c; text-decoration: none; }\n"
" @media (max-width: 700px) {\n"
" body { background-color: #fff; }\n"
" div.content {\n"
" width: auto; margin: 0 auto; border-radius: 0;\n"
" padding: 1em;\n"
" }\n"
" }\n"
"</style>\n"
"\n"
"<script language=\"javascript\" type=\"text/javascript\">\n"
"\n"
" var rooms = [];\n"
" var ws = new WebSocket(\'ws://\' + location.host + \'/ws\');\n"
"\n"
" if (!window.console) { window.console = { log: function() {} } };\n"
"\n"
" ws.onopen = function(ev) { console.log(ev); };\n"
" ws.onerror = function(ev) { console.log(ev); };\n"
" ws.onclose = function(ev) { console.log(ev); };\n"
" ws.onmessage = function(ev) {\n"
" console.log(ev);\n"
" var div = document.createElement(\'div\');\n"
" div.innerHTML = ev.data;\n"
" document.getElementById(\'messages\').appendChild(div);\n"
"\n"
" };\n"
"\n"
" window.onload = function() {\n"
" document.getElementById(\'send_button\').onclick = function(ev) {\n"
" var msg = document.getElementById(\'send_input\').value;\n"
" document.getElementById(\'send_input\').value = \'\';\n"
" ws.send(msg);\n"
" };\n"
" document.getElementById(\'send_input\').onkeypress = function(ev) {\n"
" if (ev.keyCode == 13 || ev.which == 13) {\n"
" document.getElementById(\'send_button\').click();\n"
" }\n"
" };\n"
" };\n"
"</script>\n"
"</head>\n"
"<body>\n"
" <div class=\"content\">\n"
" <h1>Websocket PubSub Demonstration</h1>\n"
"\n"
" <p>\n"
" This page demonstrates how Mongoose could be used to implement\n"
" <a href=\"http://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern\">\n"
" publishsubscribe pattern</a>. Open this page in several browser\n"
" windows. Each window initiates persistent\n"
" <a href=\"http://en.wikipedia.org/wiki/WebSocket\">WebSocket</a>\n"
" connection with the server, making each browser window a websocket client.\n"
" Send messages, and see messages sent by other clients.\n"
" </p>\n"
"\n"
" <div id=\"messages\">\n"
" </div>\n"
"\n"
" <p>\n"
" <input type=\"text\" id=\"send_input\" />\n"
" <button id=\"send_button\">Send Message</button>\n"
" </p>\n"
" </div>\n"
"</body>\n"
"</html>\n";
#include <Arduino.h>
void broadcast(MongooseHttpWebSocketConnection *from, MongooseString msg)
{
char buf[500];
char addr[32];
mg_sock_addr_to_str(from->getRemoteAddress(), addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
snprintf(buf, sizeof(buf), "%s %.*s", addr, (int) msg.length(), msg.c_str());
printf("%s\n", buf);
server.sendAll(from, buf);
}
void setup()
{
Serial.begin(115200);
#ifdef START_ESP_WIFI
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Failed!\n");
return;
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Hostname: ");
#ifdef ESP32
Serial.println(WiFi.getHostname());
#elif defined(ESP8266)
Serial.println(WiFi.hostname());
#endif
#endif
Mongoose.begin();
#ifdef SIMPLE_SERVER_SECURE
if(false == server.begin(443, server_pem, server_key)) {
Serial.print("Failed to start server");
return;
}
#else
server.begin(80);
#endif
server.on("/$", HTTP_GET, [](MongooseHttpServerRequest *request) {
request->send(200, "text/html", index_page);
});
// Test the stream response class
server.on("/ws$")->
onConnect([](MongooseHttpWebSocketConnection *connection) {
broadcast(connection, MongooseString("++ joined"));
})->
onClose([](MongooseHttpServerRequest *c) {
MongooseHttpWebSocketConnection *connection = static_cast<MongooseHttpWebSocketConnection *>(c);
broadcast(connection, MongooseString("++ left"));
})->
onFrame([](MongooseHttpWebSocketConnection *connection, int flags, uint8_t *data, size_t len) {
broadcast(connection, MongooseString((const char *)data, len));
});
}
void loop()
{
Mongoose.poll(1000);
Serial.printf("Free memory %u\n", ESP.getFreeHeap());
}

View File

@@ -0,0 +1,11 @@
This directory is intended for PIO Unit Testing and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PIO Unit Testing:
- https://docs.platformio.org/page/plus/unit-testing.html

View File

@@ -0,0 +1,35 @@
# Name: REST Client
# Id: humao.rest-client
# Description: REST Client for Visual Studio Code
# Version: 0.21.3
# Publisher: Huachao Mao
# VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=humao.rest-client
@baseUrl = http://172.16.0.87
###
GET {{baseUrl}}/ HTTP/1.1
###
GET {{baseUrl}}/get?message=Hello+World HTTP/1.1
###
POST {{baseUrl}}/post HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
message=Hello+World
###
GET {{baseUrl}}/someRandomFile HTTP/1.1
###
GET {{baseUrl}}/basic HTTP/1.1
###
GET {{baseUrl}}/stream HTTP/1.1