DEV Community

Cover image for Using DutyCalls for IoT applications
Rob
Rob

Posted on

Using DutyCalls for IoT applications

Keeping track of the measurements of all your IoT devices is very useful and sometimes even necessary. This makes it possible to intervene before problems arise. In this blog we show how you can use DutyCalls, to provide insight into the measurements of all your devices.

Although we use the Python programming language in this post to explain the technical aspects, it is also interesting for non-programmers. DutyCalls can be used in a lot of other contexts. So also in non-technical environments. For information about the DutyCalls platform, please visit: https://dutycalls.me.


Getting started with DutyCalls

Before we can start monitoring our devices, we must first create a DutyCalls account. Then we need to create a workspace and set it up. Creating the account as well as the workspace is completely free.

For more information about setting up a workspace, consult the official DutyCalls documentation.


Collecting data from a device

Now that the DutyCalls environment has been set up, it is important to collect the data that you want to send to DutyCalls. For this tutorial, we'll be using the weather station we created for a previous blog post. This weather station consists of a Raspberry PI, a Sense HAT, a Python script for collecting measurements such as temperature/air pressure/humidity and a SiriDB database for storing the measurements. Check out our previous blog for more information about this.

Now take the following Python variable as an example of the script's output:

# This is the current relative humidity, expressed in
# percentage and rounded to 2 decimal places.
humidity = 44.35
Enter fullscreen mode Exit fullscreen mode

Posting the data to DutyCalls

How can this information be made easily visible and how can you be informed if something is wrong? For example, when the humidity in your home is too high or too low. We can do this by simply creating a Ticket in DutyCalls. A Ticket is an object in DutyCalls that represents an event. We need to assign a number of characteristics to this Ticket, so that DutyCalls knows what to do with it.

For example, we could compose a Python Dictionary with the following characteristics on the basis of the measurement just mentioned:

ticket = {
    title: Humidity is fine.,
    body: fCurrent humidity is: {humidity} percent.,
    severity: low
}
Enter fullscreen mode Exit fullscreen mode

Note: The above dictionary is based on a default service. You might have to change it according to your own service mapping.

Now to post this ‘ticket’ to DutyCalls we use the dutycalls-sdk. A Python library with which we can easily communicate with the DutyCalls API.

The use of this API does require credentials. To be specific, credentials from a DutyCalls Service. For information on how to retrieve these credentials, see the DutyCalls documentation.

This is how we authenticate with DutyCalls when using the Python library:

from dutycalls import Client

client = Client(login='abcdef123456', password='abcdef123456')
Enter fullscreen mode Exit fullscreen mode

Note: You should replace these fake credentials with your own.

Now all we have to do is post the dictionary named ‘ticket’ that we composed earlier!

That can be done this way:

# Create a new Ticket in DutyCalls.
await client.new_ticket(ticket=ticket, Example-Channel)
Enter fullscreen mode Exit fullscreen mode

Note: You should replace ‘Example-Channel’ with the name of your own Channel.

To confirm that everything went well, we check whether the newly created ticket is actually in our DutyCalls inbox. And as you can see below that is the case:

Image description

But what if the humidity is above a certain self-determined limit? You probably want to be notified immediately, as this can cause problems for both your health and your home. To achieve this, we're tweaking the DutyCalls related code of the script a bit:

from dutycalls import Client

humidity = 44.35

severity = high if humidity > 70 else low
title = Humidity is too high!” if humidity > 70 else Humidity is fine.

ticket = {
    title: title,
    body: fCurrent humidity is: {humidity} percent.,
    severity: severity
}

client = Client(login='abcdef123456', password='abcdef123456')

await client.new_ticket(ticket=ticket, Example-Channel)
Enter fullscreen mode Exit fullscreen mode

Now if the humidity is above 70% percent, we will receive an equivalent Ticket to the one below in DutyCalls:

Image description

We can see that the severity of the ticket is higher than the previous one and that an Alert has also been raised. When an Alert is raised in DutyCalls, the relevant user(s) will be notified about this. How the user is notified depends on the configuration in DutyCalls, but some of the methods are: push notifications (in browsers and in the mobile app), email, SMS and voice calls.


Tips & tricks

Below are a number of cool features of DutyCalls, which are definitely worth trying out when monitoring devices.

Integrations

All kinds of applications are or can be integrated with DutyCalls. This way you can monitor your devices and use the great features of DutyCalls from within your current applications. Checkout the documentation, to see how you can integrate your favorite application(s) with DutyCalls.

Alerts

Would you like to determine yourself when users are notified of events? Make use of alerts. These can be configured globally per workspace or specifically per channel depending on the type of the alert. This way you can decide for yourself when alerts are raised. For more information about the types of alerts and how to configure them, visit the DutyCalls documentation.

Ignore rules

Do you want tickets received by DutyCalls to be ignored when they meet certain conditions? Make use of “ignore rules”. These rules can be configured per channel. For more information about these rules, please visit the documentation.

Expectations

Would you like to be notified when a certain ticket has not been received by DutyCalls? Make use of expectation rules. Expectation rules ensure that when a certain ticket is not received that is expected, an alert will be raised. These rules can be configured per channel. For more information about these rules, please visit the documentation.

Actions

Do you want DutyCalls to take immediate action after receiving a Ticket? Make use of actions. These can be configured per channel per tag. For more information about actions and tags, please visit the documentation.


Conclusion

In this post, you have seen that it is very easy to use DutyCalls for monitoring devices. All we had to do for this was collect relevant data from a device (humidity this case), compose a ticket based on this data and post the ticket to DutyCalls using the Python Dutycalls SDK. In DutyCalls we could then see the collected data very clearly and we were notified when something was wrong.

However, we have only shown a small part of the functionalities and benefits of DutyCalls in this post and we are much more curious about how you implement DutyCalls in your projects/work!

Top comments (0)