Skip to content

Commit

Permalink
Update HTTP server Arduino example
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Nov 26, 2024
1 parent 74f77c9 commit 03c9554
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
7 changes: 6 additions & 1 deletion examples/arduino/w5500-http/mongoose_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 26 additions & 32 deletions examples/arduino/w5500-http/w5500-http.ino
Original file line number Diff line number Diff line change
@@ -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;

pinMode(SS_PIN, OUTPUT);
SPI.begin();

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);
Serial.begin(115200); // Initialise serial
while (!Serial) delay(50); // for debug output

SPI.begin(); // Iniitialise SPI
pinMode(SS_PIN, OUTPUT); // to communicate with W5500 Ethernet module
pinMode(LED_PIN, OUTPUT); // Initialise LED

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(
Expand Down

0 comments on commit 03c9554

Please sign in to comment.