May 25, 2023 - 5 Min Read

Build on Momento: IoT device status

Learn how to use Momento Cache to store, update, and track the latest data from your IoT devices.

Allen Helton
Author
Caching

Share

The Internet of Things (IoT) is all around us. Everyday devices like our smartwatch, security cameras, even our automatic pet feeders all have little sensors in them that push data to the internet to help us be more connected. 

IoT devices push a lot of data. Depending on the types of sensors, a web service could receive data from a device (also known as a thing) anywhere from several times a second to once an hour. They could send location information, barometric values, moisture levels, etc…The device status either comes with the rest of the sensor data or is inferred from the data received – and it’s constantly updating.

The current state of the device is provided with every data push. Status history is typically not preserved, since we want the state of the device right now. Preserving status history might not be a cost effective solution either if we’re receiving tens of thousands of metrics every day.

This is a perfect use case for Momento Cache – a short-lived, frequently changing piece of information, available at a moment’s notice for real-time updates. So let’s dive into a build to see specifically how we could track device statuses on Momento. 

Building a Device Tracker

To build a device tracker, we first need to understand the components that go into an IoT app. A complete solution will consist of three parts:

  • Device interface: This is the firmware or software installed on the device that sends device information to the web service
  • Web service: The back-end code that ingests, analyzes, and formats the device data into meaningful values
  • User dashboard: The interface that presents the formatted data to end users

For our simple example application, we will build a single tenant app that maintains a list of all device data that comes in for a 24-hour period. It also sets the current status of a specific device in the cache, overwriting the previous status.

We use a set collection data type to track the unique device ids. A set is an unordered collection of unique elements, so no matter how many times we add a device with a specific id to it, we will only ever see it once. The time to live on the devices set item is 86400 seconds, which is 24 hours.

For tracking individual device statuses, we use a standard scalar set operation to store the value. Since we only need to track the latest value, this data type is the perfect choice.

As data comes in, the device list is updated and the device status is overwritten. Not too shabby for only 7 lines of code!

For the dashboard, we need to fetch the current data. We want to display a list of the devices that have pushed to the web service in the last 24 hours and provide their current status without clicking into them.

// GET /devices

const handler = async (event) => {
  await initializeCacheClient();

  let devices = [];
  const deviceResponse = await cacheClient.setFetch('iot', 'devices');
  if (deviceResponse instanceof CacheSetFetch.Hit) {
    const deviceList = deviceResponse.valueArrayString();
    await Promise.all(deviceList.map(async (deviceId) => {
      const statusResponse = await cacheClient.get('iot', deviceId);
      if (statusResponse instanceof CacheGet.Hit) {
        devices.push({ name: deviceId, status: statusResponse.valueString() });
      } else {
        devices.push({ name: deviceId, status: 'unknown' })
      }
    }));
  }

  return {
    statusCode: 200,
    body: JSON.stringify(devices)
  };
}

To get the list and current statuses, we first fetch the set that contains the device names. Then we do a get call for each device in the list in parallel. 

When we get a cache hit on the device status, we return it as is. However, if we get a cache miss, that means we haven’t heard from the device in a while. Every time data is pushed from a device, we reset the time to live (TTL) on the cache item that holds the status. The TTL is configured to be three times the expected interval length that we expect to hear from the device.

For example, if we expect to hear from an IoT device every 3 seconds, we set the TTL for the cache item that holds the status to 9 seconds. That way if one or two data pushes fail to our service fails we have some leniency before expiring the data.

If the item expires and we get a cache miss on lookup, then we return an unknown status and assume the device is off or in an unserviceable area.

Best practices

When tracking IoT device statuses, keep the following best practices in mind:

  • Use a set to store your list of devices. A set will automatically de-duplicate any entries, which you’ll need with the large data set you’ll be ingesting!
  • Store device status or other singleton values as a scalar value. This way you can overwrite stale information with the current value.
  • Set the time to live (TTL) on your cache items to 3+ times the expected data receival rate for a device. This will provide leniency if a device has a minor hiccup or the network is temporarily unavailable.

Ready to get started?

Momento Cache is the perfect use case for IoT device data. It’s fast, lightweight, and automatically expires data when it hasn’t heard from a device in a while. 

Assuming you’re as excited as I am, it’s time to get started! Hop on over to the Momento Console, grab a free auth token, and let’s hit the ground running! For a complete example, check out the repository on GitHub. Feel free to take bits and pieces, heck use the whole thing!

Have questions, thoughts, comments, or concerns? We’d love to hear from you! You can reach us on Discord or through our website

Share