Skip to content
Snippets Groups Projects
Commit 365a0f0a authored by Cyril Danilevski's avatar Cyril Danilevski
Browse files

Toggle an output from rest to snmp

parent 070fbb4e
No related branches found
No related tags found
1 merge request!2Initial MPOD feature
Pipeline #130993 passed
......@@ -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
......
......@@ -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(".");
}
}
#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);
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment