Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Announcements
  3. 💬 Relay

💬 Relay

Scheduled Pinned Locked Moved Announcements
139 Posts 47 Posters 33.9k Views 45 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L lekeb

    hello.
    I am building a node with several kind of sensors and two relays.
    My sensors use CHILD ID from 0 to 3. I would like to start child ID of relay from 4.
    How I can't do that with this sketch?
    I guess I have to change this part of sketch :
    "for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
    // Register all sensors to gw (they will be created as child devices)
    present(sensor, S_BINARY);"

    correct?

    thanks

    rejoe2R Offline
    rejoe2R Offline
    rejoe2
    wrote on last edited by rejoe2
    #104

    @lekeb
    You may do something like this:
    In Header:

    uint8_t DS_First_Child_ID = 7; //First Child-ID to be used by Dallas Bus
    

    an then in presentation():

    for (int i = 0; i < numSensors && i < MAX_ATTACHED_DS18B20; i++) {
        present(i + DS_First_Child_ID, S_TEMP);
    }
    

    But afaik, it's not recommended to shift the ChildID's for relays but always start with 1. Imo it's better to change the ChildID's of the other attached sensors. You may also take care (in case of shifting ID's) about a consistent mapping from ID to the PINs where the relays are attached.

    Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

    1 Reply Last reply
    0
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #105

      Agreed, set you sensors to higher ID numbers like 20 or 30 and you are good to go .

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lekeb
        wrote on last edited by
        #106

        ok thanks a lot I will try this night

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lekeb
          wrote on last edited by
          #107

          perfect it works great. I started my temperature sensor at CHILD ID 4 and let relays from 1 to 2. I had to add also "DS_First_Child_ID" in the sent message to gateway.
          thanks

          1 Reply Last reply
          0
          • pepsonP Offline
            pepsonP Offline
            pepson
            wrote on last edited by
            #108

            Hi
            Please put to this topic code your sketch....
            Thanks

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lekeb
              wrote on last edited by
              #109

              Sur,

              my node controls two relays, one water pressure sensor and three DS18b20

              /**
               code pour controle cave a vin, temp et pression
               */
              
              // Enable debug prints to serial monitor
              //#define MY_DEBUG
              
              // Enable and select radio type attached
              #define MY_RADIO_NRF24
              
              // Enable repeater functionality for this node
              #define MY_REPEATER_FEATURE
              
              #define MY_NODE_ID 15
              #include <SPI.h>
              #include <MySensors.h>
              #include <DallasTemperature.h>
              #include <OneWire.h>
              
              #define BARO_CHILD 0 //Child ID for pressure sensor
              #define BAR_SENSOR_ANALOG_PIN 0 // pin for pressure sensor
              
              #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
              
              #define ONE_WIRE_BUS 5 // Pin where dallase sensor is connected 
              #define MAX_ATTACHED_DS18B20 16
              #define RELAY_PIN 3 //pin for first relay
              #define NUMBER_OF_RELAYS 2
              #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
              
              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;
              bool metric = true;
              float lastpression;
              uint8_t DS_First_Child_ID = 4; //First Child-ID to be used by Dallas Bus
              MyMessage pressureMsg(BARO_CHILD, V_PRESSURE);
              MyMessage msg(0,V_TEMP);
              
              
              void before()
              {
                  for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
                      // Then set relay pins in output mode
                      pinMode(pin, OUTPUT);
                      // Set relay to last known state (using eeprom storage)
                      digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
                  }
                  
                  // 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 and Controller
                  sendSketchInfo("Buanderie node", "1.0");
              
                  //present pressure sensor
                  present(BARO_CHILD, S_BARO);
              
                  for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
                      // Register all sensors to gw (they will be created as child devices)
                      present(sensor, S_BINARY);
                  }
              
                  
              // 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++) {   
                   present(i + DS_First_Child_ID, S_TEMP);
                }
              }
              
              
              
              void loop()
              {
                //read water pressure
              int lecture_adc = analogRead(BAR_SENSOR_ANALOG_PIN);
                float pression = ((lecture_adc*5/1024.0)-0.50)/1.7;
                if(pression != lastpression) { 
                    send(pressureMsg.set(pression, 2));
                lastpression = pression;
                }
                
                
                // 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>((getControllerConfig().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(msg.setSensor(i + DS_First_Child_ID).set(temperature,1));
                    // Save new temperatures for next compare
                    lastTemperature[i]=temperature;
                  }
                }
              }
              
              void receive(const MyMessage &message)
              {
                  // We only expect one type of message from controller. But we better check anyway.
                  if (message.type==V_STATUS) {
                      // Change relay state
                      digitalWrite(message.sensor-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
                      // Store state in eeprom
                      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());
                  }
              }
              
              gohanG 1 Reply Last reply
              0
              • L lekeb

                Sur,

                my node controls two relays, one water pressure sensor and three DS18b20

                /**
                 code pour controle cave a vin, temp et pression
                 */
                
                // Enable debug prints to serial monitor
                //#define MY_DEBUG
                
                // Enable and select radio type attached
                #define MY_RADIO_NRF24
                
                // Enable repeater functionality for this node
                #define MY_REPEATER_FEATURE
                
                #define MY_NODE_ID 15
                #include <SPI.h>
                #include <MySensors.h>
                #include <DallasTemperature.h>
                #include <OneWire.h>
                
                #define BARO_CHILD 0 //Child ID for pressure sensor
                #define BAR_SENSOR_ANALOG_PIN 0 // pin for pressure sensor
                
                #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
                
                #define ONE_WIRE_BUS 5 // Pin where dallase sensor is connected 
                #define MAX_ATTACHED_DS18B20 16
                #define RELAY_PIN 3 //pin for first relay
                #define NUMBER_OF_RELAYS 2
                #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
                
                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;
                bool metric = true;
                float lastpression;
                uint8_t DS_First_Child_ID = 4; //First Child-ID to be used by Dallas Bus
                MyMessage pressureMsg(BARO_CHILD, V_PRESSURE);
                MyMessage msg(0,V_TEMP);
                
                
                void before()
                {
                    for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
                        // Then set relay pins in output mode
                        pinMode(pin, OUTPUT);
                        // Set relay to last known state (using eeprom storage)
                        digitalWrite(pin, loadState(sensor)?RELAY_ON:RELAY_OFF);
                    }
                    
                    // 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 and Controller
                    sendSketchInfo("Buanderie node", "1.0");
                
                    //present pressure sensor
                    present(BARO_CHILD, S_BARO);
                
                    for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
                        // Register all sensors to gw (they will be created as child devices)
                        present(sensor, S_BINARY);
                    }
                
                    
                // 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++) {   
                     present(i + DS_First_Child_ID, S_TEMP);
                  }
                }
                
                
                
                void loop()
                {
                  //read water pressure
                int lecture_adc = analogRead(BAR_SENSOR_ANALOG_PIN);
                  float pression = ((lecture_adc*5/1024.0)-0.50)/1.7;
                  if(pression != lastpression) { 
                      send(pressureMsg.set(pression, 2));
                  lastpression = pression;
                  }
                  
                  
                  // 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>((getControllerConfig().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(msg.setSensor(i + DS_First_Child_ID).set(temperature,1));
                      // Save new temperatures for next compare
                      lastTemperature[i]=temperature;
                    }
                  }
                }
                
                void receive(const MyMessage &message)
                {
                    // We only expect one type of message from controller. But we better check anyway.
                    if (message.type==V_STATUS) {
                        // Change relay state
                        digitalWrite(message.sensor-1+RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
                        // Store state in eeprom
                        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());
                    }
                }
                
                gohanG Offline
                gohanG Offline
                gohan
                Mod
                wrote on last edited by
                #110

                @lekeb said in 💬 Relay:

                MyMessage pressureMsg(BARO_CHILD, V_PRESSURE);
                MyMessage msg(0,V_TEMP);

                Why are you using the same child ID (since you set #define BARO_CHILD 0)?

                rejoe2R 1 Reply Last reply
                0
                • gohanG gohan

                  @lekeb said in 💬 Relay:

                  MyMessage pressureMsg(BARO_CHILD, V_PRESSURE);
                  MyMessage msg(0,V_TEMP);

                  Why are you using the same child ID (since you set #define BARO_CHILD 0)?

                  rejoe2R Offline
                  rejoe2R Offline
                  rejoe2
                  wrote on last edited by
                  #111

                  @gohan Effectively, he will never report any temp reading under ChildID 0. Within presentation() and the send() commands, the "0" is replaced by "i + DS_First_Child_ID".
                  But you are partly right, to avoid any misunderstandings wrt. that the statement could also be written as follows:

                  MyMessage msg(DS_First_Child_ID,V_TEMP);
                  

                  Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lekeb
                    wrote on last edited by
                    #112

                    correct, it makes sense. I will correct this error.
                    However Domoticz reads correctly the temperature and links correctly the CHILD ID's, so...

                    1 Reply Last reply
                    0
                    • Jonathan PucelJ Offline
                      Jonathan PucelJ Offline
                      Jonathan Pucel
                      wrote on last edited by
                      #113

                      Hello everybody !

                      I would like to creat a sensor with two relays and two buttons to command this relay direct from the sensor (with actualisation of their stat in domoticz)
                      Someone know the code to do this ? I'm a complete newbie on mysensor !

                      Thank's a lot !

                      rejoe2R 1 Reply Last reply
                      0
                      • Jonathan PucelJ Jonathan Pucel

                        Hello everybody !

                        I would like to creat a sensor with two relays and two buttons to command this relay direct from the sensor (with actualisation of their stat in domoticz)
                        Someone know the code to do this ? I'm a complete newbie on mysensor !

                        Thank's a lot !

                        rejoe2R Offline
                        rejoe2R Offline
                        rejoe2
                        wrote on last edited by
                        #114

                        @jonathan-pucel Have a look at the code in this post.

                        Controller: FHEM; MySensors: 2.3.1, RS485,nRF24,RFM69, serial Gateways

                        1 Reply Last reply
                        1
                        • Jonathan PucelJ Offline
                          Jonathan PucelJ Offline
                          Jonathan Pucel
                          wrote on last edited by
                          #115

                          Excellent ! It's perfect, thank's a lot rejoe2 !

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mysz0n
                            wrote on last edited by
                            #116
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              Vorace
                              wrote on last edited by
                              #117

                              Hi
                              Using Home assistant, and Optimistic set to false in the mysensors config, the switch in homeassistant would turn the relay on, but in the view in homeassistant the flip switch jumped off straight after switching on. It was solved by adding the following line to the sketch, ensuring that hassio knows that the actuator actually have received the command. Not sure if this is a good way of doing it, but it seems to work for me.
                              send(msg.set(state)); // Send new state and request ack back
                              in:
                              void receive(const MyMessage &message) {
                              // We only expect one type of message from controller. But we better check anyway.
                              if (message.isAck()) {
                              Serial.println("This is an ack from gateway");
                              }

                              if (message.type == V_LIGHT) {
                              // Change relay state
                              state = message.getBool();
                              digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF);
                              // Store state in eeprom
                              saveState(CHILD_ID, state);

                               // Write some debug info
                               Serial.print("Incoming change for sensor:");
                               Serial.print(message.sensor);
                               Serial.print(", New status: ");
                               Serial.println(message.getBool());
                               send(msg.set(state)); // Send new state and request ack back
                              

                              }
                              }

                              Thanks

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                arraWX
                                wrote on last edited by arraWX
                                #118

                                Hi!

                                I would like to control some 230VAC equipment (for now roller shutters), based on inputs from my mysensors sensors (temperature and light). For controller I use domoticz.

                                I would like a safe, robust and preferably authorized/lawful solution (I'm in EU/Denmark).

                                I came across the following solutions:

                                https://aeotec.com/z-wave-plug-in-switch
                                https://sonoff.itead.cc/en/products/sonoff/sonoff-basic
                                https://dlidirect.com/products/iot-power-relay

                                I think the first one and maybe the second one will be authorized/lawful...? However, I have experience only with mysensors and neither z wave nor sonoff...

                                Anyone has some experience/thoughts/suggestions to share?

                                Thanks.

                                1 Reply Last reply
                                0
                                • gohanG Offline
                                  gohanG Offline
                                  gohan
                                  Mod
                                  wrote on last edited by
                                  #119

                                  There are also roller shutters nodes running via zwave if you are looking at a retail solution

                                  A 1 Reply Last reply
                                  1
                                  • gohanG gohan

                                    There are also roller shutters nodes running via zwave if you are looking at a retail solution

                                    A Offline
                                    A Offline
                                    arraWX
                                    wrote on last edited by arraWX
                                    #120

                                    Thank you @gohan! Retail solution is not the keyword here. What I am searching for is an authorized/lawful (safe) solution.

                                    I decided to do my own node (mysensors) with a cheap relay module for the arduino. Once I achieve the desired functionality I will change to some more robust hardware. Following your suggestion this could be a z wave roller shutter (e.g. fibaro or qubino).

                                    bjacobseB 1 Reply Last reply
                                    0
                                    • A arraWX

                                      Thank you @gohan! Retail solution is not the keyword here. What I am searching for is an authorized/lawful (safe) solution.

                                      I decided to do my own node (mysensors) with a cheap relay module for the arduino. Once I achieve the desired functionality I will change to some more robust hardware. Following your suggestion this could be a z wave roller shutter (e.g. fibaro or qubino).

                                      bjacobseB Offline
                                      bjacobseB Offline
                                      bjacobse
                                      wrote on last edited by
                                      #121

                                      @arrawx said in 💬 Relay:

                                      Retail solution is not the keyword here. What I am searching for is an authorized/lawful (safe) solution.

                                      Sorry to comment negative, but your wording doesn't make sense. either you will go for a DIY solution, cheap and illegal (but not necessary a bad solution), or you will purchase a retail solution.
                                      Retail solution must have the required certificates to allow you to sell. Those are not cheap to get, which also provide the reason for a retail solution to
                                      be usually fairly expensive
                                      https://arbejdstilsynet.dk/da/regler/bekendtgorelser/i/sam-indretning-af-tekniske-hjaelpemidler-612

                                      And also you need Notified Body
                                      https://en.wikipedia.org/wiki/Notified_Body

                                      And I know that Cetekom can create certificate for Country Approvals
                                      https://www.cetecom.com/en/certification/country-appoval/

                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        slt1
                                        wrote on last edited by
                                        #122

                                        There is a problem with the example code for RelayActuator.ino It is not checking for ACK messages. See the other example called SecureActuator.ino that does this. Without checking for ACK messages my relay gets an ON signal and turns on then immediately thereafter gets an ACK signal for V_STATUS command which is assumed in this example to be a control and the value is "0" so it turns off the relay.

                                        mfalkviddM 1 Reply Last reply
                                        1
                                        • S slt1

                                          There is a problem with the example code for RelayActuator.ino It is not checking for ACK messages. See the other example called SecureActuator.ino that does this. Without checking for ACK messages my relay gets an ON signal and turns on then immediately thereafter gets an ACK signal for V_STATUS command which is assumed in this example to be a control and the value is "0" so it turns off the relay.

                                          mfalkviddM Offline
                                          mfalkviddM Offline
                                          mfalkvidd
                                          Mod
                                          wrote on last edited by
                                          #123

                                          @slt1 I'm not sure I'm following. RelayActuator.ino does not send any messages, so it should never receive any ack messages. Compare with RelayWithButtonActuator.ino which does send messages, and therefore also checks for ack.

                                          Could you elaborate on the problem?

                                          S 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          26

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.1k

                                          Posts


                                          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular