Temp and Relay node



  • Hi Everybody,
    First of all Thanks to all of you people and Mysensor Team for creating this and sharing it to all of us.
    I’m one of „ninjasphere“ backers, and after this project crashed, I started searching some DIY possibilities and I found this. I’m running Domoticz (ver.2563) in RPi2 and serial nano GW (on 1.4 code and libraries). I successfully created Temp/Hum and multiple Temp sensors but I got stuck on Temp/Relay node. Temperatures are working just fine (5pcs. DS 18B20) but the Relay won’t work. Temp sensors are on Pin3, Relays on 5 and 6. I’m using sketch I found here on forum:

     // Example sketch showing how to send in OneWire temperature readings
        // Example sketch showing how to control physical relays.
        // This example will remember relay state even after power failure.
        
        #include <MySensor.h>
        #include <SPI.h>
        #include <DallasTemperature.h>
        #include <OneWire.h>
        
        #define ONE_WIRE_BUS 3 // Pin where dallas sensor is connected 
        #define MAX_ATTACHED_DS18B20 16
        
        #define RELAY_1  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
        #define NUMBER_OF_RELAYS 2 // 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
        #define NUMBER_OF_TEMP_SENSORS 5
        
        unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds) 30000 orig
        OneWire oneWire(ONE_WIRE_BUS);
        DallasTemperature sensors(&oneWire);
        MySensor gw;
        float lastTemperature[MAX_ATTACHED_DS18B20];
        //int numSensors = 0;
        boolean receivedConfig = false;
        boolean metric = true;
        // Initialize temperature message
        MyMessage msg(0, V_TEMP);
        //int offsetforrelays = 5;
        void setup()
        {
          // Startup OneWire
          sensors.begin();
          gw.begin(incomingMessage, AUTO, true);        // Startup and initialize MySensors library. Set callback for incoming messages.
          gw.sendSketchInfo("Temp and Relays", "1.0");   // Send the sketch version information to the gateway and Controller
        
          for (int i = 0; i < NUMBER_OF_TEMP_SENSORS ; i++)
          {
            gw.present(i, V_TEMP); // creates 0, 1 and 2
          }
          for (int sensor = 1, pin = RELAY_1 ; sensor <= NUMBER_OF_RELAYS ; sensor++ , pin++)
            //   loop from sensor1/pin4 to sensor2/pin5 (numofrelays=2)
          {
            gw.present(NUMBER_OF_TEMP_SENSORS + sensor, S_LIGHT); // should create 3 and 4
            pinMode(pin, OUTPUT);
            digitalWrite(pin, gw.loadState(sensor) ? RELAY_ON : RELAY_OFF);
          }
        }
        
         void loop()
        {
          // Process incoming messages (like config from server)
          gw.process();
        
          // Fetch temperatures from Dallas sensors
          sensors.requestTemperatures();
        
          // Read temperatures and send them to controller
          for (int i = 0; i < NUMBER_OF_TEMP_SENSORS && 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 more then 1 degC and no error
            if (int(lastTemperature[i]) != int(temperature) && temperature != -127.00) { //added integer
        
              // Send in the new temperature
              gw.send(msg.setSensor(i).set(temperature, 1));
              lastTemperature[i] = temperature;
            }
          }
          //gw.sleep(SLEEP_TIME); //no sleep for relays!!!!
        }
        
        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
            Serial.print("Incoming change for sensor:");
            Serial.println(message.sensor);
            digitalWrite(message.sensor - 4 + 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());
          }
        }
    

    When I push switch in Domoticz it just freeze up, and I have to unplug RPI and restart it. Do I need some special adjustment in Domoticz for button?
    Does anybody have working relay/temp sketch on Domoticz, or can anybody give me some advice?
    Thanks guys


  • Hardware Contributor

    It didnt work with domoticz and that version for me. The relay was just dead. I uninstalldd domotizc and running 2608 and it works.



  • Hi sundberg84 , Thanks for your advice. I was only able to upgrade to beta 2650 (my programing skills are very poor – I followed steps in Domoticz wiki). Domoticz environment is working OK now, I can switch and icons are changing properly. Relays are still death. I replaced definition of 1/0 for relay write in arduino code so relay LED begun light. In serial monitor of node I can nicely see “incoming change for sensor” and “new status” by every command from domoticz for each relay, but there is no switching at all.



  • Sounds like a problem in the arduino <-> relay communication as the Arduino is receiving data from Domoticz. Have you tested that the relay works by directly putting power on the signal pin of the relay?



  • When I´m uploading sketch both two relays start to light and during upload switch off and when uploaded turn on.


  • Hero Member

    @mikee The switching on and off is normal as the sketch tries to restore the last saved state of the relay.
    do this:
    first try this sketch:

    #include <SPI.h>
    void setup() {
        pinMode(3, OUTPUT);
        digitalWrite(3, HIGH);
        delay(10000);
        digitalWrite(3, LOW);
        delay(10000);
        digitalWrite(3, HIGH);
        delay(10000);
        digitalWrite(3, LOW);
    }
    
    void loop()
    {}
    
        
    

    This should turn your relay (assuming it is on pin 3) on and off twice with 10 seconds between operations. I don't have one connected now so cant check it but it does compile and should work. you can also try this on arduino with no NRF.
    Let me know if it works or not



  • Hi Moshe,
    I´ll try this after I come home, thanks



  • You should not query sensor temperatures every 750 ms. Try this code with proper pause between sensor readings: http://forum.mysensors.org/topic/1562/door-motion-and-temperature-sensor/34

    Also, your code is in constant 750ms delay, so incoming messages are delayed or even skipped. You should use newer code from git with non-blocking sensor readings: https://github.com/mysensors/Arduino/blob/master/libraries/MySensors/examples/DallasTemperatureSensor/DallasTemperatureSensor.ino

    Your code looks like this now:

    void loop()
    {
        gw.process(); 
        delay(750); // 750ms of no incoming messages processing, no response to relay msgs
    }
    

  • Hardware Contributor

    Hi @mikee.

    It seems like the same problem in having at the moment: link
    Domoticz send command, node recieves and the node/relay led turns on but the coil does not activate.

    I tried with higher volatge and then the coil activated. I dont know why this happens but seems like some power issues.
    I have measure 5v over the signal but maybe to low mA to activate? According to datasheet you need 71.4mA
    I dont know how to measure this or if thats is more than the arduino can handle?



  • Exactly - I tried other sketch yesterday, that worked same way - LED turned on, but contacts stayed off. I will try to power the relay board separately with raw voltage from Arduino (9V)


  • Hero Member

    @mikee the relays can't take more than 5.5v.
    @robosensor is very right. The relay sensor sketch in the examples is made to also be a repeater node. repeater nodes (and nodes that can receive incoming messages) are looping endlessly processing messages. when you merged the sketches you got a sketch that tried to loop endlessley and to pause 750ms at the same time. this won't work.... you need to use the non blocking functions and implement the delay in the sketch the way the examples he sent are doing it.


  • Hero Member

    @sundberg84 try the mini sketch above - it does not use the NRF and has no network or controller dependence so it will tell you if there is enough juice to power the relays.



  • Thanks @robosensor , I´ll try it.
    @Moshe Livne, Sure they can - on coil (at least mine is built so), but board must have jumpers to separate it . Input must be 5V- of course transistor would burn. I'll try your suggestions, thanks



  • Hi Guys,
    So after few day I´m back again 😑 . I started with @Moshe Livne test – worked just fine - thanks. I found more relay examples and the one from @petewill http://forum.mysensors.org/topic/775/8-lamp-outlet-smart-plug-module worked!!! Now I´m trying to merge new Dallas sketch – mentioned by robosensor with petewill s 8$ outlet:

    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    //Relay 
    #define RELAY_PIN  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_CHILD 0
    #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
    
    //unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msgT(0,V_TEMP);
    MyMessage msg(RELAY_CHILD,V_LIGHT);
    
    
    void setup()  
    { 
      // Startup OneWire 
      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 w relay", "1.0");
    
      // 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);
         gw.present(RELAY_CHILD, S_LIGHT);
          digitalWrite(RELAY_PIN, RELAY_OFF);
          pinMode(RELAY_PIN, OUTPUT);
      }
    }
    
    
    void loop()     
    {     
      // 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 (lastTemperature[i] != temperature && temperature != -127.00) {
     
          // Send in the new temperature
          gw.send(msgT.setSensor(i).set(temperature,1));
          lastTemperature[i]=temperature;
        }
      }
      }
      
      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(RELAY_PIN, 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());
       } 
    
      
      
    
      
      //gw.sleep(SLEEP_TIME);
    }
    

    It compiles, temperatures are working but relay is not working, can someone help me, pls.

    @sundberg84 next day after I switch to latest Domoticz beta system started to freeze again. I´ts only after relay switch command pushed by user or even by timer (when I checked the logs of the switch all times are loged 5-7 minutes later than the switch time). Is there any tutorial or does exist system how to switch my Domoticz to your version (2606), without installing all Domoticz manually? I was able to run SD versions only and 2606 is zip file.
    Thanks for your time Guys


  • Hero Member

    You need to initialize the incoming messages in gw.begin() otherwise you will never receive a message. i.e. gw.begin(incomingMessage, AUTO, false);

    For the update of Domoticz, switch on the beta channel in settings and push the update button. No need to reinstall.



  • HI, @AWI I tried

    gw.begin(incomingMessage, AUTO, false);
    

    and then

    gw.begin(incomingMessage, NODE_ID);
    

    After I changed #define RELAY_CHILD 0 to 6 and once (one of many tries) I turned it on, but couldn’t turn it off. In serial monitor of node is no command to switch visible.

    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #define NODE_ID 19
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    //Relay 
    #define RELAY_PIN  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_CHILD 0
    #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
    
    //unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msgT(0,V_TEMP);
    MyMessage msg(RELAY_CHILD,V_LIGHT);
    
    
    void setup()  
    { 
      // Startup OneWire 
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin(incomingMessage, AUTO, false); 
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor w relay", "1.0");
    
      // 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);
         gw.present(RELAY_CHILD, S_LIGHT);
          digitalWrite(RELAY_PIN, RELAY_OFF);
          pinMode(RELAY_PIN, OUTPUT);
      }
    }
    void loop()     
    {     
      // 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 (lastTemperature[i] != temperature && temperature != -127.00) {
     
          // Send in the new temperature
          gw.send(msgT.setSensor(i).set(temperature,1));
          lastTemperature[i]=temperature;
        }
      }
    }
      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(RELAY_PIN, 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());
       }   
      //gw.sleep(SLEEP_TIME);
    }
    #include <MySensor.h>  
    #include <SPI.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    
    #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
    #define MAX_ATTACHED_DS18B20 16
    //Relay 
    #define RELAY_PIN  5  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
    #define RELAY_CHILD 0
    #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
    
    //unsigned long SLEEP_TIME = 30000; // Sleep time between reads (in milliseconds)
    OneWire oneWire(ONE_WIRE_BUS);
    DallasTemperature sensors(&oneWire);
    MySensor gw;
    float lastTemperature[MAX_ATTACHED_DS18B20];
    int numSensors=0;
    boolean receivedConfig = false;
    boolean metric = true; 
    // Initialize temperature message
    MyMessage msgT(0,V_TEMP);
    MyMessage msg(RELAY_CHILD,V_LIGHT);
    
    
    void setup()  
    { 
      // Startup OneWire 
      sensors.begin();
      // requestTemperatures() will not block current thread
      sensors.setWaitForConversion(false);
    
      // Startup and initialize MySensors library. Set callback for incoming messages. 
      gw.begin(incomingMessage, AUTO, false); 
    
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Temperature Sensor w relay", "1.0");
    
      // 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);
         gw.present(RELAY_CHILD, S_LIGHT);
          digitalWrite(RELAY_PIN, RELAY_OFF);
          pinMode(RELAY_PIN, OUTPUT);
      }
    }
    void loop()     
    {     
      // 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 (lastTemperature[i] != temperature && temperature != -127.00) {
     
          // Send in the new temperature
          gw.send(msgT.setSensor(i).set(temperature,1));
          lastTemperature[i]=temperature;
        }
      }
    }
      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(RELAY_PIN, 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());
       }   
      //gw.sleep(SLEEP_TIME);
    }
    

    Is something wrong with identification of relay? Temperatures are working just fine.


  • Hero Member

    @mikee how many temperature sensors are you using? The id of the relay sensor should be higher than that number (better to use a value higher than Max number. Else there is overlap.
    You are also presenting the relay more than once in the init loop. Once should be enough. When switching the relay from the controller it should be visible in the serial output of the node.



  • Hi @AWI , I have 5 DS 18b20s , i chose CHILD ID 6 because of that , so I´m gonna lower max . temp sensor No.
    Witch line in sketch is duplicity of relay presentation (sorry for dumb questions),
    Thanks so much


  • Hero Member

    @mikee in the setup loop for the temp sensors you are also presenting the relay. So one time for each temp sensor. You can just place it outside of the loop.

      for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {   
         gw.present(i, S_TEMP);
         gw.present(RELAY_CHILD, S_LIGHT);
          digitalWrite(RELAY_PIN, RELAY_OFF);
          pinMode(RELAY_PIN, OUTPUT);
      }
    

    It won't solve your problem but is more decent. Then try to make sure you are getting the relay input from the controller by looking at the serial output of the node


Log in to reply
 

Suggested Topics

  • 5
  • 2
  • 4
  • 2
  • 5
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts