Page 1 sur 1

Node multi sensors DHT22 + DS18B20

Publié : 13 oct. 2016, 17:59
par dokho
Bonjour,

Je cherche à créer un node multi-sensors composé de 5 sondes DS18B20 et de 2 DHT22.

Je fais des essais sur table avec 2 DS18B20 et 2 DHT et je m'en sors pas trop mal pour l'instant car j'ai toutes les remontées jusqu'à domoticz.

Mon soucis c'est que les DS18B20 sont reconnues dans domoticz comme les DHT22, à savoir: TEMP+HUMIDITY (WTGR800).

Avant de me mettre à nettoyer mon sketch, qui est vraiment crado, j'aimerais savoir si il est possible de résoudre ce problème ou si je dois m'orienter vers un node DS18B20 + un node DHT22.


Voici le code (en mode brouillon mais qui fonctionne):

Code : Tout sélectionner

// Enable debug prints
#define MY_DEBUG

// Enable and select radio type attached 
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
//#define MY_RS485
 
#include <SPI.h>
#include <MySensors.h>  
#include <DHT.h>
#include <DallasTemperature.h>
#include <OneWire.h>

#define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 2

// Set this to the pin you connected the DHT's data pin to
#define DHT1_DATA_PIN 2
#define DHT2_DATA_PIN 3

// Set this offset if the sensor has a permanent small offset to the real temperatures
#define SENSOR_TEMP_OFFSET 0

// Sleep time between sensor updates (in milliseconds)
// Must be >1000ms for DHT22 and >2000ms for DHT11
static const uint64_t UPDATE_INTERVAL = 30000;

// Force sending an update of the temperature after n sensor reads, so a controller showing the
// timestamp of the last update doesn't show something like 3 hours in the unlikely case, that
// the value didn't change since;
// i.e. the sensor would force sending an update every UPDATE_INTERVAL*FORCE_UPDATE_N_READS [ms]
static const uint8_t FORCE_UPDATE_N_READS = 10;

#define CHILD_ID_HUM1 6
#define CHILD_ID_TEMP1 7
#define CHILD_ID_HUM2 8
#define CHILD_ID_TEMP2 9


float lastTemp1;
float lastHum1;
uint8_t nNoUpdatesTemp1;
uint8_t nNoUpdatesHum1;

float lastTemp2;
float lastHum2;
uint8_t nNoUpdatesTemp2;
uint8_t nNoUpdatesHum2;
bool metric = true;

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;
bool receivedConfig = false;


MyMessage msgDallas(0,V_TEMP);
MyMessage msgHum1(CHILD_ID_HUM1, V_HUM);
MyMessage msgTemp1(CHILD_ID_TEMP1, V_TEMP);
MyMessage msgHum2(CHILD_ID_HUM2, V_HUM);
MyMessage msgTemp2(CHILD_ID_TEMP2, V_TEMP);
DHT dht;

void before()
{
  // Startup up the OneWire library
  sensors.begin();
}
void setup()
{
// requestTemperatures() will not block current thread
  sensors.setWaitForConversion(false);  
}
void presentation()  
{ 
  // Send the sketch version information to the gateway
  sendSketchInfo("DualDHTandDallas", "1.1");
  
  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID_HUM1, S_HUM);
  present(CHILD_ID_TEMP1, S_TEMP);
  present(CHILD_ID_HUM2, S_HUM);
  present(CHILD_ID_TEMP2, S_TEMP);
  
  metric = getConfig().isMetric;

   // Fetch the number of attached temperature sensors  
  numSensors = sensors.getDeviceCount();
//numSensors = 2;
  // Present all sensors to controller
  for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
     present(i, S_TEMP);
}
}





