#include <Wire.h>
#include <ESP8266WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Adafruit_GFX.h>
#include "SSD1306.h"
#include <PubSubClient.h>
#define SEALEVELPRESSURE_HPA (1013.25)
#define temperature_topic "sensor/temperatur BME280 innen" //Name für Sensorenwerte
#define hum_topic "sensor/luftfeuchte BME280 innen" //Name für Sensorenwerte
#define drk_topic "sensor/druck BME280 innen" //Name für Sensorenwerte
#define temperature_topic2 "sensor/temperatur BME280 aussen" //Name für Sensorenwerte
#define hum_topic2 "sensor/luftfeuchte BME280 aussen" //Name für Sensorenwerte
#define drk_topic2 "sensor/druck BME280 aussen" //Name für Sensorenwerte
WiFiClient Sensor_aussen;
PubSubClient client(Sensor_aussen);
#define icon_width 30
#define icon_height 30
#define mqtt_server "192.168.3.43"
#define mqtt_user "guest" // if exist
#define mqtt_password "guest" //idem
char message_buff[100];
float tempds18b20;
long lastMsg = 0;
long lastRecu = 0;
bool debug = false; //Display log message if True
Adafruit_BME280 bme1; //Innensensor
Adafruit_BME280 bme2;// Außensensor
SSD1306 display(0x3c, D2, D1);
float luftdruck1, luftdruck2, ld1, ld2, drk, see1, see2, h1, h2, t1, t2, p1, p2, pin, af; //x1 = innen, x2 = aussen
char temperatureFString[6];
char humidityString[6];
char pressureString[7];
char pressureInchString[6];
char temperatureFString2[6];
char humidityString2[6];
char pressureString2[7];
char pressureInchString2[6];
int count = 0, hoehe = 500;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
Wire.begin(D2, D1);
Wire.setClock(100000);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
client.setServer(mqtt_server, 1883); // Configure MQTT connexion
client.setCallback(callback); // callback function to execute when a MQTT message
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);
// Printing the ESP IP address
Serial.println(WiFi.localIP());
Serial.println(F("BME280 test"));
relhum();
client.println(af);
if (!bme1.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor indoor, check wiring!");
while (1);
}
if (!bme2.begin(0x77)) {
Serial.println("Could not find a valid BME280 sensor outdoor, check wiring!");
while (1);
}
// Initialising the UI will init the display too.
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Connecting to MQTT broker ...");
if (client.connect("ESP8266_BME280", mqtt_user, mqtt_password)) {
Serial.println("OK");
} else {
Serial.print("KO, error : ");
Serial.print(client.state());
Serial.println(" Wait 5 secondes before to retry");
delay(5000);
}
}
}
void getWeather() {
// BME.setWeatherMonitoring(); //Setze Parameter für Wetterueberwachung
// BME.init();
//float readReducedPress(hoehe);
//t1= bme2.readdewpoint();
h1 = bme1.readHumidity();
t1 = bme1.readTemperature();
h2 = bme2.readHumidity();
t2 = bme2.readTemperature();
see1 = bme1.readAltitude(SEALEVELPRESSURE_HPA);
see2 = bme2.readAltitude(SEALEVELPRESSURE_HPA);
p1 = bme1.readPressure() / 100.0F;
luftdruck1 = (p1 / pow(1 - ((0.0065 * hoehe) / (t1 + (0.0065 * hoehe) + 273.15)), 5.257));
ld1 = luftdruck1;
dtostrf(t1, 5, 1, temperatureFString);
dtostrf(h1, 5, 1, humidityString);
dtostrf(luftdruck1, 6, 1, pressureString);
float h1 = bme1.readHumidity();
float d1 = bme1.readTemperature();
p2 = bme1.readPressure() / 100.0F;
luftdruck2 = (p2 / pow(1 - ((0.0065 * hoehe) / (t1 + (0.0065 * hoehe) + 273.15)), 5.257));
ld2 = luftdruck2;
dtostrf(t2, 5, 1, temperatureFString2);
dtostrf(h2, 5, 1, humidityString2);
dtostrf(luftdruck2, 6, 1, pressureString2);
float h2 = bme2.readHumidity();
float d2 = bme2.readTemperature();
delay(100);
}
//Entscheidung welches Symbol bei entsprechenden Druck
void wetter() {
if (ld1 <= 1000.00) {
display.drawXbm(90, 25, icon_width, icon_height, rain_icon);
}
else if (ld1 <= 1020.00 ) {
display.drawXbm(90, 25, icon_width, icon_height, cloud_icon);
}
else if (ld1 > 1020.00 ) {
display.drawXbm(90, 25, icon_width, icon_height, sunny_icon);
}
}
//Daten an MQTT senden
void pub() {
client.publish(temperature_topic, String(t1).c_str(), true);
client.publish(hum_topic, String(h1).c_str(),true);
client.publish(drk_topic, String(luftdruck1).c_str(),true);
client.publish(temperature_topic2, String(t2).c_str(), true);
client.publish(hum_topic2, String(h2).c_str(),true);
client.publish(drk_topic2, String(luftdruck2).c_str(),true);
}
// runs over and over again
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
WiFiClient client = server.available();
if (client) {
Serial.println("New client");
// bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && blank_line) {
getWeather();
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
//----------------------your actual web page that displays temperature------------------------------------------------------
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><META HTTP-EQUIV=\"refresh\" CONTENT=\"15\"></head><body><h1>ESP8266 Weather Webserver</h1><h3>Temperatur innen = ");
client.println(temperatureFString);
client.println("°C</h3><h3>Temperatur aussen = ");
client.println(temperatureFString2);
client.println("°C</h3><h3>Luftfeuchte innen = ");
client.println(humidityString);
client.println("%</h3><h3>Luftfeuchte aussen = ");
client.println(humidityString2);
client.println("%</h3><h3>Druck innen = ");
client.println(luftdruck1);
client.println("hPa (");
client.println(bme1.readAltitude(SEALEVELPRESSURE_HPA));
client.println("m)</h3><h3>Druck aussen = ");
client.println(luftdruck2);
client.println("hPa (");
client.println(bme2.readAltitude(SEALEVELPRESSURE_HPA));
client.println("m)");
break;
}
if (c == '\n') {
// when starts reading a new line
blank_line = true;
}
else if (c != '\r') {
// when finds a character on the current line
blank_line = false;
}
}
}
// closing the client connection
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
if (count == 0) {
getWeather();
display.clear();
display.drawString(10, 6, "T=" + String(t1) + "°C");
display.drawString(10, 18, "P=" + String(luftdruck1) + "hPa");
display.drawString(10, 30, "RH=" + String(h1) + "%");
display.drawRect(0, 0, 128, 64);
display.drawRect(1, 1, 126, 62);
wetter();
display.display();
}
count = count + 1;
if (count == 20) count = 0;
delay(500);
pub();
delay(10000);
}
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
if ( debug ) {
Serial.println("Message recu => topic: " + String(topic));
Serial.print(" | longueur: " + String(length, DEC));
}
// create character buffer with ending null terminator (string)
for (i = 0; i < length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
if ( debug ) {
Serial.println("Payload: " + msgString);
}
if ( msgString == "ON" ) {
digitalWrite(D2, HIGH);
} else {
digitalWrite(D2, LOW);
}
}
Das ist der Code für den ESP. Der läuft auch ohne Probleme. Was mir noch komplett fehlt ist, wie ich den iobroker Wert vom Raspberry bekomme. Ich hab da schon mal gesucht aber irgendwie komme ich nicht klar.
Im Anhang ist der Wert, welchen gerne wieder zurück zum ESP haben möchte und auf dem OLED anzeigen will. Bzw. ist es doch möglich Werte über den ESP vom iobroker zu holen?
PS: der Code ist etwas verkürzt
MfG
Jens