Hallo zusammen,
ich verzweifle noch...
Ich möchte auf meinem Giga Display Shield einen Bildschirmschoner einrichten, der nach 30 Sekunden aktiv und bei Berührung des Displays inaktiv wird. Ich habe das ein oder andere schon versucht, aber immer hat er was zu meckern.
Für Eure Hilfe wäre ich echt dankbar!
Hardware: Arduino Giga R1 WiFi / Arduino Giga Display Shield
Hier ist mein funktionierender Code, wo der Bildschirmschoner (schwarzes Display) integriert werden soll:
#include <Arduino_H7_Video.h>
#include <Arduino_GigaDisplayTouch.h>
#include <lvgl.h>
#include <DHT.h>
#include <SPI.h>
#include <WiFi.h>
#include "arduino_secrets.h"
////////////////////////////////////////////////////////
// WiFi connect
////////////////////////////////////////////////////////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds
const long timeoutTime = 20000;
////////////////////////////////////////////////////////
// Definition für den DHT22 Sensor
////////////////////////////////////////////////////////
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Arduino_H7_Video Display(800, 480, GigaDisplayShield);
Arduino_GigaDisplayTouch TouchDetector;
lv_obj_t *temp_bar;
lv_obj_t *hum_bar;
lv_obj_t *temp_val_label;
lv_obj_t *hum_val_label;
lv_obj_t *fan_boxes[4];
lv_obj_t *rpm_bars[4];
lv_obj_t *rpm_val_labels[4];
lv_obj_t *wifi_icon;
float sensorTemp = 0;
float sensorHum = 0;
const uint8_t FAN_COUNT = 4;
const uint8_t fanPWM_PINS[FAN_COUNT] = {9, 10, 11, 12};
const uint8_t fanRPM_PINS[FAN_COUNT] = {3, 4, 5, 6};
volatile uint32_t rpmPulseCount[FAN_COUNT] = {0};
bool lastStatePoll[FAN_COUNT] = {0, 0, 0, 0};
float fanRPM[FAN_COUNT] = {0.0};
const unsigned long measurementInterval = 1000;
unsigned long lastRPMUpdate = 0;
void printWifiStatus();
void setup() {
Serial.begin(115200);
Display.begin();
TouchDetector.begin();
lv_init();
delay(100);
while (!Serial);
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
}
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
Serial.print("WLAN verbunden an ");
Serial.println(WiFi.SSID());
server.begin();
lv_obj_t *screen = lv_scr_act();
lv_obj_set_style_bg_color(screen, lv_color_hex(0x00695C), LV_PART_MAIN);
lv_obj_set_style_text_color(screen, lv_color_black(), LV_PART_MAIN);
static lv_style_t style_big;
lv_style_init(&style_big);
lv_style_set_text_font(&style_big, &lv_font_montserrat_14);
lv_obj_t *mainCont = lv_obj_create(screen);
lv_obj_set_size(mainCont, Display.width(), Display.height());
lv_obj_set_style_bg_color(mainCont, lv_color_hex(0x00695C), LV_PART_MAIN);
lv_obj_set_layout(mainCont, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(mainCont, LV_FLEX_FLOW_ROW);
lv_obj_set_flex_align(mainCont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_t *leftCont = lv_obj_create(mainCont);
lv_obj_set_size(leftCont, 120, Display.height());
lv_obj_set_style_bg_color(leftCont, lv_color_hex(0x00695C), 0);
lv_obj_set_layout(leftCont, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(leftCont, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(leftCont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_pad_all(leftCont, 10, 0);
lv_obj_set_style_border_width(leftCont, 0, LV_PART_MAIN);
// Temperaturbalken
temp_bar = lv_bar_create(leftCont);
lv_obj_set_size(temp_bar, 50, 200);
lv_bar_set_range(temp_bar, 0, 50);
lv_bar_set_value(temp_bar, 0, LV_ANIM_OFF);
lv_obj_set_style_bg_color(temp_bar, lv_color_hex(0xE0F2F1), LV_PART_MAIN);
lv_obj_set_style_bg_color(temp_bar, lv_color_hex(0x00FF00), LV_PART_INDICATOR);
lv_obj_set_style_radius(temp_bar, 0, LV_PART_MAIN);
lv_obj_set_style_radius(temp_bar, 0, LV_PART_INDICATOR);
lv_obj_set_style_border_width(temp_bar, 0, LV_PART_MAIN);
temp_val_label = lv_label_create(temp_bar);
lv_label_set_text(temp_val_label, "--");
lv_obj_align(temp_val_label, LV_ALIGN_CENTER, 0, 0);
// Luftfeuchtigkeitsbalken
hum_bar = lv_bar_create(leftCont);
lv_obj_set_size(hum_bar, 50, 200);
lv_bar_set_range(hum_bar, 0, 100);
lv_bar_set_value(hum_bar, 0, LV_ANIM_OFF);
lv_obj_set_style_bg_color(hum_bar, lv_color_hex(0xE0F2F1), LV_PART_MAIN);
lv_obj_set_style_bg_color(hum_bar, lv_color_hex(0x0277BD), LV_PART_INDICATOR);
lv_obj_set_style_radius(hum_bar, 0, LV_PART_MAIN);
lv_obj_set_style_radius(hum_bar, 0, LV_PART_INDICATOR);
lv_obj_set_style_border_width(hum_bar, 0, LV_PART_MAIN);
hum_val_label = lv_label_create(hum_bar);
lv_label_set_text(hum_val_label, "--");
lv_obj_align(hum_val_label, LV_ALIGN_TOP_MID, 0, 0);
lv_obj_t *rightCont = lv_obj_create(mainCont);
lv_obj_set_size(rightCont, 680, Display.height());
lv_obj_set_style_bg_color(rightCont, lv_color_hex(0x00695C), 0);
lv_obj_set_layout(rightCont, LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(rightCont, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(rightCont, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
lv_obj_set_style_pad_all(rightCont, 10, 0);
lv_obj_set_style_border_width(rightCont, 0, LV_PART_MAIN);
Serial.println("Setup2");
for (int i = 0; i < FAN_COUNT; i++) {
fan_boxes[i] = lv_obj_create(rightCont);
lv_obj_set_size(fan_boxes[i], 500, 80);
lv_obj_set_style_border_width(fan_boxes[i], 2, 0);
lv_obj_set_style_border_color(fan_boxes[i], lv_color_hex(0x0000FF), 0);
lv_obj_set_layout(fan_boxes[i], LV_LAYOUT_FLEX);
lv_obj_set_flex_flow(fan_boxes[i], LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(fan_boxes[i], LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
// Lüfternummer über dem rpm_bar
lv_obj_t* fan_label = lv_label_create(fan_boxes[i]);
lv_label_set_text_fmt(fan_label, "FAN %d", i+1);
lv_obj_add_style(fan_label, &style_big, 0);
lv_obj_set_width(fan_label, LV_SIZE_CONTENT);
lv_obj_align(fan_label, LV_ALIGN_TOP_LEFT, 10, 0);
rpm_bars[i] = lv_bar_create(fan_boxes[i]);
lv_bar_set_range(rpm_bars[i], 0, 2000);
lv_bar_set_value(rpm_bars[i], 0, LV_ANIM_OFF);
lv_obj_set_size(rpm_bars[i], 480, 30);
lv_obj_set_style_bg_color(rpm_bars[i], lv_color_hex(0xAAAAAA), LV_PART_MAIN);
lv_obj_set_style_bg_color(rpm_bars[i], lv_color_hex(0x00FF00), LV_PART_INDICATOR);
lv_obj_set_style_radius(rpm_bars[i], 0, LV_PART_MAIN);
lv_obj_set_style_radius(rpm_bars[i], 0, LV_PART_INDICATOR);
rpm_val_labels[i] = lv_label_create(rpm_bars[i]);
lv_label_set_text(rpm_val_labels[i], "-- RPM");
lv_obj_add_style(rpm_val_labels[i], &style_big, 0);
lv_obj_set_width(rpm_val_labels[i], LV_SIZE_CONTENT);
lv_obj_align(rpm_val_labels[i], LV_ALIGN_LEFT_MID, 10, 0);
}
Serial.println("Setup3");
for (int i = 0; i < FAN_COUNT; i++) {
pinMode(fanPWM_PINS[i], OUTPUT);
analogWrite(fanPWM_PINS[i], 0);
pinMode(fanRPM_PINS[i], INPUT_PULLUP);
lastStatePoll[i] = digitalRead(fanRPM_PINS[i]);
}
dht.begin();
wifi_icon = lv_label_create(screen);
lv_label_set_text(wifi_icon, LV_SYMBOL_WIFI);
lv_obj_align(wifi_icon, LV_ALIGN_TOP_RIGHT, -10, 10);
}
void loop() {
lv_timer_handler();
// WLAN-Statusanzeige
if (WiFi.status() == WL_CONNECTED) {
lv_obj_set_style_text_color(wifi_icon, lv_color_hex(0x76FF03), LV_PART_MAIN);
} else {
lv_obj_set_style_text_color(wifi_icon, lv_color_hex(0x808080), LV_PART_MAIN);
}
// Polling der RPM-Pins ohne Interrupts
for (int i = 0; i < FAN_COUNT; i++) {
bool currentState = digitalRead(fanRPM_PINS[i]);
if (!lastStatePoll[i] && currentState) {
rpmPulseCount[i]++;
}
lastStatePoll[i] = currentState;
}
unsigned long currentMillis = millis();
if (currentMillis - lastRPMUpdate >= measurementInterval) {
lastRPMUpdate = currentMillis;
noInterrupts();
for (int i = 0; i < FAN_COUNT; i++) {
fanRPM[i] = (rpmPulseCount[i] * 60) / 2.0;
if (fanRPM[i] > 2000) fanRPM[i] = 2000;
rpmPulseCount[i] = 0;
}
interrupts();
}
sensorTemp = dht.readTemperature();
sensorHum = dht.readHumidity();
if (isnan(sensorTemp)) sensorTemp = 0.0;
if (isnan(sensorHum)) sensorHum = 0.0;
int pwmVal;
if (sensorTemp < 24.0) {
pwmVal = 0;
} else if (sensorTemp >= 36.0) {
pwmVal = 255;
} else {
pwmVal = map(sensorTemp, 24, 36, 0, 255);
}
for (int i = 0; i < FAN_COUNT; i++) {
analogWrite(fanPWM_PINS[i], pwmVal);
}
char buf[32];
sprintf(buf, "%.1f C", sensorTemp);
lv_label_set_text(temp_val_label, buf);
lv_bar_set_value(temp_bar, (int)sensorTemp, LV_ANIM_OFF);
int bar_height = lv_obj_get_height(temp_bar);
float max_temp = 50.0;
float ratio = sensorTemp / max_temp;
int new_y = (int)(bar_height - (bar_height * ratio));
lv_obj_set_y(temp_val_label, new_y - (lv_obj_get_height(temp_val_label) / 2));
sprintf(buf, "%.1f %%", sensorHum);
lv_label_set_text(hum_val_label, buf);
lv_bar_set_value(hum_bar, (int)sensorHum, LV_ANIM_OFF);
for (int i = 0; i < FAN_COUNT; i++) {
sprintf(buf, "%u RPM", (unsigned)fanRPM[i]);
lv_label_set_text(rpm_val_labels[i], buf);
lv_bar_set_value(rpm_bars[i], fanRPM[i], LV_ANIM_OFF);
}
WiFiClient client = server.accept(); // Auf eingehende Clients horchen
if (client) {
currentTime = millis();
previousTime = currentTime;
Serial.println("New Client.");
String currentLine = "";
header = "";
while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();
if (client.available()) {
char c = client.read();
Serial.write(c);
header += c;
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE html><html>");
client.println("<head>");
client.println("<meta charset='UTF-8'>");
client.println("<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
client.println("<title>Status Lüftersteuerung</title>");
client.println("<style>");
client.println("body { font-family: Arial, sans-serif; background-color: #f5f5f5; color: #333; margin: 0; padding: 20px; }");
client.println("h1 text-align: center; { color: #0056b3; }");
client.println("table { border-collapse: collapse; width: 100%; max-width: 600px; margin: 20px auto; }");
client.println("th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }");
client.println("th { background-color: #4CAF50; color: white; }");
client.println("tr:nth-child(even) { background-color: #f2f2f2; }");
client.println("</style>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Status Lüftersteuerung</h1>");
client.println("<table>");
client.println("<tr><th>Komponente</th><th>Wert</th></tr>");
for (int i = 0; i < FAN_COUNT; i++) {
client.print("<tr><td>Lüfter ");
client.print(i + 1);
client.print("</td><td>");
client.print(fanRPM[i]);
client.println(" RPM</td></tr>");
}
client.print("<tr><td>Temperatur</td><td>");
client.print(sensorTemp);
client.println(" °C</td></tr>");
client.print("<tr><td>Luftfeuchtigkeit</td><td>");
client.print(sensorHum);
client.println(" %</td></tr>");
client.println("</table>");
client.println("</body></html>");
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
Serial.println("Client disconnected.");
}
}
void printWifiStatus() {
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}