Update Arduino sketches

This commit is contained in:
Sergey Lyubka 2024-12-01 22:04:57 +00:00
parent 7fffe0abaa
commit c5fda07d1e
3 changed files with 30 additions and 10 deletions

View File

@ -8,15 +8,11 @@
#include <time.h>
#define MG_ARCH MG_ARCH_CUSTOM
#define MG_ENABLE_SOCKET 0
#define MG_ENABLE_TCPIP 1
#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
// Enable TLS
#define MG_ENABLE_CUSTOM_RANDOM 1
// #define MG_TLS MG_TLS_BUILTIN
// #define MG_ENABLE_CUSTOM_RANDOM 1
#define MG_IO_SIZE 128

View File

@ -1,8 +1,8 @@
#include <SPI.h>
#include "mongoose.h"
#define LED_PIN 21 // Slave select pin
#define SS_PIN 17 // Slave select pin
#define LED_PIN LED_BUILTIN // LED pin - can be LED_BUILTIN
#define SS_PIN 17 // Slave select pin
struct mg_tcpip_spi spi = {
NULL, // SPI metadata
@ -17,6 +17,20 @@ uint64_t mg_millis(void) {
return millis();
}
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() {
Serial.begin(115200); // Initialise serial
while (!Serial) delay(50); // for debug output
@ -36,7 +50,18 @@ void setup() {
mg_http_listen(
&mgr, "http://0.0.0.0",
[](struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) mg_http_reply(c, 200, "", "ok\n");
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);
}

View File

@ -12,7 +12,6 @@
#define MG_ENABLE_TCPIP 1
#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_ENABLE_CUSTOM_RANDOM 1