diff --git a/README.md b/README.md index 56d53317a6bb2004df7904b163c0c75e1463e4bb..1aea79f8b61bcf462424cb6c24bad07d30659697 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,24 @@ ESP32 firmware for the IMC, based on Arduino and the `ESP32-Ethernet-Kit_A_V1.2` This project depends on [`Arduino_SNMP`](https://github.com/patricklaf/SNMP). +## Usage +The IMC is meant to be used over network using HTTP get and post. + +### Routes +All routes return JSON. + +Available routes are: + +- `/idn`: provides information about firmware and hardware version, and uptime. +It can be queried as a heartbeat, seeing the uptime increasing. + +### Serial Interface + +The serial interface is available through the micro-usb port. +It prints basic networking information, as well as a heartbeat (`-` if ethernet is disconnected, else `.`). + + +## Development ### Quirks diff --git a/icbm.ino b/icbm.ino index e104b73a2e7c6fc087244c597b3d6d74a4c11186..5e4791b1f893d8497a911ea585b1f9e778bc4a71 100644 --- a/icbm.ino +++ b/icbm.ino @@ -11,11 +11,6 @@ void setup() { Serial.begin(115200); initializeNetwork(); - while (!eth_connected) { - Serial.print("-"); - delay(100); - } - initializeRoutes(); Serial.println("Started REST server"); initializeSNMP(); @@ -32,29 +27,15 @@ enum { }; void serial_loop() { - if (Serial.available()) { - uint8_t output = NONE; - // Read command from serial - String string = Serial.readString(); - string.toLowerCase(); - // Only two commands - if (string == "on") { - output = ON; - } else if (string == "off") { - output = OFF; - } - Serial.print(output); + if (millis() - start >= 1000) { + start = millis(); + Serial.print(eth_connected ? "." : "-"); } + } void loop() { - // Manager loop function must be called to process incoming messages snmp.loop(); restServer.handleClient(); serial_loop(); - - if (millis() - start >= 1000) { - start = millis(); - Serial.print("."); - } } diff --git a/rest.cpp b/rest.cpp index 93fb91e07c3a79556e7f3f1cde9146a21c60a57e..b0a53c106570b2bf84903a2905edbb5433773a6b 100644 --- a/rest.cpp +++ b/rest.cpp @@ -1,8 +1,9 @@ #include "rest.hpp" +#include "mpod.hpp" #include <ETH.h> #ifndef ICBM_GIT_VERSION -#define ICBM_GIT_VERSION "DEVEL" // set via -DICBM_GIT_VERSION +#define ICBM_GIT_VERSION "DEVEL" #endif #ifndef ICBM_GIT_TIMESTAMP @@ -15,6 +16,7 @@ void initializeRoutes() { restServer.on("/idn", identify); restServer.on("/restart", restart); restServer.onNotFound(notFound); + restServer.on("/send", sendSNMP); restServer.begin(); Serial.println("REST Server Started"); @@ -78,3 +80,41 @@ void restart() { restServer.send(200); ESP.restart(); } + + +enum { + ON, + OFF, + NONE, +}; + +void sendSNMP() { + uint8_t output = NONE; + bool success = false; + + for (uint8_t i = 0; i < restServer.args(); i++) { + if(restServer.argName(i) == "output") { + if(restServer.arg(i) == "on") { + output = ON; + } else if (restServer.arg(i) == "off") { + output = OFF; + } + } + } + + auto ip_addr = IPAddress(10, 42, 0 ,1); + if (output != NONE) { + SNMP::Message *snmp_msg = mpod.output(output); + snmp.send(snmp_msg, ip_addr, SNMP::PORT::SNMP); + delete snmp_msg; + success = true; + } + + String http_msg = "{\n"; + http_msg += "\"target\":\"" + String(ip_addr) + "\","; + http_msg += "\"arguments\":{\"output\":\"" + String(output) + "\"},\n"; + http_msg += "\"success\":\"" + String(success) + "\""; + http_msg += "\n}"; + + restServer.send(200, "text/json", http_msg); +} diff --git a/rest.hpp b/rest.hpp index d955917369a298df78e12cadcc9eefd278647fb9..c77f5cae2ce3b4954fba331eec70368368f52a94 100644 --- a/rest.hpp +++ b/rest.hpp @@ -4,7 +4,8 @@ extern WebServer restServer; void initializeRoutes(); -void identify(); -void notFound(); -void list(); -void restart(); + +void identify(); // /idn +void notFound(); // 404 +void restart(); // /restart +void sendSNMP(); // /send?output=on