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. Development
  3. Domoticz 4 x relay

Domoticz 4 x relay

Scheduled Pinned Locked Moved Development
6 Posts 3 Posters 5.2k Views 2 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.
  • R Offline
    R Offline
    romeok01
    wrote on last edited by
    #1

    I wrote code to 4 relays:

    #include <MySigningNone.h>
    #include <MyTransportNRF24.h>
    #include <MyTransportRFM69.h>
    #include <MyHwATMega328.h>
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  3  // Pin D3
    #define RELAY_2  4  // Pin D4
    #define RELAY_3  5  // Pin D5
    #define RELAY_4  6  // Pin D6
    #define NUMBER_OF_RELAYS 4 // Liczba przekaznikow
    #define RELAY_ON 1  // Stan wysoki przekaznik wlaczony
    #define RELAY_OFF 0 // Stan niski przekaznik wylaczony
    
    // 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);
    
    void setup() {   
      // 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++)
      for (int sensor=1, pin=RELAY_2; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)
      for (int sensor=1, pin=RELAY_3; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)
      for (int sensor=1, pin=RELAY_4; 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() 
    {
      // Alway process incoming messages whenever possible
      gw.process();
    }
    
    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());
       } 
    }
    

    Domoticz detected 4 switches

    Schowek-1.jpg

    I added 4 switches in Domoticz
    Schowek-2.jpg

    I connected the LED to pins 3,4,5,6 and when I turn on switch in Domoticz the LEDs do not want to turn on.

    What is poorly written code?

    H 1 Reply Last reply
    0
    • R romeok01

      I wrote code to 4 relays:

      #include <MySigningNone.h>
      #include <MyTransportNRF24.h>
      #include <MyTransportRFM69.h>
      #include <MyHwATMega328.h>
      #include <MySensor.h>
      #include <SPI.h>
      
      #define RELAY_1  3  // Pin D3
      #define RELAY_2  4  // Pin D4
      #define RELAY_3  5  // Pin D5
      #define RELAY_4  6  // Pin D6
      #define NUMBER_OF_RELAYS 4 // Liczba przekaznikow
      #define RELAY_ON 1  // Stan wysoki przekaznik wlaczony
      #define RELAY_OFF 0 // Stan niski przekaznik wylaczony
      
      // 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);
      
      void setup() {   
        // 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++)
        for (int sensor=1, pin=RELAY_2; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)
        for (int sensor=1, pin=RELAY_3; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)
        for (int sensor=1, pin=RELAY_4; 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() 
      {
        // Alway process incoming messages whenever possible
        gw.process();
      }
      
      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());
         } 
      }
      

      Domoticz detected 4 switches

      Schowek-1.jpg

      I added 4 switches in Domoticz
      Schowek-2.jpg

      I connected the LED to pins 3,4,5,6 and when I turn on switch in Domoticz the LEDs do not want to turn on.

      What is poorly written code?

      H Offline
      H Offline
      hek
      Admin
      wrote on last edited by
      #2

      @romeok01 said:

      for (int sensor=1, pin=RELAY_1; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)
      for (int sensor=1, pin=RELAY_2; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)
      for (int sensor=1, pin=RELAY_3; sensor<=NUMBER_OF_RELAYS;sensor++, pin++)
      for (int sensor=1, pin=RELAY_4; sensor<=NUMBER_OF_RELAYS;sensor++, pin++){

      Is this really correct?

      1 Reply Last reply
      0
      • T Offline
        T Offline
        TheoL
        Contest Winner
        wrote on last edited by
        #3

        Where do you power the LED's from? In other words how did you connect them to the relays?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          romeok01
          wrote on last edited by romeok01
          #4

          I used leds to perform a functional test code.

          I connected LEDs to pins 3, 4, 5, 6 and GND.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            TheoL
            Contest Winner
            wrote on last edited by
            #5

            What I allways do during development is to uncomment the line #define DEBUG in MySensor.h that way MySensors is writing Debug info to your serial Monitor. It's getting late where I live and I need to go to bed. So I don't have time to dig into it.

            But try this as your setup code

            void setup() {   
              // 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);
              }
            }
            

            Not sure if it helps but you can try it. I personally don't use gw.saveState a lot. The EPROM doesn't support unlimited write actions. So during development it's allways good to comment lines like that.

            R 1 Reply Last reply
            0
            • T TheoL

              What I allways do during development is to uncomment the line #define DEBUG in MySensor.h that way MySensors is writing Debug info to your serial Monitor. It's getting late where I live and I need to go to bed. So I don't have time to dig into it.

              But try this as your setup code

              void setup() {   
                // 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);
                }
              }
              

              Not sure if it helps but you can try it. I personally don't use gw.saveState a lot. The EPROM doesn't support unlimited write actions. So during development it's allways good to comment lines like that.

              R Offline
              R Offline
              romeok01
              wrote on last edited by
              #6

              @TheoL Thank you very much, really helped me, now the program code work :smile:

              1 Reply Last reply
              0

              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

              With your input, this post could be even better 💗

              Register Login
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              12

              Online

              12.1k

              Users

              11.2k

              Topics

              113.4k

              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