@Alexander-Ivanov
The information in these posts may be of use.
mys-library-startup-control-after-onregistration
my_transport_dont_care_mode
In mySensors V2.1 the basic sketch for a switch that will work even if the controller/gateway is down would look something like this
// Enable debug prints to serial monitor
#define MY_DEBUG
// Enable and select radio type attached
#define MY_RADIO_NRF24
#define MY_TRANSPORT_WAIT_READY_MS 3000 //set how long to wait for transport ready.
#include <SPI.h>
#include <MySensors.h>
#include <Bounce2.h>
#define RELAY_PIN 4 // Arduino Digital I/O pin number for relay
#define BUTTON_PIN 3 // Arduino Digital I/O pin number for button
#define CHILD_ID 1 // Id of the sensor child
#define RELAY_ON 1
#define RELAY_OFF 0
Bounce debouncer = Bounce();
int oldValue = 0;
bool state;
MyMessage msg(CHILD_ID, V_STATUS);
void setup()
{
pinMode(BUTTON_PIN, INPUT); // Setup the button pin
digitalWrite(BUTTON_PIN, HIGH); // Activate internal pull-up
// After setting up the button, setup debouncer
debouncer.attach(BUTTON_PIN);
debouncer.interval(5);
pinMode(RELAY_PIN, OUTPUT); // set relay pin in output mode
digitalWrite(RELAY_PIN, RELAY_OFF); // Make sure relay is off when starting up
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("Relay & Button", "1.0");
// Register all sensors to gw (they will be created as child devices)
present(CHILD_ID, S_BINARY);
}
void loop()
{
debouncer.update();
// Get the update value
int value = debouncer.read();
if (value != oldValue && value == 0) { // check for new button push
state = !state; //Toggle the state
send(msg.set(state), false); // send new state to controller, no ack requested
digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF); // switch the relay to the new state
}
oldValue = value;
}
void receive(const MyMessage &message) {
// We only expect one type of message from controller. But we better check anyway.
if (message.type == V_STATUS) {
state = message.getBool(); // get the current state
digitalWrite(RELAY_PIN, state ? RELAY_ON : RELAY_OFF); // switch relay to current state
// Write some debug info
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}