Bei dem Versuch, die Statemachine zu umgehen, habe ich mir für die OLED-Anzeigen nun nach etwas Tüftelei folgendes ausgedacht (Wasserfluss-Interrupt aus Gründen der Übersichtlichkeit temporär herausgenommen):
// OLED Test-Sketch without delay on ESP32 Devkit v1
#define MIN_LOOP_DELAY 900
#define SCREEN_MILLISECONDS 50
float serialInterval = 30000; // Einmal alle 30 Sekunden = 30000ms
unsigned long oldSerialTime; // Timer for serial-prints
float oledInterval = 2500; // Einmal alle 2,5 Sekunden = 2500ms
unsigned long oldOledTime; // Timer for OLEDs
byte oledChange;
unsigned long resetLoops;
//
// OLED /////////////////////////////////////////////////////////////////////////////////////////////////
#include <U8g2lib.h> // OLED U8G-Library
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2_1(U8G2_R0);
U8G2_SSD1306_128X64_NONAME_2_HW_I2C u8g2_2(U8G2_R0);
//
// WIFI /////////////////////////////////////////////////////////////////////////////////////////////////
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "ssid1";
const char* password = "password1";
const char* ssid2 = "ssid2" // Alternative WiFi
const char* password2 = "password2"; // Alternative Password
IPAddress ip(192, 178, 18, 12); // ESP32 static IP address configuration
IPAddress gateway(192, 178, 18, 1); // IP Address of WiFi Router
IPAddress subnet(255, 255, 255, 0); // Subnet mask
IPAddress dns1(192, 178, 18, 1); // DNS1
IPAddress dns2(188, 229, 114, 28); // DNS2
//
// TIME LIBRARY /////////////////////////////////////////////////////////////////////////////////////////////////
#include <time.h>
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600; // Adjust the UTC offset for your timezone in milliseconds
const int daylightOffset_sec = 3600; // If daylight saving time: set it to 3600. Otherwise, set it to 0.
int second;
int minute;
int hour;
int day;
int month;
int year;
int weekday;
struct tm timeinfo;
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(115200);
oldSerialTime = serialInterval;
oldOledTime = oledInterval;
oledChange = 1;
resetLoops = 1;
// OLED /////////////////////////////////////////////////////////////////////////////////////////////////
u8g2_1.setI2CAddress(0x3C * 2); // I2C address OLED1
u8g2_2.setI2CAddress(0x3D * 2); // I2C address OLED2
u8g2_1.begin();
u8g2_2.begin();
u8g2_1.clearBuffer(); // Clear the internal memory
u8g2_2.clearBuffer();
WiFi.mode(WIFI_OFF);
WiFi.config(ip, gateway, subnet, dns1, dns2);
WiFi.mode(WIFI_STA);
delay(50);
Serial.println("");
Serial.println("============================================");
Serial.print("Connecting to WiFi ONE..");
WiFi.begin(ssid, password);
unsigned int wifitrials = 0;
while (WiFi.status() != WL_CONNECTED && wifitrials <= 5) {
delay(1000);
Serial.print(".");
wifitrials++;
}
Serial.println(" ");
if (WiFi.status() == WL_CONNECTED) {
Serial.print("ESP32 connected to the WiFi network ONE with IP: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("WiFi network NOT connected!");
delay(500);
Serial.print("Trying alternative WiFi network TWO..");
WiFi.begin(ssid2, password2);
unsigned int wifi2trials = 0;
while (WiFi.status() != WL_CONNECTED && wifi2trials <= 5) {
delay(1000);
Serial.print(".");
wifi2trials++;
}
Serial.println(" ");
if (WiFi.status() == WL_CONNECTED) {
Serial.print("ESP32 connected to the WiFi network TWO with IP: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("WiFi network STILL NOT connected!");
}
}
Serial.println(" ");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
unsigned long nowTime = millis();
if (nowTime - oldSerialTime >= serialInterval) {
printLocalTime();
Serial.println("============== TESTSKETCH ========================================================");
Serial.print("Schleifen: "); Serial.println(resetLoops);
Serial.println("");
oldSerialTime = millis();
}
if(nowTime - oldOledTime >= oledInterval) {
if(oledChange == 1) {
u8g2_1.firstPage();
do {
u8g2_1.setFont(u8g2_font_7x14B_tr);
u8g2_1.setCursor(5, 20);
u8g2_1.print("LINKS 1 EINS");
u8g2_1.setCursor(5, 35);
u8g2_1.print("LINKS 1 ZWEI");
u8g2_1.setCursor(5, 50);
u8g2_1.print("LINKS 1 DREI");
} while(u8g2_1.nextPage());
u8g2_2.firstPage();
do {
u8g2_2.setFont(u8g2_font_7x14B_tr);
u8g2_2.setCursor(5, 20);
u8g2_2.print("RECHTS 1 EINS");
u8g2_2.setCursor(5, 35);
u8g2_2.print("RECHTS 1 ZWEI");
u8g2_2.setCursor(5, 50);
u8g2_2.print("RECHTS 1 DREI");
} while(u8g2_2.nextPage());
oledChange++;
} else if(oledChange == 2) {
u8g2_1.firstPage();
do {
u8g2_1.setCursor(5, 20);
u8g2_1.print("LINKS 2 EINS");
u8g2_1.setCursor(5, 35);
u8g2_1.print("LINKS 2 ZWEI");
u8g2_1.setCursor(5, 50);
u8g2_1.print("LINKS 2 DREI");
} while(u8g2_1.nextPage());
u8g2_2.firstPage();
do {
u8g2_2.setCursor(5, 20);
u8g2_2.print("RECHTS 2 EINS");
u8g2_2.setCursor(5, 35);
u8g2_2.print("RECHTS 2 ZWEI");
u8g2_2.setCursor(5, 50);
u8g2_2.print("RECHTS 2 DREI");
} while(u8g2_2.nextPage());
oledChange++;
} else if(oledChange == 3) {
u8g2_1.firstPage();
do {
u8g2_1.setCursor(5, 20);
u8g2_1.print("LINKS 3 EINS");
u8g2_1.setCursor(5, 35);
u8g2_1.print("LINKS 3 ZWEI");
u8g2_1.setCursor(5, 50);
u8g2_1.print("LINKS 3 DREI");
} while(u8g2_1.nextPage());
u8g2_2.firstPage();
do {
u8g2_2.setCursor(5, 20);
u8g2_2.print("RECHTS 3 EINS");
u8g2_2.setCursor(5, 35);
u8g2_2.print("RECHTS 3 ZWEI");
u8g2_2.setCursor(5, 50);
u8g2_2.print("RECHTS 3 DREI");
} while(u8g2_2.nextPage());
oledChange = 1;
}
oldOledTime = millis();
}
resetLoops++;
delay(MIN_LOOP_DELAY);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
void printLocalTime() {
if(!getLocalTime(&timeinfo)) {
Serial.println("Failed to obtain time");
//return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); // %A: day of week, %B: month of year, %D: day of month, %Y: year, %H: hour, %M: minutes, %S: seconds
second = timeinfo.tm_sec;
minute = timeinfo.tm_min;
hour = timeinfo.tm_hour;
day = timeinfo.tm_mday;
month = timeinfo.tm_mon + 1;
year = timeinfo.tm_year + 1900;
weekday = timeinfo.tm_wday;
}
Um die schnell zunehmenden Loops auszubremsen, verwende ich MIN_LOOP_DELAY mit z.B. 900ms Zeitverzögerung: Wenn die Loops und aktive Schaltungen gezählt werden und als Rechengrundlage für den Prozentsatz der aktiven Laufzeit herangezogen wird, müssen die Variablen entsprechend in der Lage sein, die hohe Anzahl der Durchläufe auch aufnehmen zu können.
Kann das obige Beispiel mit festen Zeitfenstern für Serial.prints und den OLED-Anzeigen optimiert werden?