๐ฌ Relay
-
I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?
Also, after presentation, shouldn't there be a send() to the controller?@Efflon you are right about the loop() function it seems like the part where it should change the relay state is missing. Did you try to use this example code?
The button should be wired between PIN 3 and GND so that when you push the button PIN 3 is Grounded.
PIN 3 is connected to a "Debouncer" in the "setup()" ( so that when you push the button the code detects only one push and not several ).
loop() monitors the debouncer and when it detects that you have pushed the button it will send the new state to the controller. I think it should also change the state so if it does not do that you could add the following line
digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); //write new state to relay output
Before this line in the loop() function:
send(msg.set(state?false:true), true); // Send new state and request ack backYou could also add the line:
saveState(CHILD_ID, state); // Store state in eepromTo the same part of the code so that the state change is written to eeprom so that the previous state can be returned after a power failure.
-
@Efflon you are right about the loop() function it seems like the part where it should change the relay state is missing. Did you try to use this example code?
The button should be wired between PIN 3 and GND so that when you push the button PIN 3 is Grounded.
PIN 3 is connected to a "Debouncer" in the "setup()" ( so that when you push the button the code detects only one push and not several ).
loop() monitors the debouncer and when it detects that you have pushed the button it will send the new state to the controller. I think it should also change the state so if it does not do that you could add the following line
digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); //write new state to relay output
Before this line in the loop() function:
send(msg.set(state?false:true), true); // Send new state and request ack backYou could also add the line:
saveState(CHILD_ID, state); // Store state in eepromTo the same part of the code so that the state change is written to eeprom so that the previous state can be returned after a power failure.
-
I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?
Also, after presentation, shouldn't there be a send() to the controller?@Efflon said:
I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?The loop in the relay with button actuator sketch does three main things
checks for a new button push
changes the current state
Sends the new state to the controllerIf you look at the message code you can see it also asks for an ACK back from the controller. That is the true at the end of the line.
send(msg.set(state?false:true), true); // Send new state and request ack backIt is this returned ACK that is used to switch the relay to the new state in the void receive function
The one drawback in this code if it is used for say a light switch is that if this node looses contact with the controller then the relay cannot be changed by pressing the button.
-
@Efflon said:
I'm trying to wrap my head around the button example and don't understand what the loop() really does. To me it only alters the "state" but how does it affect the relay?
Is the button physically connected to the relay or should the controller send the new state back and let the receive() function handle the actual relay switching?The loop in the relay with button actuator sketch does three main things
checks for a new button push
changes the current state
Sends the new state to the controllerIf you look at the message code you can see it also asks for an ACK back from the controller. That is the true at the end of the line.
send(msg.set(state?false:true), true); // Send new state and request ack backIt is this returned ACK that is used to switch the relay to the new state in the void receive function
The one drawback in this code if it is used for say a light switch is that if this node looses contact with the controller then the relay cannot be changed by pressing the button.
@Boots33 Ok, you explanation is what was my only explanation to the "missing" code. When experimenting with this using the mqtt gateway sketch and home-assistant as controller, the ACK is just an ACK and not a "real" message with payload thus just toggling the state bool and getting things out of sync..
-
I did not get the example working properly with my 8 channel SSR. So here is my updated version of the example.
I have changed the way the pins are assigned by using a array. The current example tries to connect relays 7 and 8 to pins 9 and 10 when you select an 8 channel relays. This won't work because those pins are used by the radio module.
I think you can connect a relays to any pin which is not used. If so this will make it possible to connect up to 14 relays to the mini pro when also the pins A5-A7 are available.
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 // Enable repeater functionality for this node #define MY_REPEATER_FEATURE #include <MySensors.h> const int PINS[] = {3, 4, 5, 6, 7, 8, 14, 15}; // I/O pins 3, 4, 5, 6, 7, 8, A0, A1 for the relays #define NUMBER_OF_RELAYS 8 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay void before() { for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) { // Then set relay pins in output mode pinMode(PINS[sensor-1], OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(PINS[sensor-1], loadState(sensor)?RELAY_ON:RELAY_OFF); } } void setup() { } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Relay", "1.0"); for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_BINARY); } } void loop() { } 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(PINS[message.sensor-1], 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()); } } -
I'm attempting to get my double relay board containing two switch inputs. I see that the two sketches suggested on the relay page are there but one seems to be updated to the new version which allows you to use multiple relays on the board but the other isn't, as far as i can see. How do I go about making the sketch with buttons accept more relays?
-
I did not get the example working properly with my 8 channel SSR. So here is my updated version of the example.
I have changed the way the pins are assigned by using a array. The current example tries to connect relays 7 and 8 to pins 9 and 10 when you select an 8 channel relays. This won't work because those pins are used by the radio module.
I think you can connect a relays to any pin which is not used. If so this will make it possible to connect up to 14 relays to the mini pro when also the pins A5-A7 are available.
// Enable debug prints to serial monitor #define MY_DEBUG // Enable and select radio type attached #define MY_RADIO_NRF24 //#define MY_RADIO_RFM69 // Enable repeater functionality for this node #define MY_REPEATER_FEATURE #include <MySensors.h> const int PINS[] = {3, 4, 5, 6, 7, 8, 14, 15}; // I/O pins 3, 4, 5, 6, 7, 8, A0, A1 for the relays #define NUMBER_OF_RELAYS 8 // Total number of attached relays #define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relay void before() { for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) { // Then set relay pins in output mode pinMode(PINS[sensor-1], OUTPUT); // Set relay to last known state (using eeprom storage) digitalWrite(PINS[sensor-1], loadState(sensor)?RELAY_ON:RELAY_OFF); } } void setup() { } void presentation() { // Send the sketch version information to the gateway and Controller sendSketchInfo("Relay", "1.0"); for (int sensor=1; sensor<=NUMBER_OF_RELAYS; sensor++) { // Register all sensors to gw (they will be created as child devices) present(sensor, S_BINARY); } } void loop() { } 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(PINS[message.sensor-1], 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()); } } -
Hi @Woeka
I've been testing your code, but works inverse mode
if switch in domoticz is on the relay is off and vice versa@ijobain Try changing
#define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relayTo
#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 relayand see if that works as you expected
-
I'm attempting to get my double relay board containing two switch inputs. I see that the two sketches suggested on the relay page are there but one seems to be updated to the new version which allows you to use multiple relays on the board but the other isn't, as far as i can see. How do I go about making the sketch with buttons accept more relays?
@Samuel235 Have a look at this thread for some ideas.
-
@ijobain Try changing
#define RELAY_ON 0 // GPIO value to write to turn on attached relay #define RELAY_OFF 1 // GPIO value to write to turn off attached relayTo
#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 relayand see if that works as you expected
-
@Samuel235 Have a look at this thread for some ideas.
@Boots33 - Thanks dude, I have had a quick skim read just but will bookmark it when i get my gateway working. These issues right now are driving me insane, and spending the day to try and find the problem. Thank you for linking me to that post!
-
I just tried this sketch, works on latest version, but i cant seen any relais in Domoticz, only see the node relais, did i miss something
-
thx
-
Hi to all!
I would edit the secure relay sketch so in loop the node sends to the gateway, let's say every hours, his state (on or off).- I avoid delay otherwise Arduino cannot receive the comands from gateway, so I will use the millis sketch, modified from the official Arduino website.
My doubt is... How can I get the current state of the relay?
Relevant part of sketch is in loop and before setup()
/* TIME variables */ unsigned long previousMillis = 0; // will store last time state is sent // constants won't change: const long interval = 10000; // interval at which to send state (milliseconds === seconds x 1000). For test we try every 10 secondsAnd the main loop
void loop() { // check to see if it's time to send state; that is, if the difference // between the current time and last time you sent state is bigger than // the interval at which you want to send the state. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; // send the state here! // test Serial.println("This is the moment where I need to send state"); //send(?????????); // Send current state. Probabily will update also the log? }The base sketch is >>> https://github.com/mysensors/MySensors/blob/master/examples/SecureActuator/SecureActuator.ino
And the millis example is >>> https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Thank you to all!
-
Hi to all!
I would edit the secure relay sketch so in loop the node sends to the gateway, let's say every hours, his state (on or off).- I avoid delay otherwise Arduino cannot receive the comands from gateway, so I will use the millis sketch, modified from the official Arduino website.
My doubt is... How can I get the current state of the relay?
Relevant part of sketch is in loop and before setup()
/* TIME variables */ unsigned long previousMillis = 0; // will store last time state is sent // constants won't change: const long interval = 10000; // interval at which to send state (milliseconds === seconds x 1000). For test we try every 10 secondsAnd the main loop
void loop() { // check to see if it's time to send state; that is, if the difference // between the current time and last time you sent state is bigger than // the interval at which you want to send the state. unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; // send the state here! // test Serial.println("This is the moment where I need to send state"); //send(?????????); // Send current state. Probabily will update also the log? }The base sketch is >>> https://github.com/mysensors/MySensors/blob/master/examples/SecureActuator/SecureActuator.ino
And the millis example is >>> https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
Thank you to all!
@sineverba
Edit:
I did edit the pre-setup configurationint val = 0; // the value of digital pin #define CHILD_ID 1 // Id of the sensor child MyMessage msg(CHILD_ID,V_LIGHT);And I did edit the loop:
if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED previousMillis = currentMillis; // send the state here! // test Serial.println("This is the moment where I need to send state"); val = digitalRead(LOCK_1); Serial.print("Il valore del relay รจ "); Serial.println(val); send(msg.set(val),true); // Send new state and request ack back. }I got the update in Domoticz in Hardware > Configuration > Children of nodes

And I did test also every 3 seconds, all ok.
But I don't have the refresh in "main page":

I did test removing current to Arduino when Relay was ON. With Arduino off, I did change the state in domoticz to OFF.
Because Arduino save previous state in EEPROM, after full start the relay did go ON, in Domoticz got "1" in children (all right, because digitalRead was 1) but main panel shows OFF.
I did expect a graphical change...
Thank you (and hope that all is clear) :)
-
Hi to all!
To achieve my goal (node that sends relay state after power failure to sync with domoticz), I'm editing the button script.I have added this line
state = digitalRead(RELAY_PIN); send(msg.set(state?false:true), true); // Send new state and request ack backin setup, so I thought it send only one time at startup the current state after power failure.
But..... the relay shutdown/shuton (or viceversa) twice, in fast sequence....
Thank you very much!
-
Hi to all!
To achieve my goal (node that sends relay state after power failure to sync with domoticz), I'm editing the button script.I have added this line
state = digitalRead(RELAY_PIN); send(msg.set(state?false:true), true); // Send new state and request ack backin setup, so I thought it send only one time at startup the current state after power failure.
But..... the relay shutdown/shuton (or viceversa) twice, in fast sequence....
Thank you very much!
Bingo!
If I did understand well, the actuator sketch DOESN'T directly acts the relay, but sends to the gateway and expect from the gw itself the "new state" (in effect, you short GND & PIN 4, arduino sends new state to the gateway and gateway resend the new state).
So, 'cause I would to update the state on controller, I did this simple edit on setup:
// Set relay to last known state (using eeprom storage) state = loadState(CHILD_ID); digitalWrite(RELAY_PIN, state?RELAY_ON:RELAY_OFF); Serial.print("Lo stato ultimo era "); Serial.println(state); if ( state == 1) { state = 0; } else { state = 1; } send(msg.set(state?false:true), true); // Send new state and request ack backBasically, If relay was OFF on a power failure, and someone on Domoticz try to start the relay, the state in Domoticz will be "on".
When power returns, the arduino read the state (in my example OFF == 0), convert in 1 and send to the gateway the 1. The gateway // Domoticz will be answer with 0.
So, relay doesn't nothing and Domoticz state is update.
But.... why I need to invert the logic? :D
-
Hi to all!
To achieve my goal (node that sends relay state after power failure to sync with domoticz), I'm editing the button script.I have added this line
state = digitalRead(RELAY_PIN); send(msg.set(state?false:true), true); // Send new state and request ack backin setup, so I thought it send only one time at startup the current state after power failure.
But..... the relay shutdown/shuton (or viceversa) twice, in fast sequence....
Thank you very much!
@sineverba said in ๐ฌ Relay:
To achieve my goal (node that sends relay state after power failure to sync with domoticz)
Have a look at the sketch I used for my Synchronising Light switch you may be able to use some of that code perhaps.
-
@sineverba said in ๐ฌ Relay:
To achieve my goal (node that sends relay state after power failure to sync with domoticz)
Have a look at the sketch I used for my Synchronising Light switch you may be able to use some of that code perhaps.