MQTT is an OASIS standard messaging protocol for the Internet of Things (IoT) and one of the protocols supported by akenza.
It is designed as an extremely lightweight publish/subscribe messaging protocol that is ideal for connecting remote devices with a small code footprint and minimal network bandwidth. MQTT is used in a wide variety of industries. By using a combination of MQTT and API functionalities, akenza enables you to automatically create Digital Twins for your devices. Akenza runs an open-source MQTT broker from Eclipse Mosquitto.
In order to proceed in this tutorial, you need an Arduino Board capable to connect to the Internet. In our example, we use an Arduino Uno WiFi Rev2. To directly connect your device via MQTT with akenza, you can easily implement it with a Python script. Find out more about MQTT connectivity here.
Connect the Arduino device via MQTT on akenzaFirst of all, you need to create your akenza account. Head to the registration page of akenza to get it started.
Creating a secret
In order to safely communicate with akenza, a secret needs to be generated that has to be provided in the topic structure of the uplink request.
This step has to be performed for all devices with the same connection parameters, which you want to connect. Each device will be uniquely identified by a specific ID accordingly. A secret will be generated after the creation of the MQTT device connector on the Data Flow.
Setting up an MQTT Device Connector on akenza
Go to Data Flow in the menu and select Create Data Flow.
Choose MQTT as a device connector. As Device Type, select Passthrough, in order to receive data in the same format as sent from your MQTT device.
Choose one or multiple Output Connectors of your choice. There are several options to choose from. In this tutorial, we choose the Akenza DB. Find out more about Output Connectors.
Create an MQTT device on akenza
To create a new MQTT device, select Create Device within the menu of the Asset Inventory. Add a device name and optionally a description, Asset Tags, and Custom Fields. Select your created MQTT- Data Flow and click Proceed.
Create a new MQTT device
Generate a random ID for this device by clicking Generate ID. Finish the process by clicking on the button Create Device. Your device will be now displayed on the Asset Inventory Overview.
Setup the Arduino device
As a next step, the Arduino device has to be now configured to send data to the MQTT Broker of akenza. Go to Asset Inventory and use the filter to search for your device. Open the device detail page by selecting your device. Within API-Configuration, all the required information is available to set up the device as an MQTT- client.
Configure the Arduino device - Set up the WiFi Connection
To have the Arduino Uno Wifi able to connect to WiFi we used the WiFiNINA library, available in the Library Manager of Arduino IDE.
Manage Username and Password
In order to manage Username and Password, we have created an additional header file called arduino_secrets.h
#define SECRET_SSID "<your username>"
#define SECRET_PASS "<your password>"
WiFi connection code
The code to connect Arduino to WiFi is reported as below:
#include <WiFiNINA.h>
#include "arduino_secrets.h"
// your network SSID (name)
char ssid[] = SECRET_SSID;
// your network password (use for WPA, or use as key for WEP)
char pass[] = SECRET_PASS;
WiFiClient wifiClient;
void setup()
{
//Initialize serial and wait for port to open:
Serial.begin(9600);
// wait for serial port to connect. Needed for native USB port only
while (!Serial) {
;
}
// attempt to connect to Wifi network:
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while (WiFi.begin(ssid, pass) != WL_CONNECTED) {
// failed, retry
Serial.print(".");
delay(5000);
}
Serial.println("You're connected to the network");
Serial.println();
}
void loop ()
{}
Create a JSON message
Akenza accepts messages in JSON format for MQTT connections. To build up a well-formed JSON object, we make use of the ArduinoJson library.
Final result
Now you have all the elements needed to set up your code on Arduino and stream data via MQTT to akenza:
- Copy the code in attachment into your Arduino IDE
- Set the username and password of your WiFi connection in the code
- Configure Arduino on akenza
- Upload your code to your Arduino Uno WiFi and start streaming data to akenza
Visualize and manage your data on akenza
Akenza automatically decodes the received JSON and visualizes data received from the Arduino device. Incoming data can be viewed on the Device Detail Page within Message Logs.
Incoming data are visualized on Data Overview as Device Data KPIs and also on the History chart.
Further data processing and visualization options can now be applied to your individual use case.
For example:
Apply business logic with the Rule Engine on akenza
Data processing & notification options with Output Connectors on akenza
Comments
Please log in or sign up to comment.