Introduction
In the IoT era, every device is becoming smarter and the nature of sending alerts to users is also evolving. We cannot decide a device to be smarter if it sends data to Cloud, but in response, we yet expect some action to set off on some defined rules. While IoT has now become more on edge/fog computing where devices are ready to pick up significant decisions, but we require that message to be passed to the user as a sign, either via email, in-app or SMS. Because of some remote regions and low internet connectivity, SMS seems to be the favorite among all the notifications.
Scenario
Send SMS to an assigned number from SAP Leonardo IoT when the AC temperature ranges below 20 degrees Celsius.
Component Used:
For this use-case we are working with the following components:
- Raspberry Pi + DHT11 setup.
- SAP IoT Cockpit
- SAP Leonardo IoT
- SAP Cloud Foundry
- AWS SNS
Let's take off.
Raspberry Pi + DHT11 setup
Considering, we have a working Raspberry Pi with DHT11 which can send messages to SAP IoT using MQTT protocol. There are lots of tutorials available to set it up. Once the system is up and running, we will have temperature and humidity readings. Below is the setup:
Willingly, you can get this tutorial
Send Data with MQTT and use Paho client to send data to the SAP Cloud.
Amazon SNS service
There are several SMS gateways are available, which can be readily incorporated with any service. Twilio, Jasmin, Textlocal, Amazon SNS are some most used SMS gateways. For this blog, I have chosen AWS SNS, which supports more countries and is easy to set up.
Note: This service is chargeable. You can observe the billing dashboard and pricing
First thing first, we need an AWS account under which we will select SNS service. Remember this SNS service is available to some geographical regions, I have used us-east-1. Below are the steps to run the service.
Get the access key.
Copy and store the new Access key ID and Secret Access Key, as we will apply it during coding.
Going ahead with assimilation, we will use SAP Leonardo IoT with SNS service via a Flask API, which is deployed in SAP Cloud Foundry. The API accepts a JSON message with a phone number and a message added to it.
Let’s Code
Pre-requisite
- Python environment (better to use Python 3)
- CF installed in CLI
Create a Python script AWS_Flask
.py and use the following code.
import boto3
from flask import Flask
from flask import request
import os
app = Flask(__name__)
cf_port = os.getenv(“PORT”)
@app.route(‘/’)
def input():
return(“Ok.. “)
@app.route(‘/postjson’, methods=[‘POST’])
def postJsonHandler():
print(request.is_json)
content = request.get_json()
print(content)
phone = content[‘phone’]
message = content[‘message’]
print(“{}”.format(phone)) #you can comment it out
print(“{}”.format(message)) #you can comment it out
# Create an SNS client
client = boto3.client(
“sns”,
aws_access_key_id=“your access key id”,
aws_secret_access_key=“your secret access key”,
region_name=“us-east-1”
)
# Send your sms message.
client.publish(
PhoneNumber=“{}”.format(phone),
Message=“{}”.format(message)
)
return ‘JSON posted’
if __name__ == ‘__main__’:
if cf_port is None:
app.run(host=‘0.0.0.0’, port=5000, debug=True)
else:
app.run(host=‘0.0.0.0’, port=int(cf_port), debug=True)
Create a folder structure as mentioned below.
Open manifest.yml, and add.
---
applications:
- memory: 128MB
disk_quota: 256MB
random-route: true
Edit Procfile:
web: python AWS_Flask.py
Edit requirements.txt. Here we specify the Python Libraries.
Flask
boto3
request
Edit runtime.txt
python-3.6.7
Deploy the application to SAP Cloud Foundry
Open the command prompt from the project folder and login to the cloud foundry.
> cf api https://api.cf.eu10.hana.ondemand.com
> cf login
> cf push <name of the python application>
Copy the URI form the deployed application.
Just to check if the application is running, copy and paste the URL into a browser and we should be able to see.
Test 1
For API testing I have used POSTMAN. Use HTTP POST and form the URL as:
https://<your_api_url>.cfapps.eu10.hana.ondemand.com/postjson
Add the below JSON into the body:
{
“phone”: <your phone number>,
“message”: “Hello World!”
}
*Remember, that the phone number should include the country code.
Result
You should receive an SMS
Integration with SAP Leonardo IoT
Now, we will create a new destination where we will pass the URL:
https://<your_api_url>.cfapps.eu10.hana.ondemand.com/postjson
After performing the above step, we will tread to
SAP Leonardo IoT to operate the service against some rules.
Pre-requisite:
- Access to SAP Leonardo IoT.
- Understanding of configuring Rule service to trigger some actions.
Login to SAP Leonardo IoT Launchpad.
Create a
Rule Context and add
Rule Type as
Streaming.
Add a
Rule.
The
Rule which we have created will start if the sensor temperature reaches below 20-degree celsius. Activate the rule once done.
Under the Action tab, create a new action. From
Action Type select
Service Integration to choose the destination
SMS which we have created earlier
Test 2
To reproduce the scenario, I am placing DHT11 near an icy glass of water to lower down the temperature. Wait as the temperature reaches below 20 and we see our first SMS from SAP Leonardo IoT.
Conclusion
The complete setup can be replicated using other SMS Gateway. The setup provides addon feature to the complete platform and a better way to use SMS as a preferred notification service for any critical alerts.