Compare commits

12 Commits

Author SHA1 Message Date
burned42 60a82b896a add status topic and publish after sending text to sign 2024-10-22 19:28:39 +02:00
burned42 165540c120 reformat comments 2024-10-02 17:55:52 +02:00
burned42 ad4ee16f19 use mdns hostname instead of fqdn 2024-10-02 17:53:47 +02:00
burned42 610f86750b before sending the text to the sign, translate any unicode characters to codepage 437 representation 2024-10-02 17:50:53 +02:00
burned42 c78fde5d9f update credentials.h example file 2024-09-10 21:04:28 +02:00
burned42 34bd9746d1 add MQTT and OTA update support 2024-09-10 20:37:41 +02:00
fmueller 3746d1a4d6 Add license 2024-04-14 20:55:15 +02:00
fmueller 1d23e6cfef Add HTTP API endpoint for raw messages
RESTful version of simple socket listener.
2024-04-12 11:59:20 +02:00
fmueller 4a82fdc7f5 Extract hexDump function 2024-04-12 11:59:20 +02:00
fmueller 852932a934 Fix typo 2024-04-12 03:21:30 +02:00
fmueller 71facb7d18 Add socket server compatible with original software 2024-04-11 14:38:45 +02:00
fmueller 4b13827803 Add temperature macro 2024-04-02 15:59:06 +02:00
7 changed files with 233 additions and 23 deletions
+7
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.
+4 -3
View File
@@ -13,10 +13,11 @@ platform = espressif8266
framework = arduino framework = arduino
monitor_speed = 115200 monitor_speed = 115200
upload_speed = 500000 upload_speed = 500000
upload_protocol = espota
upload_port = led-marquee-sign
# Serial1 (sign data) maps to D4 by default # Serial1 (sign data) maps to D4 by default
[env:d1_mini] [env:d1_mini]
board = d1_mini board = d1_mini
lib_deps = adafruit/Adafruit MQTT Library@^2.5.8
[env:nodemcuv2] lib_ignore = WiFi101
board = nodemcuv2
+5
View File
@@ -5,4 +5,9 @@ namespace credentials {
const String psk = "my PSK"; const String psk = "my PSK";
const String mdnsHostname = "led-marquee-sign"; const String mdnsHostname = "led-marquee-sign";
const String mqtt_hostname = "mqtt-broker";
const uint16_t mqtt_port = 1883;
const String mqtt_username = "user";
const String mqtt_password = "pass";
} }
+68 -8
View File
@@ -1,12 +1,31 @@
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h> #include <ESP8266WiFiMulti.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "credentials.h" #include "credentials.h"
#include "webserver.h" #include "webserver.h"
#include "socketserver.h"
#include "util.h" #include "util.h"
static ESP8266WiFiMulti wifiMulti; static ESP8266WiFiMulti wifiMulti;
static SocketServer socketServer;
#define AIO_SERVER credentials::mqtt_hostname.c_str()
#define AIO_SERVERPORT credentials::mqtt_port
#define AIO_USERNAME credentials::mqtt_username.c_str()
#define AIO_KEY credentials::mqtt_password.c_str()
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Subscribe mqtt_subscribe_topic = Adafruit_MQTT_Subscribe(&mqtt, "homeassistant/marquee_sign/text/set");
Adafruit_MQTT_Publish mqtt_publish_topic = Adafruit_MQTT_Publish(&mqtt, "homeassistant/marquee_sign/text/status");
void MQTT_connect();
void setup() { void setup() {
// serial console, for use via USB (also exposed to TXD0/RXD0 GPIOs) // serial console, for use via USB (also exposed to TXD0/RXD0 GPIOs)
@@ -18,7 +37,10 @@ void setup() {
Serial1.begin(9600); Serial1.begin(9600);
// configure WiFi and connect to the network // configure WiFi and connect to the network
WiFi.mode(WIFI_STA);
WiFi.hostname(credentials::mdnsHostname);
wifiMulti.addAP(credentials::ssid.c_str(), credentials::psk.c_str()); wifiMulti.addAP(credentials::ssid.c_str(), credentials::psk.c_str());
Serial.println("Connecting to Wi-Fi network..."); Serial.println("Connecting to Wi-Fi network...");
while (wifiMulti.run() != WL_CONNECTED) { while (wifiMulti.run() != WL_CONNECTED) {
delay(1000); delay(1000);
@@ -30,23 +52,61 @@ void setup() {
Serial.print(", IP address: "); Serial.print(", IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
ArduinoOTA.begin();
mqtt.subscribe(&mqtt_subscribe_topic);
// might be handy to have the IP written on the sign // might be handy to have the IP written on the sign
sendTextToSign(WiFi.localIP().toString()); sendTextToSign(WiFi.localIP().toString());
// publish this service using mDNS so that it's easy to find it in the LAN
if (MDNS.begin(credentials::mdnsHostname)) {
Serial.println("mDNS started up successfully");
} else {
Serial.println("Error: failed to start mDNS");
}
// configure HTTP endpoints // configure HTTP endpoints
configureWebServer(); configureWebServer();
// don't forget to start the webserver! // don't forget to start all the services
webServer.begin(); webServer.begin();
socketServer.begin();
} }
void loop() { void loop() {
webServer.handleClient(); webServer.handleClient();
socketServer.handleClient();
ArduinoOTA.handle();
MQTT_connect();
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
const char* signText = (char *)mqtt_subscribe_topic.lastread;
sendTextToSign(signText);
mqtt_publish_topic.publish(signText);
}
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
uint8_t retries = 6;
// connect will return 0 for connected
while ((ret = mqtt.connect()) != 0) {
mqtt.disconnect();
// wait 10s till next try
for (int i = 0; i < 10000; i += 5) {
ArduinoOTA.handle();
delay(5);
}
retries--;
if (retries == 0) {
ESP.restart();
}
}
} }
+34
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};
};
+99 -12
View File
@@ -1,5 +1,9 @@
#pragma once #pragma once
#include <map>
#include <set>
#include <unordered_map>
// TODO: reverse engineer builtin and custom bitmaps // TODO: reverse engineer builtin and custom bitmaps
// seems like our sign listens to messages on this address // seems like our sign listens to messages on this address
@@ -60,6 +64,7 @@ enum class Macros {
Beep1 = 'q', Beep1 = 'q',
Beep2 = 'r', Beep2 = 'r',
Beep3 = 's', Beep3 = 's',
Temperature = 'H',
}; };
// symbols should be inserted as string representations of their numeric value prefixed by ^ // symbols should be inserted as string representations of their numeric value prefixed by ^
@@ -121,7 +126,7 @@ enum class ExtendedChar {
LatinSmallLetterAWithCircumflex = 0x83, LatinSmallLetterAWithCircumflex = 0x83,
// ä // ä
LatinSmallLetterAWithDiaeresis = 0x84, LatinSmallLetterAWithDiaeresis = 0x84,
// á // à
LatinSmallLetterAWithGrave = 0x85, LatinSmallLetterAWithGrave = 0x85,
// å // å
LatinSmallLetterAWithRingAbove = 0x86, LatinSmallLetterAWithRingAbove = 0x86,
@@ -245,6 +250,95 @@ enum class ExtendedChar {
GreekSmallLetterPhi = 0xed, GreekSmallLetterPhi = 0xed,
}; };
std::unordered_map<uint16_t, uint8_t> createUnicodeToCP437Map() {
std::unordered_map<uint16_t, uint8_t> cp437_map;
cp437_map[0x00E4] = 0x84; // ä in UTF-16
cp437_map[0xC3A4] = 0x84; // ä in UTF-8
cp437_map[0x00F6] = 0x94; // ö in UTF-16
cp437_map[0xC3B6] = 0x94; // ö in UTF-8
cp437_map[0x00FC] = 0x81; // ü in UTF-16
cp437_map[0xC3BC] = 0x81; // ü in UTF-8
cp437_map[0x00C4] = 0x8E; // Ä in UTF-16
cp437_map[0xC384] = 0x8E; // Ä in UTF-8
cp437_map[0x00D6] = 0x99; // Ö in UTF-16
cp437_map[0xC396] = 0x99; // Ö in UTF-8
cp437_map[0x00DC] = 0x9A; // Ü in UTF-16
cp437_map[0xC39C] = 0x9A; // Ü in UTF-8
cp437_map[0x00DF] = 0xE1; // ß in UTF-16
cp437_map[0xC39F] = 0xE1; // ß in UTF-8
cp437_map[0x20AC] = 0xB1; // € in UTF-16
cp437_map[0xE282AC] = 0xB1; // € in UTF-8 (3 Bytes)
cp437_map[0x00B5] = 0xE6; // µ in UTF-16
cp437_map[0xC2B5] = 0xE6; // µ in UTF-8
return cp437_map;
}
String convertToCP437(const String& input) {
std::unordered_map<uint16_t, uint8_t> cp437_map = createUnicodeToCP437Map();
String output = "";
size_t len = input.length();
for (size_t i = 0; i < len; i++) {
char ch = input[i];
// UTF-8 detection: check for 3 byte UTF-8
if ((ch & 0xF0) == 0xE0) {
if (i + 2 < len) {
uint32_t utf8_char = ((uint8_t)input[i] << 16) | ((uint8_t)input[i + 1] << 8) | (uint8_t)input[i + 2];
// skip 2 additional bytes
i += 2;
if (cp437_map.find(utf8_char) != cp437_map.end()) {
output += static_cast<char>(cp437_map[utf8_char]);
continue;
}
}
}
// UTF-8 detection: check for 2 byte UTF-8
else if ((ch & 0xE0) == 0xC0) {
if (i + 1 < len) {
uint16_t utf8_char = ((uint8_t)input[i] << 8) | (uint8_t)input[i + 1];
// skip second byte
i++;
if (cp437_map.find(utf8_char) != cp437_map.end()) {
output += static_cast<char>(cp437_map[utf8_char]);
continue;
}
}
}
// single UTF-16 character (or plain ANSI character)
else if ((uint8_t)ch < 0x80) {
// keep ASCII as is
output += ch;
}
else {
uint16_t utf16_char = (uint8_t)ch;
// check if character is in UTF-16 range, fallback to ?
if (cp437_map.find(utf16_char) != cp437_map.end()) {
output += static_cast<char>(cp437_map[utf16_char]);
} else {
output += '?';
}
}
}
return output;
}
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: * Message syntax:
* separator: ~ * separator: ~
@@ -273,7 +367,6 @@ enum class ExtendedChar {
* - stay time: 4 * - stay time: 4
* - brightness: bright * - brightness: bright
*/ */
void sendTextToSign( void sendTextToSign(
const String &text, const String &text,
const Method method = Method::Cyclic, const Method method = Method::Cyclic,
@@ -293,18 +386,12 @@ void sendTextToSign(
Serial.println("Sending text: \"" + text + "\""); Serial.println("Sending text: \"" + text + "\"");
String toSend = "~" + String(SIGN_ADDRESS, DEC) + "~f01" + (char) method + "\\" + (char) brightness + "\\Y" + String(speed, DEC) + "\\Z" + String(stayTime, DEC) + "\\" + (char) fontType + text + "\r\r\r"; String translatedText = convertToCP437(text);
String toSend = "~" + String(SIGN_ADDRESS, DEC) + "~f01" + (char) method + "\\" + (char) brightness + "\\Y" + String(speed, DEC) + "\\Z" + String(stayTime, DEC) + "\\" + (char) fontType + translatedText + "\r\r\r";
Serial1.print(toSend); Serial1.print(toSend);
Serial.println("Text sent to device: " + toSend); Serial.println("Text sent to device: " + toSend);
Serial.print("Hex dump: "); Serial.println("Hex dump: " + hexDump(toSend));
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();
} }
+16
View File
@@ -66,6 +66,21 @@ namespace {
sendTextToSign(text); sendTextToSign(text);
webServer.send(200, "text/plain", "ok"); 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() { void configureWebServer() {
@@ -74,4 +89,5 @@ void configureWebServer() {
webServer.on("/", HTTPMethod::HTTP_GET, handleIndex); webServer.on("/", HTTPMethod::HTTP_GET, handleIndex);
webServer.on("/", HTTPMethod::HTTP_POST, handleFormSend); webServer.on("/", HTTPMethod::HTTP_POST, handleFormSend);
webServer.on("/send", HTTPMethod::HTTP_POST, handleApiSend); webServer.on("/send", HTTPMethod::HTTP_POST, handleApiSend);
webServer.on("/raw", HTTPMethod::HTTP_POST, handleRawApiSend);
} }