Controlling IoT devices using Circuit SDK

Circuit is a team collaboration software supporting chat, voice, video, screen share and much more. It’s available on the browser, as a desktop application, iOS, and Android. The free version allows up to 100 users and 3 users per conference.

For an introduction to Circuit APIs, you may want to read this article.

In this article, we will use the Circuit SDK to control an IoT device through MQTT and Circuit forms to let Circuit users control the device directly from their Circuit clients.

The IoT device is an RGBW LED strip controller. The controller itself is outside of the scope of the article so we can concentrate on the main topics:

Here is the big picture.

The node application will use the Circuit SDK to login to Circuit, post a form to a Circuit conversation and received the post submission. At the same time, the application will connect to the MQTT broker to publish the desired status of the LED strip and will also subscribe to updates coming from the LED strip controller.

MQTT

MQTT, as defined by mqtt.org, is a lightweight machine-to-machine protocol designed to transport publish/subscribe messaging. It is ideal for IoT (Internet of Things) because of its small size, low power usage, minimized data packages, and efficient distribution of information to one or many receivers. Within MQTT, connected clients, subscribe and publish ‘topics’, and these topics have an optional payload with the topic data.

In this project the Nodejs application and the LED strip controller communicate through a Publish/Subscribe MQTT messaging.

So the node app subscribes to the ‘state’ topic published by the LED controller with the actual status of the LED strip and publishes the ‘set’ topic to change the status of the lights which the controller is subscribed to.

Circuit

Circuit clients and the Nodejs app communicate by posting and submitting web forms.

Let’s see how the Nodejs application logs into Circuit and connects to the MQTT broker to complete the end to end communication.

Logging into Circuit is fairly easy after instantiating the Circuit client just invoke logon API. Notice how the application adds an event listener for the ‘formSubmission’ event. The application will receive this event when the user submits the control form from the Circuit client.

Connecting to the MQTT broker is also straightforward thanks to the mqtt node module.

Also, in this case, the application needs to register handlers for some events like ‘connect’, ‘error’ and ‘message’. This last one is the handler that will be invoked when the ‘topic’ we subscribed for is being published. So, that is next, let subscribe for the event the RGBW LED controller will publish to report the actual state of the lights

With this, the app is initialized. Let’s post the lights control form to a Circuit conversation by means of the Circuit SDK API addTextItem:

Notice how the item contains a form object with different kinds of controls, the options values, and defaults. When posting it into a Circuit conversation, it looks like this:

Now, the Circuit client user can select the brightness, color or a flashing effect and submit the form. At this time the node application receives the ‘formSubmission’ event with the form controls values. Based on those values it builds the JSON object that is then published to the MQTT broker.

Finally, the controller receives the MQTT message and sets the lights accordingly.

Let watch a short video demonstration:

Wrap up

If you got here you are probably thinking there are more ‘practical’ ways of controlling an LED light, and you would be right. However, this article is intended to show how powerful the Circuit platform is, and how easy is to integrate anything by using the Circuit SDK.

Regardless, if you want to replicate the experiment here you can find not only the software but also all the schematics to build the RGBW controller.

The LED strip controller runs on an SoC ESP8266

For practical reasons I made both the node application and the MQTT broker running on the same machine (Raspberry PI 3B+) but, of course, they could be on different machines or devices as long as they have TCP connectivity over the broker port, typically 1883.

I used the Eclipse Mosquitto MQTT broker. Installation on the Raspberry PI cannot be easier as it is available through the main Raspberry repository.

Both the instructions to install Mosquitto, and configure and run the Nodejs application are on Github.

Thanks for reading!

--

--

--

Software Engineer — Atos Digital Expert. Views expressed are my own and do not necessarily reflect the views of my employer.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CS373 Fall 2021: Rodrigo Estrella

TypeScript Cheat Sheet

Merge Intervals — https://leetcode.com/problems/merge-intervals/

Ultimate guide to building a basic edit form and grid with Angular + Firebase + Bootstrap

PassPill, the app architecture

5 JavaScript Tips that are good to know

Go RestFull Service

Connection Indicator Using Vue & Vuex

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Walter Martins

Walter Martins

Software Engineer — Atos Digital Expert. Views expressed are my own and do not necessarily reflect the views of my employer.

More from Medium

Secure application using Nginx Auth and Docker

Log4Shell: Lua + Nginx Mitigation

Running Keycloak on Docker

Easily Create VMs for Docker Swarm Development