If Fehler mit char variable topic

Hallo zusammen
unten im Progamm Abschnitt void callback habe ich 7 "if" alle kommen immer, verstehe ich nicht, respektive ich hab nicht rausgefunden was ich falsch mache.
der topic Inhalt ist sp wie in den Ifs abgebildet....sehe vor lauter Bäume den Wald nicht? <-vermutlich ein Layer8 Fehler eines Anfängers.

Danke und Grüsse

#include <PubSubClient.h> //mqtt
#include <WiFi.h>
#include <Arduino_GFX_Library.h>
#include "HWCDC.h"
//#include <stdint.h>
#include <background.c>


#include "secrets.h"

HWCDC USBSerial;
Arduino_DataBus *bus = new Arduino_SWSPI(
    GFX_NOT_DEFINED /* DC */, 42 /* CS */,
    2 /* SCK */, 1 /* MOSI */, GFX_NOT_DEFINED /* MISO */);

Arduino_ESP32RGBPanel *rgbpanel = new Arduino_ESP32RGBPanel(
    40 /* DE */, 39 /* VSYNC */, 38 /* HSYNC */, 41 /* PCLK */,
    46 /* R0 */, 3 /* R1 */, 8 /* R2 */, 18 /* R3 */, 17 /* R4 */,
    14 /* G0 */, 13 /* G1 */, 12 /* G2 */, 11 /* G3 */, 10 /* G4 */, 9 /* G5 */,
    5 /* B0 */, 45 /* B1 */, 48 /* B2 */, 47 /* B3 */, 21 /* B4 */,
    1 /* hsync_polarity */, 10 /* hsync_front_porch */, 8 /* hsync_pulse_width */, 50 /* hsync_back_porch */,
    1 /* vsync_polarity */, 10 /* vsync_front_porch */, 8 /* vsync_pulse_width */, 20 /* vsync_back_porch */);
Arduino_RGB_Display *gfx = new Arduino_RGB_Display(
    480 /* width */, 480 /* height */, rgbpanel, 2 /* rotation */, true /* auto_flush */,
    bus, GFX_NOT_DEFINED /* RST */, st7701_type1_init_operations, sizeof(st7701_type1_init_operations));


// Set your Static IP address
IPAddress local_IP(192, 168, 178, 111);
IPAddress gateway(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(192,168,178,1);
const char* ssid = SECRET_SSID;
const char* password = SECRET_PASS;

//mqtt
const char broker[] = "192.168.178.230";
int        port     = 1883;
const char topic[]  = "globaledaten";

//EthernetClient ethClient;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;





void setup(void)
{
  USBSerial.begin(115200);
  USBSerial.println("begin....");
  
  if (!gfx->begin()) USBSerial.println("gfx->begin() failed!");
  gfx->draw16bitRGBBitmap(0, 0, (uint16_t*)hgrund,480,480);
  
  //gfx->schriftgrösse einstellen
  gfx->setTextSize(1);
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);

//wifi-------------------------------------------------------------------------
   // WIFI Configures static IP address
   if (!WiFi.config(local_IP, gateway, subnet, primaryDNS)) {
      gfx->setCursor(10, 10); gfx->println("WiFi Settings failed!");
      USBSerial.println("WiFi Settings failed!");
   }
   WiFi.begin(ssid, password);
     while (WiFi.status() != WL_CONNECTED) {
       delay(500);
       gfx->setCursor(10, 10); gfx->setTextColor(RGB565_BLACK,RGB565_WHITE); gfx->println("Connecting to WiFi...    ");
       USBSerial.println("Connecting to WiFi..");
       delay(1000);
     }
     if (WiFi.isConnected()){
       //wlan connected
       gfx->setCursor(10, 10); gfx->setTextColor(RGB565_BLACK,RGB565_WHITE); gfx->println("Connected to WiFi        ");
       USBSerial.println("Connected to WiFi");
       }
      else
       {
       //wlan disconnected
       gfx->setCursor(10, 10); gfx->setTextColor(RGB565_BLACK,RGB565_WHITE); gfx->println("Connected to WiFi failed!");
       USBSerial.println("Connecting to WiFi failed*");
       }
//mqtt
  client.setServer(broker, port);
  client.setCallback(callback);


  
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);
}


