MuleSoft IoT Notification Service Using MQTT and Twilio                                                

July 07, 2023

Blogger Image

Pooja G Bhat

Software Engineer – Enterprise Integration

IoT, or the Internet of Things, is based on the networking of things or physical objects integrated with software, sensors, and other technologies to connect and exchange data with smart devices and systems through the Internet. Connectivity is a crucial component of the Internet of Things since seamless data transmission and reception allow IoT devices to carry out their intended purpose.

In this blog, we will explore the powerful combination of MuleSoft, MQTT, and Twilio to create an efficient and reliable IoT notification service. We will delve deeper into how to set up this integration, leveraging MQTT and harnessing Twilio's messaging capabilities to enable seamless IoT notifications.

What is MQTT in IoT?

MQTT, or Message Queuing Telemetry Transport, is a lightweight machine-to-machine IoT data protocol. It features a publisher-subscriber messaging model and allows for simple data flow between different devices. MQTT does not necessitate the client and server to establish a connection simultaneously. It is a real-time messaging protocol offering fast data transfer, similar to WhatsApp or Messenger. It enables clients to subscribe to a specified group of themes to acquire the needed information.

What is MuleSoft Twilio Connector?

Twilio is a cloud communication platform used as IaaS (Infrastructure as a Service). Using Twilio, users can send SMS messages, receive messages, delete messages, and more.

MQTT Connector in MuleSoft

MQTT Connector in MuleSoft allows customers to execute asynchronous event-driven messaging patterns using the MQTT protocol common among IoT use cases - especially prevalent in the automotive, manufacturing, telecommunications, and oil & gas industries. MQTT has grown to prevalence primarily in the IoT market as a lightweight messaging protocol for high-frequency data coming off of equipment sensors (e.g. temperature, pressure) common in the process and manufacturing industries.

Business Use Case: IoT-based Temperature Monitoring System in Logistics

Connecting manufacturers, distributors, and consumers is one of the many crucial applications of a supply chain. Due to shifting climatic circumstances, the quality of the products being sent suffers during the distribution phase. As a result, businesses design tailored containers to keep their goods safe while being moved from one location to another.

Using temperature monitoring sensors allows companies to track the environmental parameters of a trailer en route, even from a remote location. This IoT monitoring solution, and advanced telemetry, establish transparency in a cold supply chain, allowing companies to maintain product quality.

The below high-level architecture diagram shows the IoT implementation with MQTT and MuleSoft. The device in the carrier comprises a GPS module and a temperature sensor module which has been added to an ESP32 Board. It is programmed such that the sensor can send the temperature and location data periodically in JSON format to the MQTT broker via its MQTT publisher. A MuleSoft instance picks the data, sends an alert to the respective authority via Twilio if the detected temperature exceeds the threshold, and logs the data to MongoDB for effective tracking and visualization.

Steps to Configure MQTT

HiveMQ Setup in Windows:

  • Download the HiveMQ zip file using link.
  • Start HiveMQ from command prompt of bin folder in unzipped file (run.bat command).
  • Go to browser and open http://localhost:8080/.
  • Use the default username (admin) and password (hivemq) to log in.
  • The published data can be seen in the Retained Messages tab (The Retain Messages has to be set to true while publishing messages from the Mule application).

Steps to Configure Twilio Account

  • Create an Account in Twilio, using this Signup link.
  • After signing in successfully, go to the Twilio Console and click on the "Get a Trial Phone number" button to get the t\Twilio number.
  • In the Twilio dashboard, you will get ACCOUNT_SID and AUTH_TOKEN fields. These fields are required to configure Twilio in the Mule flow.
  • To send or receive an SMS on a certain number, it must first be added and verified within Twilio.
  • In the left menu bar, select Verified Caller Id and enter the numbers. The number will be added once it has been verified.

Anypoint Studio Setup

  • Configure the MQTT connector using the following details:
    • Client ID: any random ID
    • Username: username of the MQTT server used
    • Password: password of the MQTT server used
    • URL: tcp://localhost:1883

Data is sent by the sensor to the MQTT topic periodically and the MuleSoft implementation will listen to new messages on the topic.

MQTT On New Message Configuration

The On New Message must listen for the same topic in which the sensor publishes the data.

MuleSoft Twilio Connector Configuration:

  • Provide the Twilio Account SID as username and Auth Token as password.
  • Test the connection.
  • To send a message, configure the payload with the below fields.
    • Body: the text to be sent as an SMS
    • From: the Twilio number to be displayed to the SMS recipient
    • To: The SMS recipient number
  • Provide the Account SID to configure the Create Message.
  • Recipient receives alert on crossing the threshold temperature of 25 degree Celsius.

Conclusion

MQTT offers consumers significant advantages, such as reliability, prompt response times, and the capacity to support unlimited devices. It provides the ideal publish/subscribe messaging for many-to-many communication, sufficient documentation, and a vibrant development community that streamline MQTT protocol implementation. These features make MQTT one of the most popular options for unreliable, low-power wireless devices.

Using the lightweight MQTT protocol for data transfer, MuleSoft's integration capabilities, and Twilio's messaging service, we can build a robust solution for real-time communication between IoT devices and users. Learn how engaging with a reliable MuleSoft Integration Services provider like Nous Infosystems can help businesses leverage the MuleSoft platform to seamlessly connect devices, data, and applications in the cloud and on-premises.

Join the conversation

What are your thoughts on this blog? Drop us a line below. We’d love to hear from you.

© 2024 Nous Infosystems. All rights reserved.