DS18B20 and DHT22 in the same sensor and sketch



  • Hi,

    I have created a sensor with both a DHT22 and a DS18B20 which will be located in my bathroom with the main unit with the DHT22 inside the bathroom and the DS18B20 (cable mounted) will be located in my sauna

    This seems to work fine but when I view the sensor in Domoticz it shows two devices with temp and humidity for both of them! I only want the DHT22 device to be temp & humidity and the other device to be temp only

    Is there a way to do this in the code?

    I have used the example sketches for Humidity and Temperature and put them together into one sketch so that I have one sensor with three child devices

    Anyone that could give me a hint?

    Regards,
    Mikael



  • Hi

    I have a node with DHT22 and a DS18B20, using this code.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     * 
     * DESCRIPTION
     * This sketch provides an example how to implement a humidity/temperature
     * sensor using DHT11/DHT-22 
     * http://www.mysensors.org/build/humidity
     *
     * Example sketch showing how to send in DS1820B OneWire temperature readings back to the controller
     * http://www.mysensors.org/build/temp
     */
    
    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <DHT.h>  
    
    #define COMPARE_TEMP 0 // Send temperature only if changed? 1 = Yes 0 = No
    #define ONE_WIRE_BUS 4 // Pin where dallas sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    #define CHILD_ID_HUM 1
    #define CHILD_ID_TEMP 2
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msg(0,V_TEMP);
    
    MySensor gw;
    DHT dht;
    float lastTemp;
    float lastHum;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    
    void setup()  
    { 
      gw.begin();
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 
    
      // Send the Sketch Version Information to the Gateway
      gw.sendSketchInfo("Humidity", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      
      metric = gw.getConfig().isMetric;
    
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin();
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor", "1.1");
    
      // Fetch the number of attached temperature sensors  
      numSensors = sensors.getDeviceCount();
    
      // Present all sensors to controller
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         gw.present(i, S_TEMP);
      }
    }
    
    void loop()      
    {  
      delay(dht.getMinimumSamplingPeriod());
    
      float temperature = dht.getTemperature();
      if (isnan(temperature)) {
          Serial.println("Failed reading temperature from DHT");
      } else if (temperature != lastTemp) {
        lastTemp = temperature;
        if (!metric) {
          temperature = dht.toFahrenheit(temperature);
        }
        gw.send(msgTemp.set(temperature, 1));
        Serial.print("T: ");
        Serial.println(temperature);
      }
      
      float humidity = dht.getHumidity();
      if (isnan(humidity)) {
          Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum) {
          lastHum = humidity;
          gw.send(msgHum.set(humidity, 1));
          Serial.print("H: ");
          Serial.println(humidity);
      }
    // Process incoming messages (like config from server)
      gw.process(); 
    
      // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures();
    
      // query conversion time and sleep until conversion completed
      int16_t conversionTime = sensors.millisToWaitForConversion(sensors.getResolution());
      // sleep() call can be replaced by wait() call if node need to process incoming messages (or if node is repeater)
      gw.sleep(conversionTime);
    
      // Read temperatures and send them to controller 
      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
     
        // Fetch and round temperature to one decimal
        float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
     
        // Only send data if temperature has changed and no error
        #if COMPARE_TEMP == 1
        if (lastTemperature[i] != temperature && temperature != -127.00 && temperature != 85.00) {
        #else
        if (temperature != -127.00 && temperature != 85.00) {
        #endif
     
          // Send in the new temperature
          gw.send(msg.setSensor(i).set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature[i]=temperature;
        }
      }
      gw.sleep(SLEEP_TIME); //sleep a bit
    }
    
    

    This is what i get from the sensors.

    DS18B20 03-03-2016 17:49:21: 0;0;3;0;9;read: 7-7-0 s=0,c=1,t=0,pt=7,l=5,sg=0:2.3
    DHT22 03-03-2016 17:49:20: 0;0;3;0;9;read: 7-7-0 s=1,c=1,t=1,pt=7,l=5,sg=0:71.2
    DHT22 03-03-2016 17:49:20: 0;0;3;0;9;read: 7-7-0 s=2,c=1,t=0,pt=7,l=5,sg=0:5.8

    Try if this works better? I use PiDome as the controller.



  • Thanks for a quick reply!

    I tested this code tonight but have the same problem, two devices popup in Domoticz but the second one (with DS18B20) also reports humidity which i guess is received from DHT22

    Not a very big problem if you know the setup but you could easily think that the humidity comes from the same measurement point as the DS18B20 which it dont



  • Hi

    I got a problem when I would vertifiera sketch

    Does anyone have a working sketch dallas and DHT



  • Hi !

    I've same problem to make a node with 2 DS18B20 and 2 DHT22.

    In domoticz the DS18B20 sensors are show with humidity value (of the 1st DHT22) and the 2nd DHT22 show the humidity value of the 1st one...

    The sketch i'm using is here: https://forum.mysensors.org/topic/5222/node-with-ds18b20-and-dht22

    (sorry for my bad english)


Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.