void callback(char* topic, byte* payload, unsigned int length) {
 gfx->setTextSize(3);gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);
 String mqttmsg;
  USBSerial.print("Message arrived on topic: ");
  USBSerial.print(topic);
   
  for (int i = 0; i < length; i++) {
   mqttmsg +=  (char) payload[i];
  }
  
  if (topic = "globaledaten/pvtotalleistung") {gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);gfx->setCursor(40, 347);gfx->println(mqttmsg);}
  if (topic = "globaledaten/batterieleistung") {gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);gfx->setCursor(238, 380);gfx->println(mqttmsg);}
  if (topic = "globaledaten/batterieSOC") {gfx->setTextColor(RGB565_BLACK,RGB565_GREEN);gfx->setCursor(238, 300);gfx->println(mqttmsg);}
  if (topic = "globaledaten/shellyleistungsmessung") {gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);gfx->setCursor(238, 440);gfx->println(mqttmsg);}
  if (topic = "globaledaten/temperatur") {gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);gfx->setCursor(70, 70);gfx->println(mqttmsg);}
  if (topic = "globaledaten/windaktuell") {gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);gfx->setCursor(200, 70);gfx->println(mqttmsg);}
  if (topic = "globaledaten/windmaximum") {gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);gfx->setCursor(344, 70);gfx->println(mqttmsg);}
   
   USBSerial.println(mqttmsg);
}

void reconnect() {
  gfx->setTextSize(1);gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);
  // Loop until we're reconnected
  while (!client.connected()) {
    USBSerial.print("Attempting MQTT connection...");
     gfx->setCursor(10, 20); gfx->setTextColor(RGB565_BLACK,RGB565_WHITE); gfx->println("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("Globaldata")) {
      USBSerial.println("connected");
      gfx->setCursor(10, 20); gfx->setTextColor(RGB565_BLACK,RGB565_WHITE); gfx->println("MQTT connected.             ");
      // Subscribe
      client.subscribe("globaledaten/pvtotalleistung");
      client.subscribe("globaledaten/batterieleistung");
      client.subscribe("globaledaten/batterieSOC");
      client.subscribe("globaledaten/shellyleistungsmessung");
      client.subscribe("globaledaten/temperatur");
      client.subscribe("globaledaten/windaktuell");
      client.subscribe("globaledaten/windmaximum");
    } else {
      USBSerial.print("failed, rc=");
      USBSerial.print(client.state());
      USBSerial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(2000);
    }
  }
  }


void loop()
{
if (!client.connected()) reconnect();
  client.loop();
  delay(100); // 5 seconds
}


  1. Ein = ist eine Zuweisung und kein Vergleich.
  2. Wenn du zwei Texte (char*) vergleichen willst, geht das mit der Funktion strcmp , nicht mit ==

Did you finish your other topic with the same subject?

Hallo mcihael_x

super, den Hinweis brauchte ich. Danke dafür.

Nur ein Auszug für andere, wenn auch mal einer ein Bsp. sucht für ein Textvergleich mit (char*) und "globaledaten/pvtotalleistung":

if (strcmp(topic, "globaledaten/pvtotalleistung") == 0) {gfx->setTextColor(RGB565_BLACK,RGB565_WHITE);gfx->setCursor(40, 347);gfx->println(mqttmsg);}

Es empfiehlt sich dafür, und für die anderen Texte, eine Konstante zu füllen und diese zu verwenden.

Bei dieser if Orgie wäre if else vielleicht besser, weil bei einem „Treffer“ die verbleibenden if´s nicht mehr ausgeführt werden. Außerdem ist es unnötig, jedes mal die Farbe neu zu setzen, wenn sie sich eh nicht ändert.

Copy & Paste Programmierung…

Einmal ist grün dabei :slight_smile:

Wenn man die Texte und Werte in ein Array/struct packt, dann könnte das jeweils ein for-Schleifen 3-Zeiler sowohl in reconnect() als auch im callback werden.

@my_xy_projekt: Danke für den Input: Tönt spannend mit dem array, aber da muss ich passen als Anfänger.
@Kai-R: Ebenso Danke für dein Input, mir ist jedoch unklar warum sollte er die restlichen IF´s nicht mehr abarbeiten?

Weil nach dem ersten feststellen der gültigen Bedingung der Rest nicht mehr gültig ist.
Danach kehrst Du aus der Funktion zurück und rufst sie mit einem neuen Topic auf.

Für was für einen Controller ist das geschrieben?
ESP32?

Dann brauchst du ein gutes C++ Grundlagenbuch.

Alternativ: (zäher zu lesen)

ein ESP32-S3-touch-LCD-4 ist im Einsatz hier.

Hätte ich versucht zu kompilieren, aber es fehlen mir noch dein background und hwdc - damit musst Du selbst probieren, ob das funktioniert.

Wenn dem so ist, dann findest Du auch, wie das geht:

#include <PubSubClient.h> //mqtt
#include <WiFi.h>
#include <Arduino_GFX_Library.h>
#include "HWCDC.h"
//#include <stdint.h>
#include <background.c>

#include "secrets.h"

HWCDC USBSerial;
Arduino_DataBus *bus = new Arduino_SWSPI(
        GFX_NOT_DEFINED /* DC */, 42 /* CS */,
        2 /* SCK */, 1 /* MOSI */, GFX_NOT_DEFINED /* MISO */);

Arduino_ESP32RGBPanel *rgbpanel = new Arduino_ESP32RGBPanel(
        40 /* DE */, 39 /* VSYNC */, 38 /* HSYNC */, 41 /* PCLK */,
        46 /* R0 */, 3 /* R1 */, 8 /* R2 */, 18 /* R3 */, 17 /* R4 */,
        14 /* G0 */, 13 /* G1 */, 12 /* G2 */, 11 /* G3 */, 10 /* G4 */, 9 /* G5 */,
        5 /* B0 */, 45 /* B1 */, 48 /* B2 */, 47 /* B3 */, 21 /* B4 */,
        1 /* hsync_polarity */, 10 /* hsync_front_porch */, 8 /* hsync_pulse_width */, 50 /* hsync_back_porch */,
        1 /* vsync_polarity */, 10 /* vsync_front_porch */, 8 /* vsync_pulse_width */, 20 /* vsync_back_porch */);
Arduino_RGB_Display *gfx = new Arduino_RGB_Display(
        480 /* width */, 480 /* height */, rgbpanel, 2 /* rotation */, true /* auto_flush */,
        bus, GFX_NOT_DEFINED /* RST */, st7701_type1_init_operations, sizeof(st7701_type1_init_operations));

// Set your Static IP address
IPAddress local_IP(192, 168, 178, 111);
IPAddress gateway(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(192, 168, 178, 1);
const char *ssid = SECRET_SSID;
const char *password = SECRET_PASS;

//mqtt
const char broker[] = "192.168.178.230";
int port = 1883;
const char topic[] = "globaledaten";

//EthernetClient ethClient;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

const char *pvTotal = "globaledaten/pvtotalleistung";
const char *batPower = "globaledaten/batterieleistung";
const char *batSOC = "globaledaten/batterieSOC";
const char *shellyPower = "globaledaten/shellyleistungsmessung";
const char *tempVal = "globaledaten/temperatur";
const char *windVal = "globaledaten/windaktuell";
const char *windMax = "globaledaten/windmaximum";

struct MYTOPIC
{
  const char *tpc;
  const uint32_t frGnd;
  const uint32_t bckGnd;
  const uint16_t hPos;
  const uint16_t vPos;
};

MYTOPIC mytopic[]
{
  {pvTotal, RGB565_BLACK, RGB565_WHITE, 40, 347,},
  {batPower, RGB565_BLACK, RGB565_WHITE, 238, 380,},
  {batSOC, RGB565_BLACK, RGB565_GREEN, 238, 300,},
  {shellyPower, RGB565_BLACK, RGB565_WHITE, 238, 440,},
  {tempVal, RGB565_BLACK, RGB565_WHITE, 70, 70,},
  {windVal, RGB565_BLACK, RGB565_WHITE, 200, 70,},
  {windMax, RGB565_BLACK, RGB565_WHITE, 344, 70,},
};

constexpr uint8_t topicNums {sizeof(mytopic) / sizeof(mytopic[0])};

void setup(void)
{
  USBSerial.begin(115200);
  USBSerial.println("begin....");

  if (!gfx->begin())
  {
    USBSerial.println("gfx->begin() failed!");
  }

  gfx->draw16bitRGBBitmap(0, 0, (uint16_t*)hgrund, 480, 480);
  //gfx->schriftgrösse einstellen
  gfx->setTextSize(1);
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);

  //wifi-------------------------------------------------------------------------
  // WIFI Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS))
  {
    gfx->setCursor(10, 10); gfx->println("WiFi Settings failed!");
    USBSerial.println("WiFi Settings failed!");
  }

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    gfx->setCursor(10, 10); gfx->setTextColor(RGB565_BLACK, RGB565_WHITE); gfx->println("Connecting to WiFi...    ");
    USBSerial.println("Connecting to WiFi..");
    delay(1000);
  }

  if (WiFi.isConnected())
  {
    //wlan connected
    gfx->setCursor(10, 10); gfx->setTextColor(RGB565_BLACK, RGB565_WHITE); gfx->println("Connected to WiFi        ");
    USBSerial.println("Connected to WiFi");
  }
  else
  {
    //wlan disconnected
    gfx->setCursor(10, 10); gfx->setTextColor(RGB565_BLACK, RGB565_WHITE); gfx->println("Connected to WiFi failed!");
    USBSerial.println("Connecting to WiFi failed*");
  }

  //mqtt
  client.setServer(broker, port);
  client.setCallback(callback);
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);
}

void callback(char* topic, byte* payload, unsigned int length)
{
  gfx->setTextSize(3); gfx->setTextColor(RGB565_BLACK, RGB565_WHITE);
  String mqttmsg;
  USBSerial.print("Message arrived on topic: ");
  USBSerial.print(topic);

  for (int i = 0; i < length; i++)
  {
    mqttmsg += (char) payload[i];
  }

  for (byte b = 0; b < topicNums; b++)
  {
    if (strcmp(topic, mytopic[b].tpc) == 0)
    {
      gfx->setTextColor(mytopic[b].fgnd, mytopic[b].bgnd);
      gfx->setCursor(mytopic[b].hpos, mytopic[b].vpos]);
      gfx->println(mqttmsg);
    }
  }

  USBSerial.println(mqttmsg);
}

void reconnect()
{
  gfx->setTextSize(1); gfx->setTextColor(RGB565_BLACK, RGB565_WHITE);

  // Loop until we're reconnected
  while (!client.connected())
  {
    USBSerial.print("Attempting MQTT connection...");
    gfx->setCursor(10, 20); gfx->setTextColor(RGB565_BLACK, RGB565_WHITE); gfx->println("Attempting MQTT connection...");

    // Attempt to connect
    if (client.connect("Globaldata"))
    {
      USBSerial.println("connected");
      gfx->setCursor(10, 20); gfx->setTextColor(RGB565_BLACK, RGB565_WHITE); gfx->println("MQTT connected.             ");

      // Subscribe
      for (byte b = 0; b < topicNums; b++)
      { client.subscribe(mytopic[b].tpc); }
    }
    else
    {
      USBSerial.print("failed, rc=");
      USBSerial.print(client.state());
      USBSerial.println(" try again in 5 seconds");
      // Wait before retrying
      delay(2000);
    }
  }
}

void loop()
{
  if (!client.connected())
  {
    reconnect();
  }

  client.loop();
  delay(100); // 5 seconds
}

ich hab noch nicht das umschreiben von payload nach string verstanden und den abbruch der Schleife bei gültigem strcmp auch noch nicht drin - erstmal will ich nur, dass es wie vorher funktioniert.

//Edit einen Typo im Code berichtigt

hi, wow, meeega, vielen dank. ja ich glaub auf den ersten blick verstehe ich wie du das machst. muss ich in aller ruhe mal hinein ziehen. schönen abend.

Bitte.

Ich hab noch kurz in der Bahnfahrt was gebaut.
Der lehnt sich an meinen ersten an, aber schon um einiges gekürzt.
Ich hab nämlich Deine Konstante topic[] gefunden und die jetzt mal knallhart mitverwendet.

Wenn der auch geht, dann hab ich hoffentlich auf meinem kleinen Display nichts übersehen.
Und als nächstes löse ich dann die delays() auf und schon bekommst Du Code, der auch noch was anderes machen kann.... :slight_smile:

Wenn Fragen sind: frag!

aufklappen

//edit: Die Forensoftware wollte noch eine Leerzeile um den Code zu erkennen....

#include <PubSubClient.h> //mqtt
#include <WiFi.h>
#include <Arduino_GFX_Library.h>
#include "HWCDC.h"
//#include <stdint.h>
#include <background.c>

#include "secrets.h"

HWCDC USBSerial;
Arduino_DataBus *bus = new Arduino_SWSPI(
        GFX_NOT_DEFINED /* DC */, 42 /* CS */,
        2 /* SCK */, 1 /* MOSI */, GFX_NOT_DEFINED /* MISO */);

Arduino_ESP32RGBPanel *rgbpanel = new Arduino_ESP32RGBPanel(
        40 /* DE */, 39 /* VSYNC */, 38 /* HSYNC */, 41 /* PCLK */,
        46 /* R0 */, 3 /* R1 */, 8 /* R2 */, 18 /* R3 */, 17 /* R4 */,
        14 /* G0 */, 13 /* G1 */, 12 /* G2 */, 11 /* G3 */, 10 /* G4 */, 9 /* G5 */,
        5 /* B0 */, 45 /* B1 */, 48 /* B2 */, 47 /* B3 */, 21 /* B4 */,
        1 /* hsync_polarity */, 10 /* hsync_front_porch */, 8 /* hsync_pulse_width */, 50 /* hsync_back_porch */,
        1 /* vsync_polarity */, 10 /* vsync_front_porch */, 8 /* vsync_pulse_width */, 20 /* vsync_back_porch */);
Arduino_RGB_Display *gfx = new Arduino_RGB_Display(
        480 /* width */, 480 /* height */, rgbpanel, 2 /* rotation */, true /* auto_flush */,
        bus, GFX_NOT_DEFINED /* RST */, st7701_type1_init_operations, sizeof(st7701_type1_init_operations));

