๐ฌ Relay
-
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.
-
Never seen the request function.
You are hero-member for a reason...
Thank you very much! :) :)@sineverba said in ๐ฌ Relay:
Never seen the request function.
You can find request and other useful info on the API page.
Hope you are enjoying your MySensors journey :) -
Hello,
since the relay has leds and is therefore power consuming, i was wondering if someone has written a sketch for a low power usage of the relay (as for the sensors) allowing the arduino (and the relay) to sleep all the time and wake up every 5 minutes for instance just to check if the controller has changed the desired state of the switch.
Of course then the relay could be as late as 5minute, but this is not an issue for my application ...
thanks for any suggestion
-
Hello,
since the relay has leds and is therefore power consuming, i was wondering if someone has written a sketch for a low power usage of the relay (as for the sensors) allowing the arduino (and the relay) to sleep all the time and wake up every 5 minutes for instance just to check if the controller has changed the desired state of the switch.
Of course then the relay could be as late as 5minute, but this is not an issue for my application ...
thanks for any suggestion
-
thanks , indeed it's because the relay is controlling a 12V battery power to solenoid valve , that low power consumption is needed. BTW the 12V is obtained from a 4S lipo (16.5V max voltage) and a 12 regulator and i dont know if its better to feed the arduino from a 5V regulator fed by the same Lipo , or use an additional set of AA batteries ... i remember that it's better to have an as low as possible drop in voltage to save power...
-
thanks , indeed it's because the relay is controlling a 12V battery power to solenoid valve , that low power consumption is needed. BTW the 12V is obtained from a 4S lipo (16.5V max voltage) and a 12 regulator and i dont know if its better to feed the arduino from a 5V regulator fed by the same Lipo , or use an additional set of AA batteries ... i remember that it's better to have an as low as possible drop in voltage to save power...
-
@fhenryco can't the relay node use power from whatever it is controlling?
For sleeping nodes, the smartsleep feature can be useful. Search for that keyword in the forum and look at https://www.mysensors.org/download/sensor_api_20#sleeping
@mfalkvidd smartsleep was not supported by domoticz at the begining of the year ... but there was a domoticz upgrade in july ... do you know if it's now supported ?