Relais sketch, auto switch on again after x seconds
-
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()); } }``` -
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.
-
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 compilingThis 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()); } } -
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 compilingThis 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()); } } -
@ Boots33: both my vera's are running with version 1.5 . Can I just update , will the sketches wich are now running still work?
@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
-
@ 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,
CorFor 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()); } }```
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