// Set your Static IP address
IPAddress local_IP(192, 168, 178, 111);
IPAddress gateway(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(192, 168, 178, 1);
const char *ssid = SECRET_SSID;
const char *password = SECRET_PASS;

//mqtt
const char broker[] = "192.168.178.230";
int port = 1883;

//EthernetClient ethClient;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

const char *topicDatabase = "Globaldata";

const char *topicRoot = "globaledaten";

const char *pvTotal = "pvtotalleistung";
const char *batPower = "batterieleistung";
const char *batSOC = "batterieSOC";
const char *shellyPower = "shellyleistungsmessung";
const char *tempVal = "temperatur";
const char *windVal = "windaktuell";
const char *windMax = "windmaximum";

struct MYTOPIC
{
  const char *tpc;
  const uint32_t frGnd;
  const uint32_t bckGnd;
  const uint16_t hPos;
  const uint16_t vPos;
};

MYTOPIC mytopic[]
{
  {pvTotal, RGB565_BLACK, RGB565_WHITE, 40, 347,},
  {batPower, RGB565_BLACK, RGB565_WHITE, 238, 380,},
  {batSOC, RGB565_BLACK, RGB565_GREEN, 238, 300,},
  {shellyPower, RGB565_BLACK, RGB565_WHITE, 238, 440,},
  {tempVal, RGB565_BLACK, RGB565_WHITE, 70, 70,},
  {windVal, RGB565_BLACK, RGB565_WHITE, 200, 70,},
  {windMax, RGB565_BLACK, RGB565_WHITE, 344, 70,},
};

constexpr uint8_t topicNums {sizeof(mytopic) / sizeof(mytopic[0])};

void setup(void)
{
  USBSerial.begin(115200);
  USBSerial.println("begin....");

  if (!gfx->begin())
  {
    USBSerial.println("gfx->begin() failed!");
  }

  gfx->draw16bitRGBBitmap(0, 0, (uint16_t*)hgrund, 480, 480);
  //gfx->schriftgrösse einstellen
  gfx->setTextSize(1);
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);

  //wifi-------------------------------------------------------------------------
  // WIFI Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS))
  {
    gfx->setCursor(10, 10); gfx->println("WiFi Settings failed!");
    USBSerial.println("WiFi Settings failed!");
  }

  WiFi.begin(ssid, password);
  gfx->setTextColor(RGB565_BLACK, RGB565_WHITE);
  gfx->setCursor(10, 10);
  gfx->println("Connecting to WiFi...    ");

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    USBSerial.println("Connecting to WiFi..");
    delay(1000);
  }

  gfx->setCursor(10, 10);
  gfx->print("Connected to WiFi ");
  USBSerial.print("Connect to WiFi");

  if (WiFi.isConnected())
  {
    //wlan connected
    gfx->println("       ");
    USBSerial.println();
  }
  else
  {
    //wlan disconnected
    gfx->println("failed!");
    USBSerial.println(" failed*");
  }

  //mqtt
  client.setServer(broker, port);
  client.setCallback(callback);
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);
}

void callback(char* topic, byte* payload, unsigned int length)
{
  String mqttmsg;

  for (int i = 0; i < length; i++)
  { mqttmsg += (char) payload[i]; }

  USBSerial.print("Message arrived on topic: ");
  USBSerial.print(topic);
  USBSerial.println(mqttmsg);
  gfx->setTextSize(3);

  for (byte b = 0; b < topicNums; b++)
  {
    char buf[strlen(topicRoot) + strlen(mytopic[b].tpc) +1] = {"\0"};  // Größe temporärer Puffer
    strcat(buf, topicRoot);                                            // Topic zusammenbauen
    strcat(buf, "/");
    strcat(buf, mytopic[b].tpc);

    if (strcmp(topic, buf) == 0)                                       // Zusammenbau entspricht Übergabe
    {
      gfx->setTextColor(mytopic[b].fgnd, mytopic[b].bgnd);             // Ausgabe
      gfx->setCursor(mytopic[b].hpos, mytopic[b].vpos]);
      gfx->println(mqttmsg);
    }
  }
}

void reconnect()
{
  gfx->setTextSize(1);
  gfx->setTextColor(RGB565_BLACK, RGB565_WHITE);

  // Loop until we're reconnected
  while (!client.connected())
  {
    USBSerial.print("Attempting MQTT connection...");
    gfx->setCursor(10, 20);
    gfx->println("Attempting MQTT connection...");

    // Attempt to connect
    if (client.connect(database))
    {
      USBSerial.println("connected");
      gfx->setCursor(10, 20);
      gfx->println("MQTT connected.             ");

      // Subscribe
      for (uint8_t b = 0; b < topicNums; b++)                                          // Alle Topics zusammenbauen
      {
        char buf[strlen(topicRoot) + strlen(mytopic[b].tpc) +1] = {"\0"};              // temporärer Puffer
        strcat(buf, topicRoot);
        strcat(buf, "/");
        strcat(buf, mytopic[b].tpc);
        client.subscribe(buf);
        // Serial.println(buf);  // debug
      }
    }
    else
    {
      USBSerial.print("failed, rc=");
      USBSerial.print(client.state());
      USBSerial.println(" try again");
      // Wait before retrying
      delay(2000);
    }
  }
}

