Also hab den Code jetzt ausprobiert aber meine werte stimmen nicht. Ich denke die variable ist auch falsch denn ich sende nicht nur Zahlen sondern auch Buchstaben und %
Die erste Zeile ist auch im Loop, das sieht man auch mit einem kurzen Flackern am Display bekommt man diese Zeile auch ohne Loop hin?
/* ___ ___ ___ _ _ ___ ___ ____ ___ ____
* / _ \ /___)/ _ \| | | |/ _ \ / _ \ / ___) _ \| \
*| |_| |___ | |_| | |_| | |_| | |_| ( (__| |_| | | | |
* \___/(___/ \___/ \__ |\___/ \___(_)____)___/|_|_|_|
* (____/
* Subscribe message from remote MQTT client and dispaly the message on the I2C led
* Tutorial URL http://osoyoo.com/2017/05/21/nodemcu-lesson-18-i2c-1602-lcd-mqtt/
* CopyRight www.osoyoo.com
*/
#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 setup() {
delay(1000);
Serial.begin(115200);
Serial.println("\nStart");
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() {
char t1[] = "pverzeugung";
byte p1[] = {7, 7, 3};
unsigned int l = sizeof(p1);
callback( t1, p1, l );
char t2[] = "verbrauch";
byte p2[] = {4, 7, 1, 1};
l = sizeof(p2);
callback( t2, p2, l );
char t3[] = "akkuentladung";
byte p3[] = {4, 2};
l = sizeof(p3);
callback( t3, p3, l );
delay(10000);
}
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:");
if ( !strncmp (topic, "pverzeugung", sizeof(topic)) ) // http://www.cplusplus.com/reference/cstring/strncmp/
{
lcd_pverzeugung.clear();
lcd_pverzeugung.setCursor(0, 0);
lcd_pverzeugung.print("PV Erzeugung");
lcd_pverzeugung.setCursor(0, 1);
for (byte i = 0; i < length; i++)
{
Serial.print(payload[i]);
lcd_pverzeugung.print(payload[i]);
}
}
if ( !strncmp (topic, "verbrauch", sizeof(topic)) )
{
lcd_verbrauch.clear();
lcd_verbrauch.setCursor(0, 0);
lcd_verbrauch.print("Verbrauch");
lcd_verbrauch.setCursor(0, 1);
for (byte i = 0; i < length; i++)
{
Serial.print(payload[i]);
lcd_verbrauch.print(payload[i]);
}
}
if ( !strncmp (topic, "akkuentladung", sizeof(topic)) )
{
lcd_akkuladezustand.clear();
lcd_akkuladezustand.setCursor(0, 0);
lcd_akkuladezustand.print("Akku Ladung");
lcd_akkuladezustand.setCursor(0, 1);
for (byte i = 0; i < length; i++)
{
Serial.print(payload[i]);
lcd_akkuladezustand.print(payload[i]);
}
}
Serial.println();
} //end callback
17:35:38.541 -> Connecting to Bärenhöhle
17:35:39.140 -> .........
17:35:43.118 -> WiFi connected
17:35:43.118 -> IP address:
17:35:43.118 -> 192.168.115.154
17:35:46.347 -> Attempting MQTT connection...connected
17:35:46.947 -> Command from MQTT broker is : [pverzeugung
17:35:46.947 -> publish data is:1009 W
17:35:47.365 -> Command from MQTT broker is : [pverzeugung
17:35:47.365 -> publish data is:1004 W
17:35:47.737 -> Command from MQTT broker is : [pverzeugung
17:35:47.737 -> publish data is:1006 W
17:35:48.802 -> Command from MQTT broker is : [pverzeugung
17:35:48.802 -> publish data is:1035 W
17:35:49.312 -> Command from MQTT broker is : [pverzeugung
17:35:49.312 -> publish data is:1033 W
17:35:49.916 -> Command from MQTT broker is : [pverzeugung
17:35:49.916 -> publish data is:1030 W
17:35:51.260 -> Command from MQTT broker is : [pverzeugung
17:35:51.260 -> publish data is:1029 W
17:35:56.893 -> Command from MQTT broker is : [pverzeugung
17:35:56.893 -> publish data is:1028 W
17:36:02.301 -> Command from MQTT broker is : [pverzeugung
17:36:02.301 -> publish data is:1017 W
17:36:02.948 -> Command from MQTT broker is : [pverzeugung
17:36:02.948 -> publish data is:987 W
17:36:03.271 -> Command from MQTT broker is : [pverzeugung
17:36:03.271 -> publish data is:988 W
17:36:03.595 -> Command from MQTT broker is : [pverzeugung
17:36:03.595 -> publish data is:986 W
17:36:04.057 -> Command from MQTT broker is : [pverzeugung
17:36:04.057 -> publish data is:1015 W
17:36:04.471 -> Command from MQTT broker is : [pverzeugung
17:36:04.471 -> publish data is:1016 W
17:36:04.840 -> Command from MQTT broker is : [pverzeugung
17:36:04.840 -> publish data is:1018 W
17:36:05.256 -> Command from MQTT broker is : [pverzeugung
17:36:05.256 -> publish data is:987 W
17:36:05.579 -> Command from MQTT broker is : [pverzeugung
17:36:05.579 -> publish data is:986 W
17:36:05.901 -> Command from MQTT broker is : [pverzeugung
17:36:05.901 -> publish data is:984 W
17:36:06.226 -> Command from MQTT broker is : [pverzeugung
17:36:06.271 -> publish data is:1015 W
17:36:07.518 -> Command from MQTT broker is : [pverzeugung
17:36:07.518 -> publish data is:987 W
17:36:07.886 -> Command from MQTT broker is : [pverzeugung
17:36:07.886 -> publish data is:986 W
17:36:08.669 -> Command from MQTT broker is : [pverzeugung
17:36:08.669 -> publish data is:987 W
17:36:09.870 -> Command from MQTT broker is : [pverzeugung
17:36:09.917 -> publish data is:982 W
17:36:10.841 -> Command from MQTT broker is : [pverzeugung
17:36:10.841 -> publish data is:984 W
17:36:11.166 -> Command from MQTT broker is : [pverzeugung
17:36:11.166 -> publish data is:989 W
17:36:11.949 -> Command from MQTT broker is : [pverzeugung
17:36:11.949 -> publish data is:990 W
17:36:12.273 -> Command from MQTT broker is : [pverzeugung
17:36:12.273 -> publish data is:992 W
17:36:13.060 -> Command from MQTT broker is : [pverzeugung
17:36:13.060 -> publish data is:990 W
17:36:14.077 -> Command from MQTT broker is : [pverzeugung
17:36:14.077 -> publish data is:1019 W
17:36:14.493 -> Command from MQTT broker is : [pverzeugung
17:36:14.493 -> publish data is:1022 W
17:36:15.416 -> Command from MQTT broker is : [pverzeugung
17:36:15.416 -> publish data is:1019 W
17:36:16.246 -> Command from MQTT broker is : [pverzeugung
17:36:16.246 -> publish data is:1017 W
17:36:16.616 -> Command from MQTT broker is : [pverzeugung
17:36:16.662 -> publish data is:1015 W
17:36:17.355 -> Command from MQTT broker is : [pverzeugung
17:36:17.355 -> publish data is:1014 W
17:36:17.771 -> Command from MQTT broker is : [pverzeugung
17:36:17.771 -> publish data is:1019 W
17:36:18.511 -> Command from MQTT broker is : [pverzeugung
17:36:18.511 -> publish data is:1018 W
17:36:19.527 -> Command from MQTT broker is : [pverzeugung
17:36:19.527 -> publish data is:1016 W
17:36:19.897 -> Command from MQTT broker is : [pverzeugung
17:36:19.897 -> publish data is:1011 W
17:36:20.867 -> Command from MQTT broker is : [pverzeugung
17:36:20.867 -> publish data is:1017 W
17:36:21.653 -> Command from MQTT broker is : [pverzeugung
17:36:21.653 -> publish data is:1016 W
17:36:22.901 -> Command from MQTT broker is : [pverzeugung
17:36:22.901 -> publish data is:1015 W
17:36:23.318 -> Command from MQTT broker is : [pverzeugung
17:36:23.318 -> publish data is:1017 W
17:36:24.012 -> Command from MQTT broker is : [pverzeugung
17:36:24.012 -> publish data is:1016 W
17:36:24.427 -> Command from MQTT broker is : [pverzeugung
17:36:24.427 -> publish data is:1014 W
17:36:25.168 -> Command from MQTT broker is : [pverzeugung
17:36:25.168 -> publish data is:1013 W
17:36:25.537 -> Command from MQTT broker is : [pverzeugung
17:36:25.537 -> publish data is:1015 W
17:36:25.952 -> Command from MQTT broker is : [pverzeugung
17:36:25.952 -> publish data is:1017 W
17:36:26.322 -> Command from MQTT broker is : [pverzeugung
17:36:26.368 -> publish data is:1015 W
17:36:26.738 -> Command from MQTT broker is : [pverzeugung
17:36:26.738 -> publish data is:1017 W
17:36:27.153 -> Command from MQTT broker is : [pverzeugung
17:36:27.153 -> publish data is:1021 W
17:36:27.523 -> Command from MQTT broker is : [pverzeugung
17:36:27.523 -> publish data is:1020 W
17:36:27.939 -> Command from MQTT broker is : [pverzeugung
17:36:27.939 -> publish data is:1021 W
17:36:28.446 -> Command from MQTT broker is : [pverzeugung
17:36:28.446 -> publish data is:10
also die Werte stimmen nicht und ändern sich auch nicht
18:05:10.006 -> Command from MQTT broker is : verbrauch
18:05:10.006 -> publish data is:4711
18:05:10.006 -> Command from MQTT broker is : akkuentladung
18:05:10.006 -> publish data is:42
18:05:11.026 -> Command from MQTT broker is : pverzeugung
18:05:11.026 -> publish data is:773
18:05:11.072 -> Command from MQTT broker is : verbrauch
18:05:11.072 -> publish data is:4711
18:05:11.118 -> Command from MQTT broker is : akkuentladung
18:05:11.118 -> publish data is:42
18:05:12.136 -> Command from MQTT broker is : pverzeugung
18:05:12.136 -> publish data is:773
18:05:12.136 -> Command from MQTT broker is : verbrauch
18:05:12.183 -> publish data is:4711
18:05:12.183 -> Command from MQTT broker is : akkuentladung
18:05:12.183 -> publish data is:42
18:05:13.201 -> Command from MQTT broker is : pverzeugung
18:05:13.201 -> publish data is:773
18:05:13.247 -> Command from MQTT broker is : verbrauch
18:05:13.247 -> publish data is:4711
18:05:13.247 -> Command from MQTT broker is : akkuentladung
18:05:13.293 -> publish data is:42
18:05:14.310 -> Command from MQTT broker is : pverzeugung
18:05:14.310 -> publish data is:773
18:05:14.310 -> Command from MQTT broker is : verbrauch
18:05:14.310 -> publish data is:4711
18:05:14.356 -> Command from MQTT broker is : akkuentladung
18:05:14.356 -> publish data is:42
18:05:15.376 -> Command from MQTT broker is : pverzeugung
18:05:15.376 -> publish data is:773
18:05:15.422 -> Command from MQTT broker is : verbrauch
18:05:15.422 -> publish data is:4711
18:05:15.422 -> Command from MQTT broker is : akkuentladung
18:05:15.422 -> publish data is:42
18:05:16.485 -> Command from MQTT broker is : pverzeugung
18:05:16.485 -> publish data is:773
18:05:16.485 -> Command from MQTT broker is : verbrauch
18:05:16.485 -> publish data is:4711
ich musste noch char hinzufügen.
Gibt es eigentlich eine Möglichkeit den Text auf dem Display zu zentrieren?
Die erste Zeile im Display flackert noch sehr wenn sich der Wert ändert, gibt es eine Möglichkeit das zu fixen?
Ich bin am überlegen für den verbrauch eine größeres mehrzeiliges Display zu nehmen. Kann man den Code auch so anpassen "Verbrauch Gesamt: (Wert aus MQTT)" oder muss man immer eine Gesamte Zeile nehmen? Zur not könnte ich den Text auch im MQTT mitschicken.
Code
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:");
if ( !strncmp (topic, "pverzeugung", sizeof(topic)) ) // http://www.cplusplus.com/reference/cstring/strncmp/
{
lcd_pverzeugung.clear();
lcd_pverzeugung.setCursor(0, 0);
lcd_pverzeugung.print("PV Erzeugung");
lcd_pverzeugung.setCursor(0, 1);
for (byte i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
lcd_pverzeugung.print((char)payload[i]);
}
}
if ( !strncmp (topic, "verbrauch", sizeof(topic)) )
{
lcd_verbrauch.clear();
lcd_verbrauch.setCursor(0, 0);
lcd_verbrauch.print("Verbrauch");
lcd_verbrauch.setCursor(0, 1);
for (byte i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
lcd_verbrauch.print((char)payload[i]);
}
}
if ( !strncmp (topic, "akkuladezustand", sizeof(topic)) )
{
lcd_akkuladezustand.clear();
lcd_akkuladezustand.setCursor(0, 0);
lcd_akkuladezustand.print("Akku Ladung");
lcd_akkuladezustand.setCursor(0, 1);
for (byte i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
lcd_akkuladezustand.print((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();
}
Einfacher wäre die Anzeige, wenn Du gleich eine Zeichenkette schickst, also sowas wie int wert = atoi(payload); Serial.print(wert); funktionieren würde.
Aber wolltest Du nicht ein OLED benutzen? Dann sollte man das mit dem OLED machen.
wie stelle ich es an das Zeile 1 leer bleibt wenn kein Wert in Zeile 2 steht.
Habe das bei meinen topics so eingestellt das alles unter 3W ausgeblendet wird, damit die Anzeige nicht dauernd 0W anzeigt.
Bei OLED Displays wäre es eben klasse wenn diese dann komplett dunkel sind
Ja.
Ich habe in einem Post von gestern zwei Funktionen für die Ausgabe.
Die Funktion ausgabe() übernimmt die zwei Zeilen fürs Display, vergleicht deren Inhalt mit der vorherigen Anzeige.
Wenn der nicht übereinstimmt, wird die Ausgabe neu gebaut mit printDisplay(). Die ermittelt die Länge der Zeichenkette udn anhand des Wissens um die Anzahl der Spalten wird berechnet wieviele Leerzeichen vorher rein müssen und dann wird der Text, gefolgt von Leerzeichen bis Zeilenende ausgegeben.
Nachprüfen, ob etwas in Zeile 2 steht.
Ne Idee....
if ( !strncmp (topic, "pverzeugung", sizeof(topic)) ) // http://www.cplusplus.com/reference/cstring/strncmp/
{
bool isChar = false;
lcd_pverzeugung.clear();
lcd_pverzeugung.setCursor(0, 1);
for (byte i = 0; i < length; i++)
{
if (isPrintable((char)payload[i]))
isChar = true;
Serial.print((char)payload[i]);
lcd_pverzeugung.print((char)payload[i]);
}
if (isChar)
{
lcd_pverzeugung.setCursor(0, 0);
lcd_pverzeugung.print("PV Erzeugung");
}
}