In previous articles, we talked about connecting your NetBurner embedded systems to AWS IoT Core and Azure IoT Hub using MQTT. But as we know, in this IoT world, there’s a wide variety of options to choose from. The best choice will depend on the characteristics of your application, budget, and even personal tastes. With this in mind, we bring you another IoT platform alternative called Ubidots.
This article is a small tutorial on how to start sending and receiving data using UBIDOTS and NetBurner systems through MQTT. This can help you take your next IoT project to the field quickly and easily.
Ubidots is an IoT platform that empowers everything from prototypes, education, and research projects to scalable production-level IoT projects. It has different plans and a free version for non-commercial projects that includes most of the features of the paid version.
The Ubidots platform allows you to send and receive data to the cloud from internet-enabled devices. You can use a variety of protocols, including MQTT, HTTP (REST API), and TCP/UDP for data transfer. It uses authentication methods and can be implemented over SSL/TLS protocols for data encryption (which we highly recommend using whenever possible).
One of the features of Ubidots is that it can function as a broker, in other words, as data middleware. It also allows you to visualize and modify the transmitted data using drag and drop widgets, which can be integrated into Dashboards. This will enable you to have an IoT application that you can show to your team or customers in a short period of time. Figure 1 shows an example of two such dashboards.
One last feature we’d like to point out is that it also has an event handler. This is where you can create rules that trigger actions such as sending an email, SMS, voice call, or even calling another REST API using WebHooks. All the information about events is here.
With this short introduction out of the way, let’s get started on our small tutorial that will demonstrate how to start sending and receiving data to Ubidots using MQTT with NetBurner development boards.
Create an Ubidots account.
To use Ubidots services, we need to create an account. The good news is that there’re free licenses available that we can take advantage of, as long as we only use them for prototyping, learning, or teaching IoT.
To create an account, you need to follow these steps:
- Go to the official Ubidots website and click on the SIGNUP button.
2. Choose the “TAKE ME TO UBIDOTS STEM” option, the free license for non-commercial applications (in this article, you can find the limits of the free version). If you are looking to develop a production application, we recommend you check the prices of the paid license here.
3. Fill in the requested fields on the right side and then click on the “SIGN UP FOR FREE” button.
4. If the registration was successful, a series of welcome windows will appear, with information, links to valuable resources, and demos of what you can do with Ubidots. Just click on the “-> (Next Step)” button.
5. At the end of the welcome screen, Ubidots creates an example Dashboard. Just click on the “GO TO MY DASHBOARD” button to see it. The demo has a Gauge value from 0 to 100 and a table with historical data.
Customizing the dashboard.
Now that we have an account, we can start playing with the Ubidots tools. The user interface is intuitive, so we recommend you bring out your inner explorer and do a little digging around.
Adding a variable.
Now that you’ve gotten your hands a little dirty, let’s get to work. First, we’re going to add a variable to send data to our embedded system. To do this, we follow the following steps:
- Under the Devices option, choose the submenu item with the same name.
2. A table will appear with a device. This was created by Ubidots automatically to save us time (we can add up to 3 in the free version). Click on the name Demo.
3. The device administration panel will appear. You can add new variables (maximum 10 in the free version), view the last value of the variables, see the latest activity time, set the device’s location, etc. Click on the “Add Variable” section and then choose the “Raw” option (more information about the types of variables here).
4. Choose a name for the variable. In this case, we’ll use Leds. This name is essential since it’ll identify the variable when sending or receiving data. You can change the color of the variable with the icon in the upper right corner (the paintbrush) and the representative icon itself with the one in the upper left corner (the cloud).
Adding a widget.
Once the new variable has been added, we can add a new widget to the dashboard to visualize the variable or change its status. To do so, follow the steps below:
- Click on the Data menu option and then on the Dashboards option.
2. Once in the demo Dashboard, click on the + button to add a new widget.
For this example, we’ll choose a slider widget. More information about widgets can be found here.
3. The slider configuration data will appear. Choose the variable we created in the previous section by clicking on the Add variables option. Choose the device, then the Leds variable, and click on the “ok” button.
4. Add the following settings in the next tab and click on the “ok” button.
5. The new widget will appear on the dashboard. According to your preferences, you can modify its position by dragging it or changing its size.
Connect to Ubidots using MQTT and NetBurner.
So far, we have configured a dashboard in Ubidots to view and modify data through a graphical environment. Still, the internet of things cannot exist without… things, meaning a device connected to the internet.
In this section, we’ll configure our NetBurner development system to send and receive data from Ubidots.
Ubidots Broker MQTT.
As we have mentioned before, Ubidots has several ways to communicate with devices, one of which is through MQTT. It’s the one we’ll use in this section. The program that we’ll see next uses the Ubidots MQTT broker rules. The following article provides a good overview if you want to know them in detail.
We have developed an example program that fulfills three fundamental functions to help you build your projects faster using MQTT and Ubidots.
- Connecting to a broker using MQTT.
- Publish values to variables.
- Subscribe to variables.
The code is based on the MQTT-Paho library, implemented for NetBurner systems. You can get it from the following GitHub link.
Here is a brief explanation of the available methods:
- connect(): Connects to the Ubidots MQTT broker.
- publish(label, value): Publishes the value specified in the variable with the name label.
- subscribe(label, callback): Subscribes to the last value received from the broker of the variable with a name label. When receiving data, the callback passed in is executed. Subscriptions must be managed after a successful connection with the broker.
- keepAlive(): Allows the MQTT connection to stay open and receive subscription data. It must be called recurrently.
- registerCallback(event, callback): The specified callback is called when an event is executed. The available events are:
- UBIDOTS_EVENT_CONNECTED: Successful connection with broker.
- UBIDOTS_EVENT_DISCONNECTED: Broker connection failed.
- UBIDOTS_EVENT_SUBSCRIBED: Successful subscription.
- UBIDOTS_EVENT_PUBLISHED: Successful publication.
- UBIDOTS_EVENT_ERROR: Error during connection.
Connecting to our Ubidots example.
Now that we have everything we need, we need to perform the following steps to start the integration.
Import source code to NBEclipse.
Once the source code has been downloaded from GitHub, it’s necessary to import it to NBEclipse to modify, compile, and upload it to our NetBurner development system. To do this, follow the new project instructions found in our documentation.
The application requires some additional data to connect to the Ubidots broker. These fields are:
Token: This is a unique string of characters from our Ubidots account to authenticate the MQTT connection.
Device name: Device to which the data will be sent.
It’s necessary to put this data into the project. To do this, we perform the following steps.
- Login to your Ubidots account. Click on the “My Account” option and the “API Credentials” option in the upper right corner.
2. A tab with your account credentials will be displayed. Click on the icon next to the Token section. This will copy a string of alphanumeric characters that we’ll use later to your clipboard. Keep this token in a safe place. Remember that it provides MQTT access to your project.
3. In main.cpp file, change the value of the UBIDOTS_TOKEN definition to the token of your application (the one you got before).
#define UBIDOTS_TOKEN "Add Here your Ubidots token."
4. In main.cpp file, change the value of the UBIDOTS_DEVICE_NAME definition to the name of your device.
#define UBIDOTS_DEVICE_NAME "Add here the device name."
If you followed the tutorial from the beginning, the name should be “demo”. If you want to check it yourself or set it to something else, you can find it in the admin panel of your device in the “API label” section.
PUB/SUB data to Ubidots.
To send data, we use the publish method. In our Ubidots dashboard, we have a variable called “demo” with a table of historical data and a gauge, so we add that name to the first parameter of the method, as follows:
To receive the changes of a variable, we use the subscribe method. Previously we configured a slider with the name “leds”. To subscribe to the changes of that variable, we would add the following.
If your variables aren’t named the same as the ones in the tutorial, you can get them by clicking on the variable in the admin panel of your device in the API Label section. The name will be displayed here.
Testing the application.
Once the changes are made to the base program, you’ll be required to compile and upload the program to your NetBurner development system.
If everything is set correctly (the token, device name, and variable name), you’ll see some confirmation messages in the MTTTY serial terminal (red box in the image).
When data is published, you’ll see a confirmation message (green squares in the image), and the Ubidots dashboard will update the table value and gauge. When you change the slider, you’ll see a confirmation message (blue color in the image).
The following image shows the interaction of the MTTTY terminal with the current value in the Ubidots dashboard.
Thanks for joining us on our journey into Ubidots. As always, we’d love to hear your thoughts and questions in the comments below. If you’ve used Ubidots in your own project, tell us about it! You can also email us directly at sales*at*netburner.com.