void loop()
{
  if (!client.connected())
  { reconnect(); }

  client.loop();
  delay(100); 
}

@my_xy_projekt

Dankeschön. Ich habs mal eingebettet. Dabei sind mir zwei Dinge aufgefallen:

gfx->setTextColor(mytopic[b].frGnd, mytopic[b].bckGnd);
gfx->setCursor(mytopic[b].hPos, mytopic[b].vPos);

Dann gings auch sehr schön. Hab dann noch die Einheit für jedes Topic ergänzt.
Bin dann auf zwei Dinge gestoßen. In meinem Code hab ich noch abhängig der shellyPower zwei Grafiken. Einmal für "-"Werte und einmal "+" Werte. Dito Batterieleistung. Dazu dann das neg. Vorzeichen wegfallen lassen. Da steh ich mit der wirklich wirklich sehr eleganten Lösung noch an. Hab dann mal probiert die Grafik einzubringen für die zwei Fälle. Naja gescheitert nach 2Stunden mit Müdigkeit. Aber ich konnte wenigstens mal alles andere lösen, dank Deinen Erklärungen. Nun bin ich am Überlegen wie weiter. Nahe am Ziel ob ich das noch ausarbeiten soll, der "Elegantheit".
Schönen Abend

Ok, das hab ich übersehen. Wie gesagt, ich kann das nicht kompilieren. Dann wärs mir aufgefallen :wink:

Da musst Du genauer beschreiben, was Du da hast und willst.
Ich werde das nachher nochmal in die Hand nehmen und das delay() da rausoperieren.

Für Deine Grafikgeschichten findet sich bestimmt auch eine Lösung.

@my_xy_projekt
Ich denke dich interessiert das auch ziemlich, darum schau in Anhang. Deins ist da nicht eingebettet, das hab ich separat in einem weiteren Sketch. Der Anhang ist komplett bis auf secret.h und zeigt somit das ganze Projekt mit allen Grafiken. (Interessiert vielleicht auch andere später).
*Wenn die Batterie liefert kommt die Grafik "batterieentladen" zum Einsatz neben dem Batteriesymbol. Wenn der Wert batterieleistung positiv ist.
*Wenn die Batterie ladet kommt die "batterieladen" Grafik zum Einsatz neben dem Batteriesymbol. Wenn der Wert batterieleistung negativ ist.
*Dito zwischen Haus und EW, jeweils mit einer Grafik. Abhängig vom Shellywert der den Stromverbrauch misst.
Home_Display.zip (33,5 KB)

Schönen Tag

Ich hab mal kurz drüber geschaut - das mit der Bitmap 480*480 ist aber auch ein spannendes Konstrukt.

Was mir noch nicht ganz klar ist:
Du willst die beiden Zeichen darstellen, was ich als solches für machbar halte.
Aber: Was ist da für ein Hintergrund? Der müsste ja dann mit aktuallisiert werden...

Was steht denn tatsächlich in mqttmsg?

Dann sehe ich, dass Du Einheiten mit anzeigen willst. Kann man machen. ICH würde die dann gleich passend zum topic in das struct MYTOPIC integrieren. Kann ja nur W, C und m/s sein... Der Zusammenbau ist dann zur Laufzeit kein Problem.

Hast Du mal ein Bild vom Display, wie das aussehen soll, damit ich mir das vorstellen kann, was wohin gehört?

Wurde so sagen der TO hat sich nicht mit den Möglichkeiten der Arduino GFX auseinander gesetzt, die Bittet nämlich die Möglichkeit Schriftarten vom U8G2 so wie Adafruit GFX zu nutzen, dann brauch man nicht den ganzen Krempel mit den Bitmaps.
Dazu kann er einfach mit den Schriftzeichen sogar Animation machen. Das Arbeiten mit Textgöße sieht so wie so nicht so toll aus, und ja man kann sogar Adafruit und U8G2 gleich zeitig nutzen, ich wurde das auf keinen Fall so machen wie es ist, der ESP32S3 ist ein N16R8 Teil der hat genügend Ressourcen um rumspielen, Die Arduino GFX ist ziemlich schnell und verbraucht Relativ wenig Speicher.

Erst wenn Du den hier am laufen hast, mach ich noch weiter - denke dran: alles was ich tu ist raten. Ich hab nunmal nur die Bahnfahrten....

