Created
February 3, 2017 11:54
-
-
Save bathoryr/3dc744d0cc40692f1d77d0d408c4101c to your computer and use it in GitHub Desktop.
MySensors - Get value from other node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Client side (requesting node). | |
| #define SERVER_NODE 99 | |
| void setup() | |
| { | |
| // Request for value from other node, response will be precessed by receive() | |
| requests(CHILD_ID_LIGHT, V_PERCENTAGE, SERVER_NODE); | |
| } | |
| void receive(const MyMessage &message) | |
| { | |
| if (message.sensor == CHILD_ID_LIGHT) { | |
| if (message.type == V_PERCENTAGE) { | |
| if (message.getCommand() == C_SET) { | |
| dimmerValue = message.getInt(); | |
| Serial.print("Receved new value for dimmer: "); | |
| Serial.println(dimmerValue); | |
| if (message.sender == SERVER_NODE) { | |
| // Message come from "server" node, send new value to controller | |
| MyMessage dimmerMsg(CHILD_ID_LIGHT, V_PERCENTAGE); | |
| send(dimmerMsg.set(dimmerValue)); | |
| } | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Server side (responding node). Replies to queries for value (request messages). | |
| int dimmerValue = 0; | |
| void setup() | |
| { | |
| // Request value stored on controller. Must be supported by controller. | |
| request(CHILD_ID_LIGHT, V_PERCENTAGE); | |
| } | |
| void receive(const MyMessage& message) | |
| { | |
| // Specify childId, typeId | |
| if (message.sensor == CHILD_ID_LIGHT) { | |
| if (message.type == V_PERCENTAGE) { | |
| if (message.getCommand() == C_SET) { | |
| // C_SET command received - set value of the dimmer | |
| dimmerValue = message.getInt(); | |
| } | |
| if (message.getCommand() == C_REQ) { | |
| // C_REQ command - Other node is requesting value | |
| MyMessage replyMsg(message.sensor, message.type); | |
| replyMsg.setDestination(message.sender); | |
| // Send response msg with new value | |
| send(replyMsg.set(dimmerValue)); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment