Compare commits
6 Commits
main
...
longterm-s
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
215344d2a8 | ||
|
|
de08f0b909 | ||
|
|
ebda3f2c24 | ||
|
|
ff77dd1207 | ||
|
|
61fda5250d | ||
|
|
dd2185216a |
|
|
@ -31,5 +31,7 @@ public:
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void cyclic(unsigned long now);
|
void cyclic(unsigned long now);
|
||||||
|
|
||||||
|
int sendMessage(const String& chat_id, const String& text);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#define TELEGRAM_BOT_TOKEN "123123123123:BBCeAAewGN_wayA123VnbqRNJL4l3dpXy2S"
|
#define TELEGRAM_BOT_TOKEN "123123123123:BBCeAAewGN_wayA123VnbqRNJL4l3dpXy2S"
|
||||||
#define TELEGRAM_BOT_OWNER "321123321"
|
#define TELEGRAM_BOT_OWNER "321123321"
|
||||||
|
//#define DEBUGGER_TELEGRAM_ID "32121321" //address to send debugging information to via Telegram
|
||||||
|
|
||||||
#define WIFI_SSID "Wifi_SSID"
|
#define WIFI_SSID "Wifi_SSID"
|
||||||
#define WIFI_PASSWORD "secretPassword"
|
#define WIFI_PASSWORD "secretPassword"
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ lib_deps =
|
||||||
witnessmenow/UniversalTelegramBot @ ~1.3.0
|
witnessmenow/UniversalTelegramBot @ ~1.3.0
|
||||||
adafruit/Adafruit GFX Library @ ~1.12.3
|
adafruit/Adafruit GFX Library @ ~1.12.3
|
||||||
knolleary/PubSubClient @ ~2.8
|
knolleary/PubSubClient @ ~2.8
|
||||||
build_flags=-DTELEGRAM_DEBUG
|
; build_flags=-DTELEGRAM_DEBUG
|
||||||
; OTA update path: hostname is flipdottelegram.fritz.box
|
; OTA update path: hostname is flipdottelegram.fritz.box
|
||||||
upload_protocol = espota
|
upload_protocol = espota
|
||||||
upload_port = 172.30.30.50
|
upload_port = 172.30.30.50
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,12 @@ void Telegram::handleNewMessages(int numNewMessages)
|
||||||
String chat_id = bot.messages[i].chat_id;
|
String chat_id = bot.messages[i].chat_id;
|
||||||
String text = bot.messages[i].text;
|
String text = bot.messages[i].text;
|
||||||
|
|
||||||
|
#ifdef DEBUGGER_TELEGRAM_ID
|
||||||
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe von ") + bot.messages[i].from_name + "(" + bot.messages[i].from_id + ")" +
|
||||||
|
"im Chat " + bot.messages[i].chat_title + "(" + bot.messages[i].chat_id + ")" +
|
||||||
|
"folgenden Text empfangen:\n\n" + text);
|
||||||
|
#endif
|
||||||
|
|
||||||
String from_name = bot.messages[i].from_name;
|
String from_name = bot.messages[i].from_name;
|
||||||
if (from_name == "")
|
if (from_name == "")
|
||||||
from_name = "Guest";
|
from_name = "Guest";
|
||||||
|
|
@ -55,6 +61,9 @@ void Telegram::handleNewMessages(int numNewMessages)
|
||||||
if (DisplayText.isEmpty())
|
if (DisplayText.isEmpty())
|
||||||
{
|
{
|
||||||
bot.sendMessage(chat_id, "Display text ist leer!!");
|
bot.sendMessage(chat_id, "Display text ist leer!!");
|
||||||
|
#ifdef DEBUGGER_TELEGRAM_ID
|
||||||
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + "Display text ist leer!!");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -62,6 +71,9 @@ void Telegram::handleNewMessages(int numNewMessages)
|
||||||
Answer += DisplayText;
|
Answer += DisplayText;
|
||||||
Answer += "\".";
|
Answer += "\".";
|
||||||
bot.sendMessage(chat_id, Answer);
|
bot.sendMessage(chat_id, Answer);
|
||||||
|
#ifdef DEBUGGER_TELEGRAM_ID
|
||||||
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + Answer);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -79,6 +91,9 @@ void Telegram::handleNewMessages(int numNewMessages)
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.sendMessage(chat_id, welcome);
|
bot.sendMessage(chat_id, welcome);
|
||||||
|
#ifdef DEBUGGER_TELEGRAM_ID
|
||||||
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, String("Ich habe an ") + chat_id + " folgenden Text geschrieben:\n\n" + welcome);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -141,7 +156,10 @@ void Telegram::init()
|
||||||
serializeJson(doc, jsonString);
|
serializeJson(doc, jsonString);
|
||||||
bot.setMyCommands(jsonString);
|
bot.setMyCommands(jsonString);
|
||||||
|
|
||||||
bot.sendMessage(TELEGRAM_BOT_OWNER, "Hi, ich bin online.");
|
bot.sendMessage(TELEGRAM_BOT_OWNER, "Hi, ich bin online und brauche einen neuen Zustand.");
|
||||||
|
#ifdef DEBUGGER_TELEGRAM_ID
|
||||||
|
bot.sendMessage(DEBUGGER_TELEGRAM_ID, "Hi DEBUGGER, ich bin online und brauche einen neuen Zustand.");
|
||||||
|
#endif
|
||||||
bot_lasttime = millis();
|
bot_lasttime = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,3 +185,8 @@ void Telegram::cyclic(unsigned long now)
|
||||||
bot_lasttime = now;
|
bot_lasttime = now;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Telegram::sendMessage(const String& chat_id, const String& text)
|
||||||
|
{
|
||||||
|
return bot.sendMessage(chat_id, text);
|
||||||
|
}
|
||||||
|
|
|
||||||
119
src/main.cpp
119
src/main.cpp
|
|
@ -218,6 +218,7 @@ void loop()
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
static unsigned long lastDisplayRefresh = now;
|
static unsigned long lastDisplayRefresh = now;
|
||||||
static unsigned long lastTelegramRefresh = now;
|
static unsigned long lastTelegramRefresh = now;
|
||||||
|
static unsigned long lastWifiDisconnect = now;
|
||||||
|
|
||||||
if (WiFi.status() == WL_CONNECTED)
|
if (WiFi.status() == WL_CONNECTED)
|
||||||
{
|
{
|
||||||
|
|
@ -283,6 +284,9 @@ void loop()
|
||||||
// if wifi is not connected wait for connection
|
// if wifi is not connected wait for connection
|
||||||
if (WiFi.status() != oldWifiStatus)
|
if (WiFi.status() != oldWifiStatus)
|
||||||
{
|
{
|
||||||
|
/* remember las disconnection time to restart if needed */
|
||||||
|
lastWifiDisconnect = now;
|
||||||
|
|
||||||
/* we freshly disconnected so tell everybody that we are offline */
|
/* we freshly disconnected so tell everybody that we are offline */
|
||||||
Text = "WLAN \"";
|
Text = "WLAN \"";
|
||||||
Text += WIFI_SSID;
|
Text += WIFI_SSID;
|
||||||
|
|
@ -294,6 +298,14 @@ void loop()
|
||||||
|
|
||||||
oldWifiStatus = WiFi.status();
|
oldWifiStatus = WiFi.status();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((now - lastWifiDisconnect) > (5 /* min */ * 60 /* sec/min */ * 1000 /* millisec/sec */))
|
||||||
|
{
|
||||||
|
Serial.println("Wifi disconnected - restarting now");
|
||||||
|
|
||||||
|
delay(2000);
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* FlipDot Handling */
|
/* FlipDot Handling */
|
||||||
|
|
@ -418,12 +430,23 @@ String printClose(String Args)
|
||||||
return Text;
|
return Text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String replaceUTF8Char(const char * message, int &i, unsigned int length);
|
||||||
|
|
||||||
String printText(String Args)
|
String printText(String Args)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
auto message = Args.c_str();
|
||||||
|
auto len = Args.length();
|
||||||
|
|
||||||
|
Text = "";
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
{
|
||||||
|
Text += replaceUTF8Char(message, i, len);
|
||||||
|
}
|
||||||
|
|
||||||
Serial.print("Display would show '");
|
Serial.print("Display would show '");
|
||||||
Serial.print(Args);
|
Serial.print(Text);
|
||||||
Serial.println("'");
|
Serial.println("'");
|
||||||
Text = Args;
|
|
||||||
publishClose();
|
publishClose();
|
||||||
|
|
||||||
return Text;
|
return Text;
|
||||||
|
|
@ -445,6 +468,92 @@ String printLate30(String Args)
|
||||||
return Text;
|
return Text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String replaceUTF8Char(const char * message, int &i, unsigned int length)
|
||||||
|
{
|
||||||
|
String str = "_";
|
||||||
|
|
||||||
|
if ((message[i] < 0x7E))
|
||||||
|
{
|
||||||
|
//ASCII Char
|
||||||
|
str = message[i];
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC3) && (message[i + 1] == 0xA4))
|
||||||
|
{
|
||||||
|
str = "ae";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC3) && (message[i + 1] == 0x84))
|
||||||
|
{
|
||||||
|
str = "Ae";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC3) && (message[i + 1] == 0xB6))
|
||||||
|
{
|
||||||
|
str = "oe";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC3) && (message[i + 1] == 0x96))
|
||||||
|
{
|
||||||
|
str = "Oe";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC3) && (message[i + 1] == 0xBC))
|
||||||
|
{
|
||||||
|
str = "ue";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC3) && (message[i + 1] == 0x9C))
|
||||||
|
{
|
||||||
|
str = "Ue";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC3) && (message[i + 1] == 0x9F))
|
||||||
|
{
|
||||||
|
str = "ss";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC2) && (message[i + 1] == 0xB2))
|
||||||
|
{
|
||||||
|
str = "2";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC2) && (message[i + 1] == 0xB3))
|
||||||
|
{
|
||||||
|
str = "3";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC2) && (message[i + 1] == 0xB4))
|
||||||
|
{
|
||||||
|
str = "'";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 1) < length) && (message[i] == 0xC2) && (message[i + 1] == 0xB5))
|
||||||
|
{
|
||||||
|
str = "u";
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
else if (((i + 2) < length) && (message[i] == 0xE2) && (message[i + 1] == 0x82) && (message[i + 2] == 0xAC))
|
||||||
|
{
|
||||||
|
str = "EUR";
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
else if (message[i] & 0xC0)
|
||||||
|
{
|
||||||
|
//if utf8 char was not handled above find end of utf8 char
|
||||||
|
while (i < length)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if (!(message[i] & 0x80) || ((message[i] & 0xC0) == 0xC0))
|
||||||
|
{
|
||||||
|
i--;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
void mqttCallback(char *topic, unsigned char *message, unsigned int length)
|
void mqttCallback(char *topic, unsigned char *message, unsigned int length)
|
||||||
{
|
{
|
||||||
String MessageString;
|
String MessageString;
|
||||||
|
|
@ -452,7 +561,7 @@ void mqttCallback(char *topic, unsigned char *message, unsigned int length)
|
||||||
/* Convert all char* to Strings for simpler handling */
|
/* Convert all char* to Strings for simpler handling */
|
||||||
for (int i = 0; i < length; i++)
|
for (int i = 0; i < length; i++)
|
||||||
{
|
{
|
||||||
MessageString += (char) message[i];
|
MessageString += replaceUTF8Char((const char*) message, i, length);
|
||||||
}
|
}
|
||||||
String ToppicString(topic);
|
String ToppicString(topic);
|
||||||
|
|
||||||
|
|
@ -471,6 +580,8 @@ void mqttCallback(char *topic, unsigned char *message, unsigned int length)
|
||||||
{
|
{
|
||||||
printClose(MessageString);
|
printClose(MessageString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tele.sendMessage(TELEGRAM_BOT_OWNER, String("Display zeigt \"") + Text + "\" via MQTT");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if topic is the text topic try deserializing the json text */
|
/* if topic is the text topic try deserializing the json text */
|
||||||
|
|
@ -478,5 +589,7 @@ void mqttCallback(char *topic, unsigned char *message, unsigned int length)
|
||||||
{
|
{
|
||||||
Serial.println("Text topic received: " + MessageString);
|
Serial.println("Text topic received: " + MessageString);
|
||||||
printText(MessageString);
|
printText(MessageString);
|
||||||
|
|
||||||
|
tele.sendMessage(TELEGRAM_BOT_OWNER, String("Display zeigt \"") + Text + "\" via MQTT");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user