Status Display für IoBroker

Allgemein

[codesyntax lang=“c“]

// Oled und eine ESP2866 als iObroker Statusdisplay
//  VCC >> 3,3V, GND >> GND, SDA >> D2, SCL >> D1


#include <SPI.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
 
// WLAn zugang und IP des MQTT Servers, Das Port kann man unten ändern.
const char* ssid = "tom_home!";
const char* password = "heimnetz";
const char* mqtt_server = "192.168.178.33";
 
String _DisplayLine1 = "";
String _DisplayLine2 = "";
String _DisplayLine3 = "";
String _DisplayLine4 = "";
 
#define OLED_RESET 0
Adafruit_SSD1306 display(OLED_RESET);
 
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
 
void setup_wifi() {
 
delay(10);
// We start by connecting to a WiFi network
Serial.println();
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("Message arrived [");
Serial.print(topic);
Serial.print("] ");
 
if ( strcmp( topic, "display_line1" ) == 0 )
{
_DisplayLine1 = "";
for (int i = 0; i < length; i++) { _DisplayLine1 += (char)payload[i]; }
}
 
if ( strcmp( topic, "display_line2" ) == 0 )
{
_DisplayLine2 = "";
for (int i = 0; i < length; i++) { _DisplayLine2 += (char)payload[i]; }
}
 
if ( strcmp( topic, "display_line3" ) == 0 )
{
_DisplayLine3 = "";
for (int i = 0; i < length; i++) { _DisplayLine3 += (char)payload[i]; }
}
 
if ( strcmp( topic, "display_line4" ) == 0 )
{
_DisplayLine4 = "";
for (int i = 0; i < length; i++) { _DisplayLine4 += (char)payload[i]; }
}
 
UpdateDisplay();
}
 
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 (client.connect(clientId.c_str())) {
Serial.println("connected");
 
client.subscribe("display_line1");
client.subscribe("display_line2");
client.subscribe("display_line3");
client.subscribe("display_line4");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
 
void UpdateDisplay()
{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
 
display.setCursor(1,0);
display.print(_DisplayLine1.c_str());
display.setCursor(1,8);
display.print(_DisplayLine2.c_str());
display.setCursor(1,16);
display.print(_DisplayLine3.c_str());
display.setCursor(1,24);
display.print(_DisplayLine4.c_str());
display.display();
}
 
void setup() {
Serial.begin(115200);
 
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
 
display.begin( SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(1);
display.display();
}
 
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}

[/codesyntax]

Tags:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.