RAPIFIRE

Blog

Connecting Raspberry PI to RAPIFIRE

The Internet of Things gets easier with every passing day and there are a lot of devices that may be connected together. In this article I'll show you how to connect Raspberry PI (or any device with Python onboard) to RAPIFIRE with the use of the MQTT protocol.

What is MQTT?

MQTT is a lightweight protocol that allows low overhead machine-to-machine communication and is a perfect match for IoT platforms. It works on top of TCP/IP. For those familiar with web technologies, you may think of it as a HTTP for devices. MQTT allows us to publish to certain channels, which means sending data to RAPIFIRE or other devices as well as subscribing to certain channels, which means getting data or commands back from RAPIFIRE or other devices. In our case all of the needed MQTT functionality is already implemented inside Python's paho-mqtt library.

Setting up

The first step is to create your RAPIFIRE account here. After signing in, go to the Things tab where you will find there a default thing you can use. Please edit it's name to suit your needs. There will be a box with a Thing ID, Thing Token and topics which your device can write to and read from. This is essential data that will allow us to connect and use RAPIFIRE in a secure manner.

Thing details page

To install the required Python library on Raspberry PI execute the following command from an RPI terminal:

pip install paho-mqtt  
RAPIFIRE Application

We need to import the proper library, then create a MQTT client object.

#!/usr/bin/env python
import paho.mqtt.client as mqtt  
client = mqtt.Client()  

The screenshot above shows where you can find the Thing ID as well as the Thing Token. Pass the Thing ID as a first parameter (username) and the Thing Token as a second one (password).

client.username_pw_set('_Ip1yEc2bZcZtXJNy_vFqB323LQ=', 'EQx5spqr7mIg')  

To connect to RAPIFIRE's MQTT broker you need to use the url mqtt.rapifire.com with the default MQTT port 1883. Then start the connection loop.

client.connect("mqtt.rapifire.com", 1883, 60)  
client.loop_start()  

Now our application needs to send some data to the RAPIFIRE. It may be location data, if your device is aboard some vehicle, some attached sensor data, like temperature and wind information, system information (like disk space left), or whatever you imagine. For now let's use a hardcoded snippet with temperature data. For RAPIFIRE to understand, it needs to be formatted as SenML. It's a simple JSON format as described in our What is SenML? blog post.

msg = '{"e":[{"n":"temp", "v":21.5, "u":"Cel"}]}'  

We decide to send temperature data every 5 seconds. That requires an import section fix.

import time  

Finally, here is a final loop. We publish our SenML message to the Data Publishing Topic for your thing as per screenshot above.

while True:  
    client.publish('/mRRRRRRRRmq/data', msg)
    time.sleep(5)

Let's put the whole source code together. To make sure we connect successfully, there is another tiny bit of code here that inform us about it.

#!/usr/bin/env python

import paho.mqtt.client as mqtt  
import time

client = mqtt.Client()

# Print to STDOUT if connection is established. CONNACK packet and it's result code is a part of MQTT protocol.
def on_connect(client, userdata, flags, rc):  
    print("CONNACK received, result code: " + str(rc))
client.on_connect = on_connect

# Pass the Thing ID as a username and the Thing Token as a password here.
client.username_pw_set('_Ip1yEc2bZcZtXJNy_vFqB323LQ=', 'EQx5spqr7mIg')  
client.connect("mqtt.rapifire.com", 1883)  
client.loop_start()

while True:  
    # SenML formatted message to publish.
    msg = '{"e":[{"n":"temp", "v":21.5, "u":"Cel"}]}'

    # Pass Data Publishing Topic as a topic and SenML formatted message.
    client.publish('/mRRRRRRRRmq/data', msg)

    time.sleep(5)

And that's it! When you open the Live Console you can see your Raspberry PI device send data to RAPIFIRE now.