Connect to an external MQTT broker

Updated: May 3, 2020



Time to cook: 60min

Difficulty: +++++




This recipe covers the configuration of the MQTT client on our Smart HUB, which allows us to connect our Smart HUB to other MQTT brokers. As an example we will connect to the AllThingsTalk MQTT broker. MQTT is the ideal glue to build a system-of-systems







 

Ingredients


  • A Raspberry Pi with OpenHabian installed

Prerequisites


  • An account on AllThingsTalk Maker

Tools


  • Nihil


The Building Plan



The Architecture


The picture below shows the architecture setup.





AllThingsTalk maker uses a concept of grounds. A ground allows people with common interest to build and share their IoT devices, assets and data either internally within a team, externally to a selected audience or publicly with everyone.


We will Setup the MQTT Integration between the open HAB HUB and AllThingsTalk Maker using a Ground token to authenticate. This means that we can read and write all MQTT topics for devices which reside in the Ground for which we specified the Ground Token in the configuration. If you would want to give access to all of your grounds in AllThingsTalk Maker to your HUB, you could opt to use your OAuth user account username and password to authenticate.



Configuring the MQTT client


As shown in the Architecture picture above, we need to configure an MQTT client on openHAB to connect to the MQTT message broker infrastructure of AllThingsTalk Maker.


This can be done by creating Manually a connection to a MQTT broker from within paperUI.


  • Goto Configuration

  • Select Things

  • Click on the + sign

  • Select MQTT Binding

  • Click ‘Add manually’

  • Select MQTT Broker



The Broker hostname for the ALSO AllthingsTalk Maker environment is api.allthingstalk.io. The MQTT port is standard 1883.


To connect toThe ALSO AllthingsTalk Message broker, you can either use Oauth authentication or use a token for the ground you want to use to integrate with openHAB.


example:

username="< enter you ground token here>", password="xxx"



You can find the Ground token in your ALSO AllThingsTalk Ground under settings. You don’t need to provide a password, as the authentication and authorisation is handled by the token, but MQTT clients don’t like a blank password, therefore enter a password of your choice but it will not be used by the MQTT Message Broker. For example use as a password the word ‘fake’.







If everything went well, you should see the MQTT Broker with the status ONLINE. The status will switch to ‘online’ if the MQTT client could establish a connection with the AllThingsTalk MQTT messaging Broker.




Alternative, you can configure the MQTT Broker connection by creating a definition file in /etc/openhab2/things.



  • login using an SSH session on your Raspberry Pi. You can use Putty on a Windows PC or the built-in terminal application on a MAC computer.

  • example: ssh openhabian@192.168.0.18

  • Provide the proper password you have setup for your openhabian installation. defaults it’s ‘openhabian’

  • go to the folder /etc/openhab2/things

  • cd /etc/openhab2/things

  • create a file called attalk.things

  • sudo nano attalk.things



The content of the file looks like this:




// ATTALK MQTT Broker


Bridge mqtt:broker:ATTALK "ATTALK MQTT Broker" @ "MQTT" [

host="api.allthingstalk.io",

secure=false,

port=1883,

qos=0,

retain=false,

//clientid="paho2951342446110",

//certificate="",

//certificatepin=false,

//publickey="",

//publickeypin=false,

keep_alive_time=60000,

reconnect_time=60000,

//lastwill_message="",

//lastwill_qos=1,

//lastwill_topic="",

username="<Enter Your Ground Token Here>",

password="xxx"

]



the explanation for each parameter can be found here.


  • Save the above file

  • Ctrl-x


This file will create our ATTALK MQTT Broker connection as a ‘Thing’ on openHAB


The result of this recipe is an MQTT integration between AllThingsTalk Maker and the openHAB HUB. In another recipe, we can use this integration to collect data from LPWAN devices which are rolled out on AllThingsTalk Maker and use this data on our Smart HUB.





}

}


5 views0 comments