Arduino – Temperatur und Feuchte messen

Mit dem ESP8266 einigen Sensoren und einem OLED Display werden im Endausbau 3 Temperaturen und die Luftfeuchtigkeit gemessen. Eine Temperaturmessung werde ich verwenden, um die Wassertemperatur des Aussenpools zu messen. Den anderen Temperatursensoren werde ich für die Messung der Lufttemperatur verwenden. Dabei werden die Werte aus zwei Sensoren gemittelt.

Alle Messwerte werden via MQTT an einen Server, in meinem Fall einen Rasperry mit iOBroker, gesendet.

Image title

Your subtitle here

Versuchsaufbau auf dem Breathboard

Your subtitle here

//Debug
#define oled 1          // Ein OLED ist angeschlossen
#define dht11 1         //Der DHT11 ist angeschlossen
#define onwire 1        //Dallas Temperatursensor ist angeschlossen
#define debugserial 1   //Debg Informationen werden an Serial ausgegeben

//Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#include <DallasTemperature.h>
#include <OneWire.h>

#include <NTPClient.h>
#include <WiFiUdp.h>

#ifdef dht11
  #include <DHT.h>
  #define DHTPIN 1
  #define DHTTYPE DHT11
  DHT dht(DHTPIN, DHTTYPE);
#endif

#ifdef onwire
  //OnWire
  #define ONE_WIRE_BUS 5                         
  OneWire oneWire(ONE_WIRE_BUS);
  DallasTemperature sensors(&oneWire);
#endif

#ifdef oled
  #include "SSD1306.h"
  SSD1306  display(0x3c, 0, 2);
#endif


//WLAN
#define SSID "yourSSID"
#define WLANPW "yourWLANPAssword"
WiFiClient wifi;

//MQTT
#define MQTT_Server "10.10.10.31"
#define MQTT_PORT 1883
#define MQTT_USER "YourMQTTUser"
#define MQTT_PW "YourMQTTPassword"
Adafruit_MQTT_Client mqtt(&wifi, MQTT_Server, MQTT_PORT, MQTT_USER, MQTT_PW);

//MQTT Feeds
Adafruit_MQTT_Publish airtemp             = Adafruit_MQTT_Publish(&mqtt, "Pool/Meter/Temp_Air");
Adafruit_MQTT_Subscribe airtemp_offset    = Adafruit_MQTT_Subscribe(&mqtt, "Pool/Meter/Temp_Air_Offset");
Adafruit_MQTT_Publish watertemp           = Adafruit_MQTT_Publish(&mqtt, "Pool/Meter/Temp_Water");
Adafruit_MQTT_Subscribe watertemp_offset  = Adafruit_MQTT_Subscribe(&mqtt, "Pool/Meter/Temp_Water_Offset");
Adafruit_MQTT_Publish othertemp           = Adafruit_MQTT_Publish(&mqtt, "Pool/Meter/Temp_Other");
Adafruit_MQTT_Subscribe othertemp_offset  = Adafruit_MQTT_Subscribe(&mqtt, "Pool/Meter/Temp_Other_Offset");
Adafruit_MQTT_Publish humidity            = Adafruit_MQTT_Publish(&mqtt, "Pool/Meter/Humidity");
Adafruit_MQTT_Subscribe humidity_offset   = Adafruit_MQTT_Subscribe(&mqtt, "Pool/Meter/Humidity_Offset");
Adafruit_MQTT_Subscribe mqtt_intervall    = Adafruit_MQTT_Subscribe(&mqtt, "Pool/Config/Intervall");
Adafruit_MQTT_Publish lastconnect         = Adafruit_MQTT_Publish(&mqtt, "Pool/Config/Last_Connect");

Adafruit_MQTT_Subscribe *subscription;


//NTP
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
String formattedDate;
String dayStamp;
String timeStamp;

void MQTT_connect();

void setup(void)
{ 
  int x = 0;
  #ifdef debugserial
    Serial.begin(9600);
  #endif

  #ifdef oled
  // Initialising the UI will init the display too.
    display.init();
    display.flipScreenVertically();
    display.setFont(ArialMT_Plain_16);
    display.setTextAlignment(TEXT_ALIGN_LEFT);
  #endif

  #ifdef onwire
    sensors.begin();
  #endif
  
  WiFi.begin(SSID, WLANPW);
  #ifdef oled
    display.clear();
  #endif
  
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    #ifdef debugserial
      Serial.print(".");
    #endif
    #ifdef oled
      //Display
      display.drawString(0, x, "."); 
      display.display();
      x=x+1;
    #endif

  }

  #ifdef debugserial
    Serial.println();
    Serial.print("Connected, IP address: ");
    Serial.println(WiFi.localIP());
  #endif

  // NTPClient initialisieren
  timeClient.begin();
  // Setzen des Offset in Sekunden umd die Zeitzone einzustellen
  // GMT +1 = 3600
  // GMT +8 = 28800
  // GMT -1 = -3600
  // GMT 0 = 0
  timeClient.setTimeOffset(3600);

  //MQTT zu lesende Werte initialisieren
  mqtt.subscribe(&airtemp_offset);
  mqtt.subscribe(&watertemp_offset);
  mqtt.subscribe(&othertemp_offset);
  mqtt.subscribe(&humidity_offset);
  mqtt.subscribe(&mqtt_intervall);

  //Verbindung mit DHT11 initialisieren
  #ifdef dht11
    dht.begin();
  #endif

  
}


//Globale Variablen definieren
float temp_1=0;
float temp_offset_1=0;
float temp_2=0;
float temp_offset_2=0;
float temp_3=0;
float temp_offset_3=0;
float humidity_1=0;
float humidity_offset_1=0;

unsigned long intervall=1000;
unsigned long time_now = 0;
char DateTime[20];

void loop() {
    time_now = millis();

    //Mal die Zeit lesen...
    while(!timeClient.update()) {
      timeClient.forceUpdate();
    }

    //Zeit und Datum formatieren
    //und nach MQTT senden
    formattedDate = timeClient.getFormattedDate();
    formattedDate.toCharArray(DateTime,20);
    lastconnect.publish(DateTime);    

    //Werte aus MQTT lesen
    MQTT_read();

    #ifdef onwire
      //Dallas lesen
      sensors.requestTemperatures();                // Send the command to get temperatures  
      temp_1 = sensors.getTempCByIndex(0);
      temp_2 = sensors.getTempCByIndex(1);
    #endif
    
    //DHT11 lesen
    #ifdef dht11
      temp_3 = dht.readTemperature();
      humidity_1 = dht.readHumidity();
    #endif
    
    //Werte nach MQTT schreiben
    airtemp.publish(temp_1 + temp_offset_1);    
    watertemp.publish(temp_2 + temp_offset_2);  
    othertemp.publish(temp_3 + temp_offset_3);  
    humidity.publish(humidity_1 + humidity_offset_1);  

    #ifdef oled
      //Display
      display.clear();
      display.drawString(0, 0, "Humidity: " + String(humidity_1 + humidity_offset_1) + "%\t"); 
      display.drawString(0, 16, "Temp1: " + String(temp_1 + temp_offset_1) + "C"); 
      display.drawString(0, 32, "Temp2: " + String(temp_3 + temp_offset_3) + "C"); 
      display.display();
    #endif
        
    //Den Intervall abwarten und sonst was tun
    while(millis() < time_now + intervall){
      //Mal MQTT lesen 
      MQTT_read();

      //Hier kann sonst was getan werden

    }
}

void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  #ifdef debugserial
    Serial.print("Connecting to MQTT... ");
  #endif 
  
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       #ifdef debugserial
         Serial.println(mqtt.connectErrorString(ret));
         Serial.println("Retrying MQTT connection in 5 seconds...");
       #endif
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  #ifdef debugserial
    Serial.println("MQTT Connected!");
  #endif
}

void MQTT_read(){

  MQTT_connect();

  //MQTT lesen
  while (subscription = mqtt.readSubscription(100)) {
    if (subscription == &airtemp_offset) {
      temp_offset_1 = atof((char *)airtemp_offset.lastread);
    }
    if (subscription == &watertemp_offset) {
      temp_offset_2 = atof((char *)watertemp_offset.lastread);
    }
    if (subscription == &othertemp_offset) {
      temp_offset_3 = atof((char *)othertemp_offset.lastread);
    }
    if (subscription == &mqtt_intervall) {
      intervall = atol((char *)mqtt_intervall.lastread);
    }
  }
  
}

Schreib einen Kommentar

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