Skip to content

Instantly share code, notes, and snippets.

@LosantGists
Created April 12, 2016 15:21
Show Gist options
  • Save LosantGists/7d5d6a8e545aa3826148de5f431aa5b8 to your computer and use it in GitHub Desktop.
Save LosantGists/7d5d6a8e545aa3826148de5f431aa5b8 to your computer and use it in GitHub Desktop.
losant-sneak-preview-2-m2m-communication
void buttonPressed() {
Serial.println("Button Pressed!");
// Losant uses a JSON protocol. Construct the simple state object.
// { "button" : true }
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["button"] = true;
// Send the state to Losant.
device.sendState(root);
}
void toggle() {
Serial.println("Toggling LED.");
ledState = !ledState;
digitalWrite(LED_PIN, ledState ? HIGH : LOW);
}
// Called whenever the device receives a command from the Losant platform.
void handleCommand(LosantCommand *command) {
Serial.print("Command received: ");
Serial.println(command->name);
if(strcmp(command->name, "toggle") == 0) {
toggle();
}
}
// Register the command handler to be called when a command is received
// from the Losant platform.
device.onCommand(&handleCommand);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment