Kingsley Amankwah is a software engineer and technical writer.

As a software developer who lives with his aging parents, I was worried about their well-being and wanted to ensure that they were safe and secure at all times. So, I created an IoT application that would help me ­­monitor their activities in real-time and alert me in case of any emergency.

I started by installing sensors in different parts of the house to monitor temperature, humidity, and sound levels. I then used Solace technology to build an IoT application that would use the data from these sensors to alert him in case of any abnormal activity. I connected the sensors to the Solace messaging platform, which would send messages to my smartphone whenever there was a change in the sensor data. For instance, when the temperature in the house dropped below a certain level, I received an alert on my phone, prompting me to check on my parents’ heating system.

Overall, the real-time event-driven architecture (EDA) of my IoT application gave me a sense of security and peace of mind. I was able to keep an eye on my parents’ activities at all times and respond quickly to any changes or emergencies. Solace technology proved to be a reliable and efficient messaging platform that helped me build this robust and secure IoT application. In this article, I’ll provide an overview of how I used Solace technology to build this application.

Overview of Event-Driven Architecture

EDA is an approach to software architecture that uses events to trigger and communicate between services, applications, and systems. Events can be anything that represents a change in state, such as a sensor reading or a user action. By processing events in real-time, organizations can respond quickly to changes and opportunities, enabling faster and more accurate decision-making.

Why Choose Solace Technology?

When I set out to create my application, I knew that I needed a messaging platform that could handle high volumes of events in real-time while providing enterprise-grade security and reliability. After researching various options, I ultimately chose Solace technology for several reasons:

  1. Its ability to handle high volumes of events and distribute them to multiple consumers in real-time. This was critical for my application, which involved collecting sensor data from multiple devices and processing it in real-time.
  2. Enterprise-grade security and reliability, which were important for me because I needed to ensure that the data collected from the sensors was secure and that the messaging platform could handle the demands of a real-world application.

Overall, I’m very pleased with my decision to use Solace technology, as it has provided me with the performance, security, and reliability that I needed to build a successful solution.

To showcase how I developed this real-time IoT application, I will walk you through a simple application that I created to monitor temperature and humidity. For this, I used a Raspberry Pi and a DHT11 sensor. The application sends events to a Solace message broker that I set up, which then distributes these events to multiple consumers in real-time.

Setting Up the Raspberry Pi and DHT11 Sensor

Step 1. Connect the DHT11 sensor to the Raspberry Pi GPIO pins.

When I first started working on my project, I knew I needed a temperature and humidity sensor. After some research, I decided to use the DHT11 sensor because of its digital capabilities and 1-wire protocol. To connect it to my Raspberry Pi, I followed these steps:

  1. Shut down my Raspberry Pi to avoid any electrical issues during the connection process.
  2. Located the GPIO pins on my Raspberry Pi board. They were easy to find as they were located on the top of the board.
  3. Connected the VCC pin of the DHT11 sensor to the 5V pin on my Raspberry Pi, ensuring that the voltage requirements matched.
  4. Connected the GND pin of the DHT11 sensor to the GND pin on my Raspberry Pi, ensuring the connection was secure.
  5. Connected the DATA pin of the DHT11 sensor to GPIO pin 4, which was the one I had selected to use for the project, but you can use any other GPIO pin you prefer based on your project requirements.

Here is the wiring diagram I used for reference:

DHT11 Sensor            Raspberry Pi
    VCC         ->          5V.
    GND         ->          GND
    DATA        ->         GPIO 4

Step 2: Install the Adafruit Python DHT library.

I found that the Adafruit Python DHT library is a great tool for reading data from the DHT11 sensor using Python. Installing this library was quite easy for me, I just followed these simple steps:

  1. Opened the terminal on my Raspberry Pi.
  2. Updated my package lists by running the following command:
    sudo apt-get update
  3. Installed the Adafruit Python DHT library by running the following command:
     sudo pip3 install Adafruit_DHT

Note: You may need to install pip3 first if it’s not already installed on your system. You can do this by running the following command:

 sudo apt-get install python3-pip

Step 3: Write a Python script that reads temperature and humidity values from the sensor.

Once I connected the DHT11 sensor to my Raspberry Pi and installed the Adafruit Python DHT library, I wrote a Python script that reads temperature and humidity values from the DHT11 sensor connected to GPIO pin 4:

import Adafruit_DHT

# Set the sensor type and GPIO pin number
sensor = Adafruit_DHT.DHT11
pin = 4

# Try to read the temperature and humidity from the sensor
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Check if the temperature and humidity values were successfully read
    if humidity is not None and temperature is not None:
        print('{{'Temperature':{:.1f}'.format(temperature))
        print('{{'Humidity':{:.1f}%'.format(humidity))
    else:
        print('Failed to read sensor data.')

Explanation of the code:

The first line imports the Adafruit_DHT library.

The second and third lines set the sensor type (DHT11) and the GPIO pin number (4) that the sensor is connected to.

The fourth line tries to read the temperature and humidity from the sensor using the Adafruit_DHT.read_retry() function. This function will attempt to read the sensor data multiple times if it fails the first time.

The fifth and sixth lines check if the temperature and humidity values were successfully read. If they were, the values are printed to the console. If not, an error message is printed.

Configuring the Solace Message Broker

Step 1: Sign up for a Solace PubSub+ Cloud account.

PubSub+ Cloud is a cloud-based messaging service that provides a fully-managed message broker. To use it, you’ll need to sign up for an account at https://cloud.solace.com.

Step 2: Configure the Raspberry Pi Python script to connect to the Solace message broker using the client username and password.

To connect to the Solace message broker from my Raspberry Pi Python script, I used the Solace Python API. Here’s how I configured my Python script to connect to the Solace message broker:

  1. Installed the Solace Python API on my Raspberry Pi by running the following command in the terminal:
    sudo pip3 install solace-semp-config
  2. Created a new service by clicking on the “Create Service” button and following the instructions.
  3. Obtained the connection details (host, username, password, and port) from the Solace Cloud console. To do this, I clicked on the “Connect” tab and then clicked on my messaging service.
  4. Updated my Python script to connect to the Solace message broker using the connection details obtained in the previous step. Here’s an example Python script that connects to the Solace message broker and publishes a message:
import solace_semp_config
import time

# Set up the connection details
solace_host = "<your-solace-host>"
solace_username = "<your-solace-username>"
solace_password = "<your-solace-password>"
solace_port = "<your-solace-port>"
solace_vpn = "<your-solace-vpn>"
solace_topic = "<your-solace-topic>"

# Create a new Solace API client
client = solace_semp_config.SempClient(solace_host, solace_username, solace_password, solace_vpn, solace_port)

# Connect to the Solace message broker
client.connect()

# Publish a message to the Solace message broker
while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
        message = '{{"temperature":{:.1f},"humidity":{:.1f}}}'.format(temperature, humidity)
        client.publish(solace_topic, message)
        print("Published message: " + message)
    else:
        print("Failed to read sensor data")

    # Wait for some time before publishing the next message
    time.sleep(5)

# Disconnect from the Solace message broker
client.disconnect()

Explanation of the code:

The first line imports the Solace Python API.

The next few lines set up the connection details, including the Solace host, username, password, port, VPN, and topic.

The solace_semp_config.SempClient() function creates a new Solace API client using the connection details.

The client.connect() function connects to the Solace message broker.

The client.publish() function publishes a message to the Solace message broker. The message is a JSON object that contains the temperature and humidity values.

The time.sleep() function  adds a delay of 5 seconds before publishing the next message. This is done to avoid overwhelming the message broker with too many messages at once.

client.disconnect(): This line of code disconnects the Solace API client from the message broker. It’s a good practice to disconnect from the broker when you’re done using it, to ensure that you don’t leave any connections open unnecessarily.

 Consuming the Events

  1. I created a new Python script on my laptop, which has access to the Solace message broker.
  2. To build the necessary functionalities, I imported two modules, namely paho.mqtt.client for the MQTT client and json for parsing the incoming JSON message.
  3. I defined the callback function that gets triggered every time a message is received. My callback function was designed to parse the incoming message and display it on the console.
  4. Next, I created an MQTT client instance, and I set the client ID and username/password as appropriate for my Solace Cloud account.
  5. To connect my MQTT client to the Solace message broker, I used the appropriate connection details.
  6. I subscribed to the topic that the Raspberry Pi script is publishing to.
  7. Finally, I started the MQTT client loop, which listens for incoming messages and calls the callback function whenever a message is received.

Here is an example code snippet that demonstrates these steps:

import paho.mqtt.client as mqtt
import json

