Update HTTP server Arduino example

This commit is contained in:
Sergey Lyubka 2024-11-26 20:53:21 +00:00
parent 74f77c9ded
commit 03c955410c
2 changed files with 30 additions and 31 deletions

View File

@ -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

View File

@ -1,42 +1,36 @@
#include <SPI.h>
#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(