Skip to content

Instantly share code, notes, and snippets.

@bathoryr
Created February 3, 2017 11:54
Show Gist options
  • Select an option

  • Save bathoryr/3dc744d0cc40692f1d77d0d408c4101c to your computer and use it in GitHub Desktop.

Select an option

Save bathoryr/3dc744d0cc40692f1d77d0d408c4101c to your computer and use it in GitHub Desktop.
MySensors - Get value from other node
// 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));
}
}
}
}
}
// 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