How to use a MQTT library for Arduino to send streams to Altair SmartCore

0. Introduction

This easy tutorial familiarizes you with an Arduino MQTT library.
Using this library you can send a JSON to Altair SmartCore by simply calling a predetermined method
In this tutorial you will learn how to:

  • Structure the data you want to send to be compatible with the Altair SmartCore API
  • Send the data to Altair SmartCore using the publish method

Difficulty:

Ingredients

  • 1 x Arduino Uno
  • 1 x Ethernet Shield. You can also use a WiFi or GRPS Shield

Music to listen: Fraggle Rock Opening

1. Altair SmartCore Project and Device Registration

Device

If youre registered in Altair SmartCore, you have a default device already created for you.
Check your control panel and see what it looks like.
Basically you need the device id_developer that might be something like defaultDevice@myusername.
But if you want, you can create a new device and use it in this example.

Apikey

Now, go to your control panel “My account menu” and check your Apikey.
Its a big alphanumeric token like:
98346673a6377ef1fde2357ebdcb0da582b150b00cabcd5a0d83045425407ab4.
You need this number to complete the example.

TOP

2. How to use libraries in Arduino

Arduino libraries allow the sketches to have extra funcionality. Arduino has many libraries that are already established and come with the IDE. However, it's also possible to write your own libraries or use libraries written by others. Each library has a header file which describes each method and a source file, which elaborates the code. To read more about Arduino libraries or writing a library see Arduino-Libraries.

In this tutorial we will use a MQTT library to send streams to Altair SmartCore.

To use the library, first download it here. Once you have downloaded the library you must decompress it and then copy it into the arduino/libraries file. Now you should be able to use the library in any sketch you create.

TOP

3. Code implemented on Arduino to test the library

The method used to connect to Altair SmartCore MQTT is the method "connect". It has 3 parameters that you need to define in order to send a stream correctly.
The first parameter is your USAERNAME.
The second parameter is the APIKEY.
The thid parameter is the password, NONE for Altair SmartCore MQTT. (It's mandatory, can not be left empty)

The method used to publish streams to Altair SmartCore MQTT is the method "publish". It has 2 parameters that you need to define in order to send a stream correctly.
The first parameter is the toppic, it consists of your apikey/MODE (Mode can be "streams" or "status")
The second parameter is the stream itself.
This is the Arduino code to test the publish method. Note that some values should be replaced by your own values.

In this tutorial we check for the connection status each "loop" and start a connection loop if conection went lost.
Once connection has been stablished we send the stream.

TOP

4. Checking Data is collected in Altair SmartCore

If you get here, your device must be sending streams to Altair SmartCore

Its time to test our control panel and check if we have new streams.
Go to “Data management” → “Data streams” and you will see your data.

  • menu option
  • new data

TOP

5. From here on: Enhancing the project

Now you know how to use the an Arduino device to send data to Altair SmartCore with MQTT. But you can enhance this project in many ways:

  • Try to send dynamic data using the MQTT library

TOP