Text Copied
 Home    Blog    Develop Scalable IoT Application

Developing Scalable IoT Application with MQTT Broker

by Jesmi | Apr 07, 2022 | Uncategorized | 0 comments


iot application

Do you want to quickly and easily create an IoT implementation that meets your requirements? This is the right blog for you. Let’s see what would be the best choice to develop an IoT application easily. Building an IoT application comes with several challenges, which includes security, connectivity, data collection & processing etc. You already know that building an IoT application from scratch is one of the most difficult tasks. It is because you must develop code for each one individually. Can you believe that a single framework contains everything you need to build any IoT application? Yes, Bevywise MQTT Broker is an IoT application framework that is suitable for the implementation of any IoT application. It provides solutions to all the challenges mentioned above. Now let’s look at how we can configure an IoT application using that framework.

Components required

Creating an application usually necessitates the use of components. It is essential that we have to be ready with those necessary components for developing an IoT implementation. Those essential components are : MQTT Protocol, MQTT Clients, and MQTT Broker. When it comes to developing a large-scale IoT application, the MQTT protocol is the best choice. MQTT was found to be a flexible and scalable network protocol that is well-suited for developing an IoT application for greater implementation, after taking into account a variety of developing and mature IoT protocols. MQTT Clients, the devices that are used for your implementation, can be used to develop your IoT application based on your requirements. MQTT Broker is the one that act as a middle ware to connect and communicate the MQTT clients. In that case, our Bevywise MQTT Broker also comes with Python extensions, making it perfect for building a full-fledged IoT application with high-level IoT implementation.


MQTTRoute, a Bevywise MQTT Broker offers a free trial that allows you to experiment with the features and Python extensions to create IoT applications. Download & try the free trial pack of MQTT Broker.


The MQTTRoute framework comprises of all the necessary options required to develop an IoT application. You can readily construct a large-scale IoT application with this and benefit from it. If you want to get in-depth detail about the installation and basic configuration of MQTTRoute, please refer our Broker help documentation.


Now, let us move on to the example to know more about the MQTT protocol. Assume you have two devices and various weather sensors (a humidity sensor and a temperature sensor). You’d like to send data on the humidity level and the temperature level to two different devices.


This task can be simply managed with MQTT. First you must set up a MQTT broker service. Then, on the broker, you may connect two sensors as clients and configure them to deliver data on the topics “Humid” and “Temp.”


After that, you can connect the devices to the broker and subscribe the first one to the topic “Temp” and the second to the topic “Humid.” As a result, whenever the sensors publish the appropriate information to the broker, two connected devices will receive messages regarding humidity and temperature.


From the above example it is clear that the client sends the message to the MQTT broker. It is because of the reason which is previously stated that the clients can’t communicate with each other directly. The messages sent by the clients are stored to the default database in the broker and then it sends the messages to the appropriate subscribed client. The MQTT broker’s interaction with the devices was indicated in this example. Depending on your needs, developing an IoT application will require a specific UI and database. They can be found in the Python Extensions of the Bevywise MQTT Broker. Let’s look at how the Python Extensions for MQTT Broker allows you to customise an IoT implementation to your specific requirements. Here’s how these extensions can be used to create an IoT application.

Custom data storage based on IoT implementation

The first and foremost step is to store data for an application. As previously stated, the data for an application will be automatically stored in the MQTT Broker’s default database. By default, the Bevywise MQTT broker has the ability to store data in the following databases: MYSQL, MSSQL, PostgreSQL, and SQLite. However, we have the custom storage extension for you, if you need advanced storage for developing your IoT implementation. If your IoT application necessitates the use of other advanced databases, you can specify the name and port of the database and use it in your implementation. For my IoT implementation, I’m using an elastic database through the use of the custom storage option. It is because, I want to analyse large amounts of data quickly and in near real time, which the Elastic Search database allows me to do.

Let’s see how to configure the elastic search database here:


  • Make the necessary changes in the data_store.conf folder first. Set the DATASTORE option to CUSTOM after enabling the CUSTOMSTORAGE option.
  • The database’s PORT number and the INDEX NAME, which is the name by which you want the data to be stored, can be specified here based on your needs.
  • We have commited the configuration files and the python extensions file in Git. Please refer to the data_store.conf folder and perform the above procedure with the help of this Github page.
  • After that, set up the custom_store.py folder. The elastic search connector was used to set this up. Refer to the code below for a better understanding.
  • The websocket process is used to display the stored data in the user interface. This will be useful when implementing a custom UI.
  • You can refer the custom_store.py extension files in this Github page.

