Well, it looks like you are still mixing up the meaning of sensor
and sender
in the code. If the controller sends a message to the sensor you have set up in the sketch above (20), while you are comparing against 0 in receive()
, you will never detect that message. Compare against 20.
Remember how I advised to get rid of magic numbers and use constants instead? If you would add something like #define SENSOR_ID 20
and use that variable instead of 0 and 20, you might be able to avoid such confusions, because you give those arguments a meaningful name.
Let's try to explain it another way, so that you can adapt it to any situation in the future.
Sensor: In the context of a MySensors sketch, stop thinking of a sensor being a (physical) device. It is just a unique identifier for one of many different data points a device (MySensors calls this device a node) wants to share with others. Think of a sensor (or also often called child) as one of up to 255 wires going from one node to any other, whereby each wire represents a single type of data, like a temperature, a string, voltage level, a boolean value.
Sender: When a node sends a message, it includes a reference to itself - the node ID - as the sender
, as well as a reference to the target node as the destination
. Both sender
and destination
enable MySensors to route messages through the network, no matter if it is a direct A-to-B connection or if the message needs to be forwarded by multiple repeaters.
The MyMessage class is used to manage those messages. It stores all kinds of information neccessary to share data between nodes, send and request commands independently from the selected transport method (RF24, Sub-GHz, wired) and controller connection (Serial, MQTT, WiFi, Ethernet).
Imagine a simplified MyMessage instance as a collection of variables and a bunch of helper functions to make your life easier. When the controller (via the GW) sends the message to the node, as you described above, the message would look like this on the receiving node:
MyMessage msg
{
sender = 0; // Node ID of the message's origin (the GW)
destination = 7; // Node ID of this device (I assumed this number!)
sensor = 20; // Child ID / data point that this message wants to update
type = 3; // S_BINARY == 3
[...]
getBool(); // Returns the payload as a boolean
getSensor(); // Returns the value of sensor
setSensor(n); // Changes the value of sensor
getDestination(); // Returns the value of destination
[...]
}
So what do you have to do if you want to update the local variable RESET_SOFT
on that node whenever it receives a new value? You have to test that the incoming message is of the expected type
and that it concerns the right sensor
. If you also want to make sure that only the controller or GW can cause an update of RESET_SOFT
, you must validate that sender
- in other words, the origin of this message - is valid as well.
I really hope this makes sense to you, as I'm running out of ideas how to explain what is going on behind the scenes.
Maybe a look at the Serial API introduction can also help you further.