mongoose/examples/arduino/w5500-http/w5500-http.ino

72 lines
2.5 KiB
Arduino
Raw Normal View History

#include <SPI.h>
#include "mongoose.h"
2024-12-02 06:04:57 +08:00
#define LED_PIN LED_BUILTIN // LED pin - can be LED_BUILTIN
#define SS_PIN 17 // Slave select pin
2024-11-27 04:53:21 +08:00
struct mg_tcpip_spi spi = {
2024-11-27 04:53:21 +08:00
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
};
2024-11-27 04:53:21 +08:00
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();
}
2024-12-02 06:04:57 +08:00
bool mg_random(void *buf, size_t len) { // For TLS
uint8_t *p = (uint8_t *) buf;
while (len--) *p++ = (unsigned char) (rand() & 255);
return true;
}
// Crude function to get available RAM, for quick profiling
size_t getFreeRAM(void) {
size_t size = 0, increment = 100;
void *p;
while ((p = malloc(size)) != NULL) free(p), size += increment;
return size;
}
void setup() {
2024-11-27 04:53:21 +08:00
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(
&mgr, "http://0.0.0.0",
[](struct mg_connection *c, int ev, void *ev_data) {
2024-12-02 06:04:57 +08:00
if (ev == MG_EV_HTTP_MSG) {
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
if (mg_match(hm->uri, mg_str("/api/led/on"), NULL)) {
digitalWrite(LED_PIN, HIGH);
mg_http_reply(c, 200, "", "{%m: %d}\n", MG_ESC("led"), digitalRead(LED_PIN));
} else if (mg_match(hm->uri, mg_str("/api/led/off"), NULL)) {
digitalWrite(LED_PIN, LOW);
mg_http_reply(c, 200, "", "{%m: %d}\n", MG_ESC("led"), digitalRead(LED_PIN));
} else {
mg_http_reply(c, 200, "", "ok, free RAM: %u\n", getFreeRAM());
}
}
},
&mgr);
}
void loop() {
mg_mgr_poll(&mgr, 1);
}