The best tools to make your project dreams come true

Login or Signup

10/28/2019 | By Staff

How to Use Digi-Key’s IoT Studio Buffer

Ideally, all electronic devices would be able to process data as soon as it is available with no delays. Unfortunately, there are many weak points in the data processing chain, which can result in situations where a processing point cannot physically process the data fast enough. In these scenarios, the incoming data can be temporarily stored to prevent losing incoming data. This memory system is called a buffer, which plays a key part in modern electronics. In this how-to, we’ll see how to use the Digi-Key IoT Studio buffer element!

Parts to use

Adding and Removing Data from a Buffer

Not all buffers are the same, and how they function can vary depending on the task. For example, some buffers are circular, meaning that when they are full, they start back at the beginning, while others have specific methods for data going in and out. Before we look at the buffer element used in the Digi-Key IoT Studio, we need to know about a few terminologies when talking about buffers.

  • Push – When data is placed into a buffer, it is said to have been pushed in
  • Pop – When data is removed from a buffer, it is said to have been popped out

While some buffers can have any element retrieved, it is rarely done. Instead, buffers are used sequentially to preserve the order in which data was sent. The buffer element used in the Digi-Key IoT Studio is known as a First In Last Out buffer, which means that data that was pushed in first will be the last data to be popped out. To better understand this concept, let’s take a simple example:

  • If the numbers 5, 4, and 3 are pushed in that order onto a buffer, they will be popped out in a 3, 4, and 5 order.
  • If the numbers 3 and 4 are pushed onto a buffer and then one number is popped, the number will be 4. If, after this, the number 5 is pushed, then 5 and 3 will be popped.

Using the Buffer

The use of the buffer in this example will demonstrate how the buffer can be used to store data. The Adafruit Huzzah 32 will be programmed to use a new element — the counter — that will be incremented once every two seconds. Each time the counter is incremented, the value of the counter is sent to the app via Bluetooth. The interval element, which triggers once every two seconds, is used to increment the counter, as well as retrieve its current value. The counter can be made to have a roll over value, which causes it to reset to 0 once that value has been reached. In this example, we leave the counters defaults which have the counter count between the numbers 0 and 9.

How to Use Digi-Key’s IoT Studio Buffer

The app side of the how-to is essential because it uses an interval to probe a Bluetooth element once every second for values that have been sent to it. When valid data is returned by the Bluetooth element, the data is then pushed onto the buffer. At the same time, the buffer sends the data that was just pushed to it to a label element, so we can see what the last number to be sent was.

Pushing a value onto the buffer also causes another label element to display the current contents of the buffer (this allows us to visually see what resides in the buffer). A button element is then connected to the buffer and is set so that when the button is pushed, it causes a value to be popped off the buffer. This popped value is then sent to another label element, so we can see what the value is.

When using values from the buffer, it is important that you use the correct name. In a previous article, we saw how entries in the Digi-Key IoT Studio can accept JavaScript-like code, and the buffer is a prime example of this. If, for example, we want to access pushed data, then we need to use the variable pushedValue. If we want to access popped data, then we need to use the variable popped value. The contents of the whole buffer can be accessed with the variable buffer, which will be used in another how-to when we will look at the foreach element.

How to Use Digi-Key’s IoT Studio Buffer