#include <PubSubClient.h> //mqtt
#include <WiFi.h>
#include <Arduino_GFX_Library.h>
#include "HWCDC.h"
//#include <stdint.h>
#include <background.c>

#include "secrets.h"

HWCDC USBSerial;
Arduino_DataBus *bus = new Arduino_SWSPI(
        GFX_NOT_DEFINED /* DC */, 42 /* CS */,
        2 /* SCK */, 1 /* MOSI */, GFX_NOT_DEFINED /* MISO */);

Arduino_ESP32RGBPanel *rgbpanel = new Arduino_ESP32RGBPanel(
        40 /* DE */, 39 /* VSYNC */, 38 /* HSYNC */, 41 /* PCLK */,
        46 /* R0 */, 3 /* R1 */, 8 /* R2 */, 18 /* R3 */, 17 /* R4 */,
        14 /* G0 */, 13 /* G1 */, 12 /* G2 */, 11 /* G3 */, 10 /* G4 */, 9 /* G5 */,
        5 /* B0 */, 45 /* B1 */, 48 /* B2 */, 47 /* B3 */, 21 /* B4 */,
        1 /* hsync_polarity */, 10 /* hsync_front_porch */, 8 /* hsync_pulse_width */, 50 /* hsync_back_porch */,
        1 /* vsync_polarity */, 10 /* vsync_front_porch */, 8 /* vsync_pulse_width */, 20 /* vsync_back_porch */);
Arduino_RGB_Display *gfx = new Arduino_RGB_Display(
        480 /* width */, 480 /* height */, rgbpanel, 2 /* rotation */, true /* auto_flush */,
        bus, GFX_NOT_DEFINED /* RST */, st7701_type1_init_operations, sizeof(st7701_type1_init_operations));

// Set your Static IP address
IPAddress local_IP(192, 168, 178, 111);
IPAddress gateway(192, 168, 178, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(192, 168, 178, 1);

const char *ssid = SECRET_SSID;
const char *password = SECRET_PASS;


//mqtt
const char *broker = "192.168.178.230";
const uint16_t port = 1883;

//EthernetClient ethClient;
WiFiClient espClient;
PubSubClient client(espClient);

// Static Text für Display & Debug
const char *wifi = "WiFi";
const char *failed = "failed";
const char *Settings = "Settings";
const char *connectet = "connectet";

// Database & MQTT Text
const char *topicDatabase = "Globaldata";

const char *topicRoot = "globaledaten";

const char *pvTotal = "pvtotalleistung";
const char *batPower = "batterieleistung";
const char *batSOC = "batterieSOC";
const char *shellyPower = "shellyleistungsmessung";
const char *tempVal = "temperatur";
const char *windVal = "windaktuell";
const char *windMax = "windmaximum";

//
struct MYTOPIC
{
  const char *tpc;
  const uint32_t frGnd;
  const uint32_t bckGnd;
  const uint16_t hPos;
  const uint16_t vPos;
  const char *unit;
};

MYTOPIC mytopic[]
{
  {pvTotal, RGB565_BLACK, RGB565_WHITE, 40, 347, "W",},
  {batPower, RGB565_BLACK, RGB565_WHITE, 238, 380, "W",},
  {batSOC, RGB565_BLACK, RGB565_GREEN, 238, 300, "W",},
  {shellyPower, RGB565_BLACK, RGB565_WHITE, 238, 440, "W",},
  {tempVal, RGB565_BLACK, RGB565_WHITE, 70, 70, "C",},
  {windVal, RGB565_BLACK, RGB565_WHITE, 200, 70, "m/s",},
  {windMax, RGB565_BLACK, RGB565_WHITE, 344, 70, "m/s",},
};

constexpr uint8_t topicNums {sizeof(mytopic) / sizeof(mytopic[0])};

constexpr uint32_t _ONESECOND {1000};
void setup(void)
{
  USBSerial.begin(115200);
  USBSerial.println("begin....");

  if (!gfx->begin())
  { USBSerial.println("gfx->begin() failed!"); }

  gfx->draw16bitRGBBitmap(0, 0, (uint16_t*)hgrund, 480, 480);
  gfx->setTextSize(1);
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);

  //wifi-------------------------------------------------------------------------
  // WIFI Configures static IP address
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS))
  {
    gfx->setCursor(10, 10);
    printGfxWithDebug("WiFi Settings failed!");
  }

  WiFi.begin(ssid, password);
  gfx->setTextColor(RGB565_BLACK, RGB565_WHITE);
  gfx->setCursor(10, 10);
  printGfxWithDebug("Connecting...");
  
  while (WiFi.status() != WL_CONNECTED)
  {
    USBSerial.print('.');
    delay(100);
  }

  USBSerial.println();
  
  gfx->setCursor(10, 10);
  printGfxWithDebug("Connected to WiFi ");

  if (WiFi.isConnected())     //wlan connected
  { printGfxWithDebug("       ", true); }
  else     //wlan disconnected
  { printGfxWithDebug(failed, true); }

  client.setServer(broker, port);
  client.setCallback(callback);
  //gfx->fillScreen(RGB565_BLACK);
  gfx->setTextColor(RGB565_WHITE);
}

