Hi @Sunday_Ajiroghene,
The {{toggle-0}} value you are sending to the device is not being used. Within the Input Block, your toggle can either be true
or false
. There are two things being sent to your device: the Command Name
and the Payload
:
If you were to push Send
in the current configuration, the {{toggle-0}} value would be false
, and the data would be sent to your device in this format:
{
"name": "Trigger",
"payload": {
"Trigger": "false",
}
}
The code you currently have is only looking for the name
of the Command, which would be “Trigger” ("name":"Trigger"
), and is not using the payload value. Thus, the way the code is currently written, the light will toggle each time the button is triggered:
void handleCommand(LosantCommand *command) {
Serial.println();
Serial.print("Command received: ");
Serial.println(command->name);
//this line prints the name of the command, which is Trigger
if (strcmp(command->name, "Trigger") == 0) {
//if the name of the command is trigger, toggle the light
toggle();
}
}
The if statement you have above is always true, as the name of the command is “Trigger”, so toggle() is being called each time the button is pushed.
You must access the payload you sent your device to know if the toggle was true
or false
. Here is an example:
void handleCommand(LosantCommand *command) {
JsonObject& payload = *command->payload;
payload.printTo(Serial); // print the entire payload
Serial.println(payload['key']); // print the 'key' property of the payload object
}
You will only need to access this value to create your conditional statement. Then, if true
is sent on the payload, you could toggle the light on, and if false
is sent, you could toggle it off. The payload sent from the Input Block can help you create cases for when the light should be on or off.
Let me know how else I can help!
Julia