166 views
# Sensoren von Herr Mehren Soon™ to be moved to blog.herrmehren.de ## Feinstaubsensor Ganz langweilig einfach https://luftdaten.info/feinstaubsensor-bauen/ mit der default Firmware. Nach 2 Jahren einen neuen DHT22 eingebaut. OpenHAB parst das JSON regelmäßig. *services/http.cfg* ``` # url definition feinstaubsensor.url=http://192.168.178.62/data.json # update every 1 min feinstaubsensor.updateInterval=60000 ``` *feinstaub.items* ``` Number Feinstaub_Humid "Luftfeuchtigkeit [%.1f %%]" <humidity> {http="<[feinstaubsensor:60000:JSONPATH($.sensordatavalues[?(@.value_type == 'humidity')].value)]"} Number Feinstaub_Temp "Temperatur [%.1f °C]" <temperature> {http="<[feinstaubsensor:60000:JSONPATH($.sensordatavalues[?(@.value_type == 'temperature')].value)]"} Number Feinstaub_PM25 "Feinstaub PM 2,5 [%.1f]" <flow> {http="<[feinstaubsensor:60000:JSONPATH($.sensordatavalues[?(@.value_type == 'SDS_P2')].value)]"} Number Feinstaub_PM10 "Feinstaub PM 10 [%.1f]" <flow> {http="<[feinstaubsensor:60000:JSONPATH($.sensordatavalues[?(@.value_type == 'SDS_P1')].value)]"} Number Feinstaub_Signal "Signal [%.1f %%]" <flow> {http="<[feinstaubsensor:60000:JSONPATH($.sensordatavalues[?(@.value_type == 'signal')].value)]"} Number Feinstaub_Age "Age [%.1f]" {http="<[feinstaubsensor:60000:JSONPATH($.age)]"} ``` ## ESP8266 & SHT31 Ein Wemos D1 mit [homie](https://github.com/homieiot/homie-esp8266) und [SHT31](https://www.exp-tech.de/sensoren/temperatur/6818/adafruit-sensiron-sht31-d-temperatur-feuchtigkeitssensor-breakout). Homie wirft die Daten in MQTT. Firmware aus Homie-Code und Adafruit-Lib zusammengebaut. Hab noch ein LC-Display drangesteckt. ``` #include <Homie.h> #include <Wire.h> #include "Adafruit_SHT31.h" #include <TFT_eSPI.h> // Graphics and font library for ILI9341 driver chip #include <SPI.h> HomieNode tempNode("sht31", "sht31", "sht31"); Adafruit_SHT31 sht31 = Adafruit_SHT31(); #define TFT_GREY 0x5AEB // New colour TFT_eSPI tft = TFT_eSPI(); // Invoke library const int TEMPERATURE_INTERVAL = 30; unsigned long lastTemperatureSent = 0; void resetLcd() { tft.init(); tft.setRotation(3); tft.setTextColor(TFT_WHITE,TFT_BLACK); tft.setTextSize(2); tft.fillScreen(TFT_BLACK); tft.setCursor(0, 0, 4); } void setupHandler() { tempNode.setProperty("temperature_unit").send("C"); Homie.getLogger() << "Initializing SHT31 sensor" << endl; if (!sht31.begin(0x44)) { tempNode.setProperty("sensorError").send("true"); Homie.getLogger() << "Failed initializing SHT31 sensor" << endl; Homie.getLogger() << "Resetting in a few seconds." << endl; Homie.prepareToSleep(); } Homie.getLogger() << "Successfully initialized SHT31 sensor" << endl; tempNode.setProperty("sensorError").send("false"); resetLcd(); tft.println("Booting..."); } void eventHandler(HomieEvent &event){ if (event.type == HomieEventType::READY_TO_SLEEP) { Homie.getLogger() << "Sleeping..." << endl; // sleep for 5 seconds Homie.doDeepSleep(5e6); } } void loopHandler() { if (millis() - lastTemperatureSent >= TEMPERATURE_INTERVAL * 1000UL || lastTemperatureSent == 0) { resetLcd(); tft.setCursor(0, 0, 4); float t = sht31.readTemperature(); float h = sht31.readHumidity(); if (!isnan(t) && !isnan(h)) { Homie.getLogger() << "Temperature: " << t << " °C" << endl; tempNode.setProperty("temperature").send(String(t)); tft.println("Temperatur:\n " + String(t) + " C"); Homie.getLogger() << "Humidity: " << h << " percent" << endl; tempNode.setProperty("humidity").send(String(h)); tft.println("Luftfeuchte\n " + String(h) + " %"); tempNode.setProperty("sensorError").send("false"); } else { tempNode.setProperty("sensorError").send("true"); tft.println("Sensorfehler!"); } lastTemperatureSent = millis(); } } void setup() { Serial.begin(115200); Serial << endl << endl; Homie_setFirmware("esp_temp", "2.0.0"); Homie.setSetupFunction(setupHandler).setLoopFunction(loopHandler); tempNode.advertise("temperature_unit"); tempNode.advertise("temperature"); tempNode.advertise("humidity"); tempNode.advertise("sensorError"); Homie.setup(); } void loop() { Homie.loop(); } ``` ## ESP8266 & BME280 Ein Wemos D1 Mini Pro mit [ESPEasy](https://www.letscontrolit.com/wiki/index.php?title=ESPEasy) und GY-BME280 (gibts bei ebay). ESPEasy wirft die Daten nach MQTT. ## MQTT nach OpenHAB Das neue [MQTT Binding](https://www.openhab.org/addons/bindings/mqtt/#mqtt-binding) installieren. Things definieren: *mqtt.things* ``` Bridge mqtt:broker:hp-nas [ host="192.168.178.64", secure=false, qos=2 ] { Thing topic esp_fuehler_mini { Channels: Type number : temperature "Temperatur" [ stateTopic="ESP_Easy/BME280/Temperature" ] Type number : humidity "Luftfeuchtigkeit" [ stateTopic="ESP_Easy/BME280/Humidity" ] Type number : pressure "Luftdruck" [ stateTopic="ESP_Easy/BME280/Pressure" ] } Thing topic esp_fuehler { Channels: Type number : temperature "Temperatur" [ stateTopic="homie/60019405792f/sht31/temperature" ] Type number : humidity "Luftfeuchtigkeit" [ stateTopic="homie/60019405792f/sht31/humidity" ] } } ``` Items aus den Things machen: *mqtt.items* ``` Number ESP_Fuehler_Temp "Raumtemperatur (SMT31): [%.1f °C]" <temperature> (FF_SchlafzimmerDavid) {channel="mqtt:topic:hp-nas:esp_fuehler:temperature"} Number ESP_Fuehler_Humid "Luftfeuchtigkeit (SMT31): [%.1f %%]" <humidity> (FF_SchlafzimmerDavid) {channel="mqtt:topic:hp-nas:esp_fuehler:humidity"} Number ESP_Fuehler_Mini_Temp "Raumtemperatur (BME280): [%.1f °C]" <temperature> (FF_SchlafzimmerDavid) {channel="mqtt:topic:hp-nas:esp_fuehler_mini:temperature"} Number ESP_Fuehler_Mini_Humid "Luftfeuchtigkeit (BME280): [%.1f %%]" <humidity> (FF_SchlafzimmerDavid) {channel="mqtt:topic:hp-nas:esp_fuehler_mini:humidity"} Number ESP_Fuehler_Mini_Press "Luftdruck (BME280): [%.1f hPa]" <pressure> (FF_SchlafzimmerDavid) {channel="mqtt:topic:hp-nas:esp_fuehler_mini:pressure"} ``` ## OpenHAB nach InfluxDB [InfluxDB Addon](https://www.openhab.org/addons/persistence/influxdb/) einrichten und [persistence](https://www.openhab.org/docs/configuration/persistence.html#persistence) config erstellen: ``` Strategies { everyMinute : "0 * * * * ?" everyHour : "0 0 * * * ?" everyDay : "0 0 0 * * ?" default = everyMinute, everyChange, restoreOnStartup } Items { * : strategy = everyChange, restoreOnStartup } ``` OpenHAB tut nun einfach alle Werte von allen Items die es kennt in die Datenbank.