Searching...

Matching results

    Enable BeagleBone Black connection to AirVantage using MQTT

    This article explains the different steps to enable AirVantage connection for BeagleBoneBlack using MQTT.

    Prerequisite

    • Have a basic knownlege of MQTT. See the other tutorial about MQTT for airvantage: MQTT API.
    • Have a basic knownlege of NodeJS.
    • Have a BeagleBone Black setup with an Ångström distribution with an Internet access.

    Step 1: Setup Hardware

    • Plug the BeagleBone Black to USB and install USB drivers by reading this getting started .

    • Set up Internet on your device with an ethernet cable.

    • Go to the Cloud9 IDE by openning the following url in a browser: http://192.168.7.2:3000/ . Where 192.168.7.2 is the default IP of the BeagleBone.

    • Create a new file in the workspace.

    Step 2: Implementing an application

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

    Open the created file to start coding an example using the following snippets.

    Initialize the MQTT channel:

    At first, we need to create a MQTT client instance with the right configuration.

    var mqtt = require('mqtt');
    
    var port = 1883;
    var server = "na.airvantage.net";
    
    var serialnumber = "3713BBBK7777"; //serialnumber
    var password = "1234";
    
    var mqtt_options = {username: serialnumber, password: password};
    
    var client = mqtt.createClient(port, server, mqtt_options);
    
    console.log("Client started");
    

    With same serialnumber and password used for system creation on AirVantage.

    Send data to Airvantage

    We will send three temperature values and one threshold value. The variables string identifier machine.temperature and machine.threshold are the same as definied in your system application model.

    //Send values
    console.log("Sending value");
    
    //Publish a message on the right topic
    var payload= {};
    var timestamp = new Date().getTime();
    
    payload[timestamp] = { "greenhouse.temperature": 42.2, "greenhouse.threshold": 30};
    payload[timestamp - 1*60*1000] = { "greenhouse.temperature": 24.5};
    payload[timestamp - 2*60*1000] = { "greenhouse.temperature": 42.9};
    
    client.publish(serialnumber + '/messages/json', JSON.stringify(payload));
    
    console.log(payload);
    

    Receive messages

    This snippet handles two different messages sent by AirVantage. The first message contains an updated value of the threshold setting. The second message contains a request from AirVantage to get current temperature.

    console.log("Waiting for task");
    
    client.on('connect', function() { // When connected
      // subscribe to a topic
      client.subscribe(serialnumber + '/tasks/json', function() {
        // when a message arrives, do something with it
        client.on('message', function (topic,message){
     
          var data = JSON.parse(message);
     
          //receiving a new threshold value
          if ("write" in data[0] && data[0].write[0] === "machine.threshold") {
              console.log("new threshold received: " + data[0].write[0]["machine.threshold"]);         
          }
           
          //receiving a request to send back the current temperature
          if ("read" in data[0] && data[0].read[0] === "machine.temperature") {
              console.log("AirVantage is asking for the last temperature");
               
              //publish on the right topic the current temperature
              var payload = {};
              payload[new Date().getTime()] = {"machine.temperature": 12 }; //quite cold indeed
              client.publish(serialnumber + '/messages/json', JSON.stringify(payload));
          }
        });
      });
    });
    

    Going forward, send an IO.

    The following snippet send an IO value every 5 seconds.

    //Send an IO
    var b = require('bonescript');
    
    //function to send a value
    function sendValue(x) {
        console.log("Sending P8_19 value: "+ x.value);
        if ( x.err !== null) {
            var payload = {};
            payload[new Date().getTime()] = { "machine.P8_19": x.value };
            client.publish(serialnumber + '/messages/json', JSON.stringify(payload));
        } else {
            console.log("Error while retriving pin P8_19 value: "+ x.err);
        }
    }
    
    //set pin as Input
    b.pinMode('P8_19', b.INPUT);
    
    //get a pin value and send it every 5s
    setInterval( function(){
        b.digitalRead('P8_19', sendValue);
    }, 5000);
    

    Find the full sample in github and clone it!

    Next Step

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

    TOP