Connecting Devices Together

Your devices can talk to each other! Using the “MQTT” protocol, you can send small pieces of data between your devices to create a network. Note that this mini-lesson is a bit more advanced, and you should be comfortable with other aspects of programming etc. before trying this one out; but once you get here, it’s SO. MUCH. FUN to set up your own communications network across Canada.

What is MQTT?

MQTT is an internet transfer protocol to send information between devices. It’s like Email or Slack, except for tiny pieces of data. It’s light-weight, easy to use, and can be *extremely* powerful as you get into more advanced MQTT applications. The basic premise is that you can PUBLISH a message to a Channel, and you can SUBSCRIBE to messages from a channel. In the middle is a “Broker”, that collects and sends information between all the publishers and subscribers. The same device can be both a publisher AND a subscriber, which means that your devices can all talk to each other.

What is MQTT and why do we need it in IIoT? Description of MQTT protocol

What broker can we use?

You can set up your own broker if you know how to use a Raspberry Pi (it’s *really* easy), but that will only work on your local network without extra modifications. You won’t be able to talk to devices on an external network (such as another Shad!). So instead, we use a “public broker”, and there are several that offer free packages that can handle our requirements. Chris suggests using EMQX.io , which provides a free and fast option. You can setup your m5sticks to communicate with EMQX with the following settings (Advanced > MQTT):

How do I learn this?

The best way is to dive in, look at code and try messing around. Here are some sample programs that Chris wrote. Working in pairs, download a different one to each of your devices, and then see what you can do with them! These programs keep track of how many A-button presses have occurred BETWEEN both sticks simultaneously.  Hitting B on either stick will reset the count to zero.  With a partner, load the code onto two sticks, and no matter how far apart they are, they’ll send a number to each other.  

mqtt04v1 mqtt04v2

Details – some notes about MQTT

  • We’re using EMQT, which is a free “public” MQTT broker.  This is not a secure broker, so you should *never* send personal information via MQTT to this broker!  You will see all the information in the “MQTT SETUP BLOCK” on the sample code that allows you to connect to the broker
  • When you “publish” to a “channel”, you are sending a message “payload” to that channel (just like messaging on slack).  Similarly, when you “Subscribe” to a channel, you are listening to things that are being posted to that channel.
  • The m5stick has a relatively limited functionality with MQTT.  When you get into the details, it only works with Quality of Service (QOS) of 0, not higher; and cannot handle “retained” messages.  Don’t worry if you don’t know what this means, you’ll work it out as you play with it. 

Some things Chris really struggled with

  • You MUST “download” the program to the m5sticks, not just “run” it for MQTT to work.  You also have to put in a line to get your program to connect to your local wifi network.  See the example picture above, find the green block under “Network” 
  • Every device that talks to each other MUST have a different ClientID.  Using the same ClientID will cause all kinds of mayhem.
  • The MQTT protocol works best by sending “strings”.  You have to convert those strings to integers (if you need integers).