Navigation

    • Register
    • Login
    • OpenHardware.io
    • Categories
    • Recent
    • Tags
    • Popular
    1. Home
    2. Commodoreuno
    3. Posts
    • Profile
    • Following
    • Followers
    • Topics
    • Posts
    • Best
    • Groups

    Posts made by Commodoreuno

    • RE: DHT22 wrong sensor type in Home Assistant

      Okay, I have now taken all the suggestions into account, including deleting the application in Home Assistant. I also worked with the official script for deleting the Arduino, which I hadn't done before, and made changes in the program, especially with the IDs and various names, in order to essentially create a completely new program. Now it works with this script. Thanks so far for the help!

      // Enable debug prints
      #define MY_DEBUG
      
      // Set static node ID
      #define MY_NODE_ID 45
      
      // Enable only USB, disable radio
      #define MY_GATEWAY_SERIAL
      
      #include <SPI.h>
      #include <MySensors.h>
      #include <DHT.h>
      #include <Adafruit_Sensor.h>
      
      // ---------- DHT CONFIG ----------
      #define DHTPIN 4         // Datenpin des DHT22
      #define DHTTYPE DHT22    // Sensor-Typ DHT22
      #define USE_CELSIUS true // Immer Celsius verwenden
      #define SENSOR_TEMP_OFFSET 0
      
      #define UPDATE_INTERVAL 5000           // 5 Sekunden
      #define FORCE_UPDATE_N_READS 10        // nach 10 gleichen Werten senden
      
      #define CHILD_ID_HUM 20
      #define CHILD_ID_TEMP 21
      
      DHT dht(DHTPIN, DHTTYPE);
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      
      float lastTemp;
      float lastHum;
      uint8_t nNoUpdatesTemp = 0;
      uint8_t nNoUpdatesHum = 0;
      
      // ---------- BUTTON CONFIG ----------
      #define PRIMARY_BUTTON_PIN 2
      #define SECONDARY_BUTTON_PIN 3
      
      #define PRIMARY_CHILD_ID 22
      #define SECONDARY_CHILD_ID 23
      
      MyMessage msgButton1(PRIMARY_CHILD_ID, V_TRIPPED);
      MyMessage msgButton2(SECONDARY_CHILD_ID, V_TRIPPED);
      
      static uint8_t lastButton1 = 2;
      static uint8_t lastButton2 = 2;
      
      // ---------- SETUP & PRESENTATION ----------
      void presentation() {
        sendSketchInfo("Combined Sensor Node", "1.0");
      
        // Sensoren anmelden
        present(CHILD_ID_HUM, S_HUM);
        present(CHILD_ID_TEMP, S_TEMP);
        present(PRIMARY_CHILD_ID, S_DOOR);
        present(SECONDARY_CHILD_ID, S_DOOR);
      }
      
      void setup() {
        dht.begin();  // DHT starten
      
        pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
        pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
      }
      
      // ---------- LOOP ----------
      void loop() {
        // ----- DHT AUSLESEN -----
        float temperature = dht.readTemperature(); // immer Celsius
        float humidity = dht.readHumidity();
      
        if (!isnan(temperature) && (temperature != lastTemp || nNoUpdatesTemp >= FORCE_UPDATE_N_READS)) {
          lastTemp = temperature + SENSOR_TEMP_OFFSET;
          send(msgTemp.set(lastTemp, 1));
          nNoUpdatesTemp = 0;
          Serial.print("Temp: ");
          Serial.println(lastTemp);
        } else {
          nNoUpdatesTemp++;
        }
      
        if (!isnan(humidity) && (humidity != lastHum || nNoUpdatesHum >= FORCE_UPDATE_N_READS)) {
          lastHum = humidity;
          send(msgHum.set(lastHum, 1));
          nNoUpdatesHum = 0;
          Serial.print("Hum: ");
          Serial.println(lastHum);
        } else {
          nNoUpdatesHum++;
        }
      
        // ----- BUTTONS -----
        uint8_t val1 = digitalRead(PRIMARY_BUTTON_PIN);
        if (val1 != lastButton1) {
          send(msgButton1.set(val1 == HIGH));
          lastButton1 = val1;
        }
      
        uint8_t val2 = digitalRead(SECONDARY_BUTTON_PIN);
        if (val2 != lastButton2) {
          send(msgButton2.set(val2 == HIGH));
          lastButton2 = val2;
        }
      
        // ----- SLEEP -----
        //sleep(PRIMARY_BUTTON_PIN - 2, CHANGE, SECONDARY_BUTTON_PIN - 2, CHANGE, UPDATE_INTERVAL);
        delay(50);
      }
      
      
      posted in Troubleshooting
      Commodoreuno
      Commodoreuno
    • RE: DHT22 wrong sensor type in Home Assistant

      @OldSurferDude First of all, thanks for the reply. I tried it that way, but it looks like the third “door” sensor is completely gone. The issue is probably in the code after all.

      posted in Troubleshooting
      Commodoreuno
      Commodoreuno
    • DHT22 wrong sensor type in Home Assistant

      Hi, I’m pretty sure the error is somewhere in the program, I just don’t know where. I want to use an Arduino Uno to read two limit switches and a DHT22 sensor. For this, I combined two programs with the help of ChatGPT, made some small adjustments, and tested it. Everything seemed to work fine in the Serial Monitor. However, in Home Assistant, the limit switches are recognized correctly, but the DHT22 is also detected as a door contact. Where is my mistake here?

      
      // Enable debug prints
      #define MY_DEBUG
      
      // Enable only USB, disable radio
      //#define MY_RADIO_RF24
      //#define MY_RADIO_RFM69
      //#define MY_RS485
      
      //USB Modus
      #define MY_GATEWAY_SERIAL
      
      #include <SPI.h>
      #include <MySensors.h>
      #include <DHT.h>
      #include <Adafruit_Sensor.h>
      
      // ---------- DHT CONFIG ----------
      #define DHTPIN 4       // <-- DHT Datenpin
      #define DHTTYPE DHT22  // DHT-Typ (z.B. DHT11, DHT22)
      DHT dht(DHTPIN, DHTTYPE);
      
      #define SENSOR_TEMP_OFFSET 0
      static const uint64_t UPDATE_INTERVAL = 30000;  // 30 Sekunden für DHT-Update
      static const uint8_t FORCE_UPDATE_N_READS = 10;
      
      #define CHILD_ID_HUM 0
      #define CHILD_ID_TEMP 1
      
      float lastTemp;
      float lastHum;
      uint8_t nNoUpdatesTemp;
      uint8_t nNoUpdatesHum;
      bool metric = true;
      
      MyMessage msgHum(CHILD_ID_HUM, V_HUM);
      MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
      
      // ---------- BUTTON CONFIG ----------
      #define PRIMARY_CHILD_ID 2
      #define SECONDARY_CHILD_ID 3
      
      #define PRIMARY_BUTTON_PIN 2     // bleibt auf D2
      #define SECONDARY_BUTTON_PIN 3   // bleibt auf D3
      
      #if (PRIMARY_BUTTON_PIN == SECONDARY_BUTTON_PIN)
      #error PRIMARY_BUTTON_PIN and SECONDARY_BUTTON_PIN cannot be the same
      #endif
      #if (PRIMARY_CHILD_ID == CHILD_ID_TEMP || PRIMARY_CHILD_ID == CHILD_ID_HUM || \
           SECONDARY_CHILD_ID == CHILD_ID_TEMP || SECONDARY_CHILD_ID == CHILD_ID_HUM)
      #error Child IDs must be unique
      #endif
      
      MyMessage msgButton1(PRIMARY_CHILD_ID, V_TRIPPED);
      MyMessage msgButton2(SECONDARY_CHILD_ID, V_TRIPPED);
      
      static uint8_t lastButton1 = 2;
      static uint8_t lastButton2 = 2;
      
      void presentation() {
        sendSketchInfo("Combined Sensor Node", "1.0");
      
        // DHT
        present(CHILD_ID_HUM, S_HUM);
        present(CHILD_ID_TEMP, S_TEMP);
      
        // Buttons
        present(PRIMARY_CHILD_ID, S_DOOR);
        present(SECONDARY_CHILD_ID, S_DOOR);
      
        metric = getControllerConfig().isMetric;
      }
      
      void setup() {
        dht.begin();
        delay(2000); // Sensor bootzeit
      
        pinMode(PRIMARY_BUTTON_PIN, INPUT_PULLUP);
        pinMode(SECONDARY_BUTTON_PIN, INPUT_PULLUP);
      }
      
      void loop() {
        // ----- BUTTON HANDLING ----- (Ständig überprüfen)
        uint8_t val1 = digitalRead(PRIMARY_BUTTON_PIN);
        if (val1 != lastButton1) {
          send(msgButton1.set(val1 == HIGH));
          lastButton1 = val1;
        }
      
        uint8_t val2 = digitalRead(SECONDARY_BUTTON_PIN);
        if (val2 != lastButton2) {
          send(msgButton2.set(val2 == HIGH));
          lastButton2 = val2;
        }
      
        // ----- DHT SENSOR ----- (Nur alle UPDATE_INTERVAL Millisekunden aktualisieren)
        static uint64_t lastDHTUpdate = 0;
        uint64_t currentMillis = millis();
      
        if (currentMillis - lastDHTUpdate >= UPDATE_INTERVAL) {
          lastDHTUpdate = currentMillis;
      
          float temperature = dht.readTemperature(metric); // true = Celsius
          float humidity = dht.readHumidity();
      
          if (!isnan(temperature) && (temperature != lastTemp || nNoUpdatesTemp == FORCE_UPDATE_N_READS)) {
            lastTemp = temperature + SENSOR_TEMP_OFFSET;
            send(msgTemp.set(lastTemp, 1));
            nNoUpdatesTemp = 0;
            Serial.print("T: ");
            Serial.println(lastTemp);
          } else {
            nNoUpdatesTemp++;
          }
      
          if (!isnan(humidity) && (humidity != lastHum || nNoUpdatesHum == FORCE_UPDATE_N_READS)) {
            lastHum = humidity;
            send(msgHum.set(lastHum, 1));
            nNoUpdatesHum = 0;
            Serial.print("H: ");
            Serial.println(lastHum);
          } else {
            nNoUpdatesHum++;
          }
        }
      
        // Sleep für Buttons und DHT
        delay(100);  // Schnelle Verzögerung, damit der Button-Check nicht zu oft passiert
      }
      
      

      I should mention that although I tested the DHT22 earlier, I didn’t connect it when testing with Home Assistant—just in case that’s relevant to the issue.

      posted in Troubleshooting
      Commodoreuno
      Commodoreuno