Getting Started with ESP32 IDF and ByteBeam Part 3 - Handling Actions

Getting Started with ESP32 IDF and ByteBeam Part 3 - Handling Actions
Learn to create and handle actions on your IoT devices using Bytebeam cloud

Earlier in Part 1 and Part 2, we understood some basic concepts of bytebeam cloud that can be very helpful for your IoT application. In Part 1 we discussed device management and data visualization.
In Part 2 we performed over the air firmware update on our ESP32 device.
Within this article, we will be focusing on action handling using the Bytebeam cloud.

What are Actions

turn on your AC remotely so that you get a nice environment when you reach your home or turn off your lights remotely to save energy bills.

These are some examples of actions that you can add using the bytebeam cloud. An action has a command-response model. It contains a set of commands that can be used to perform tasks or operations and generates a response on completion. These actions become very handy when you want to control your IoT devices remotely.

How Bytebeam helps with Action Handling

Bytebeam offers this cool feature to create an action using simple steps. Next, we will guide you through the procedure to create an action using the bytebeam cloud and handle the action on your IoT device. Here in this tutorial, we will be using action to toggle ESP32 built-in led

So let's get started.

Hardware Requirements

  • ESP 32 dev module

Configuring actions in ESP32

To understand action we will toggle the state of ESP32 built-in led. We will write an action handler to receive action from the bytebeam cloud.

  • Navigate to basic_example -> main -> app_main.c
  • Define ESP32 GPIO. ESP32 built-in led is configured at GPIO 2
#define BLINK_GPIO 2
  • In configure_led function, we will configure the GPIO mode as GPIO_MODE_OUTPUT
static void configure_led(void)
{
    gpio_reset_pin(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
}
  • Add an action handler using bytebeam_add_action_handler function. It contains the following arguments
    • bytebeam_client: structure containing host, port, certificates and device id information
    • toggle_led: handler function to toggle led
    • toggle_board_led: action name.
 bytebeam_add_action_handler(&bytebeam_client, toggle_led, "toggle_board_led");
  • Now flash and upload the example
Idf.py -p PORT flash monitor

In the next section, we will add "toggle_board_led" action on the bytebeam cloud.

Adding action on bytebeam cloud

  • Login to your bytebeam cloud account and go to the Device Management tab
  • Here you can see preconfigured actions like Update Firmware, Update Configuration, and Push File.
  • To create a new action Click on the Admin tab and then navigate to Actions. Then click on Create Action to configure a new action.
  • On the next screen, you can see a Type edit text and a drop-down to select Icon.
  • In Type edit text we need to write down the action name i.e. toggle_board_led in our case. Earlier in Configuring actions in ESP32 section we created an action handler with toggle_board_led as the action name.
  • Then select an icon from the drop-down. We have selected power off icon for this example. You can choose any from the list. Then hit submit

  • Now you can find newly created actions on the action list

Triggering an Action

We need to follow these easy steps to trigger an action:

  • Navigate to the Devices tab under Device Management
  • Click on the checkboxes to select the list of devices you want to trigger the Action
  • Click on the Action button you want to trigger. In our case, we have selected a device with #id 1 and triggered 'Toggle Board Led' Action.
  • In the Last Action column, you can see the Action status as Queued and you can monitor the progress of the action from the progress bar
  • The Action will be sent to the selected Devices now. If the device is offline when the Action is triggered, the system will retry sending Actions to the device every 5s.
  • After successful execution of the action, the Action status will be charged to Completed.

Conclusion

Actions can be very handy in many use cases. I hope you find this guide useful. We will come up with more interesting tutorials and maker content. Stay tuned.