Practical IoT fun(ctionality) at the office, with Coreflux / MQTT / Unity

Paulo Mota
Jun 14 · 6 min read

As an enthusiast of the Maker movement with multiple interests, one of the qualities I value in digital tools (both software and hardware) is versatility. And so, me and my team often revel in devising pet projects that simultaneously have a practical utility and allow for an exploration and expansion of our digital toolsets!

In the didactic spirit of Makers, and realizing this may eventually have some interest for other people, here’s an account of the latest hobby project undertaken at Setlevel: to create the groundwork for a future “Internet of Things”-enabled office. Such a system, now increasingly common in the age of IoT, allows for remote supervision and control of equipment and facilities. This capability is often coupled with multiple benefits, such as power-saving, reduction of maintenance requirements, and more.

But building a complete system from scratch takes time! So first things first… a journey of a thousand miles begins with a single step. The goal for today was simple: to enable MQTT control over a specific set of the office lights. The necessary tools were already at hand, just waiting to be put to good use: a beta-tester license of Coreflux (more on that below for those who haven’t contacted with it yet), an MQTT relay node, and Unity (for desktop and mobile software development, in this case to be compiled for Android).

Choice of node hardware (and relay module configuration)

For this part, some market research was necessary. There are multiple equipments available out there, each with different capabilities versus prices ranges. What you should get depends very much on what you wish to accomplish, and what specific requirements you have.

The choice in this case went to an Aptinex Relay Node (RP4CE8) board. This module allows the control of four relays through Wi-Fi, acting as a MQTT client. Coming straight from the manufacturer in Sri Lanka, it took a week or so to reach the office.

Using an Aptinex Relay Node (RP4CE8) as hardware for Setlevel’s office automation

The Aptinex relay module has a configuration panel that allows you to customize different aspects of its use.

Configuring all the necessary elements

Overall, the RP4CE8 was pretty straightforward to configure and install. Recommended, likely to be used in future projects.

Coreflux

An industrial-grade communications tool! Versatility coupled with ease of use.

In its essence, this software uses native protocols to allow communication between different devices, across a diverse range of operating systems and hardware suppliers. Focus is placed on gathering and passing data, instead of development, providing various options in terms of connectivity.

To get Coreflux, if you’re a member of the community you’ll soon be able to download the latest version of the installer file from its website.

An overview of the installation process follows. First, the usual: download, double-click on the installer, read the texts, and proceed with all steps. Then, run the shortcut that will be created on your desktop. Once that is done, you will be prompted for a login.

At the front door

Once you sign in, you’re presented with a screen listing the available apps and those you have already installed (first-time users will have none on this field yet). The one you’ll want to install for this project is the MQTT Broker… click on it to open the pre-install configuration, and then press “Install”:

Looking good

The broker should now appear on your list. Click on its blue icon to initialize the service, and wait for the status to change to “Running”.

And voilá, you now have an MQTT Broker running straight from your computer. Using any kind of client, you can now publish and subscribe to it (in this case we conducted initial tests using a software called MQTT FX).

Ready to go

Electrical installation

Getting hands-on… remember, safety first! Before even considering touching electrical wires, check if the local power is disconnected from the electrical board. Wear appropriate gear, and use proper tools when interacting with your electrical installation. Use stable ladders to reach high places. If you are unsure on how to do any of this, find someone who is qualified to help you.

Above and beyond

Here’s the corresponding schematic, straight from the Aptinex website:

Unity: User Interface and Scripting

Time to devise a user interface / frontend for our endeavour! Moving over to Unity, a quick prototype project was created.

Remember the emphasis at this point was on functionality, not aesthetics. A full-fledged control application will allow for dynamic feedback, animations, and much more. And Unity is the perfect tool to achieve balance between look and function (will probably be publishing future evolutions of this, as it is gradually implemented around the office).

So, as a first UI element, a button prefab was built to use as a template for all instances. This prefab was comprised of a button (of course), with a text label and an icon for ease of readability.

Quite spartan… functional, not (necessarily) pretty

Next, the Coreflux Unity API package was imported. On the “Assets” tab, select “Import Package”, and “Custom Package”. A file dialog box will open, and the correct package should be selected. Two new files shall be added to the project, namely the Coreflux DLL and a MQTT control script.

Importing comms magic into the project

All set to begin with code. The actual functionality was then implemented on a script, with the Coreflux dependency added (“using CorefluxUnityAPI.Networking.Mqtt;”).

First, some additional detail on the MQTT control script, that handles the connection to the broker. This script includes three variables that will be filled in on Unity, specifically where you’ll input your broker’s IP and port, as well as ClientID. The DebugLocation is used to output info on the state of your connection, adding useful feedback to your app.

Controlling the connection

Done! Now add this script to your Unity canvas (or any other element where it’s easy to find), filling the input fields with your connection data.

And now, for the lamp control. To begin, we require a topic string, and a boolean for interpretation of the state. Afterwards, when the button is clicked, a 1 or a 0 is published to MQTT and thus sent to the relay node (depending on its current state).

Quick and to the point

And that’s right about it! Proceed to the Build Settings option, and choose the platform you wish to run the app in. In this case, we compiled for Android.

Conclusion

Objective complete! Control at your fingertips.

A sighting of a Coreflux / MQTT / Unity / Android software on the wild

We now have remote control of the office lights, and a framework to keep on implementing domotics control over different aspects of the entire office.

This also accomplishes a small demonstration of the potential and ease of use of a cross-hardware communications software like Coreflux. It had already proven its capabilities in industrial context, but here also makes an appearance in a more personal context (especially when coupled with technologies we already use in our daily lives).

What’s next? Endless possibilities, many new topics on the hobby To-Do list.

Paulo Mota

Written by

Technology through the scope of Human Factors

Coreflux Blog

IoT for All

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade