RAPIFIRE

Blog

Connecting Arduino YUN to RAPIFIRE

In this article we will show you how to connect Arduino YUN to RAPIFIRE with the usage of MQTT protocol.

Arduino YUN Configuration

We assume that the your Arduino is properly configured and it has an access to the internet. If it is not please follow this manual.

RAPIFIRE MQTT Library

The RAPIFIRE development team has prepared a library for Arduino that simplifies the integration with the RAPIFIRE platform. RapifireMqttClient is available in the Library Manager.

Arduino IDE - Library Manager

RapifireMqttClient was build on the top of PubSubClient so this dependency also should be installed in your Arduino IDE.

PubSubClient in the libraries manager.

RAPIFIRE Registration

Once your development environment is ready you can create an account in RAPIFIRE in order to get authentication details. The registration form is available here.

After signing in, go to the Things tab and click on the Thing that was automatically created for you. In the top right corner you will see API Access Keys.

API Access Keys

Run the Example

Now you can open an example provided with the library. It is available in File > Examples menu.

Open Example

Please replace the defines at the beginning of the file with the values displayed in API Access Keys section and upload the compiled sketch to the board.

Arduino IDE 1.6.7

When Arduino YUN establishes the connection with RAPIFIRE it starts publishing events to the platform. The events are visible on the console.

Console

Source Code

The latest version of this example is available on github.

Explanation

Here you can find a bunch of the implementation details.

Thing's Authentication

Arduion YUN connects with RAPIFIRE in order to publish events. The Arduino library takes care of the connection details so the only thing that you have to do is to provide your Thing details:

#define THING_ID "YOUR_THING_ID"
#define THING_KEY "YOUR_THING_KEY"
The Internet Connection

The library uses YunClient API to connect to the server so you need to initialize YUN's bridge in the setup function.

void setup() {  
  Serial.begin(9600);

  Serial.println(F("Starting bridge..."));
  Bridge.begin();

  Serial.println(F("Bridge ready."));
}
MQTT Connection

You have to initialize the connection before you start publishing events.

  if (!rapifire.connected()) {
    Serial.println(F("Connecting to RAPIFIRE.."));

    if (rapifire.connect()) {
      Serial.println(F("Connected."));
    }
  }
Message Building

SenML event may consists of one or more values so the library provides a method that allows to add each of them to the message:

rapifire.addValue("test", "", counter++);  
Message Publishing

Once the message is ready you can publish them to the platform.

rapifire.publish();  

Thank you for reading this blog.