Added download and parsing for daily texts

This commit is contained in:
Blue Fox 2022-08-21 18:54:42 +02:00
parent 0b7c66a684
commit 429e400e32
6 changed files with 81 additions and 11 deletions

0
LICENSE.md Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

14
display.ino Normal file → Executable file
View File

@ -33,15 +33,23 @@ String getRow(String text, int width, int letter_width, int text_of_row_x) { //
}
// remove special characters (ä,ö,ü,ß)
String removeSpecialChars(String text) {
String replaceSpecialChars(String text) {
if(text[0] == ' ') { text = text.substring(1,text.length()-1); }
text.replace("ä", "ae");
text.replace("Ä", "AE");
text.replace("ä", "ae");
text.replace("Ä", "AE");
text.replace("ö", "oe");
text.replace("Ö", "OE");
text.replace("ö", "oe");
text.replace("Ö", "OE");
text.replace("ü", "ue");
text.replace("Ü", "UE");
text.replace("ü", "ue");
text.replace("Ü", "UE");
text.replace("ß", "ss");
text.replace("", "SS");
text.replace("ß", "ss");
return text;
}
@ -49,8 +57,8 @@ String removeSpecialChars(String text) {
// show the daily text on the epd
void showDailyText(String losung, String lehrtext, String losungPosition, String lehrtextPosition) {
// clean the strings for
losung = removeSpecialChars(losung);
lehrtext = removeSpecialChars(lehrtext);
losung = replaceSpecialChars(losung);
lehrtext = replaceSpecialChars(lehrtext);
// position vars
int text_x = 0;

62
http_grab.ino Executable file
View File

@ -0,0 +1,62 @@
#include <HTTPClient.h>
String textBeginString = "<b>";
String textEndString = "</b><br>";
String sourceBeginString = textEndString; // because source begins where text ends
String sourceEndString = "</font></p>";
String getHTML(String url) {
String result = "";
if(WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(url); // initialize the http instance
int httpCode = http.GET(); // get the page; httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
result = http.getString();
}
} else {
Serial.printf("[HTTP] GET: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
return result;
}
String getLosungFromHTML(String html) {
int losungBeginIndex = html.indexOf(textBeginString)+textBeginString.length();
int losungEndIndex = html.indexOf(textEndString);
return html.substring(losungBeginIndex, losungEndIndex);
}
String getLehrtextFromHTML(String html) {
String losung = getLosungFromHTML(html);
int losungIndex = html.indexOf(losung);
int lehrtextBeginIndex = html.indexOf(textBeginString, losungIndex)+textBeginString.length();
int lehrtextEndIndex = html.indexOf(textEndString, losungIndex+losung.length()+textEndString.length()); // calculating is needed for better performance and to get the second index, not the first
return html.substring(lehrtextBeginIndex, lehrtextEndIndex);
}
String getLosungSourceFromHTML(String html) { // get the source of losung in given html
int losungSourceBeginIndex = html.indexOf(sourceBeginString)+sourceBeginString.length();
int losungSourceEndIndex = html.indexOf(sourceEndString);
return html.substring(losungSourceBeginIndex, losungSourceEndIndex);
}
String getLehrtextSourceFromHTML(String html) { // get the source of losung in given html
String losungSource = getLosungSourceFromHTML(html);
int losungSourceIndex = html.indexOf(losungSource);
int lehrtextSourceBeginIndex = html.indexOf(sourceBeginString, losungSourceIndex)+sourceBeginString.length();
int lehrtextSourceEndIndex = html.indexOf(sourceEndString, losungSourceIndex+losungSource.length()+sourceEndString.length()); // calculating is needed for better performance and to get the second index, not the first
return html.substring(lehrtextSourceBeginIndex, lehrtextSourceEndIndex);
}

View File

@ -32,11 +32,11 @@ void setup() {
}
Serial.println("[INFO] Initialized e-Paper!");
String losung = "Gott spricht: Ich will für Israel wie der Tau sein, dass es blüht wie eine Lilie.";
String lehrtext = "Ich bin der Weinstock, ihr seid die Reben. Wer in mir bleibt und ich in ihm, der bringt viel Frucht; denn ohne mich könnt ihr nichts tun.";
showDailyText(losung, lehrtext, "Hosea 14,6", "Johannes 15,5");
connectWiFi();
String html = getHTML("https://www.losungen.de/fileadmin/media-losungen/heute/2022/0821.html");
showDailyText(getLosungFromHTML(html), getLehrtextFromHTML(html), getLosungSourceFromHTML(html), getLehrtextSourceFromHTML(html));
}
void loop() {
connectWiFi();
}

8
wifi.ino Normal file → Executable file
View File

@ -3,12 +3,12 @@
void connectWiFi() { // connect to the wifi with the above defined credentials
if(WiFi.status() == WL_CONNECTED) { return; } // return if not connected
Serial.println("Connecting to WiFi...");
Serial.println("[WiFi] Connecting to WiFi...");
WiFi.begin(wiFiSSID, wiFiPSK);
delay(5000); // pause the program 5 secs to give time for connection
if(WiFi.status() != WL_CONNECTED) { return; }
Serial.printf("Connected to WiFi %s, got IP-Adress ", wiFiSSID);
delay(10000); // pause the program 5 secs to give time for connection
if(WiFi.status() != WL_CONNECTED) { Serial.printf("[WiFi] Failed connecting to WiFi \"%s\".", wiFiSSID); return; }
Serial.printf("[WiFi] Connected to WiFi \"%s\", got IP-Adress ", wiFiSSID);
Serial.println(WiFi.localIP());
return;