Getting started with the MQTT client for C

Get up and running with the sample MQTT client for C on any platform on which you can compile the C source. Verify that you can run the sample MQTT client for C with either IBM® MessageSight or IBM WebSphere® MQ as the MQTT server.

Before you begin

About this task

Follow this task to compile and run the sample MQTT client for C on Windows from the command line or from Microsoft Visual Studio 2010. Microsoft Visual Studio 2010 is also used to compile the client in the command-line example. Modify the command-line scripts to compile and run the sample on other platforms.

Procedure

  1. Choose an MQTT server to which you can connect the client app.

    The server must support the MQTT version 3.1 protocol. All MQTT servers from IBM do this, including IBM WebSphere MQ and IBM MessageSight. See Getting started with MQTT servers.

  2. Install a C development environment on the platform on which you are building.
    The makefiles in the examples in this topic target the following tools:
    • iOSFor iOS, on Apple Mac with OS X 10.8.2 with the iOS development tools from Xcode.
    • LinuxFor Linux®, gcc version 4.4.6 from Red Hat® Enterprise Linux version 6.2.

      The minimum supported level of the glibc C library is 2.12, and of the Linux kernel is 2.6.32.

    • WindowsFor Microsoft Windows, Visual Studio version 10.0.
  3. Download the Mobile Messaging and M2M Client Pack and install the MQTT SDK.
    There is no installation program, you just expand the downloaded file.
    1. Download the Mobile Messaging and M2M Client Pack.
    2. Create a folder where you are going to install the SDK.

      You might want to name the folder MQTT. The path to this folder is referred to here as sdkroot.

    3. Expand the compressed Mobile Messaging and M2M Client Pack file contents into sdkroot. The expansion creates a directory tree that starts at sdkroot\SDK.
  4. Optional: Follow the steps in Building the MQTT client for C libraries.

    Do this step only if the Mobile Messaging and M2M Client Pack does not include the C client library for your target platform.

  5. Compile and run the MQTT client sample C app, MQTTV3Sample.c.

Compile and run the MQTT client sample C app from the command line

Compile and run the MQTT client sample C app from the command line. The sample is in the MQTT SDK. It demonstrates an MQTT publisher and subscriber.

Before you begin

Install a C development environment; for example the Microsoft Visual Studio 2010 as used in the example.

About this task

Compile and run the C sample, MQTTV3Sample, in the SDK clients subdirectory, sdkroot\SDK\clients\c\samples.

Procedure

Create a script in the client samples directory to compile and run Sample on your chosen platform.

The following script compiles and runs the sample on a Windows 32-bit platform, built with Microsoft Visual Studio 2010. Run the script from the sdkroot\SDK\clients\c\samples subdirectory.

@echo off
setlocal
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
cl  /nologo /D "WIN32" /I "..\include" "MQTTV3Sample.c" /link /nologo ..\windows_ia32\mqttv3c.lib
set path=%path%;..\windows_ia32;
start "MQTT Subscriber" MQTTV3Sample -a subscribe -b localhost -p 1883
@rem Sleep for 2 seconds
ping -n 2 127.0.0.1 > NUL 2>&1
MQTTV3Sample -b localhost -p 1883
pause
endlocal

Results

The publisher and subscriber write output to their command windows:
Figure 1. Output from the publisher
Screen capture of output from publisher sample program
Figure 2. Output from the subscriber
Screen capture of output from subscriber sample program

Compile and run the MQTT client sample C app from Microsoft Visual Studio

Compile and run the MQTT client sample C app from the Microsoft Visual Studio. The sample is in the Mobile Messaging and M2M Client Pack. It demonstrates an MQTT publisher and subscriber.

Before you begin

The example uses Microsoft Visual Studio 2010. You can use other C development environments on Windows and other platforms; for example Eclipse IDE for C/C++ Developers.

About this task

Compile and run the C sample, MQTTV3Sample with Microsoft Visual Studio 2010. MQTTV3Sample.c is in the SDK clients subdirectory, sdkroot\SDK\clients\c\samples.

Procedure

  1. Start Microsoft Visual Studio.
  2. Create a new project from existing code.
    1. Click File > New > Project from Existing Code.
    2. Select Visual C++ as the type of project to create.
    3. Click Next.
  3. Specify the parameters in the Project Location and Source Files window.
    1. Click Browse and locate the sdkroot\SDK\clients\c\samples directory.
    2. Name the project MQTTV3Sample.
    3. Click Next.
  4. Select Console application project in the Project type list. Click Finish
  5. Configure only the Debug configuration.

    By default Microsoft Visual Studio creates both a release and debug configuration. In the tutorial, you configure the debug configuration. To suppress build errors, clear the Build option for the release configuration.

    1. Click Project > MQTTV3Sample Properties > Configuration Manager. Select Release as the Active solution configuration and clear Build.
    2. Select Debug as the Active solution configuration > Close.

      Check that you are modifying the Debug configuration in all the following steps.

  6. Modify the C/C++ settings in the MQTTV3Sample Property Pages.
    1. In the MQTTV3Sample Property Pages window, open Configuration properties > C/C++ > General.
    2. In the list of general properties, click Additional Include Directories, and add your directory path to sdkroot\SDK\clients\c\include and click Apply.
  7. Modify the Linker settings
    1. Open Configuration properties > Linker > General.
    2. In the list of general properties, click Additional Library Directories, and add your directory path to sdkroot\SDK\clients\c\windows_ia32
    3. In the list of Linker properties, click Command line. Type mqttv3c.lib in the Additional options data entry area and click Apply.
  8. Remove the MQTTV3SSample.c source file from the project.
    1. Open the MQTTV3sample > Source Files folder in the Solution Explorer window.
    2. Right-click MQTTV3SSample.c > Exclude from Project
  9. Build the MQTTV3Sample project.
    1. Right-click the MQTTV3sample project in the Solution Explorer and click Build.

      The build completes without any errors.

  10. Add two new projects to run MQTTV3Sample as both a Subscriber and Publisher runtime instance.

    The Publisher and Subscriber projects are to contain the commands to run the MQTTV3Sample. They are not built, and contain no code.

    1. In the Solution Explorer, right-click Solution `MQTTV3Sample` > Add > New project.
    2. Type Subscriber into the Name field. Leave Win32 Console Application selected. Click OK.

      The Win32 Application Wizard starts.

    3. In the Win32 Application Wizard, click Next. Check Empty project > Finish
    4. Repeat these steps to add a Publisher project.
    5. Right-click the Subscriber project and click Set as StartUp project
    The Solution Explorer window is shown in Figure 3.
    Figure 3. MQTTV3Sample solution
    Screen capture of Microsoft Visual Studio 8 Solution Explorer
  11. Configure the Subscriber property pages.
    1. Right-click Subscriber in the Solution Explorer Properties > Configuration Properties > Properties > Debugging

      Check the window title is Subscriber Property Pages.

    2. Click Environment. Type path=%path%;sdkroot\SDK\clients\c\windows_ia32 and click Apply.

      Change sdkroot to suit your environment.

    3. Click Command, and replace $(TargetPath) by the path to the MQTTV3Sample module

      For example, sdkroot\SDK\clients\c\samples\debug\MQTTV3Sample

      Change sdkroot to suit your environment.

    4. Click Command Arguments and type -a subscribe -b localhost -p 1883 and click Apply.
  12. Configure the Publisher property pages.
    Tip: You can switch the property pages project by clicking the projects in the Solution Explorer window.
    1. Repeat the Subscriber steps for the Publisher.

      The command argument is -b localhost -p 1883

  13. Stop the build process that builds the Publisher and Subscriber projects.
    1. In the property pages of any of the projects, click Configuration Manager and clear Build for the Publisher and Subscriber in both the Release and Debug configurations. Click Close.
  14. Run the sample.
    1. Click F5 to start the Subscriber
    2. Right click Publisher in the Solution Explorer, Debug > Start a new instance

Results

The publisher and subscriber output to command windows. Visual Studio closes the publisher window. Look at the subscriber window, which is shown in the following figure, then close the subscriber window.
Figure 4. Output from the subscriber
Screen capture of output from subscriber sample program

What to do next

Build and run the asynchronous publisher and subscriber. The examples are MQTTV3ASample.c and MQTTV3ASSample.c in sdkroot\SDK\clients\c\samples.