# Define the callback function
def on_message(client, userdata, message):
        payload = json.loads(message.payload)
        print("Temperature: {}°C, Humidity: {}%".format(payload["temperature"], payload["humidity"]))

# Set up the MQTT client
client = mqtt.Client(client_id="my-client-id")
client.username_pw_set(username="my-username", password="my-password")

# Connect to the Solace message broker
client.connect("mr-broker.messaging.solace.cloud", port=1883)

# Subscribe to the topic
client.subscribe("my/topic")

# Start the MQTT client loop
client.loop_forever()

In this example, the callback function on_message is defined to parse the incoming JSON message and print the temperature and humidity values to the console. The MQTT client is then set up with the appropriate client ID, username, and password, and is connected to the Solace message broker. The client subscribes to the topic that the Raspberry Pi script is publishing to, and the client loop is started to listen for incoming messages. When a message is received, the on_message callback function is called.

Best Practices and Considerations for Using Solace Technology in Real-World Scenarios

When using Solace technology in real-world scenarios, there are several best practices and considerations that should be taken into account. These are important to ensure that your Solace-based real-time event-driven architecture is reliable, scalable, and secure. Let’s dive into each of these best practices and their benefits.

Designing for High Availability and Scalability

Designing for high availability and scalability is crucial in real-world scenarios as it ensures that your application can handle a large number of events and users without experiencing downtime or performance issues. This involves setting up Solace messaging infrastructure in a clustered, highly available, and fault-tolerant configuration. By doing so, you can ensure that your messaging infrastructure is always available and can handle any load that comes its way.

  • Increased reliability: Your messaging infrastructure will be highly available and fault-tolerant, ensuring that your application can always communicate with Solace.
  • Scalability: The infrastructure can easily handle an increased number of events and users, so you don’t have to worry about any performance issues.

Ensuring Data Security and Privacy

Data security and privacy are critical in any application, and even more so in real-time event-driven architecture. It’s important to ensure that sensitive data is protected from unauthorized access and that data is transmitted securely. This can be achieved by implementing encryption, access control, and other security measures.

  • Improved security: Protects sensitive data from unauthorized access and ensures that the communication between Solace and other components of the application is secure.
  • Compliance: Ensuring data security and privacy helps meet compliance requirements and protects the reputation of your organization.

Monitoring and managing performance

Monitoring and managing performance are essential to ensure that your application is performing optimally and meeting the desired service level agreements (SLAs). This involves setting up monitoring and alerting mechanisms to proactively identify and address performance issues.

  • Increased uptime: Early detection of performance issues can prevent downtime and ensure that your application is always available.
  • Improved performance: Proactive monitoring and management can help identify bottlenecks and improve the overall performance of your application.

Integrating with other systems and technologies

Integrating Solace with other systems and technologies is essential to ensure that your application can communicate with other components of your application ecosystem. This involves integrating Solace with other messaging systems, databases, and other components.

  • Improved interoperability: Integrating Solace with other systems and technologies can improve the interoperability of your application ecosystem.
  • Increased functionality: Integration with other systems and technologies can help add new functionalities and features to your application.

By following these best practices and considerations, organizations can ensure that their Solace-based real-time event-driven architecture is reliable, scalable, and secure. Designing for high availability and scalability, ensuring data security and privacy, monitoring and managing performance, and integrating with other systems and technologies are crucial for the success of your application. Implementing these best practices will help you achieve your business goals and provide a better user experience.

Conclusion

Finally, building a real-time IoT application using Solace technology can be an exciting and rewarding experience. Whether you are using a Raspberry Pi, an app, or both, the possibilities are endless for what you can achieve with Solace. I hope that you found this guide helpful and informative, and I encourage you to continue exploring the world of IoT and Solace technology to unlock even more potential in your projects. With the right tools and approach, you can create innovative and impactful solutions that make a real difference in the world. Happy Coding!

About the Author

Kingsley Amankwah

Kingsley is a software engineer and technical writer with several years of experience in the software development industry. He has a passion for exploring new technologies and sharing his knowledge with others. In his free time, Kingsley enjoys reading articles and playing games on PlayStation.

Solace Scholar
Solace Scholars

The Solace Scholars Program encourages writers from our community to create technical and original content that describes what our technology and/or third-party integrations are being used for and exciting projects that are made possible by event-driven architecture. Solace Scholars are great at solving challenges and explaining complex ideas. If you're interested in writing for us and learning about what you can earn, check out the website and submit an idea to us!