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:
- How to request user input with Circuit forms.
- How to write a Circuit Node.js to control the IoT device.
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, 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 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:
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.
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!