From 365a0f0a29c3785f05da3b1cb96af456cce6cd96 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski <cydanil@gmail.com> Date: Fri, 9 Feb 2024 12:12:39 +0100 Subject: [PATCH] Toggle an output from rest to snmp --- README.md | 18 ++++++++++++++++++ icbm.ino | 27 ++++----------------------- rest.cpp | 42 +++++++++++++++++++++++++++++++++++++++++- rest.hpp | 9 +++++---- 4 files changed, 68 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 56d5331..1aea79f 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 e104b73..5e4791b 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 93fb91e..b0a53c1 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 d955917..c77f5ca 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 -- GitLab