From 03c955410c0aeae33ad61cb98e87a3eee8d439db Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 26 Nov 2024 20:53:21 +0000 Subject: [PATCH] Update HTTP server Arduino example --- examples/arduino/w5500-http/mongoose_config.h | 7 ++- examples/arduino/w5500-http/w5500-http.ino | 54 +++++++++---------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/examples/arduino/w5500-http/mongoose_config.h b/examples/arduino/w5500-http/mongoose_config.h index dc8f7b61..31a4d635 100644 --- a/examples/arduino/w5500-http/mongoose_config.h +++ b/examples/arduino/w5500-http/mongoose_config.h @@ -13,5 +13,10 @@ #define MG_ENABLE_DRIVER_W5500 1 #define MG_ENABLE_TCPIP_DRIVER_INIT 0 #define mkdir(a, b) (-1) +#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 +#define MG_ENABLE_CUSTOM_MILLIS 1 #define MG_IO_SIZE 128 -//#define MG_ENABLE_LOG 0 + +// Enable TLS +// #define MG_TLS MG_TLS_BUILTIN +// #define MG_ENABLE_CUSTOM_RANDOM 1 diff --git a/examples/arduino/w5500-http/w5500-http.ino b/examples/arduino/w5500-http/w5500-http.ino index f31d6cbb..0073be24 100644 --- a/examples/arduino/w5500-http/w5500-http.ino +++ b/examples/arduino/w5500-http/w5500-http.ino @@ -1,42 +1,36 @@ #include #include "mongoose.h" -#define SS_PIN 17 // Slave select pin -struct mg_mgr mgr; // Mongoose event manager +#define LED_PIN 21 // Slave select pin +#define SS_PIN 17 // Slave select pin + struct mg_tcpip_spi spi = { - NULL, // SPI data - [](void *) { digitalWrite(SS_PIN, LOW); }, // begin transation - [](void *) { digitalWrite(SS_PIN, HIGH); }, // end transaction - [](void *, uint8_t c) { return SPI.transfer(c); }, // execute transaction + NULL, // SPI metadata + [](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); }, + [](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); }, + [](void *, uint8_t c) { return SPI.transfer(c); }, // Execute transaction }; -struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // network interface +struct mg_mgr mgr; // Mongoose event manager +struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface + +uint64_t mg_millis(void) { + return millis(); +} void setup() { - Serial.begin(115200); - mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); - mg_log_set(MG_LL_DEBUG); - while (!Serial) (void) 0; + Serial.begin(115200); // Initialise serial + while (!Serial) delay(50); // for debug output - pinMode(SS_PIN, OUTPUT); - SPI.begin(); + SPI.begin(); // Iniitialise SPI + pinMode(SS_PIN, OUTPUT); // to communicate with W5500 Ethernet module + pinMode(LED_PIN, OUTPUT); // Initialise LED - mg_mgr_init(&mgr); - // delay(3000); - MG_INFO(("Starting TCP/IP stack...")); - SPI.transfer(0); - MG_INFO(("--->...")); - - mif.driver = &mg_tcpip_driver_w5500; - mif.driver_data = &spi; - mg_tcpip_init(&mgr, &mif); - - // Start a 5 sec timer, print status message periodically - mg_timer_add( - &mgr, 5000, MG_TIMER_REPEAT, - [](void *) { - MG_INFO(("ethernet: %s", mg_tcpip_driver_w5500.up(&mif) ? "up" : "down")); - }, - NULL); + mg_mgr_init(&mgr); // Initialise Mongoose event manager + mg_log_set(MG_LL_DEBUG); // Set debug log level + mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL); // Log serial + mif.driver = &mg_tcpip_driver_w5500; // Use W5500 built-in driver + mif.driver_data = &spi; // Pass SPI interface to W5500 driver + mg_tcpip_init(&mgr, &mif); // Initialise built-in TCP/IP stack // Setup HTTP listener. Respond "ok" on any HTTP request mg_http_listen(