Thank you mfalkvidd, I did it and it is working well now!.
For those who want a simple solution and as future reference, here is my simple code:
#define MY_DEBUG
#define MY_BAUD_RATE 38400
#define MY_RADIO_NRF24
#define MY_RF24_PA_LEVEL RF24_PA_HIGH
#define MY_RF24_CHANNEL 115
#define MY_RF24_DATARATE RF24_250KBPS
#define MY_RF24_BASE_RADIO_ID 0x00,0xFC,0xE1,0xA8,0xA8
#define MY_NODE_ID 164
#include <SPI.h>
#include <MySensors.h>
#define RELAY_PIN 2 // 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
#define RELAY_CHILD_ID 1 // Id of the sensor child RELAY
#define MOTION_CHILD_ID 2 // Id of the sensor child MOTION
#define DIGITAL_INPUT_SENSOR 3 // The digital input you attached your motion sensor. (Only 2 and 3 generates interrupt!)
uint32_t SLEEP_TIME = 10000; // Sleep time between reports (in milliseconds)..NOT USED ANYMORE BECAUSE IT TURNS OFF THE NODE AND IT CAN'T RECEIVE RELAY REMOTE TRIGGERING ORDER
boolean lastMotion = false;
// Initialize motion message
MyMessage msg(MOTION_CHILD_ID, V_TRIPPED);
void before()
{
for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
// Then set relay pins in output mode
pinMode(pin, OUTPUT);
//Always starts OFF
digitalWrite(pin, RELAY_OFF);
}
pinMode(DIGITAL_INPUT_SENSOR, INPUT); // sets the motion sensor digital pin as input
}
void setup()
{
}
void presentation()
{
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay_PLUS_PIR", "1.0");
for (int sensor=1, pin=RELAY_PIN; sensor<=NUMBER_OF_RELAYS; sensor++, pin++) {
// Register all sensors to gw (they will be created as child devices)
present(sensor, S_LIGHT);
}
// Register all sensors to gw (they will be created as child devices)
present(MOTION_CHILD_ID, S_MOTION);
}
void loop()
{
// Read digital motion value
bool motion = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH;
if (lastMotion != motion) {
lastMotion = motion;
Serial.println(motion);
send(msg.set(motion?"1":"0")); // Send tripped value to gw
}
// Sleep until interrupt comes in on motion sensor. Send update every two minute.
// NOT USED
// sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
// NOT USED
}
void receive(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( RELAY_PIN, message.getBool()?RELAY_ON:RELAY_OFF);
//digitalWrite(message.sensor-1+RELAY_PIN, 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());
}
}
Regards!