Mysensort mit DHT12 (i2C)



  • Hello

    Does anyone built up an Mysenors Humidity Node with an DHT12 it is an I2C sensor...

    but I2C is needet for the NRF24+ or?

    Thanks


  • Mod

    @michlb1982 the nrf radio uses SPI, not I2C.

    I haven't seen anyone using the dht12, but there are examples that use other i2c sensors.
    https://www.mysensors.org/build/pressure
    https://www.mysensors.org/build/light-bh1750
    https://www.mysensors.org/build/rfid



  • Well i did it... i've got a working MYSENSOR with DHT12...
    here the Code ... its not nice but it works...

    // Enable debug prints
    #define MY_DEBUG
    
    // Define Node ID
    #define MY_NODE_ID 128
    //#define MY_REPEATER_FEATURE
    
    
    // Enable and select radio type attached 
    #define MY_RADIO_NRF24
    
    #include <SPI.h>
    #include <MySensors.h>  
    #include <DHT12.h>
    #include <Wire.h>
    DHT12 dht12;
    
    #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;
    
    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);
    
    
    
    void presentation()  
    { 
      // Send the sketch version information to the gateway
      sendSketchInfo("DHT12", "1.1");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_HUM, S_HUM);
      present(CHILD_ID_TEMP, S_TEMP);
    
      //metric = getControllerConfig().isMetric;
    }
    
    
    void setup()
    {
      Wire.begin();
    }
    
    
    void loop()      
    {  
      // Force reading sensor, so it works also after sleep()
      //dht12.readSensor(true);
    
      // Get temperature from DHT library
      float temperature = dht12.readTemperature();
      if (isnan(temperature)) {
        Serial.println("Failed reading temperature from DHT!");
      } else if (temperature != lastTemp || nNoUpdatesTemp == 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
        lastTemp = temperature;
    
        // apply the offset before converting to something different than Celsius degrees
        temperature += SENSOR_TEMP_OFFSET;
    
        if (!metric) {
          temperature = dht12.readTemperature();
        }
        // Reset no updates counter
        nNoUpdatesTemp = 0;
        send(msgTemp.set(temperature, 1));
    
        #ifdef MY_DEBUG
        Serial.print("T: ");
        Serial.println(temperature);
        #endif
      } else {
        // Increase no update counter if the temperature stayed the same
        nNoUpdatesTemp++;
      }
    
      // Get humidity from DHT library
      float humidity = dht12.readHumidity();
      if (isnan(humidity)) {
        Serial.println("Failed reading humidity from DHT");
      } else if (humidity != lastHum || nNoUpdatesHum == 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
        lastHum = humidity;
        // Reset no updates counter
        nNoUpdatesHum = 0;
        send(msgHum.set(humidity, 1));
    
        #ifdef MY_DEBUG
        Serial.print("H: ");
        Serial.println(humidity);
        #endif
      } else {
        // Increase no update counter if the humidity stayed the same
        nNoUpdatesHum++;
      }
    
      // Sleep for a while to save energy
      sleep(UPDATE_INTERVAL); 
      //wait(UPDATE_INTERVAL); //for repeaters
    }
    

    i Used this library: https://github.com/Bobadas/DHT12_library_Arduino/blob/master/README.md

    here the datasheet of the dht12: http://www.robototehnika.ru/file/DHT12.pdf


Log in to reply
 

Suggested Topics

  • 87
  • 3
  • 7
  • 8
  • 1
  • 5

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts