Culture / Edge / IoT

Off-The-Shelf Hacker: Build a Portable MQTT Broker with an Artik

6 May 2017 3:00am, by

For the longest time, I’ve wanted to build a wireless data control “hub.” I envision a battery-powered device with Linux and the ability to connect to many sensors at once. It would have to be robust enough to work out in a field or up on top of a building.

Finding a way to manage data to and from multiple sensors simultaneously has been a roadblock for me. For example, it’s pretty easy to write a little client program for an ESP8266 device that sends simple text data to a Linux machine, attached to my LAN. But that approach only supports one client connected to the server at a time.

How about MQTT on an Artik?

Remember the Samsung Artik we reviewed last year? We did the basic Python blink-an-LED exercise. There were problems sorting out the WiFi, so afterward I put the Artik on the shelf and worked on other projects, knowing that troubleshooting inspiration would eventually return to my AO (area of observation).

The Artik 10 is Samsung’s flagship system-on-a-chip (SoC) for Internet of Things (IoT) usage. It sports 8 cores, WiFi, Zigbee, 2GB of memory, 8GB of flash and Fedora Linux. I have the development kit, which has a socket and support electronics for the Artik 10 chip.

For me, running Linux in Flash is the future of operating system-based IoT modules, like the Artik. The device boots up fast, runs cool, and you don’t have to put a piece of tape over the end of the micro-SD card to keep from losing it if it pops out of its slot.

Over the last few days, I’ve solved the WiFi issues on the Artik.

A lightweight publish/subscribe (called pub-sub) server/client protocol, MQTT appeared on my radar a while back and can apparently handle several thousand messages per second without much sweat. MQTT runs on a variety of Linux systems and hardware platforms.

So, MQTT running on a WiFi-enabled Artik seems like a great fit for a portable wireless data control hub. Let’s see about getting WiFi and MQTT up and running on the Artik, shall we?

Fixing WiFi on the Artik

Just like last time, I hooked up a USB cable between my ASUS Linux notebook and the Artik. You can plug a standard cell phone data cable into the micro-USB port (next to the Ethernet port) on the Artik board with the other end going to your notebook.

Next, I started the minicom serial application and logged into the Artik using root as the username and password.

Artik login using minicom over the USB port

I’d need root permissions for the USB ports on my notebook for this project, so using sudo is necessary for minicom to work:

Updating the Artik software came next. Since Artik uses Red Hat Enterprise Linux, you don’t just do an apt-get to update things like you would on a Debian or Ubuntu system. Red Hat uses a different software package manager called DNF.

After a bit of churning and asking if you want to load modules, the updating will finish and return to the command prompt.

At this point, I plugged an Ethernet cable into the port on the Artik dev board, then tried to log in using ssh from my Linux notebook. No go.

A quick check with ifconfig over the USB serial connection showed that the wired connection was being assigned an IP address (192.168.1.108) from my router’s DHCP server.

Although I could ping the Artik from my Linux notebook, I still couldn’t ssh into it over the network.

Perhaps something happened to ssh before I received the board, for review? On a hunch, I tried reinstalling ssh.

Bam, I tried ssh again from my notebook and could now log into the Artik using the wired Ethernet connection. With that working, I closed minicom and disconnected the USB cord.

Before WiFi works on any system, you need to add your wireless router information to the configuration files so it can connect to your network.

First go to the /etc/wpa_supplicant directory:

Next, execute the following to build the wpa_supplicant.conf file, replacing the SSID and PASSWORD with the ones from your network.

You may also have to add the following lines to the beginning of the wpa_supplicant.conf file.

I used vi to edit the file and add the lines.

Your file will look like the following, with an expanded psk line.

You’ll probably also have a #psk line present. It’s not needed, so just remove it.

Now, reboot the Artik.

After the Artik restarts log in over the wired connection again.

It turns out that the DHCP client for WiFi has to be started manually, according to the Samsung Artik 10 user documentation. It would be great to make that work automatically on boot up.

A quick check of ifconfig showed a new IP address (192.168.1.109) for the wlan0 device.

With that, I could log into the Artik over WiFi from my Linux notebook using a new terminal window and the 192.168.1.109 address. I also logged out of the wire connection and removed the CAT 5 cable from the Artik’s port.

Turning Artik into an MQTT Broker

Next came the job of setting up the Artik as an MQTT broker. While logged in over WiFi, I used the following to install mosquitto, a well-regarded MQTT broker and client, using the Red Hat application manager.

Once that finished, mosquitto was started in the background.

Remember the old ESP8266/PIR yard sensor project? Through sheer good karma, I had hard-coded the IP address into the firmware, so whenever the device booted up it would try to connect to an MQTT broker, which was at 192.168.1.109.

As soon as mosquitto came up, the yard sensor connected from 192.168.1.103.

I started a mosquitto subscription in a terminal on my Linux notebook.

As I moved my hand in front of the yard sensor, a stream of data successfully flowed forth, on the terminal screen.

MQTT subscription data in the terminal

What’s Next

I’ve run the Artik with the mosquitto MQTT broker for a couple of days and it seems stable and reliable. I’ve connected and disconnected, rebooted and even tried watching the messages using an MQTT client on my Android super-phone. All worked great without a hitch.

To make the rig truly portable, I’ll need to set up a 5 volt, 5 amp power supply, get DHCP to automatically come up on wlan0 at boot-up, and figure out some kind of case for the Artik development board and chip.

It might be interesting to attach a few sensors or actuators to the Artik’s general purpose input/output pins. We might track local temperature, ambient light and other environmental factors, using a Python script. We could make the data available to other devices through mosquitto_pub calls.

Wouldn’t it be useful to run the Artik as a WiFi access point, that allows all the sensor devices in the area to connect to it, automatically? Now that would be a portable wireless data control hub.

I’ll definitely have to work on that one.

Feature image via Pixabay.

A newsletter digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.