---
title: "Mongoose library arduino w5550 example"
url: "https://maker.wiznet.io/Benjamin/projects/mongoose-library-arduino-w5550-example/"
markdown_url: "https://maker.wiznet.io/Benjamin/projects/mongoose-library-arduino-w5550-example/md"
type: "UCC: User Created Content"
author: "Cesanta Software"
author_url: "https://github.com/cesanta/mongoose/tree/master/examples/arduino/w5500"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "Cesanta Software"
original_url: "https://github.com/cesanta/mongoose/tree/master/examples/arduino/w5500"
published: "2023-06-05"
language: "en"
hardware: ["Arduino Ethernet Shield"]
likes: 0
views: 775
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Mongoose library arduino w5550 example

> Example of configuring an HTTP server using the Mongoose library on an Arduino-based system.

Original author: Cesanta Software (source: https://github.com/cesanta/mongoose/tree/master/examples/arduino/w5500)

## Components

- **Arduino Ethernet Shield** x 1 ([docs](https://docs.arduino.cc/retired/shields/arduino-ethernet-shield-without-poe-module))
- Software: **Arduino IDE** ([docs](https://www.arduino.cc/en/software))

## Article

This code is an example of configuring an HTTP server using the Mongoose library on an Arduino-based system.

Here's a look at the individual parts

```cpp
#define SS_PIN 3    // Slave select pin
struct mg_mgr mgr;  // Mongoose event manager
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
};
struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}};  // network interface
```

In this part, we are setting up SPI communication and initializing the network interface with the Mongoose Event Manager. Settings for SPI data transmission are defined. SS_PIN is the pin that selects the slave device for SPI communication.

```cpp
void setup() {
  Serial.begin(115200);
  pinMode(SS_PIN, OUTPUT);
  SPI.begin();

  // Set logging function to a serial print
  mg_log_set_fn([](char ch, void *) { Serial.print(ch); }, NULL);
  mg_mgr_init(&mgr);

  delay(3000);
  MG_INFO(("Starting TCP/IP stack..."));

  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);

  // Setup HTTP listener. Respond "ok" on any HTTP request
  mg_http_listen(
      &mgr, "http://0.0.0.0",
      [](struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
        if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "ok\n");
      },
      &mgr);
}
```

In the setup() function, we start the SPI communication, set up the logging function, initialize the Mongoose manager, and start the TCP/IP stack. Next, we start a timer to print the Ethernet connection status every 5 seconds, and set up an HTTP listener to respond "ok" to all HTTP requests.

```cpp
void loop() {
 mg_mgr_poll(&mgr, 1);
}
```

Finally, the loop() function continuously polls for events from the Mongoose manager and processes them.

This code is an example of creating a server that establishes a network connection over Ethernet with SPI communication and simply responds to HTTP requests. This allows you to perform web server functionality on an embedded system.

---

Source: https://maker.wiznet.io/Benjamin/projects/mongoose-library-arduino-w5550-example/
