Learn how to create action handlers and map them to specific actions in the Bytebeam platform with this comprehensive document. It includes a code snippet for creating a handler for the “ToggleLED” action and mapping it.
Actions are commands that the platform sends to the client. Every action has a type and we need to register an action handler for each action type. Following example shows a way to create action handler and map it to a particular action:
Copy
Ask AI
// handler for ToggleLED actionint ToggleLED_Handler(char* args, char* actionId) { // Log the recieved arguments and action id to serial for the reference Serial.printf("*** args : %s , actionId : %s ***\n", args, actionId); // // Include command for toggling LED over here // bool status = true; // This function allows platform to know about the completion of particuar action status = Bytebeam.publishActionCompleted(actionId); if(!status) { // // handle the publish action completed error here // return -1; } return 0;}//// Once you have created an action handler for particular action then it should be// mapped with particular action. Below function call would do that mapping.//// ToggleLED_Hanlder : pointer to action handler// ToggleLED : actionBytebeam.addActionHandler(ToggleLED_Handler, "ToggleLED");
While we execute the action, we need to send the progress back to the server so that the user can monitor the action remotely. To do so the following APIs can be used:
Copy
Ask AI
//// Action completed status response can be published by using below function//bool Bytebeam.publishActionCompleted(char* actionId);//// Action failed status response can be published by using below function//bool Bytebeam.publishActionFailed(char* actionId, char* error = "Action Failed");//// Action progress status response can be published by using below function//bool Bytebeam.publishActionProgress(char* actionId, int progressPercentage, char* status = "Progress");
Have a look at the ToggleLED example sketch for a full-fledged example showing how to add the action and publish the action status response to bytebeam cloud.