Introduction to Arduino MKRFox1200 – Part 2

Following the previous post where we saw how to transmit data on Sigfox with an Arduino MKRFox1200, in the second part we will learn how to consume these data in a backend site. To make this simple we are going to implement a solution using Adafruit.io service. This service allows to create graphics from data received from an API. We will configure Sigfox backend to push device data to this API.

Initialize Adafruit.Io configuration

The first step is to create an account on adafruit.io. The platform is free up to 30 messages per minute. On Sigfox it means about 300 devices communicating so it is basically large for our need.

The Adafruit platform is based on two different concepts:

  • Feeds : a feed is collecting data.
  • Dashboard : is presenting data collected from the feed.

So the first step is to create a feed, for this, you go on feeds >> Actions >> Create a New Feed and enter a name for the feed. Let say “Temp_xxxx”

So now we can click on the feed created, we need to keep the feed name in mind to use it later for the API call, the URL helps us : https://io.adafruit.com/xxxxx/feeds/temp-disk91

Now in the feed panel we can create a data manually but it is not what we want to do. The objective is to automate this with API calls.

Send the data over the Adafruit API

Adafruit API requires a API key, to obtain it you can click on View AIO Key link on the left menu. You will get the user name and associated active key

This key can be use as a header X-AIO-Key containing the key or as a url parameter. The API Documentation is indicating how to send data to your feed. To test this we can use the following curl command:

curl -X POST -H 'Content-Type: application/json' \ 
-H "X-AIO-Key: e7cb2388af104a48bec3XXXXXX" \
-d '{"datum":{"value": "10"}}' \
https://io.adafruit.com/api/v2/iuXX/feeds/temp-disk91/data

In the feed you will see the new point in the list and on the graph.

Now we need to connect our Sigfox device to this feed.

Configure Sigfox Backend to send data to Adafruit.Io platform

To automate the transmission, we are going to use a Sigfox callback. A callback is an operation the Sigfox backend executes automatically every time a message is sent by the device.

For this we need to connect to https://backend.sigfox.com then we need to click on Device Type menu and select the device’s device type. Now on the left menu you have a Callback entry. You need to click on it.

Once in this screen (initially empty), you can click on the button on the top right of the screen New

You will see a list of predefined callback and the possibility of creating custom callback.

 

 

Basically most of the callback are made to automatically feed an IoT Cloud platform, AWS, Azure, IBM. This is not what we are going to do : we will create our own callback to feed the Adafruit platform. So we choose Custom callback

Once in the callback creation screen, we have different fields to set:

  • Callback type : there are different type of callback, some for service information or error reporting. The one we want is DATA. This one is triggered on every data transmission from the device.
  • Then we can select if we only want uplink or bidirectionnal. As Adafruit will never respond with a downlink, the UPLINK choice is the best.
  • Then we select URL ; it is also possible to send an email instead of calling an URL.
  • The custom payload should automatically filled if you followed the part 1 of this tutorial. Basically we want : temperature:0:int:8 corresponding of the decoding of our data and creating a field {temperature} we can use in the body.
  • Then we will select the http method as POST
  • The URL pattern is https://io.adafruit.com/api/v2/username/feeds/feed-name/data (this needs to be adapted to your context)
  • You need to set a Header with the API Key
    • key : X-AIO-Key
    • value : e7cb2388af104a48bec3XXXXXX
  • You need to set the content-type as application/json
  • For ending you need to set the body content
      { "datum":
       { 
         "value": "{customData#temperature}",
         "epoch": {time}
       }
      }
    • {customData#temperature} will be replaced by the value decoded in the custom payload part.
    • {time} will be automatically set with the current unix time

So the Callback configuration should look like this:

Now the object can send data and you should get see that on the backend.

So for this we can click on the DEVICE menu on the top and select the device corresponding to the devkit. Then go to MESSAGES on the left menu to display the messages received from the device.

The new message ( on the top ) have now the callback arrow green. it means all the callback have been executed successfully. You can click on the arrow to get the details on the url called and message content as you can see the response code from the platform.

The temperature has been pushed to Adafruit.Io platform. You can check the feed on adafruit.io and check the result:

The new value have been received.

Now, let’s play with the Sigfox API

In this part we are doing to do the opposite of the previous part: instead of pushing device data out of Sigfox backend, we are going to pull them out. For this we are going to use the Sigfox API.

The first step is to obtain an API key to access the API. To get this you need, in the backend, to go to the GROUP menu, select the group the device is belonging to and go to API ACCESS on the left menu. Then you click on New on the left.

You can give the name you want and select DEVICES_MESSAGES[R] right as a start to access the messages content.

 

You are getting an api access with a login and a password like on this example. (Do not try these one, if in clear it is because they have been revoked)

Now we are ready to access the API.

The first step is to get access to the documentation.

The API documentation is accessible fo this link:

https://backend.sigfox.com/apidocs/user/5b2622359058c21a839d5f2b

Where the id at the end of the url is the login you obtained before. To start let’s take a look to the authentication:

The API is using a Basic authentication with the login and password, so to make a GET or POST, you need to specify them:

curl -u login_value:password_value

Now you can take a look to the Mesages sent by a device part of the documentation. The URL to call is:

https://backend.sigfox.com/api/devices/{device-id}/messages

where the {device-id} is the device Id you want to access.

So the command to be used is the following with the values obtained earlier:

curl -u 5b2622359058c21a839d5f2b:2a934726af363e7c5e44777b03ded751 \
      https://backend.sigfox.com/api/devices/18AC60/messages

This is returning a Json structure containing the last messages received from the device.

You can see different information like the device information, data transmitted and one entry for each of the station having received the message.

Next steps

Now, you have seen all the basis elements for getting started with Sigfox using a MKR1200 Arduino platform. Do not forget we have just make a prototype and the steps for creating a real products are really longer. At first you should take a look to my post on creating an IoT platform from the Poc to the Prod.

This entry was posted in IoT, Sigfox and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.