Navigation

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

    Best posts made by turborick

    • RE: 2 or more DHT22 - Sensors on one Arduino pro mini

      I found this forum thread but it must be for a version prior to 2.0
      link text

      can this sketch be converted to 2.0?

      #include <SPI.h>
      #include <MySensor.h>
      #include <DHT.h>
      
      #define NUM_SENSORS 2
      
      unsigned long SLEEP_TIME = 3000UL; // Sleep time between reads (in milliseconds)
      
      MySensor gw;
      MyMessage tempMssg(0, V_TEMP);
      MyMessage humMssg(NUM_SENSORS, V_HUM);
      
      byte tempID[NUM_SENSORS] = {0,1};
      byte humID[NUM_SENSORS] = {2,3};
      
      
      DHT* dht[NUM_SENSORS];
      byte sensorPin[NUM_SENSORS] = {3, 4};
      float lastTemp[NUM_SENSORS] = {0.0, 0.0};
      float lastHum[NUM_SENSORS] = {0.0, 0.0};
      
      boolean metric = true;
      
      void setup()
      {
        Serial.begin(9600);
        gw.begin();
        for (int i = 0; i < NUM_SENSORS; i++)
        {
          dht[i] = new DHT;
          dht[i]->setup(sensorPin[i]);
        }
      
        gw.sendSketchInfo("Humidity", "1.0");
        
        for (int i = 0; i < NUM_SENSORS; i++)
        {
          gw.present(tempID[i], S_TEMP);
          gw.present(humID[i], S_HUM);
        }
        
        metric = gw.getConfig().isMetric;
        Serial.println(F("Setup Complete."));
      }
      
      void loop()
      {
        for (int i = 0; i < NUM_SENSORS; i++)
        {
          delay(dht[i]->getMinimumSamplingPeriod());
          float temperature = dht[i]->getTemperature();
          if (isnan(temperature))
          {
            Serial.print(F("Failed reading temperature from DHT"));
            Serial.println(i);
          }
          else if (temperature != lastTemp[i])
          {
            lastTemp[i] = temperature;
            if (!metric)
            {
              temperature = dht[i]->toFahrenheit(temperature);
            }
            gw.send(tempMssg.setSensor(i).set(temperature, false));  // no ack
            Serial.print(F("T"));
            Serial.print(i);
            Serial.print(F("= "));
            Serial.println(temperature);
          }
          float humidity = dht[i]->getHumidity();
          if (isnan(humidity)) 
          {
            Serial.print("Failed reading humidity from DHT");
            Serial.println(i);
          } 
          else if (humidity != lastHum[i]) 
          {
            lastHum[i] = humidity;
            gw.send(humMssg.setSensor(i).set(humidity, false));  // no ack
            Serial.print(F("H"));
            Serial.print(i);
            Serial.print(F("= "));
            Serial.println(humidity);
          }
        }
        gw.sleep(SLEEP_TIME); //sleep a bit
      }```
      posted in Development
      turborick
      turborick