107 lines
3.7 KiB
C
Executable File
107 lines
3.7 KiB
C
Executable File
#include "Wire.h"
|
|
|
|
#define Anzahl_Sensoren_BME280 1 // Mögliche Werte: '0','1','2'
|
|
//#define Korrektur_Luftdruck KorrekturLuftdruck // Korrekturwert um Abweichungen zu offiziellen Wetterstationen auszugleichen
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
// Konfiguration BME280 - Temperatur und Luftfeuchte
|
|
//----------------------------------------------------------------
|
|
#include "Adafruit_Sensor.h"
|
|
#include "Adafruit_BME280.h"
|
|
uint8_t BME280_adresse[2] = {0x76, 0x76};
|
|
#ifndef SEALEVELPRESSURE_HPA
|
|
#define SEALEVELPRESSURE_HPA (1013.25f)
|
|
#endif
|
|
//----------------------------------------------------------------
|
|
|
|
bool F_BME280 = true;
|
|
|
|
const float No_Val = 999.99;
|
|
float Temp[2] = {No_Val, No_Val};
|
|
float Feuchte[2] = {No_Val, No_Val};
|
|
float L_Druck[2] = {No_Val, No_Val};
|
|
|
|
struct {
|
|
char temperature[15] = {0};
|
|
char pressure[15] = {0};
|
|
char approx_altitud[15] = {0};
|
|
char humity[15] = {0};
|
|
} BME280Data;
|
|
|
|
void Sensor_BME280() {
|
|
if (Anzahl_Sensoren_BME280 > 0) {
|
|
float Temperatur_BME;
|
|
float Luftfeuchte_BME;
|
|
float Luftdruck_BME;
|
|
boolean check;
|
|
|
|
Adafruit_BME280 my_bme;
|
|
|
|
for (byte i = 0; i < Anzahl_Sensoren_BME280; i++) {
|
|
check = my_bme.begin(BME280_adresse[i]); // I2C Adresse
|
|
delay (100); // time to get system ready
|
|
if (check) { // if bme ok
|
|
Temperatur_BME = my_bme.readTemperature();
|
|
Luftfeuchte_BME = my_bme.readHumidity();
|
|
Luftdruck_BME = my_bme.readPressure();
|
|
Luftdruck_BME = (Luftdruck_BME/100) + KorrekturLuftdruck;
|
|
//Luftdruck_BME = 220;
|
|
Serial.print("Temperature (BME280):\t\t");
|
|
Serial.print(Temperatur_BME) + -KorrekturTemperaturBME;
|
|
Serial.println(" °C");
|
|
Serial.print("Luftfeuchtigkeit (BME280):\t");
|
|
Serial.print(Luftfeuchte_BME);
|
|
Serial.println(" %");
|
|
Serial.print("Luftdruck (BME280):\t\t");
|
|
Serial.print(Luftdruck_BME);
|
|
Serial.println(" hPa");
|
|
}
|
|
else {
|
|
Temperatur_BME = No_Val;
|
|
Luftfeuchte_BME = No_Val;
|
|
Luftdruck_BME = No_Val;
|
|
Serial.println("BME280 not connected!");
|
|
F_BME280 = false;
|
|
}
|
|
if (i == 0) { // erster BME
|
|
Temp[0] = Temperatur_BME; // Hier kann die Zuordnung der Sensoren geändert werden
|
|
Feuchte[0] = Luftfeuchte_BME; // Hier kann die Zuordnung der Sensoren geändert werden
|
|
L_Druck[0] = Luftdruck_BME;
|
|
}
|
|
if (i == 1) { // zweiter BME
|
|
Temp[1] = Temperatur_BME; // Hier kann die Zuordnung der Sensoren geändert werden
|
|
Feuchte[1] = Luftfeuchte_BME; // Hier kann die Zuordnung der Sensoren geändert werden
|
|
L_Druck[1] = Luftdruck_BME;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void M2M_BME280(String deviceId = "4711") {
|
|
char topic[100];
|
|
dtostrf(Temp[0],7,1,BME280Data.temperature);
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/temperature_BME_280" );
|
|
client.publish(topic, BME280Data.temperature, true);
|
|
|
|
dtostrf(Feuchte[0],7,1,BME280Data.humity);
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/humity_BME280" );
|
|
client.publish(topic, BME280Data.humity, true);
|
|
|
|
dtostrf(L_Druck[0],7,1,BME280Data.pressure);
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/pressure" );
|
|
client.publish(topic, BME280Data.pressure, true);
|
|
}
|
|
void Clear_BME280(String deviceId = "4711"){
|
|
char topic[100];
|
|
char test[1] = "";
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/temperature_BME_280" );
|
|
client.publish(topic,test, false);
|
|
delay(100);
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/humity_BME280" );
|
|
client.publish(topic,test, false);
|
|
delay(100);
|
|
sprintf(topic, "%s%s%s", "hjk/devices/", deviceId.c_str(), "/telemetry/pressure" );
|
|
client.publish(topic,test, false);
|
|
delay(100);
|
|
} |