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. General Discussion
  3. Relais sketch, auto switch on again after x seconds

Relais sketch, auto switch on again after x seconds

Scheduled Pinned Locked Moved General Discussion
9 Posts 5 Posters 2.9k Views 4 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.
  • CorvlC Offline
    CorvlC Offline
    Corvl
    wrote on last edited by
    #1

    Hello everyone,

    I just build a simple single relais , using the sketch from the examples. ( see also below) . Is it possible to change it a bit , so the relais when switched off , switches on again automatically after for example 5 seconds?

    Thanks,
    Cor

    // Example sketch showing how to control physical relays. 
    // This example will remember relay state even after power failure.
    
    #include <MySensor.h>
    #include <SPI.h>
    
    #define RELAY_1  3  // 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
    
    MySensor gw;
    
    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);
      }
    }
    
    
    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());
       } 
    }```
    1 Reply Last reply
    0
    • rejoe2R Offline
      rejoe2R Offline
      rejoe2
      wrote on last edited by
      #2

      Hi Cor,

      this ain't no big deal, you may just study my example in https://github.com/rejoe2/MySensors_Small/blob/master/MyS103/MyS103.ino.

      You have to store the sitch off-time, check in loop(), if your intended off-time has past and then switch on again, ideally informing your controller about the node having done so.

      Btw: your sketch is for MySensors Version 1.x. You may have to make additional changes to transfer my code back.

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

      1 Reply Last reply
      0
      • mfalkviddM Offline
        mfalkviddM Offline
        mfalkvidd
        Mod
        wrote on last edited by
        #3

        The solution in https://forum.mysensors.org/post/58102 could be good as a base. It waits for 0.5 seconds.

        1 Reply Last reply
        1
        • CorvlC Offline
          CorvlC Offline
          Corvl
          wrote on last edited by Corvl
          #4

          No big deal :cold_sweat:

          I tried with the code from mfalkvidd , change what I though looked good.

          But I get the error in compiling:

          RelayActuator.ino: In function 'void setup()':
          RelayActuator.ino:28:30: error: 'RELAY_Off' was not declared in this scope
          RelayActuator.ino:30:10: error: 'wait' was not declared in this scope
          RelayActuator.ino:31:19: error: 'RELAY_On' was not declared in this scope
          RelayActuator.ino: In function 'void incomingMessage(const MyMessage&)':
          RelayActuator.ino:47:27: error: 'RELAY_Off' was not declared in this scope
          RelayActuator.ino:49:9: error: 'wait' was not declared in this scope
          RelayActuator.ino:50:40: error: 'RELAY_On' was not declared in this scope
          Error compiling

          This is what I did:
          I changed exactly what was in that code , but since I didn't have "loadState" but I did have "gw.loadState" I changed it in "gw.loadState" but also "Loadstate"gives an error compling.

          // Example sketch showing how to control physical relays. 
          // This example will remember relay state even after power failure.
          
          #include <MySensor.h>
          #include <SPI.h>
          
          #define RELAY_1  3  // 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
          
          MySensor gw;
          
          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) 
           if (gw.loadState(sensor) == RELAY_Off) {
          digitalWrite(pin, RELAY_Off);
          wait(5000);
          digitalWrite(pin, RELAY_On);
          }
            }
          }
          
          
          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
           if (message.getBool() == RELAY_Off) {
          digitalWrite(message.sensor-1+RELAY_1, RELAY_Off);
          wait(500);
          digitalWrite(message.sensor-1+RELAY_1, RELAY_On);
          }
               // 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());
             } 
          }
          Boots33B 1 Reply Last reply
          0
          • CorvlC Corvl

            No big deal :cold_sweat:

            I tried with the code from mfalkvidd , change what I though looked good.

            But I get the error in compiling:

            RelayActuator.ino: In function 'void setup()':
            RelayActuator.ino:28:30: error: 'RELAY_Off' was not declared in this scope
            RelayActuator.ino:30:10: error: 'wait' was not declared in this scope
            RelayActuator.ino:31:19: error: 'RELAY_On' was not declared in this scope
            RelayActuator.ino: In function 'void incomingMessage(const MyMessage&)':
            RelayActuator.ino:47:27: error: 'RELAY_Off' was not declared in this scope
            RelayActuator.ino:49:9: error: 'wait' was not declared in this scope
            RelayActuator.ino:50:40: error: 'RELAY_On' was not declared in this scope
            Error compiling

            This is what I did:
            I changed exactly what was in that code , but since I didn't have "loadState" but I did have "gw.loadState" I changed it in "gw.loadState" but also "Loadstate"gives an error compling.

            // Example sketch showing how to control physical relays. 
            // This example will remember relay state even after power failure.
            
            #include <MySensor.h>
            #include <SPI.h>
            
            #define RELAY_1  3  // 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
            
            MySensor gw;
            
            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) 
             if (gw.loadState(sensor) == RELAY_Off) {
            digitalWrite(pin, RELAY_Off);
            wait(5000);
            digitalWrite(pin, RELAY_On);
            }
              }
            }
            
            
            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
             if (message.getBool() == RELAY_Off) {
            digitalWrite(message.sensor-1+RELAY_1, RELAY_Off);
            wait(500);
            digitalWrite(message.sensor-1+RELAY_1, RELAY_On);
            }
                 // 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());
               } 
            }
            Boots33B Offline
            Boots33B Offline
            Boots33
            Hero Member
            wrote on last edited by
            #5

            @Corvl What version of MySensors are you using. the sketch you have is for the earlier 1.xx version. If possible you should move to MySensors version 2

            1 Reply Last reply
            0
            • CorvlC Offline
              CorvlC Offline
              Corvl
              wrote on last edited by
              #6

              @ Boots33: both my vera's are running with version 1.5 . Can I just update , will the sketches wich are now running still work?

              BartEB 1 Reply Last reply
              0
              • CorvlC Corvl

                @ Boots33: both my vera's are running with version 1.5 . Can I just update , will the sketches wich are now running still work?

                BartEB Offline
                BartEB Offline
                BartE
                Contest Winner
                wrote on last edited by
                #7

                @Corvl yes you should be able to upgrade.byt you have top upgrade the gateway aswel.

                It is quicker to solve the compile errors:

                Do note that C in case sensitive so

                • change RELAY_Off to RELAY_OFF on line 28 and 47
                • change RELAY_On to RELAY_ON on line 31 and 50

                Since you use the old library you also should change wait to gw.wait on line 30 and 49

                That should fix the compiler error.
                I think the second gw.wait should also be 5000 iso 500 if you want to wait 5 seconds also.

                As a general comment, when programming C it is general practice to use indents for beter readability of the code so please add some whitespace in front of the added lines

                1 Reply Last reply
                1
                • CorvlC Offline
                  CorvlC Offline
                  Corvl
                  wrote on last edited by
                  #8

                  Thanks,

                  I am thinking of upgrading to 2.x , not sure yet if that is wise. Just made another topic for some questions.

                  Thanks so far,
                  Cor

                  1 Reply Last reply
                  0
                  • CorvlC Offline
                    CorvlC Offline
                    Corvl
                    wrote on last edited by Corvl
                    #9

                    @ BartE , thanks a lot , I changed all the _Off to _OFF and the same with the On , gw. wait as well. also the 500 to 5000

                    Working perfectly !

                    Many thanks

                    I haven't had a definite anwer yet if libabry 2.x works on vera , I have only read about problems . For the moment I stick to 1.5

                    edit : one small thing I just noticed , when I switch via the vera Gui the relais to off , it nicely goes back to on after 5 seconds , but it doesn't report back to vera that it is again in an on state.

                    is there still something wrong with the code?

                    Again many thanks,
                    Cor

                    For future reference:

                    // Example sketch showing how to control physical relays. 
                    // This example will remember relay state even after power failure.
                    
                    #include <MySensor.h>
                    #include <SPI.h>
                    
                    #define RELAY_1  3  // 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
                    
                    MySensor gw;
                    
                    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) 
                         if (gw.loadState(sensor) == RELAY_OFF) {
                         digitalWrite(pin, RELAY_OFF);
                         gw.wait(5000);
                         digitalWrite(pin, RELAY_ON);
                    }
                      }
                    }
                    
                    
                    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
                     if (message.getBool() == RELAY_OFF) {
                    digitalWrite(message.sensor-1+RELAY_1, RELAY_OFF);
                    gw.wait(5000);
                    digitalWrite(message.sensor-1+RELAY_1, RELAY_ON);
                    }
                         // 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());
                       } 
                    }```
                    1 Reply Last reply
                    1
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    20

                    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