OK, I rephrase the request.
I have at home this CONFIGURATION
I created the following script:
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
//#define MY_RADIO_RFM69
// Enabled repeater feature for this node
#define MY_REPEATER_FEATURE
#include <SPI.h>
#include <MySensors.h>
#define RELAY_PIN 3 // Arduino Digital I/O pin number for relay
#define BUTTON_PIN 4 // Arduino Digital I/O pin number for button
#define CHILD_ID 1 // Id of the sensor child
#define RELAY_ON 0
#define RELAY_OFF 1
bool state;
int luce;
int soglia = 25; // imposed sensor sensitivity
int statorelay = 0; // variable that saves in memory initial state relay
MyMessage msg(CHILD_ID,V_LIGHT);
void setup()
{
pinMode(RELAY_PIN, OUTPUT); //
digitalWrite(RELAY_PIN, RELAY_OFF); // Initial setting relay on off
}
void presentation() {
sendSketchInfo("RELAY DEVERTER", "1.0");
present(CHILD_ID, S_LIGHT);
}
void loop()
{
int value;
luce = (1023-analogRead(A0))/10.23; //
I note if there is current on the bulb. I use a 220v led with a photoresistor.
if(luce < soglia) {
send(msg.set(value==HIGH ? 1 : 0)); //voltage yes
}
else {
send(msg.set(value==LOW ? 1 : 0)); // voltage no
}
delay (1000);
}
void receive(const MyMessage &message) {
// SECTION ENTER POWER RELAY WITH REVERSE
if (message.getBool()== 1, statorelay ==0) { // POWER ON MESSAGE WITH BULB OFF
Serial.print("ACCENDI RELAY - Lampanida accesa ");
digitalWrite(RELAY_PIN, RELAY_ON);
statorelay=1; // salvo in memoria stato relay
return; // blocco script
}
if (message.getBool()== 0, statorelay ==1) { // SHUTDOWN WITH MESSAGE ON BULB
Serial.print("SPEGNI RELAY - Lampanida spenta");
digitalWrite(RELAY_PIN, RELAY_OFF);
statorelay=0;
return;
}
if (message.getBool()== 0, statorelay ==0) { // SHUTDOWN MESSAGE WITH BULB OFF - REVERSE
Serial.print("ACCENDI RELAY - Lampanida spenta ");
digitalWrite(RELAY_PIN, RELAY_ON);
statorelay=1;
return;
}
if (message.getBool()== 1, statorelay ==1) { //POWER ON MESSAGE WITH BULB ON - REVERSED
Serial.print("SPEGNI RELAY - Lampanida accesa ");
digitalWrite(RELAY_PIN, RELAY_OFF);
statorelay=1;
return;
}
}
This script works, and allows me to see if the bulb is actually on or off, and turn it off or turn it on indipendetemente the state of the relay.
The problem is the script:
- I would like to add more relays with detectors.
- I would like to send the status of the bulb to the gateway only when actually changes.
Can you help me? I'VE BEEN CLEAR?
THANK YOU!!!