Compare commits

...

6 Commits

Author SHA1 Message Date
3746d1a4d6 Add license 2024-04-14 20:55:15 +02:00
1d23e6cfef Add HTTP API endpoint for raw messages
RESTful version of simple socket listener.
2024-04-12 11:59:20 +02:00
4a82fdc7f5 Extract hexDump function 2024-04-12 11:59:20 +02:00
852932a934 Fix typo 2024-04-12 03:21:30 +02:00
71facb7d18 Add socket server compatible with original software 2024-04-11 14:38:45 +02:00
4b13827803 Add temperature macro 2024-04-02 15:59:06 +02:00
5 changed files with 78 additions and 11 deletions

7
LICENSE.txt Normal file
View File

@ -0,0 +1,7 @@
Copyright 2024 Fabian Müller <fabian@fablab-altmuehlfranken.de>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -4,9 +4,11 @@
#include "credentials.h"
#include "webserver.h"
#include "socketserver.h"
#include "util.h"
static ESP8266WiFiMulti wifiMulti;
static SocketServer socketServer;
void setup() {
// serial console, for use via USB (also exposed to TXD0/RXD0 GPIOs)
@ -43,10 +45,12 @@ void setup() {
// configure HTTP endpoints
configureWebServer();
// don't forget to start the webserver!
// don't forget to start all the services
webServer.begin();
socketServer.begin();
}
void loop() {
webServer.handleClient();
socketServer.handleClient();
}

34
src/socketserver.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
class SocketServer {
public:
static constexpr int Port = 3000;
void begin() {
_server.begin();
}
void handleClient() {
auto client = _server.available();
if (client) {
Serial.println("Client connected to socket server: " + client.remoteIP().toString() + ":" + client.remotePort());
} else {
return;
}
while (client.connected()) {
while (client.available() > 0) {
auto chr = client.read();
Serial.write(chr);
Serial1.write(chr);
}
}
client.stop();
Serial.println();
Serial.println("Client disconnected");
}
private:
WiFiServer _server{Port};
};

View File

@ -60,6 +60,7 @@ enum class Macros {
Beep1 = 'q',
Beep2 = 'r',
Beep3 = 's',
Temperature = 'H',
};
// symbols should be inserted as string representations of their numeric value prefixed by ^
@ -121,7 +122,7 @@ enum class ExtendedChar {
LatinSmallLetterAWithCircumflex = 0x83,
// ä
LatinSmallLetterAWithDiaeresis = 0x84,
// á
// à
LatinSmallLetterAWithGrave = 0x85,
// å
LatinSmallLetterAWithRingAbove = 0x86,
@ -245,6 +246,19 @@ enum class ExtendedChar {
GreekSmallLetterPhi = 0xed,
};
String hexDump(String in) {
String out;
for (size_t i = 0; i < in.length(); ++i) {
auto hexChar = String(in[i], HEX);
if (hexChar.length() == 1) {
out.concat('0');
}
out.concat(hexChar);
out.concat(' ');
}
return out;
}
/*
* Message syntax:
* separator: ~
@ -298,13 +312,5 @@ void sendTextToSign(
Serial1.print(toSend);
Serial.println("Text sent to device: " + toSend);
Serial.print("Hex dump: ");
for (size_t i = 0; i < toSend.length(); ++i) {
auto hexChar = String(toSend[i], HEX);
if (hexChar.length() == 1) {
hexChar = '0' + hexChar;
}
Serial.print(hexChar + ' ');
}
Serial.println();
Serial.println("Hex dump: " + hexDump(toSend));
}

View File

@ -66,6 +66,21 @@ namespace {
sendTextToSign(text);
webServer.send(200, "text/plain", "ok");
}
// RESTful API endpoint -- returns a proper status code rather than a redirect
void handleRawApiSend() {
String toSend = webServer.arg("plain");
if (toSend.isEmpty()) {
webServer.send(404, "text/plain", "empty body");
return;
}
Serial.println("Raw message sent to device: " + toSend);
Serial.println("Hex dump: " + hexDump(toSend));
Serial1.print(toSend);
webServer.send(200, "text/plain", "ok");
}
}
void configureWebServer() {
@ -74,4 +89,5 @@ void configureWebServer() {
webServer.on("/", HTTPMethod::HTTP_GET, handleIndex);
webServer.on("/", HTTPMethod::HTTP_POST, handleFormSend);
webServer.on("/send", HTTPMethod::HTTP_POST, handleApiSend);
webServer.on("/raw", HTTPMethod::HTTP_POST, handleRawApiSend);
}