Hallo bin ganz neu in Sachen Arduino und habe auch nicht so die Programmiererfahrung.
Kann mir zwar aus Vorlagen was zusammenbasteln, aber etwas von 0 an programmieren klappt noch nicht.
Ich möchte mir eine Anzeige für meine Solaranlage basteln mit mehreren 1602 Displays.
Habe auch ein Code gefunden der mit einem Display super funktioniert, aber ich schaffe es nicht die MQTT topics auf mehrere Displays zu verteilen.
MQTT Server läuft einwandfrei nur bei der Programmierung des ESP32 hapert es. Wenn mir da jemand helfen könnte wäre das super.
Bin ich richtig informiert das ich maximal 6 Displays an einen ESP32 anschließen kann zwecks Adressierung
Das ist mein momentaner Code:
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h> // This library is already built in to the Arduino IDE
#include <LiquidCrystal_I2C.h> //This library you can add via Include Library > Manage Library >
LiquidCrystal_I2C lcd_pverzeugung(0x27, 16, 2);
LiquidCrystal_I2C lcd_verbrauch(0x26, 16, 2);
LiquidCrystal_I2C lcd_akkuladezustand(0x25, 16, 2);
/*0x27 means the address of this 1602 I2C LCD display,different lcd may have the different address,
if the LCD do not work,please read below post about how to get the right address for your I2C LCD dispaly:
http://osoyoo.com/2014/12/07/16x2-i2c-liquidcrystal-displaylcd/ */
// Update these with values suitable for your network.
const char* ssid = "Bärenhöhle";//put your own wifi ssid here
const char* password = "xxxxxx";// put your wifi password here
//const char* mqtt_server = "broker.mqttdashboard.com";// choose your mqtt server
const char* mqtt_server = "192.168.115.7";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(100);
// We start by connecting to a WiFi network
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Command from MQTT broker is : [");
Serial.print(topic);
Serial.println();
Serial.print(" publish data is:");
lcd_pverzeugung.clear();
{
for(int i=0;i<length;i++)
{
Serial.print((char)payload[i]);
lcd_pverzeugung.setCursor(0, 0);
lcd_pverzeugung.print("PV Erzeugung"); // Start Print text to Line 1
lcd_pverzeugung.setCursor(i, 1);
lcd_pverzeugung.write((char)payload[i]);
lcd_verbrauch.setCursor(0, 0);
lcd_verbrauch.print("Verbrauch"); // Start Print text to Line 1
lcd_verbrauch.setCursor(i, 1);
lcd_verbrauch.write((char)payload[i]);
lcd_akkuladezustand.setCursor(0, 0);
lcd_akkuladezustand.print("Akku Ladung"); // Start Print text to Line 1
lcd_akkuladezustand.setCursor(i, 1);
lcd_akkuladezustand.write((char)payload[i]);
}
}
Serial.println();
} //end callback
void reconnect() {
// Loop until we're reconnected
while (!client.connected())
{
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
//if you MQTT broker has clientID,username and password
//please change following line to if (client.connect(clientId,userName,passWord))
if (client.connect(clientId.c_str()))
{
Serial.println("connected");
//once connected to MQTT broker, subscribe command if any
client.subscribe("pverzeugung");
// client.subscribe("einspeisung");
// client.subscribe("bezug");
client.subscribe("verbrauch");
client.subscribe("akkuladezustand");
// client.subscribe("akkuladung");
// client.subscribe("akkuentladung");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 6 seconds before retrying
delay(6000);
}
}
} //end reconnect()
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
lcd_pverzeugung.init(); // initializing the LCD
lcd_pverzeugung.backlight(); // Enable or Turn On the backlight
lcd_verbrauch.init(); // initializing the LCD
lcd_verbrauch.backlight(); // Enable or Turn On the backlight
lcd_akkuladezustand.init(); // initializing the LCD
lcd_akkuladezustand.backlight(); // Enable or Turn On the backlight
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.setCallback(callback);
client.loop();
}






