Please select an option to see content specific to your location and shop online.

  • We are shipping but experiencing some delays due to COVID-19 situation. Thank you for your continued support. We are shipping but experiencing some delays due to COVID-19 situation. Thank you for your continued support.
  • Free delivery on orders over £60 Free delivery on orders over £60

Project

Playing in the MQTT Sandbox with BeagleBone® Black Wireless

Scroll down

By now you’ve powered through our tutorials on getting up and running with Beaglebone Black Wireless. You’ve flashed things (both OS’s and LEDS!), monitored temperatures, and even created your own webserver and client pages! But now you want to dig a little deeper and get into some real autonomous fun.

You’ve more than likely heard about IOT by now, this ubiquitous sea of devices pinging and ponging data all around to help sync up and automate all the data we generate, to make it work for us. For our example, we’ll keep it simple and leave it at – a giant cloud of devices that are sending and sharing data. Well if you want to learn how to swim sometimes it’s best to just jump in the pool get wet. That’s what we’ll do. We are going to take the Beaglebone Black Wireless, connect it to the internet, then use MQTT to send messages to a host in the cloud. We will then take another BBBW and use it to listen for these messages, and display the results using the User LEDS. You may be wondering what fantastic data we will be sending – something truly wonderful and mystic – and in this case, well something much more useful. We will create a random Rock, Paper, Scissors, Spock opponent. By uploading a time, and a RPSS value we can create a client that can listen for changes in value, and display the results.

While you are likely familiar with IOT, you may not have heard of MQTT. Taken from MQTT.org:

MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.

It is a simple, easy to use way to upload specific data, to a storage pool, that can be monitored by any number of clients. You setup specific devices, to publish very specific data to a location of your own naming. This location is on a broker. Think of a broker like a card catalog, where you can create a new drawer for each project, and then individual cards for each variable you are reporting. If you want you can subdivide further, but we’ll try and keep it simple for now. Which means we will also use the simplest broker, a public sandbox. There are a few public sandboxes provided for your use to test MQTT devices and applications. One very popular one is iot.eclipse.org, which is what we will be using. This makes it very simple to get up and running with MQTT and see just how powerful it can be. In a future tutorial we will walk through setting up the BBBW as a broker itself, basically setting it up as a self-sufficient private cloud, keeping your data secure from prying eyes, and showing some of the higher end potential of the hardware. For now, using the public sandbox our data will be accessible by anyone with an internet connection – so keep that in mind in your naming of your storage locations and the data you send. Once we put the data up there, we need a client, or a device which will monitor the storage location and digest the data, giving us some sort of result or reaction. We have created what we think is one of the simplest ways to demonstrate this protocol in action so lets get started.

Step 1

First – make sure your BBBW is running the latest OS and is up and running and available to be connected to from the computer you are using. We connected to our BBBW at 192.168.7.2 to make sure it was up and running, and could navigate to Cloud9 and see all our old examples! So we know we are up and running, but now we have a little prep work to do.

We need to get the beaglebone black wireless on the Internet, and to do this we will connect the wifi connection to a local hotspot. We will accomplish this through an SSH connection. On the Mac we used Terminal, but you can also use a program like Putty on Windows, etc.

We will use the command: ssh debian@192.168.7.2 Debian is the default user name so we are saying connect to our BBBW (which has an ip of 192.168.7.2 – please substitute your IP if different) as user Debian.

You may see something like the following the first time you connect:

The authenticity of host ‘192.168.7.2 (192.168.7.2)’ can’t be established. ECDSA key fingerprint is SHA256:mzZQlmr0S3jvYQzh/wx8sJyfXOQp9G5+d5QH+YO5R8k. Are you sure you want to continue connecting (yes/no)?

This is because the first time you connect is needs to verify your certificate, and once you say yes, it won’t bother you again! (With some minor caveats about other devices using the same address later – but we promised we’d keep this easy!)

It will then ask us for a password. The default password is: temppwd Now we need to get SuperUser access to be able to setup the wifi. We’ll do this using the standard command: sudo su This will give us permanent root access until we choose to end it. It will ask us again for the Debian password. (Reminder – default is: temppwd) (Also reminder – if at first you fatfinger it, try try again!)

 

Step 2

We are now connected to the board and can get down to the business of setting up the wifi.

We will use the standard connection manager that is provided to us as part of the image: connmanctl

To do this we enter the following at the root prompt: connmanctl We will then see we are at a prompt that says “connmanctl>”. This means we are now inside the manager. We will use the command following commands in the following order to set it up:

enable wifi (which will give us the response Enabled wifi)

scan wifi (which will give us the response Scan completed for wifi)

services (which will list all the wifi hotspots found. We need to take note of the wifi_* value beside the name of the hotspot we want to connect to – this is what we will use to connect) agent on (which will give us an Agent registered)

connect wifi_* (use the full name from above…. Yes its LONG, and a pain, but this works. It will then ask you for the passphrase, which you will enter then it will tell you when its connected)

quit

Step 3

At this point I like to test that I’m connected. I like to use the command : ping 8.8.8.8 This runs a ping against the google servers, which if we can see them means we are on the internet and all is working. At this point we can close down the terminal session and go back the BBBW webpage (ours is at 192.168.7.2).

We will scroll down until we get to the Node-Red section. Previously we used Cloud9 and Bonescript, but since we are branching out and learning new things we are going to check out Node-Red today. Node-Red is another amazingly simple way to program the BBBW. It uses a drag and drop style GUI to let you easily script your workflows, and enable us to get data to our sandbox.

Step 4

When we click on the Node-Red title it will open up the Node-Red Editor.

From there we need another easy setup step. Since we need to randomly generate a value between 1 (Rock) and 4 (Spock) we will need a random number generator. There is an easy node type to do this but it isn’t installed yet. So we will install it by using the Palette Manager. Click on the Menu (three bars) in the upper right hand corner of the page and the menu will drop down. We will click on “Manage Palette”. Once inside click on the Install tab. Then enter the search term “random”. Click on the tiny install button (bottom right) of the node-red-node-random option. It will then take a few minutes to install this new node. Once it’s done you can click the red “Close” button at the top.

 

Step 5

We will create a new tab for each workflow. By clicking on the tab at the top you can edit the tab name to help keep it straight! You can also enable or disable the tab (in case you want to turn off certain functionality while testing other workflows). (NOTE: Make sure the tabs you want running are enabled! You can spend a lot of time debugging things only to find out the tab is actually disabled). We named our first workflow “SEND MQTT”. Click “Done” when finished.

 

We are going to send two values, a time value and a random number value, so to get everything we need for the workflow we will drag out one “Inject” input node, one of our new “Random” function nodes, and two “MQTT” output nodes. Each of these is grouped under their own heading and you can see how they need to be wired up in the following photo. We wire them up in the same way we would hook up a flowchart. We drag from the output port on one node to the import node on the next. The Time Inject node will go to both the MQTT output for time, and the random number generator to kick it off. The Random Number generator will go to the MQTT output for the RPSS value.

 

Step 6

We will setup each node by double clicking on it.

First double click on the Inject node and enter the data as follows. NOTE: Please set your OWN UNIQUE topic. We will be using different topics for different data points, but remember this is like a card catalog. Since we are doing this example as an OKDO Demo, we are naming our “Drawer” OKDODemo, then we are naming our project (or drawer section) “MQTTRPSS”, then the individual variables (the “topics”) we will name “rpss” and “time” respectively. You will see this in the entry screen. If you use the EXACT SAME NAMES then it will overwrite any data there, and will be overwritten by anyone else using the same name. So if say, 6 people are running this example they will be constantly overwriting each other, which for our example isn’t a catastrophe, but in most situations would be a mess. This is another drawback to using a public sandbox, but the ease of use can’t be beat.

Then double click on the random node and enter as follows.

Step 7

As soon as we enter we will want to go and setup our broker, by clicking on the edit button (pen) to the right of the add new mqtt-broker selection in the server selection box. Fill in the server m2m.eclipse.org as shown and you will be set! We will use this same server for all of our MQTT nodes. We named ours MQTT Sandbox to keep it straightforward.

Step 8

Fill in the rest of the data in the MQTT time node as shown (REMEMBER: Use your own Topic).

Then double click and fill in the MQTT node for the Rock Paper Scissors Spock value as shown.

That was it! That wasn’t so bad was it??

Step 9

Now click the red “Deploy” button. When it’s finished deploying it is now running, and we can monitor the data. To do so, and to show it’s working, you can use any of a number of MQTT Viewers or Clients, we chose MQTT Explorer, which is free for Mac. Once you connect to the same sandbox, you can search for your topics, and here we see our OKDODemo group, and our individual values.

So we are now sending messages but what about receiving.

Step 10

Now we can fire up our other BBBW (or if you only have one, you can run both workflows on the same one, just to see how the demo would work!) and connect to it! Fire up Node-Red and create a new Tab for “Receive MQTT”.

Here we will drop an MQTT Input, four function nodes (one for each option) and four gpio nodes. Connect them up as shown. The function nodes will allow us to script the input variable to set each LED Value appropriately.

 

Step 11

One thing we haven’t discussed to much is the idea of the message (msg) being passed between these nodes. The message has a payload, which is the value we care about, as well as some other data associated, such as a name, topic, etc. For now what we care about is that these nodes pass a message to each other and that msg.payload is how we look at the value we are looking for.

Also for this example we use a separate function to feed each separate GPIO node, using one node for each of the LEDs we will control. By doing it this day we greatly simplify the function code, and make it much easier, although a bit more busy on the screen.

Double click on the MQTT input node and fill it in as follows:

Double click on the first function node. We will use this one to set the Rock value. Fill it in as follows

Double click on the first GPIO node, we will use this to turn the USR0 LED on and off. To do this first we must setup the board. In the “Board” line next to add new nodebot click the edit button (pen). The following screen will pop up and often will default to an Arduino. We will set it up as follows, choosing BeagleBone Black, and naming it whatever you want. Much like our sandbox setup, this same board will be used by ALL the GPIO nodes.

 

After the board has been setup fill the rest in as follows.

Here is the setup for the rest of the nodes:

Set PAPER

GPIO PAPER

Set SCISSORS

GPIO SCISSORS

Set SPOCK

GPIO SPOCK

Once again we will click “Deploy”.

And VOILA!!!

You will notice now that you User LEDs will change (on roughly the same 15 second interval we set in our initial Sending workflow).

There you have it! You’ve successfully used MQTT to send messages between devices easily and quickly. In our future tutorials we will show you how to setup your BBBW as a Broker, letting you maintain control over all of your messages. We will also create nodes to send sensor data and separate clients to receive them. Now go have some fun with your new IOT Wizardry!

Like what you read? Why not show your appreciation by giving some love.

From a quick tap to smashing that love button and show how much you enjoyed this project.