Hardware

Build M2M applications with MQTT


Many of the most interesting IoT applications involve multiple local devices communicating with an internet-connected gateway. Temboo can generate code for complete multi-device application scenarios, in which edge devices use a common IoT communications protocol to send Temboo requests through a gateway. The gateway handles all communication with Temboo, enabling local edge devices to interact with the huge range of web-based resources supported by Temboo.

In this tutorial we'll discuss how to use Temboo to set up a multi-device application that uses MQTT, but we also offer support for CoAP and HTTP. Here, we're setting up a gateway and edge device that log sensor data to Google Sheets. Based on the requirements of your particular application, you can easily swap out Google Sheets for any of the 2000+ processes in our Library.



Let's Get Started

The first thing we'll do is get set up with Google Sheets so we can log sensor data from our devices. If you don't want to use Google Sheets for your application, but want to learn how to use Temboo to generate MQTT M2M code for a different application scenario, you can skip to the next section to get started setting up your gateway device.

1Make sure you have a Temboo account. If you don't already have one, you can register for a free account here.

2Since this sketch uses a Google spreadsheet, you'll also need a Google account.

3Login to Google's Developer Console, and create a new Project if you haven't done so already.

4Under the API Manager section, select Library and make sure you've enabled API Access for the Google Sheets API.

5Select Credentials in the API Manager section, and create a new Client ID specifying Web application for the Application Type.

6When configuring the Consent Screen, make sure you fill out the Email Address and Product Name fields.

7Save the Consent Screen details and specify the callback URL below as the Authorized Redirect URI. Make sure to replace ACCOUNT_NAME with your Temboo account name.

https://ACCOUNT_NAME.temboolive.com/callback/google

8 Go to the Google > OAuth > InitializeOAuth Choreo page, and fill in the Client ID from the app you registered at Google and the following Scope: https://www.googleapis.com/auth/spreadsheets. Then click Generate Code to run the Choreo from our site.

Google OAuth Inputs

The InitializeOAuth choreo will return an authorization URL and a callback ID (required for the FinalizeOAuth step).

9Open a new web browser, navigate to the authorization URL returned by the InitializeOAuth Choreo, and click Accept to grant the app access to your Google account.

Google OAuth Accept

10Go to the Google > OAuth > FinalizeOAuth Choreo page, and specify the callback ID returned earlier by the InitializeOAuth Choreo. Then click Generate Code to run the Choreo from our site. This process will return a Refresh Token which can be used along with the Client ID and Client Secret to authenticate with Google.

11Create a Google Spreadsheet. In this example our spreadsheet has two columns as seen below: time (in milliseconds) and sensor values.

Google Spreadsheet

A screenshot of a spreadsheet taking data from a Yún - note the column names

12When viewing your spreadsheet, you'll see a spreadsheet ID in your browser's URL bar. Copy this ID because you'll need it when running the AppendValues Choreo. In the example below, the highlighted part is the Spreadsheet ID.

Google Spreadsheet

Configure Your MQTT Gateway

Now that you're all set up with Google Sheets, you're ready to start building your MQTT application. We'll begin by setting up the Gateway device.

1Since our gateway device is going to be responsible for logging data to a Google Sheet, we'll start over on the Google > Sheets > AppendValues Choreo page.

2Use the drop down menu at the top of the page to select a device that is capable of acting as a Gateway. In this example we're using a Samsung ARTIK 10 with MQTT as our protocol.

alt=

Selecting the MQTT protocol from the Gateway dropdown

3Next, enter the details of your MQTT gateway device.

Enter and save MQTT gateway settings

Entering MQTT gateway device settings

4With your gateway device configured, populate the Google Sheets Choreo form with all of the details that we need to log data to our spreadsheet, using the credentials we generated earlier. You can find example inputs at the bottom of the Choreo page, and you can test it out by hitting the Generate Code button, which will trigger the data to log to your Google Sheet directly from our website.

Google Spreadsheets Inputs

Testing the AppendValues Choreo from our website

5Now, scroll down to the Code section of the Choreo page and you'll find the auto-generated code for your Samsung ARTIK gateway device. Now we're ready to move on and set up our edge devices.