Crafting your own UI

The data is usually stored in a MQTT broker before being sent to the appropriate subscriber. But how can we be certain that the data is arriving and that the process is proceeding? That’s why we have a user interface for viewing messages between clients. By default, the Bevywise MQTT broker’s UI is useful for extracting data from devices, but you can also customise your dashboard and widgets here. However, if you want a completely different UI for your IoT implementation, use our Custom UI functionality for your high-level IoT application, which is one of the python extensions.


You can refer the custom_ui_server.py

extension files in this Github page.

Reference code for line graph

xaxis: {
type: ‘date’,
range: [olderTime, futureTime]
}
};
Plotly.relayout (graph_id, minuteView);
Plotly.extendTraces (graph_id, update, d)

live temperature data

This UI reference code is for live temperature monitoring application. Plotly provided this reference code. It takes the temperature of a room and displays it in a user interface. We can view it through the line graph’s output in this case.

Configure scheduler to generate alerts

We’ve already seen how to create your own database and customise the UI to view the data the way you want it. Taken the example given below, the room temperature monitor, and we’ll look at how to create conditions, alarms, alerts, and warnings in it. To do the above, you’ll need to use a custom Scheduler. This is also one of the Python extensions in the MQTT Broker.


You can refer the custom_scheduler.py extension files in the following Github page.


Create your code based on your IoT implementation and insert it into the #write your code here section. A small example of the scheduler option is provided below, and you can write your own code to suit your needs.

Reference code for Alerting high temperature in room

def schedule_conf( ):
schedules={}
schedules={
‘STATUS’ : ‘DISABLE’,
‘SCHEDULES’ : [
{‘OnceIn’ : 5,‘methodtocall’ : fiveminschedule },
{‘OnceIn’ : 10,‘methodtocall’ : tenminschedule } ] }
if ( p_avg < data3 [‘msg’] [‘message’] [0] ){
document.getElementById (“Alarm”).innerHTML=data3 [‘msg’] [‘message’] [0] + “ALERT!” + “Temperature High”;
}
return schedules

The above code represents the condition applied which aggregate the temperature between every 5 minutes and 10 minutes schedule data and also refers to the alert setting when the temperature is high in the room. The data from the scheduler is pushed into the UI by the conditions listed above. If the temperature in the room is too high, we will receive an alert message via the UI. This way, you can easily configure it based on your needs to suit your IoT implementation.

Enabling Advanced Authentication

Let’s take a look at MQTT broker security now. The data security in MQTT Broker is excellent. It is dependent on the security of TLS / SSL. However, your data still requires security, and we offer Custom authentication if you think security is essential for your IoT application. Custom authentication functionality in Python extensions will prove to be beneficial. Because, Bevywise MQTT Broker provides custom authentication functionality, allowing users to integrate any central IAM and SSO for your big level IoT implementation & for managing lot of users in your application based on your requirements.


Let’s see how to configure authentication in broker.conf folder

  • Set the REMOTEAUTH_ENABLED =YES

You can refer the broker.conf and custom_auth.py extension files in this Github page.

# Request Retries Count
requests.adapters.DEFAULT_RETRIES = 3
# Request URL
url = “https://mqttserver.com/auth”
# Request Timeout
request_timeout = 0.1
# Request Method
request_auth_method = “POST”
# POST | GET | PUT

When we manage a large number of users, as previously stated, if one of them forgets or mistypes his password, you can specify how many times you can try again in the requests.adapters.DEFAULT RETRIES field. This field’s default value is 3. You can change the count based on your IoT implementation. You can enter the URL of your authentication landing page in this field, “url = “https://mqttserver.com/auth”. This validates your attempt to connect using with your login credentials.


All of the functionality described above is provided by the Python extensions in Bevywise MQTT Broker. With these, you can easily set up a large-scale, flexible, and scalable implementation of your IoT application.


In our MQTTRoute, all python extensions were pushed to Git. Refer this Github page to get to know all of the folders.

To develop a scalable IoT application, start by downloading the Bevywise MQTT Broker.

For the detail information about python extensions to develop any scalable IoT implementation, refer our Python MQTT Broker interface documentation.

Submit a Comment

Please take a moment to fill this form