Archive for the 'MQTT' Category

A QuickStart guide to PubSub with IBM Internet of Things Foundation (sample C client)

At work, I’ve been working with IBM’s recently launched Internet of Things Foundation (IoTF), both on MessageSight (the underlying MQTT server) and other odds and ends. IoTF allows you to quickly start doing simple pub/sub via the MQTT protocol without even having to register yourself or the apps/devices doing the publishing or the subscribing, you configure apps to connect and messages can immediately start to flow for free.

As MQTT is used as the protocol you can use pretty much any programming language (including but definitely not limited to: Javascript, python, Go, C or Java). As an old-skool C hacker I often default to C and I forget the format for the id the publishers and subscribers need to use. To get started with a sample C app you:

  1. Download the Paho MQTT C client
  2. Extract the libraries (or “make” if you used the source, Luke)
  3. Build the samples (Unless you built the libraries and got it to build the samples at the same time (see README.md))
    1. gcc stdinpub.c -o stdinpub -I../include -L ../lib -lpaho-mqtt3c -Wl,-rpath,../lib
    2. gcc stdoutsub.c -o stdoutsub -I../include -L ../lib -lpaho-mqtt3c -Wl,-rpath,../lib
  4. Setup a subscriber:

    ./stdoutsub 'iot-2/type/jonexampletype/id/device1/evt/+/fmt/+' --host quickstart.messaging.internetofthings.ibmcloud.com --clientid 'a:quickstart:jonexamplesubber' --verbose
  5. In another terminal start a publishing app:

    ./stdinpub iot-2/evt/status/fmt/json --host quickstart.messaging.internetofthings.ibmcloud.com --clientid 'd:quickstart:jonexampletype:device1' --verbose
  6. Send some messages by typing them into your publisher (as we are publishing on a topic that claims to be JSON a simple example message might be:
    {"d":{"somenumber":42}}

In this example, the samples are linked to the classic, simpler, synchronous C library, in the same Paho bundle you get other (more powerful) variants, see the README.me for more details

A key to the colours used above:

  • Device Type: The type of device publishing, you can make it up and use it to group your devices
  • Device Id: The id of the device publishing, you can make it up. You need the combination of devicetype:deviceid to be unique across all devices connecting to quickstart… when a “duplicate” device connects, any existing devices with the same type:id will be disconnected
  • Event Type: The category of messages being published, again you can make it up. In this case, the subscriber used ‘+’ which means any event type.
  • Message Format: The format of the messaged published. You can make it up but JSON is special, in quickstart you can visualise data from devices published in JSON format and for registered devices, JSON messages can be recorded by the IoTF for future use
  • Application Identifier: Identifies the application, needs to be unique as duplicates are disconnected in the same way as mentioned in the Device Id: above.
  • Organisation:See below

In this example we were using the quickstart organisation which allows you to get going quickly. You know you that won’t be charged (or contacted by a sales person) as you haven’t given any contact details. After the initial thrill of getting going quickly, there are downsides – the device/application client identifiers have to unique across everyone using quickstart and there is no security for your data. Once you’ve taken your first steps you probably want to register – depending on your needs it can be as cheap as free, but it gives you e.g. access controls, security and the ability to log published messages in the cloud.

I plan to eventually do equivalents of this posts for other languages (and possibly with a registered account), let me know in the comments if there’s something you want covered.