Using DutyCalls for IoT applications
Tags: iot, monitoring, saas, raspberrypi
Published: 2022-04-26 10:26:52

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](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](https://docs.dutycalls.me/getting-started/).

---

## 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](https://siridb.com/blog/using-siridb-for-iot-applications/). 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: 

```python
# This is the current relative humidity, expressed in
# percentage and rounded to 2 decimal places.
humidity = 44.35
```

---

## 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:


```python
ticket = {
    “title”: “Humidity is fine.”,
    “body”: f“Current humidity is: {humidity} percent.”,
    “severity”: “low”
}
```

> **Note**: The above dictionary is based on a default service. You might have to change it according to your own [service mapping](https://docs.dutycalls.me/getting-started/add-service/#configuration).

Now to post this ‘ticket’ to DutyCalls we use the [dutycalls-sdk](https://pypi.org/project/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](https://docs.dutycalls.me/rest-api/authentication/).

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

```python
from dutycalls import Client

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

> **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:

```python
# Create a new Ticket in DutyCalls.
await client.new_ticket(ticket=ticket, ‘Example-Channel’)
```

> **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr428xrc8tbu3ro5lbvp.png)

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:

```python
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”: f“Current humidity is: {humidity} percent.”,
    “severity”: severity
}

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

await client.new_ticket(ticket=ticket, ‘Example-Channel’)
```

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hqs3utk7hoz0qdqzevc2.png)

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](https://docs.dutycalls.me/integrations/), 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](https://docs.dutycalls.me/alerts/).

### 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](https://docs.dutycalls.me/ignore-rules/).

### 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](https://docs.dutycalls.me/expectations/).

### 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](https://docs.dutycalls.me/actions/).

---

## 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](https://github.com/transceptor-technology/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!