The best tools to make your project dreams come true

Login or Signup
USD

How to Receive Cloud Commands with DK IoT Studio

12/2/2019 | By Maker.io Staff

Cloud Commands with DK IoT Studio

In a previous how-to, we learned how cloud events can be triggered, which allows the app and devices to submit data to the cloud. This is highly beneficial in data logging applications such as environmental monitors, but what about situations when the cloud needs to inform the device or app of an event? This is where cloud commands come in, and they allow the Digi-Key IoT Cloud to interact with devices by sending them data upon some condition. The example in this how-to demonstrates how cloud commands can be paired with cloud events to incorporate data received confirmation, so that devices can be assured that their data has been stored.

Like in our previous how-to, we will be using a simple example that includes an interval, a GPIO, a counter, and a Bluetooth element. The interval triggers the counter to increment, and upon incrementing, the counter triggers the GPIO pin connected to the onboard LED to toggle as well as sending the current counter value over Bluetooth. The purpose of the GPIO toggle is to provide a visual cue that the program is functioning correctly.

How to Receive Cloud Commands with DK IoT Studio

BOM

Programming the App

The device app only has three GUI elements, but it also has a number of element interactions to make a system that allows users to submit data to the cloud. The first element in the app is an interval that probes the Bluetooth element for data from the device (which is sending numbers of increasing value), and upon receiving new data, the CounterDisplay label is made to show the current counter value.

A button on the app allows users to submit the current value of the counter to the cloud, and when this button is pressed, it performs two actions. The first action is to change the text of the CloudStatus label to “Data Sending…”, so that users know that the data has been submitted. The second task performed by the button press is sending the value of the counter display label to the cloud as a cloud event.

How to Receive Cloud Commands with DK IoT Studio

Executing the Program on the Cloud

The program executed by the cloud is very simple and consists of the cloud event (which receives submitted data), a cloud storage element to store the data, and a cloud command that provides our app with the data submission response. The cloud event takes the data from the label and saves the value to a variable called “CounterValue.” This is achieved by using a JSON object, whereby the first entry (in quotations) is the variable name and the second entry is the variable itself (called value). When data is added to the cloud storage element, the cloud command element is also triggered (upon storing data). This element is made to send back the text “OK,” so that the device app can display this message to the user.

How to Receive Cloud Commands with DK IoT Studio

Back to The App

If we go back to the app, we can now look at the last step of the app’s function. The reason for looking at the cloud system before this last program step is because of logical flow. Now that the cloud has sent back a response “OK,” we need to display this message to the user on the app. The cloud command is event triggered and does not require an interval element (i.e., it triggers as soon as a command is received), and this element shows its message on the CloudStatus label. However, unlike other elements that use the variable “value,” the CloudStatus returns a JSON object. Therefore, to extract the message we use value.command which looks at the command variable of the JSON object.

Receiving Cloud Commands opens a host of exciting possibilities for the Digi-Key IoT Studio that we will continue to explore with future posts!

How to Receive Cloud Commands with DK IoT Studio