IoT MQTT - Send Text and Images to Mobile Device

Last updated: 8 June 2022

Now we've got our IoT MQTT broker and publishing client set up, we'll subscribe to them and receive messages on a mobile device running an open source MQTT app. This is a typical use of MQTT protocol for IoT communication.

Stuff you'll need

  • Access to an MQTT broker (assumes you've read this post)
  • An MQTT client installed on your PC (assumes you've read this post)
  • An MQTT client installed on your mobile phone (see below)

Steps

Install an MQTT client for Android or IOS

In this example we'll install Routix MQTT dash on Android (which you can install securely and quickly from the Google Play Store). There are other apps for Android but MQTT Dash is by far the most intuitive and provides the most options for configuration. It really allows you to get an IoT project set up in literally minutes.

Configure connection to the broker

Once you've installed MQTT Dash, open the app and do the following (screenshots below):

  1. Hit the icon to create a broker connection (1).
    MQTT client - add broker
    MQTT client - add broker
  2. In the next screen fill in the connection parameters:
    Step Field Value
    2 Name Some "sensible" name - can be anything you like
    3 Address the exact same URL that appears in your broker configuration
    4 Port 8883
    5 SSL/TLS encryption enable
    6 Self-signed certificates enable
    7 User name The exact same user name you entered when you created your broker
    8 User password The exact same password you entered when you created your broker
    MQTT client set-up
    MQTT client set-up

IoT text communication over MQTT example

  1. Tap the broker connection that you just created.
  2. Tap the ⊕ icon and select Text from the pop-up dialogue (because the payload of this topic will just be text).
  3. Fill in the Topic (sub) field. Let's create a new one called my/test/topic2. You don't have to modify any other parameters on this screen.
    MQTT text topic set-up
    MQTT text topic set-up
  4. Open a terminal window on your PC and enter the following (the URL, user name and password must be the ones registered with your broker):
    mqtt pub -h {URL} -p 8883 -s -u {your user name} -pw {your password} -t 'my/test/topic2' -m "Hello!"

You should now see this pop up on your phone:

IoT text over MQTT
IoT text over MQTT

IoT image communication over MQTT example

To send an image we just repeat the previous process except that this time we'll tell the MQTT Dash app on our mobile phone to expect an image, and instead of publishing text from the terminal window we'll send an image from our hard drive.

  1. Open the MQTT Dash app and tap the broker connection that you already created.
  2. Tap the ⊕ icon and select Image from the pop-up dialogue (because this time the payload will be image data).
  3. Fill in the Topic (sub) field. We'll call it my/test/images.
  4. Open a terminal window on your PC and fire off the following command. Note that you must also include the path to an image prefixed by the -m:file flag:
    mqtt pub -h {URL} -p 8883 -s -u {your user name} -pw {your password} -t 'my/test/images' -m:file "path/to/image.png"

You should now see something like this pop up on your phone (your image will be different, of course):

IoT image over MQTT
IoT image over MQTT

What next?

Now it's time to experiment publishing with sensors (instead of manually publishing from the terminal) and maybe coding a customised client subscriber :)

© 2022