void callback(const char* topic, const byte* payload, const uint8_t length)
{
  char mqttmsg[length + 1] = "\0";                                        // Puffer für Nachricht

  for (uint8_t i = 0; i < length; i++)                                    // Puffer füllen
  { mqttmsg[i] = static_cast < char > (payload[i]); }

  mqttmsg[length] = '\0';                                                 // sicherstellen, dass Ende gesetzt ist.
  USBSerial.print("Message arrived on topic: ");
  USBSerial.print(topic);
  USBSerial.println(mqttmsg);
  gfx->setTextSize(3);

  for (byte b = 0; b < topicNums; b++)
  {
    char buf[strlen(topicRoot) + strlen(mytopic[b].tpc) +
             strlen(mytopic[b].unit) +1] = {"\0"};                                // Größe temporärer Puffer
    sprintf(buf, "%s%s%s%s", topicRoot, "/", mytopic[b].tpc, mytopic[b].unit);    // Topic zusammenbauen

    if (strcmp(topic, buf) == 0)                                          // Zusammenbau entspricht Übergabe
    { printGfxMqttmsg(mytopic[b], mqttmsg); break;}                       // Ausgabe & Abbruch der Schleife, da nichts mehr weiter passen kann
  }
}

bool checkConnect()                                                        // Die Funktion lässt sich noch weiter ausbauen!
{
  bool isConnect = false;                                                  // lokaler Merker - default: nicht am Netzwerk und DB angemeldet
  static uint32_t lastFailTime = 0;                                        // Merker, wann letzter Connectversuch
  static uint32_t connectIntervall = _ONESECOND / 2;                       // Festlegung für Reconnect
  gfx->setTextSize(1);
  gfx->setTextColor(RGB565_BLACK, RGB565_WHITE);

  if (millis() - lastFailTime > connectIntervall)
  {
    gfx->setCursor(10, 20);

    if (!client.connected())                                               // Wenn kein Netzwerk-Connecct
    {
      printGfxWithDebug("Attempting MQTT connection...");
      connectIntervall = _ONESECOND / 2;
    }
    else                                                                    // Wenn Netzwerkverbindung steht
    {
      // Attempt to connect
      if (client.connect(database))                                         // Wenn Datenbankverbindung steht
      {
        printGfxWithDebug("MQTT connected.             ");
        subscriptTopics();
        isConnect = true;                                                   // Database ist erreichbar
      }
      else                                                                  // Keine Datenbankverbindung (Aber Netzwerk)
      {
        USBSerial.print("failed, rc=");
        USBSerial.print(client.state());
        USBSerial.println(" try again");
        connectIntervall = _ONESECOND * 2;
      }
    }

    if (!isConnect)                                                         // Es ist ein Connectfehler aufgetreten
    { lastFailTime = millis(); }                                            // merke Zeit, wann der letzte fehlerhafte DB-Connectversuch
  }

  return isConnect;
}

void subscriptTopics()
{
  for (uint8_t b = 0; b < topicNums; b++)                                    // Alle Topics zusammenbauen
  {
    char buf[strlen(topicRoot) + strlen(mytopic[b].tpc) +1] = {"\0"};        // temporärer Puffer
    sprintf(buf, "%s%s%s", topicRoot, "/", mytopic[b].tpc);                  // Topic zusammenbauen
    client.subscribe(buf);
    // USBSerial.print("Subscrib: "); USBSerial.println(buf);                  // debug
  }
}

void printGfxMqttmsg(MYTOPIC &t, const char *msg)                            // Ausgabe
{
  gfx->setTextColor(t.frGnd, t.bckGnd);
  gfx->setCursor(t.hPos, t.vPos);
  gfx->println(msg);
}

void printGfxWithDebug(const char *msg, const bool lineEnd)
{
  printGfxWithDebug(msg);

  if (lineEnd)
  {
    gfx->println();
    USBSerial.println();
  }
}

void printGfxWithDebug(const char *msg)
{
  gfx->println(msg);
  USBSerial.println(msg);
}

void loop()
{
  if (checkConnect() == true)
  { client.loop(); }

  heartbeat(_ONESECOND);
}

void heartbeat(const uint32_t intervall)
{
  static uint32_t lastTick = 0;

  if (millis() - lastTick > intervall)
  {
    USBSerial.println("*** Tik ***");
    lastTick = millis();
  }
}