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. Troubleshooting
  3. Anyone help with 4 relays please?

Anyone help with 4 relays please?

Scheduled Pinned Locked Moved Troubleshooting
34 Posts 6 Posters 5.1k Views 9 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.
  • Petr NosekP Petr Nosek

    Hi skywatch,
    would you be so kind and share your ack and heart beat code snippets? I tried to use the code from examples, but it seems teh heartbeat never worked as expected. Regarding the ack - I am getting constantly a message: Error: MySensors: Repeating previous command (2/2) - seems to me to be related to ack as it simply does not get acknowledgement from the node and repeats the command again. But in 99% cases the command seems to be executed fine.

    skywatchS Offline
    skywatchS Offline
    skywatch
    wrote on last edited by skywatch
    #13

    @petr-nosek

    See below for the current code I run for my 4 relay test node.....

    #define MY_DEBUG
    #define MY_RADIO_NRF24
    #define MY_RF24_PA_LEVEL   RF24_PA_HIGH
    #define MY_NODE_ID 140
    #define MY_RF24_CHANNEL (97)
    #define MY_PARENT_NODE_ID 0
    #define MY_PARENT_NODE_IS_STATIC
    //#define MY_REPEATER_FEATURE
    //#define MY_SIGNING_ATSHA204
    //#define MY_SIGNING_REQUEST_SIGNATURES 
    #include <MySensors.h>
    #include <SPI.h>
    #include <Bounce2.h>
    #define RELAY_ON 1                      // switch around for relay HIGH/LOW state
    #define RELAY_OFF 0
    //
    unsigned long HEARTBEAT_TIME = 60000*10; //every 10 mins.
    unsigned long last_heartbeat_time = 0;
    #define noRelays 4                     //2-4
    const int relayPin[] = {4,5,6,7};          //  switch around pins to your desire
    const int buttonPin[] = {A0,A1,A2,A3};      //  switch around pins to your desire
    
    class Relay             // relay class, store all relevant data (equivalent to struct)
    {
    public:                                      
      int buttonPin;                   // physical pin number of button
      int relayPin;             // physical pin number of relay
      byte oldValue;                    // last Values for key (debounce)
      boolean relayState;               // relay status (also stored in EEPROM)
    };
    
    Relay Relays[noRelays]; 
    Bounce debouncer[noRelays];
    MyMessage msg[noRelays];
    
    void setup(){
       
        wait(250);
        // Initialize Relays with corresponding buttons
        for (int i = 0; i < noRelays; i++){
        Relays[i].buttonPin = buttonPin[i];              // assign physical pins
        Relays[i].relayPin = relayPin[i];
        msg[i].sensor = i;                                   // initialize messages
        msg[i].type = V_STATUS;
        debouncer[i] = Bounce();                        // initialize debouncer
        debouncer[i].attach(buttonPin[i]);
        debouncer[i].interval(5);
        pinMode(Relays[i].buttonPin, INPUT_PULLUP);
        digitalWrite(Relays[i].relayPin, RELAY_OFF);
        wait(250);
        pinMode(Relays[i].relayPin, OUTPUT);
        Relays[i].relayState = loadState(i);                               // retrieve last values from EEPROM
        digitalWrite(Relays[i].relayPin, Relays[i].relayState? RELAY_ON:RELAY_OFF);   // and set relays accordingly
        send(msg[i].set(Relays[i].relayState? true : false),true);                  // make controller aware of last status  
        wait(250);
        }
    }
    void presentation()  {
          sendSketchInfo("MYS-4-Relay", "0.2");
          wait(250);
          present(0, S_BINARY,"Relay 1"); 
          wait(150);
          present(1, S_BINARY,"Relay 2");    
          wait(150);
          present(2, S_BINARY,"Relay 3");    
          wait(150);
          present(3, S_BINARY,"Relay 4");    
    
    }
    void loop()
        {
        // Send heartbeat 
        if ((millis() - last_heartbeat_time) > HEARTBEAT_TIME) {
        sendHeartbeat();
        last_heartbeat_time = millis();
      
      #ifdef MY_DEBUG
        Serial.println("Sent heartbeat");
      #endif
    }      
        for (byte i = 0; i < noRelays; i++){
        debouncer[i].update();
        byte value = debouncer[i].read();
        if (value != Relays[i].oldValue && value==0){
        Relays[i].relayState = !Relays[i].relayState;
        digitalWrite(Relays[i].relayPin, Relays[i].relayState?RELAY_ON:RELAY_OFF);
        send(msg[i].set(Relays[i].relayState? true : false),true);
        saveState( i, Relays[i].relayState );}                 // save sensor state in EEPROM (location == sensor number)
        Relays[i].oldValue = value;
        wait(50);
        
        }
    }
    // process incoming message 
    void receive(const MyMessage &message){        
       if (message.type == V_STATUS){ 
       if (message.sensor < noRelays){            // check if message is valid for relays..... previous line  [[[ if (message.sensor <=noRelays){ ]]]
       Relays[message.sensor].relayState = message.getBool(); 
       digitalWrite(Relays[message.sensor].relayPin, Relays[message.sensor].relayState? RELAY_ON:RELAY_OFF); // and set relays accordingly
       saveState( message.sensor, Relays[message.sensor].relayState ); // save sensor state in EEPROM (location == sensor number)
       }
      }
    }
    

    This includes my latest attempt at heartbeat and ack. Heartbeat works fine, but ack is still a problem with mycontroller.
    I can't risk using it until ack and signing are implemented and fully reliable as it will be controlling mains devices and that needs to be done securely.

    As for your sauna problem don't forget to connect the grounds between power supplies. You could try adding a 0.1uF capacitor across the 5v supply along with a nice big 220uF 6.3V or higher electrolytic. That might help with spikes or momentary sags in the voltage caused by current rush.

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

      If you want complete isolation between relays and arduino you don't need to share common ground if your relays have optocouplers.

      1 Reply Last reply
      0
      • skywatchS Offline
        skywatchS Offline
        skywatch
        wrote on last edited by
        #15

        @gohan, Good point! :)

        1 Reply Last reply
        0
        • Petr NosekP Offline
          Petr NosekP Offline
          Petr Nosek
          wrote on last edited by
          #16

          Good day everyone,
          in case someone faces the same issues I did:

          • controlling multiple relays from one arduino and sharing the same power (powering relay board from Arduino
            or
          • controlling relay which switches high loads circuit causing connected LCD to show garbage and again powering the relay from Arduino...

          ...the solution proposed above is the only one which helps - provide separated power supply for the relay boards. Once this is done with an extra little bucket power source for 5V (3W) all the issues with relays not working, domoticz not switching relays, LCD going crazy - all solved. I should have done this long before.

          Thanks everyone!

          P.S. For those, who still do not follow, the relay boards for Arduino contain extra jumper connecting VCC and JD-VCC (plus sometimes extra GND pin). So remove the jumper and connect the standalone, not common sharing 5V power source to this extra JD-VCC and GND (or GND near the IN1 pin) and disconnect the GND from Arduino and then you have a full optosiolation and then there are no interferences and other garbage breaking the Arduino functionality by switching relays coils. (also explained here: https://forum.arduino.cc/index.php?topic=470134.0)

          1 Reply Last reply
          2
          • parachutesjP Offline
            parachutesjP Offline
            parachutesj
            wrote on last edited by
            #17

            If this really solves the problem...would have saved me a lot of headaches. Got rid of all my relay nodes in the meantime.

            Petr NosekP 1 Reply Last reply
            0
            • parachutesjP parachutesj

              If this really solves the problem...would have saved me a lot of headaches. Got rid of all my relay nodes in the meantime.

              Petr NosekP Offline
              Petr NosekP Offline
              Petr Nosek
              wrote on last edited by
              #18

              @parachutesj I do say it does. I have about 50 nodes in my house, 15 relays or so, temperature sensors and some others. All the relays i power now with standalone little china made 5V, 3W adapters and as I sit now in my house I do switch on and off lights, turn on gate, control pumps in greenhouse, all works on the first tap. I am finally happy with my mysensors equipped domoticz, first after 2 years. It took my so long to just give it a try.

              parachutesjP 1 Reply Last reply
              0
              • Petr NosekP Petr Nosek

                @parachutesj I do say it does. I have about 50 nodes in my house, 15 relays or so, temperature sensors and some others. All the relays i power now with standalone little china made 5V, 3W adapters and as I sit now in my house I do switch on and off lights, turn on gate, control pumps in greenhouse, all works on the first tap. I am finally happy with my mysensors equipped domoticz, first after 2 years. It took my so long to just give it a try.

                parachutesjP Offline
                parachutesjP Offline
                parachutesj
                wrote on last edited by
                #19

                @petr-nosek my problem was maily unrealiability. They worked for minutes or weeks and then stopped (or anything in between).
                All other nodes work flawless.
                Maybe give it a try at next project

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

                  have you been using watchdogs on your sketches?

                  Petr NosekP 1 Reply Last reply
                  0
                  • gohanG gohan

                    have you been using watchdogs on your sketches?

                    Petr NosekP Offline
                    Petr NosekP Offline
                    Petr Nosek
                    wrote on last edited by
                    #21

                    @gohan I have tried all kinds of things. I believe that the fact that I used watchdogs which checked the MySensors nodes every 8 seconds (or less) helped me to recover from relay failures so the nodes appeared to be functional, but only randomly. In real life it looked good when I started them. did several turn on and off and it worked. In 2-3 minutes I did try again, it worked for 2 out of 4 attempts and then it stopped responding. In another 20-30 seconds I did try again and then again it appeared to work - I believe it was thanks to the watchdogs recovering the frozen node after the relay peak caused them to stop working. But only a theory, I have no deeper knowledge to prove this.

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

                      if node restarts you should see it in the logs of the gateway or the controller

                      Petr NosekP 1 Reply Last reply
                      0
                      • gohanG gohan

                        if node restarts you should see it in the logs of the gateway or the controller

                        Petr NosekP Offline
                        Petr NosekP Offline
                        Petr Nosek
                        wrote on last edited by
                        #23

                        @gohan Hmm, then I dare to say I have never seen such entry. I could see in the past (before I added second power supply for relay boards) many information about repeating last command and lately issues with sending gmail notifications (caused by securing gmail account) but I do not recall seeing an information about node restart and by that I mean even manual restarts I did by unplugging the node. But I guess you refer to watchdog driven from the controller - Domoticz Gateway. I was perhaps answering on another "watchdog" - a internal Arduino node code which monitors the Arduino internally and checks whether it is running and if not, then it restarts itself. I cant imagine a remote restart of a node which is not responding to commands, how could it react on a remote restart? I suppose it can work just in case the arduino works, only the node itself is somehow not reporting data or so, right?

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

                          I was referring to the internal Arduino watchdog

                          Petr NosekP 1 Reply Last reply
                          0
                          • gohanG gohan

                            I was referring to the internal Arduino watchdog

                            Petr NosekP Offline
                            Petr NosekP Offline
                            Petr Nosek
                            wrote on last edited by
                            #25

                            @gohan OK, then this one I never saw in logs doing anything, or it perhaps actually never worked for me :)

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              palmerfarmer
                              wrote on last edited by
                              #26

                              I Know this is quite an old topic now but mine is working quite stable (for at least one day)
                              I noticed the sketch has
                              #define MY_RF24_PA_LEVEL RF24_PA_HIGH
                              If you are too close to the gateway or using a capacitor across the 3v rail on the NRF24L01 and switching quickly you really have no chance of it being stable. (or any of one these 3 things)
                              I have mine set to:-
                              #define MY_RF24_PA_LEVEL (RF24_PA_MIN)
                              and using a 5v to 3v NRF plugin module, search ebay for 'NRF24L01 Wireless RF Transceiver Socket Adapter Board 3.3v'

                              1 Reply Last reply
                              0
                              • parachutesjP Offline
                                parachutesjP Offline
                                parachutesj
                                wrote on last edited by
                                #27

                                come back in a year and let us know :-)

                                Well, I have only two single relay nodes left in my setup (lamps). They work good without issues, but need to mention that those are solid state.

                                1 Reply Last reply
                                0
                                • P Offline
                                  P Offline
                                  palmerfarmer
                                  wrote on last edited by
                                  #28

                                  Ah ok, good to know. I also spoke too soon....
                                  Today they were unstable, I noticed that some of the on /off commands were not being recieved (small LED flash on the arduino) looked at the logs on domoricz they were fine, however the serial monitor on the arduino node confirmed they weren't always being seen.
                                  Have changed the mysensors libraries on the node to be the same as the usb gateway and its all stable again.
                                  next action will be to monitor the logs on the gateway, node and domoticz at the same time

                                  I have one of those blue 4 relay units from ebay, the jumper has been removed so only the LED lights when i activate the channel.

                                  1 Reply Last reply
                                  0
                                  • parachutesjP Offline
                                    parachutesjP Offline
                                    parachutesj
                                    wrote on last edited by
                                    #29

                                    I tried so much and never reproduced it. had a test setup with a 4 and 8–way relay switching every couple of seconds something. Guess what: after 10 days I gave up because it worked flawless. But then again under load it failed.
                                    But everything except relays work very reliable.

                                    1 Reply Last reply
                                    0
                                    • Petr NosekP Offline
                                      Petr NosekP Offline
                                      Petr Nosek
                                      wrote on last edited by
                                      #30

                                      I tell you guys, I have about 10 nodes at home with relays, some switching lights, some doors, some 12 motors. All I had to do was to provide separate source to the relay from the source to the arduino. Since then, last 2 years I get 98% reliability.

                                      parachutesjP 1 Reply Last reply
                                      1
                                      • skywatchS Offline
                                        skywatchS Offline
                                        skywatch
                                        wrote on last edited by skywatch
                                        #31

                                        With relays involved that usually means switching of higher current devices so I suggest that whether or not you use separate power supplies that you add capacitors everywhere.
                                        Capacitors on the pro-mini or whatever device, the 5V/3.3V power supply and across the power input to the relay board.

                                        You will need to add more than one capacitor at each point. I suggest a good ceramic or electrolytic of 100uF or more for a little reserve and 10n or 100n (better both) to remove 'glitches' from the power lines. (All capacitors should be in parallel across the +/- DC terminals).

                                        If you have access to an oscilliscope then it will be a great help in tracking this down. But switching of high powered loads or inductive loads can send emf pulses that get picked up by the wiring in your project, hence the need to add caps on all boards.

                                        As soon as (or if) my gateway/controller combo becomes stable enough I will proceed with this sort of work and will share any findings with you all. But the above should help and is good practice to follow as the boards from China will only have the bare minimum components they need to work.

                                        Petr NosekP 1 Reply Last reply
                                        0
                                        • skywatchS skywatch

                                          With relays involved that usually means switching of higher current devices so I suggest that whether or not you use separate power supplies that you add capacitors everywhere.
                                          Capacitors on the pro-mini or whatever device, the 5V/3.3V power supply and across the power input to the relay board.

                                          You will need to add more than one capacitor at each point. I suggest a good ceramic or electrolytic of 100uF or more for a little reserve and 10n or 100n (better both) to remove 'glitches' from the power lines. (All capacitors should be in parallel across the +/- DC terminals).

                                          If you have access to an oscilliscope then it will be a great help in tracking this down. But switching of high powered loads or inductive loads can send emf pulses that get picked up by the wiring in your project, hence the need to add caps on all boards.

                                          As soon as (or if) my gateway/controller combo becomes stable enough I will proceed with this sort of work and will share any findings with you all. But the above should help and is good practice to follow as the boards from China will only have the bare minimum components they need to work.

                                          Petr NosekP Offline
                                          Petr NosekP Offline
                                          Petr Nosek
                                          wrote on last edited by
                                          #32

                                          @skywatch, thanks for the reply. I read about using capacitaators, but never in such a complex and straightforward way. I can surely try it as well and switch back to one power supply and see "the difference".
                                          I look forward your tests and findings, if you can post them here. Is there any "formula" to calculate the capacitor values - when you say 100 uF or more - what can you propose as more and what is already too much?
                                          Also when you say "good ceramic or electrolytic" - what do you refer to as "good"?
                                          Basically if I read you well, it is the same as having the capacitors on NRF24 radio on + and GND to avoid issues with signal glitches?
                                          Thanks again to take time to reply here. Really appreciate this kind of feedback!

                                          skywatchS 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