void loop()      
{ 

// 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)
  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>((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
      send(msgDallas.setSensor(i).set(temperature,1));
      Serial.print("Dallas");
      Serial.println(i);
      Serial.println(" :");
    Serial.println(temperature);
      // Save new temperatures for next compare
      lastTemperature[i]=temperature;
    //}
  }

// Sleep for a while to save energy
  sleep(UPDATE_INTERVAL);

   
  dht.setup(DHT1_DATA_PIN); // set data pin of DHT1 sensor
  if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
    Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  }
  // Sleep for the time of the minimum sampling period to give the sensor time to power up
  // (otherwise, timeout errors might occure for the first reading)
  sleep(dht.getMinimumSamplingPeriod());
  
  // Force reading sensor, so it works also after sleep()
  dht.readSensor(true);
  
  // Get temperature from DHT library
  float temperature1 = dht.getTemperature();
  if (isnan(temperature1)) {
    Serial.println("Failed reading temperature from DHT!");
  } else if (temperature1 != lastTemp1 || nNoUpdatesTemp1 == FORCE_UPDATE_N_READS) {
    // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
    lastTemp1 = temperature1;
    if (!metric) {
      temperature1 = dht.toFahrenheit(temperature1);
    }
    // Reset no updates counter
    nNoUpdatesTemp1 = 0;
    temperature1 += SENSOR_TEMP_OFFSET;
    send(msgTemp1.set(temperature1, 1));

    //#ifdef MY_DEBUG
    //Serial.print("T1: ");
    //Serial.println(temperature1);
    //#endif
  } else {
    // Increase no update counter if the temperature stayed the same
    nNoUpdatesTemp1++;
  }

  // Get humidity from DHT library
  float humidity1 = dht.getHumidity();
  if (isnan(humidity1)) {
    Serial.println("Failed reading humidity from DHT");
  } else if (humidity1 != lastHum1 || nNoUpdatesHum1 == FORCE_UPDATE_N_READS) {
    // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
    lastHum1 = humidity1;
    // Reset no updates counter
    nNoUpdatesHum1 = 0;
    send(msgHum1.set(humidity1, 1));
    
    #ifdef MY_DEBUG
    Serial.print("H1: ");
    Serial.println(humidity1);
    #endif
  } else {
    // Increase no update counter if the humidity stayed the same
    nNoUpdatesHum1++;
  }

  // Sleep for a while to save energy
  sleep(UPDATE_INTERVAL); 


dht.setup(DHT2_DATA_PIN); // set data pin of DHT2 sensor
  if (UPDATE_INTERVAL <= dht.getMinimumSamplingPeriod()) {
    Serial.println("Warning: UPDATE_INTERVAL is smaller than supported by the sensor!");
  }
  // Sleep for the time of the minimum sampling period to give the sensor time to power up
  // (otherwise, timeout errors might occure for the first reading)
  sleep(dht.getMinimumSamplingPeriod());
  
  // Force reading sensor, so it works also after sleep()
  dht.readSensor(true);
  
  // Get temperature from DHT library
  float temperature2 = dht.getTemperature();
  if (isnan(temperature2)) {
    Serial.println("Failed reading temperature from DHT!");
  } else if (temperature2 != lastTemp1 || nNoUpdatesTemp2 == FORCE_UPDATE_N_READS) {
    // Only send temperature if it changed since the last measurement or if we didn't send an update for n times
    lastTemp1 = temperature2;
    if (!metric) {
      temperature2 = dht.toFahrenheit(temperature2);
    }
    // Reset no updates counter
    nNoUpdatesTemp2 = 0;
    temperature2 += SENSOR_TEMP_OFFSET;
    send(msgTemp2.set(temperature2, 1));

    //#ifdef MY_DEBUG
    //Serial.print("T2: ");
    //Serial.println(temperature2);
    //#endif
  } else {
    // Increase no update counter if the temperature stayed the same
    nNoUpdatesTemp2++;
  }

  // Get humidity from DHT library
  float humidity2 = dht.getHumidity();
  if (isnan(humidity2)) {
    Serial.println("Failed reading humidity from DHT");
  } else if (humidity2 != lastHum2 || nNoUpdatesHum2 == FORCE_UPDATE_N_READS) {
    // Only send humidity if it changed since the last measurement or if we didn't send an update for n times
    lastHum2 = humidity2;
    // Reset no updates counter
    nNoUpdatesHum2 = 0;
    send(msgHum2.set(humidity2, 1));
    
    #ifdef MY_DEBUG
    Serial.print("H2: ");
    Serial.println(humidity2);
    #endif
  } else {
    // Increase no update counter if the humidity stayed the same
    nNoUpdatesHum2++;
  }

 
  // Sleep for a while to save energy
  sleep(UPDATE_INTERVAL); 

}
Quelqu'un saurait me dire d'où cela peut provenir ?

Re: Node multi sensors DHT22 + DS18B20

Publié : 15 oct. 2016, 15:18
par dokho
Personne ? :(