Help with combination DHT and Relay



  • Hey I am building up a useful mysensors network around my apartment and could use a helping hand. I have several single purpose setups going (relay, dht, moisture, etc) but I would really like to learn to roll them together. My first attempt is for a DHT and relay. I have combined the code but I am getting radio init error in the serial monitor. Could someone lend an eye to help me learn this? Thanks in advance!

    /** combo mysensors - DHT11/22 and Single Relay
     *  
     *   * 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.  
     */
    
    /** 
     *  Humidity
     *   * 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
     *  
     */
    
    /**
     * Relay
     * * REVISION HISTORY
     * Version 1.0 - Henrik Ekblad
     * 
     * DESCRIPTION
     * Example sketch showing how to control physical relays. 
     * This example will remember relay state after power failure.
     * http://www.mysensors.org/build/relay
     */
    
    
    /**-------------------------------------------------------------------------------------------------------------
     * INCLUSIONS
     */
    
    /**
     * Humidity
     */
    #include <SPI.h>
    #include <MySensor.h>  
    #include <DHT.h>  
    /**
     * Relay
     */
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    
    /**-------------------------------------------------------------------------------------------------------------
     * DEFINE
     */
    
    /**
     * Humidity
     */
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    /**
     * Relay
     */
    #define RELAY_1  4  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define NUMBER_OF_RELAYS 1 // Total number of attached relays
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    /**-------------------------------------------------------------------------------------------------------------
     * RADIO - I THINK
     */
    
    /**
     * Relay
     */
    
    // NRFRF24L01 radio driver (set low transmit power by default) 
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);  
    //MyTransportRFM69 radio;
    // Message signing driver (none default)
    //MySigningNone signer;
    // Select AtMega328 hardware profile
    MyHwATMega328 hw;
    // Construct MySensors library
    MySensor gw(radio, hw);
    
    
    /**
     * Humidity
     */
    
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true; 
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    
    
    /**-------------------------------------------------------------------------------------------------------------
     * VOID SETUP
     */
    
    void setup()  
    { 
    
      /**
     * Humidity
     */
    
    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;
    
    /**
     * Relay
     */
     
      // Initialize library and add callback for incoming messages
      gw.begin(incomingMessage, AUTO, true);
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Relay", "1.0");
    
      // Fetch relay status
      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++) {
        // Register all sensors to gw (they will be created as child devices)
        gw.present(sensor, S_LIGHT);
        // Then set relay pins in output mode
        pinMode(pin, OUTPUT);   
        // Set relay to last known state (using eeprom storage) 
        digitalWrite(pin, gw.loadState(sensor)?RELAY_ON:RELAY_OFF);
      }
    
    }
    
    /**-------------------------------------------------------------------------------------------------------------
     * VOID LOOP
     */
    
    void loop()      
    {  
      /**
     * Humidity
     */
    
    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);
      }
    
      gw.sleep(SLEEP_TIME); //sleep a bit
    
    /**
     * Relay
     */
    
    // Alway process incoming messages whenever possible
      gw.process();
    
    }
    
    /**-------------------------------------------------------------------------------------------------------------
     * INCOMING MESSAGE RELAY ONLY
     */
    
    void incomingMessage(const MyMessage &message) {
      // We only expect one type of message from controller. But we better check anyway.
      if (message.type==V_LIGHT) {
         // Change relay state
         digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);
         // Store state in eeprom
         gw.saveState(message.sensor, message.getBool());
         // Write some debug info
         Serial.print("Incoming change for sensor:");
         Serial.print(message.sensor);
         Serial.print(", New status: ");
         Serial.println(message.getBool());
       } 
    }
    
    
    
     
    

  • Contest Winner

    @punter9

    a little advice...

    comments seem really helpful for beginners, but believe me you will look to lose as much of that as possible once you really learn this stuff.

    put what I did (untested) side-by-side with your code and look for the critical differences in the setup() function.

    
    #include <SPI.h>
    #include <MySensor.h>
    #include <DHT.h>
    
    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    
    #define CHILD_ID_HUM 0
    #define CHILD_ID_TEMP 1
    #define CHILD_ID_RELAY 2
    #define RELAY_PIN 4
    #define HUMIDITY_SENSOR_DIGITAL_PIN 3
    //unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    
    #define RELAY_ON 1  // GPIO value to write to turn on attached relay
    #define RELAY_OFF 0 // GPIO value to write to turn off attached relay
    
    MyTransportNRF24 radio(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
    
    MyHwATMega328 hw;
    
    MySensor gw(radio, hw);
    
    DHT dht;
    float lastTemp;
    float lastHum;
    boolean metric = true;
    MyMessage msgHum(CHILD_ID_HUM, V_HUM);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    
    void setup()
    {
      gw.begin(incomingMessage, AUTO, true);
      dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
      gw.sendSketchInfo("Combo", "1.0");
      gw.present(CHILD_ID_HUM, S_HUM);
      gw.present(CHILD_ID_TEMP, S_TEMP);
      gw.present(CHILD_ID_RELAY, S_LIGHT);
      metric = gw.getConfig().isMetric;
      pinMode(RELAY_PIN, OUTPUT);
      digitalWrite(RELAY_PIN, gw.loadState(1) ? RELAY_ON : RELAY_OFF);
    }
    
    void loop()
    {
      gw.process();
      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);
      }
    }
    
    void incomingMessage(const MyMessage &message) 
    {
      if (message.type == V_LIGHT) 
      {
        digitalWrite(RELAY_PIN, message.getBool() ? RELAY_ON : RELAY_OFF);
        gw.saveState(1, message.getBool());
        Serial.print("Incoming change for sensor:");
        Serial.print(message.sensor);
        Serial.print(", New status: ");
        Serial.println(message.getBool());
      }
    }
    

    @hek

    folks trying to extend that multi-relay sketch always seem to have issues. Perhaps a better executed extensible sketch is in order!



  • Thanks! right off the bat I am seeing this being added

    gw.present(CHILD_ID_RELAY, S_LIGHT);

    and this instead of each sketch called out this being in there

    gw.sendSketchInfo("Combo", "1.0");

    will this still work with domoticz?


  • Contest Winner

    @punter9 said:

    this creates an instance of the MySensor class called "gw"

    MySensor gw(radio, hw);
    

    this runs the "present" function of the MySensors class in the gw instance, which tells your controller to create an instance of the device on Domoticz

    gw.present(CHILD_ID_RELAY, S_LIGHT);
    

    this function sends the name of the sketch and its version number to Domoticz to the parent device. You can only have one name...

     gw.sendSketchInfo("Combo", "1.0");
    

    will this still work with domoticz?

    recommend that you try it!



  • ha! you are awesome. Thank you so much


  • Admin

    @BulldogLowell said:

    folks trying to extend that multi-relay sketch always seem to have issues. Perhaps a better executed extensible sketch is in order!

    Yes, do you have an idea how this would be designed?



  • okay got it working and both sensors have been found in domoticz. Only issue is relay doesn't turn on or off with control. But it has been found. I have it plugged into pin 4, checked that it worked by manually jumping the pins, and I can see the tx/rx light blink when I click the button in domoticz (but no relay click).


  • Contest Winner

    @hek @punter9 What do you guys think the sketch should do? Just control one single relais? If you guys don't have the time I'm glad to help you. Just want to return the favour for all that you guys have done. I'm really happy with MySensors.



  • Thanks for checking in @TheoL . My intent for this sketch was to learn how to combine sketches and make a useful project. Got that done and working! It would be cool if there was an easy way to help newbies like myself out doing this. I can tell you without this awesome forum I would have not figured out some pretty basic stuff! Thanks for all the help, this is an awesome site


  • Contest Winner

    @punter9 said:

    hanks for checking in @TheoL . My intent for this sketch was to learn how to combine sketches and make a useful project. Got that done and working! It would be cool if there was an easy way to help newbies like myself out doing this. I can tell you without this awesome forum I would have not figured out some pretty basic stuff! Thanks for all the help, this is an awesome site

    what does the serial monitor show when you click the button in Domoticz?


Log in to reply
 

Suggested Topics

  • 3
  • 8
  • 6
  • 1
  • 2

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts