Searching...

Matching results

    Using MQTT on Raspberry Pi

    This article is about using AirVantage API for devices. It will focus on a device: the famous Raspberry Pi.

    Prerequisite

    • Have a basic knownlege of MQTT. See the other tutorial about MQTT for airvantage: MQTT API.
    • Have a basic knownlege of c.
    • Have an Internet access with an HL6 (by ethernet or using the AirPi shield.

    Step 1: Setup Hardware

    Operating System & network access

    Starting this tutorial, I assume you have flushed a Rapsbian on your Raspberry Pi SD. To find it on the network, I used the Internet sharing capabilities of my Mac and nmap.

    $ sudo nmap -sn 192.168.2.0/24
    Starting Nmap 6.40 ( http://nmap.org ) at 2013-11-25 16:49 CET
    Nmap scan report for 192.168.2.6
    Host is up (0.00070s latency).
    MAC Address: B8:27:EB:76:EA:56 (Raspberry Pi Foundation)
    Nmap scan report for 192.168.2.1
    Host is up.
    Nmap done: 256 IP addresses (2 hosts up) scanned in 2.02 seconds
    

    This command shows us an unique Raspberry Pi available at 192.168.2.6.

    Connect to Raspberry Pi

    • Connect to the Raspberry Pi we just find is simple.
    $ ssh 192.168.2.6
    
    • Now, we are ready to perform some work.

    Step 2: Implementing an application

    It is now time to write an app for our Pi. You can have a look about using MQTT in AirVantage, if you want to have a description about the serialisation details.

    This C sample shows how to use MQTT to communicate with AirVantage.

    This source code is based on paho sample code.

    The app

    • Edit the entry_point.c and update the server adress and the password at the end of the file.
    MQTTConfig_t config = {
            .bufferSize   = sizeof( buffer),
            .buffer     = buffer,
            .callback   = serverCallback,
            .callbackCtx  = NULL,
            .deviceId   = deviceId,  // imei of device
            .serverAddress  = "eu.airvantage.net",
            .serverPort   = 1883,
            .password     = "myPassword"
        };
    
    • Compile the application by running the make command (in the same directory than the Makefile supplied in the sample):
    $ make
    

    View your application running

    • Go to the Target Management view by using the Window menu > Open Perspective

    Look, it is alive:

    [entry_point] Device id 353270040962229
    [entry_point] Network up, starting application...
    
    [entry_point] Device id 353270040962229 connecting
    [MQTT] connecting to client eu.m2mop.net:1883
    [MQTT] Connection established successfully
    ...
    [MQTT] Subscribing to 353270040962229/tasks/json
    ...
    [Entry_point] Reading measures :  temperature=25, humidity=44.000000, luminosity=74.000000
    [Entry_point] Reading measures :  temperature=25, humidity=36.000000, luminosity=79.000000
    [Entry_point] Reading measures :  temperature=25, humidity=45.000000, luminosity=75.000000
    ...
    

    Your data is now sent. You can check your data communication. Just go to Inventory > Systems, then pick your own and click Timeline. That is it.

    If you want to try it but feeling a bit lazy, it is all available at GitHub.

    Next Step

    Continue the tutorial by using tools supplied by AirVantage to test your communication with your device.

    TOP