Direct pairing of two nodes implementation

I've been looking for some time for implementation of pairing two nodes to each other to be able to send messages directly omitting controller. I've seen some other posts suggesting to add this functionality to mysensors core, and proposing base scheme of how it should look like. After some reading I realized that it is not hard to write such code by myself. So I tried my best and now I have what seems to be a working code for two test nodes and a gateway. One of the nodes is modified sketch for binary button the other one is simple one channel relay. I'm not a professional programmer, so code can be optimized more, I guess, and maybe rewritten in better manner.

It's primarily useful for direct communication between a button node (a light switch, or motion sensor) and some relay or other actuator. The key feature is the ability to avoid necessity of controller presence for some simple usage like light switching. Also it provides better reliability in case of controller failure.

Both nodes must have specific pairing button which also serves as "Clear EEPROM" button if pressed before node is powered on. So basically when pairing button is pressed node enters pairing mode for required amount of time (in my case 10 seconds) and sends special message to controller which collects its node and children id's and then waits for 10 seconds for another node to send request in which case it then sends id's vice versa. My code is written only for single button/relay nodes, but it can be made to pair specific child sensors on every node.

@rfm69 It was more a proof of concept. I didn't really use it since I wrote it. But then it worked for my testing quiet well. I'd like to know, what you had to change to make it work on your setup. And I am happy that someone made use of my little effort

@monte I was wondering if you'd used it or what your experience of it was. I'm also just enjoying learning by doing... but wanted to see the node to node communication working and more easily configurable. I'd like to configure this from the controller also perhaps.

I've moved much of the functionality to its own class so that it can be more easily added to nodes. Heres what I've got so far.

I used avdweb_Switch.h to simplify and abstract a little the button press/etc.

I'd still like to clear up a little and add a few more things, like having multiply nodes paired together etc.

Its still pretty much your origional code.

There was a small error that both nodeid & sensorid had to be different from the master and slave paired node... that was in gateway.ico

@rfm69 Thank you for the tip about an error, it was really a wrong condition to check.

As for the button sending message directly to paired node but not to controller I made this intentionally. In my use scenario you have simple binary button that sends a message when it pressed. This message doesn't describe "on" or "off" state, it's receiving part who manages the logic of flipping binary state. So if you chose to send message both to paired node and to controller it will arrive to destination node 2 times. At least thats how my controller (domoticz) works. It definitely will mess up your switching logic on destination node, or you will need to write the logic on both node and controller, but message will still be send second time without any purpose. So I chose that button will communicate with controller only if it's not paired, and the paired node will send it's state to controller itself (if message received form paired node).

If you don't mind, could you publish your "avdweb_Switch.h" when you finish your work? So the community could benefit from it

@monte I didn't think, or worry to much yet about the exact logic of buttons or what would happen with confused logic between controller yet. I just commented some things out for testing so I could get it working again, after I had refactored it into the separate class. I think I agree with you and what you said, but have to get my head around it.

The avdweb_switch.h code, it isn't mine, but I took it from link text. I'll document and finish up the code a little more, and of course happy to share everything.

I've been trying the NodeManager code as well, which made me want to modualize your code a little, because well I get confused easily and need things to look simple.

Added registering multiply pairings, flashing led to indicate if nodes are in pairing mode etc. Still needs some work though, any feed back welcome.

Id like to add some messaging so controller could query the nodes and how they are connected, and configure them also from the controller, but don't know yet how to to this with templates and custom buttons etc in mycontroller.