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.
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.
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:
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.
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:
Here is the wiring diagram I used for reference:
DHT11 Sensor Raspberry Pi VCC -> 5V. GND -> GND DATA -> GPIO 4
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:
sudo apt-get update
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
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.
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.
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:
sudo pip3 install solace-semp-config
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.
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.
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 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.
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.
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.
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.
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.
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!