NB-IoT: sending sensor and GPS data to AllThingsTalk (updated)

Finally, we got our hands on the Sodaq NB-IoT shield with T-Mobile SIM!

nb-iot-shield-deluxe

During a workshop at Sodaq premises, we learned how to use the shield and received some examples to get started. For these examples look at the Sodaq support site here: http://support.sodaq.com/sodaq-one/nb-iot_tutorials/

What was still missing in these very nice examples is a way to send all sensor and GPS data to AllThingsTalk (where the packaged SIMs send their data to) in such a way that it can be displayed and tracked in the dashboard (or pinboard as they call it). After some experimenting and walking through the documentation I found out how to get all things together into a proper arduino sketch and payload conversion functions. In this blog I will describe how to do this in detail.

How to get started?

First, you need to follow the tutorial (see link above) to make your shield work with the T-Mobile SIM and activate it for AllThingsTalk. The data will come through from your device through the T-Mobile network and delivered within the device playground in AllThingsTalk.

Do not get frustrated when data does not seem to appear directly, check the data you send and if the payload function uses the correct defined asset and decoding function.

Tips:

  • Asset name is case sensitive! So check asset against the payload conversion function asset name.
  • Check the length of the message you send, if it is shorter than the payload conversion function expects it will just wait and you seem to receive nothing.
  • When you do not see activity being logged, switch on the Data retention under Settings.

Send Temperature and Humidity

The used examples just use text to send out the values gathered from the sensor to AllThingsTalk (ATT). We need to send the proper numbers and adapt the conversion function so ATT sees it as proper values to display in a pinboard and have an activity log.

For this, I used the HTS221_HumidityTemperature_nbiot of Jan van Loenen as a basis and adapted it.

Instead of using a String, a byte array message is defined in which the temperature and humidity values are put:

byte message[30] ;
uint16_t cursor = 0; //cursor to add values to the array:
int16_t help; // help int for value conversion:

Next, we read the temperature and humidity from the sensor and put the values into the message array and send it through:

help = hts221.readTemperature()*100;
message[cursor++] = help >> 8;
message[cursor++] = help;

help = hts221.readHumidity();
message[cursor++] = help;

// Send the message
nbiot.sendMessage(message);

On the ATT side, we need to create 2 assets for the temperature and humidity with the Profile type number.

Then a payload conversion will convert the received values to numbers within the proper asset:

{
"sense": [
{
"asset": "temperature",
"value": {
"byte": 0,
"bytelength": 2,
"type": "integer",
"calculation": "val / 100"
}
},
{
"asset": "humidity",
"value": {
"byte": 2,
"bytelength": 1,
"type": "integer"
}
}
]
}

When you then assemble everything together; upload the sketch to the Arduino, setup the assets and payload conversion into ATT.

You should then be able to get values from your shield through the NB-IoT network!:

temp_hum_att

Now adding the value from the pressure sensor is straightforward. Take the example of the LPS22HB_Pressure_nbiot_demo and copy over the stuff that is needed to initialize, setup and read the sensor.

Send pressure

To add the pressure to the values send through to ATT use this code:

      help = lps22hb.readPressure();
      DEBUG_STREAM.println(help);
      message[cursor++] = help >> 8;
      message[cursor++] = help;

On the ATT side, we need to create the asset for the pressure with the Profile type number.

Then add this payload conversion to convert the received value to numbers within the proper asset:

{
"sense": [
{
"asset": "pressure",
"value": {
"byte": 3,
"bytelength": 2,
"type": "integer"
}

Upload your new sketch and the pressure should now also be visible in ATT:

pressure_att

Sending GPS position to ATT

Now we can send sensor data to ATT and display it sounds easy right? Well, it is, but first, you need to know what data ATT accepts to be proper latitude and longitude… This I could not find in the documentation, but I figured it out by looking at some of the examples.

To start with using the ublox gps; have a look at the test_ublox_gps under nbIOT_gps. This code we need to integrate within the sketch we just created above.

The part to include the GPS latitude and longitude looks like this:

        help = sodaq_gps.getLat()*100;
        message[cursor++] = help >> 8;
        message[cursor++] = help;
        help = sodaq_gps.getLon()*100;
        message[cursor++] = help >> 8;
        message[cursor++] = help;

As you probably see it has only 2 digits behind the comma, need to amend so the position is more accurate. [TODO]

Now to be able to use a map within your pinboard we need to put the values within an asset location object. First, create the asset “location” with object as profile type. Then change the payload conversion and add this:

    {
      "asset": "location",
      "value": {
        "lat": {
          "byte": 5,
          "bytelength": 2,
          "type": "integer",
          "calculation": "val / 100"
        },
        "long": {
          "byte": 7,
          "bytelength": 2,
          "type": "integer",
          "calculation": "val / 100"
        }
      }
    }

When you now upload your new sketch you should see the following asset and its data coming in:

gps_att

Pinboard with sensor data and GPS map

Now that all data is coming in we can have a look at adding a pinboard with asset pins.

This is what mine looks like:

pinboard_att

The temperature, humidity and pressure pins are straightforward. The location has a twist. To be able to pin a map with long/lat you need to edit the asset and create an advanced profile, like this one:

{
“type”: “object”,
“properties”: {
“lat”: { “type”: “number” },
“long”: { “type”: “number” }
}
}

Then you will be able to add a pin with Map control.

And that’s it!

Sodaq has placed new versions of the examples on their github. A few things have to be taken into account now:

The NB-IoT library is now implemented within the library manager, you need to remove the libraries from the examples’ folder and download the latest version from the library manager. Then change #include “Sodaq_nbIOT.h” to #include <Sodaq_nbIOT.h>

Another important change is to activate the NB-IoT modules, in the new version the configuration parameters are set to false.
We did this manually within the workshop with AT commands.

Advertenties

3 reacties op ‘NB-IoT: sending sensor and GPS data to AllThingsTalk (updated)

  1. AllThingsTalk pointed me to their documentation to create an asset that will provide for the proper map under pin: http://docs.allthingstalk.com/cloud/concepts/pinboards/controls/#map

    So to be able to pin a map with long/lat you need to edit the asset and create an advanced profile, like this one:
    {
    “type”: “object”,
    “properties”: {
    “lat”: { “type”: “number” },
    “long”: { “type”: “number” }
    }
    }
    Then you will be able to add a pin with Map control

    Like

  2. Sodaq has placed new versions of the examples on their github. A few things have to be taken into account now:

    The NB-IoT library is now implemented within the library manager, you need to remove the libraries from the examples’ folder and download the latest version from the library manager. Then change #include “Sodaq_nbIOT.h” to #include <Sodaq_nbIOT.h>

    Another important change is to activate the NB-IoT modules, in the new version the configuration parameters are set to false.
    We did this manually within the workshop with AT commands.

    Like

  3. Hoi Hans, ook ik ben op de NB-IOT cursus geweest in Hilversum maar ik had toch even dit blog nodig om de stip op de kaart te krijgen.
    Top en bedankt.

    Like

Geef een reactie

Vul je gegevens in of klik op een icoon om in te loggen.

WordPress.com logo

Je reageert onder je WordPress.com account. Log uit /  Bijwerken )

Google photo

Je reageert onder je Google account. Log uit /  Bijwerken )

Twitter-afbeelding

Je reageert onder je Twitter account. Log uit /  Bijwerken )

Facebook foto

Je reageert onder je Facebook account. Log uit /  Bijwerken )

Verbinden met %s