Auto-generated MQTT gateway device code

Auto-generated MQTT gateway device code

Set Up Your Edge Devices

Now that we've configured our MQTT gateway and generated code so that it will log data from its sensors to a Google Sheet, we can set up an edge device to log its sensor data, too.

1First, add a new edge device by clicking on the +Edge tab.

Add a new MQTT edge device

Adding a new edge device

2Next, select your edge device type and how it is connected to the gateway. We're going to use a Texas Instruments LaunchPad connected via Wifi.

Select a MQTT edge device type and connectivity

Configuring the edge device type and connectivity

3Now, we're going to pick the Choreo that this edge device will run via the MQTT gateway. Since this edge device will be logging its sensor data as well, we'll use the Google > Sheets > AppendValues Choreo.

Select an edge device Choreo to call on the M2M gateway via MQTT

Selecting a Choreo for the edge device to call via the gateway

4You can use this same technique to add many edge devices to perform different physical and web-based operations relevant to your application.

5Scroll down to the Code section on each edge device you set up, and you'll find that the relevant code has been generated for your edge device. This means that we're ready to take our generated code for all devices, start the gateway, and run the application.

Run The MQTT Application

Now that you've generated code for your gateway and edge devices, you're ready to deploy the code and test out your M2M application. We'll start by running the MQTT gateway, then starting up the gateway and edge device Choreo programs.

1Make sure that you have the latest Temboo image or package for your gateway device - this will ensure that you have the necessary Temboo MQTT gateway libraries on your device. You can find Samsung's ARTIK images here, and for the Arduino Yún you can run: opkg upgrade temboo

2SSH into your gateway device and run the MQTT gateway. After navigating to the location of the Temboo MQTT gateway, you'll first want to start the Mosquitto broker on your device. The -d option makes the broker operate in the background, but does not suppress any of the messages. After starting the broker, you may see some debug notices, but you can still start the gateway by navigating to the location of the gateway script and then by running the script. You can see this in action in the gif below.

Samsung ARTIK 10 gateway location

cd /opt/iothub/artik/temboo/temboo_artik_library/m2m/mqtt

Arduino Yún gateway location

cd /usr/bin/temboo-m2m/mqtt

Start MQTT gateway

mosquitto -d
python TemooMQTTGateway.pyc -s yes -d

Setting up the Temboo M2M gateway with MQTT

Starting the Temboo MQTT gateway

Now's a good time to review the script options that we're using above. We've chosen to use HTTPS to send Choreo requests to Temboo from our gateway, and to turn debug mode on so that we can monitor activity more clearly. You can adjust these options as desired.

  • -s yes|no|ifavailable, --https=yes|no|ifavailable
  • -d, --debug

Note: The Arduino Yún does not include SSL for Python by default, so you'll need to install that yourself if required. Communication with edge devices is always handled via HTTP.

For instructions on how to get set up with other protools, check out our CoAP and HTTP tutorials.

3The last step is to compile and run your gateway and edge device Choreo programs. If you haven't run Temboo code on a device before and and would like to learn more about the basics, we have simple getting started instructions for all devices. Building the files for Arduino or TI devices follows the standard process via their respective IDEs. Building edge device code for MQTT on the Samsung ARTIK requires running the command below, where myEdgeDeviceProgram should be replaced with the name of the file containing your edge device code.

g++ -L/opt/iothub/artik/temboo/temboo_artik_library/lib -ltemboomqtt -I/opt/iothub/artik/temboo/temboo_artik_library/include/m2m myEdgeDeviceProgram.cpp -o myEdgeDeviceProgram

When the Choreo programs are running successfully, you'll be able to monitor the MQTT messages being sent to the gateway by watching the output on the terminal window that you used to start your gateway.

4That's it! You've built a multi-device M2M application that uses MQTT to communicate with with Temboo via an internet-connected gateway.

WHAT'S NEXT?

Now that you know how to generate code for multiple devices, why not check out the 2000+ Choreos in our Library and start thinking about all the possibilities for your next M2M application?

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back