Part 2.1: Remote temperature and humidity control
Link to part 1 of the Blog Series – https://blogs.sap.com/2018/06/19/iot-prototype-with-business-rules-and-workflow-blog-series-14/
Lets get started with the configuration for the first scenario in the prototype. Configuration is required in various services of SCP for the completion of the IoT prototype.
Step 1- Device management: Set up message types, device types and device in Internet of things cockpit.
Step 2 – Business Rules: Create business rule services and set up rules.
Step 3 -Workflow: Create workflow and related user interfaces in Web IDE. (check part 2.2 for details)
Step 4 – IoT application: Create Java application to integrate all these services and Python code to send the sensor data from the device .(check part 2.2 for details).
Since I will be covering the configuration in detail for the first scenario, I have decided to split Part 2 of the series into two. I will keep it brief for the next 2 scenarios as the steps are almost the same.
Steps 1 & 2 will be covered in this blog and Steps 3 & 4 in the next blog.
Make sure that the following services are enabled in SAP Cloud Platform Neo environment under your trial account.
- Internet of Things
- Business Rules
- Web IDE full stack
Step 1: Device Management
The device management steps shown below covers the configuration of all three scenarios of the prototype
Go to ‘Internet of Things’ cockpit – https://iotcockpitiotservices-s000xxxxxxxtrial.hanatrial.ondemand.com/com.sap.iotservices.cockpit
Go to Device Management -> Message Types
Create four message types as shown below:
THSensorData: This message type will be used to send temperature and humidity data from device to SCP.
LowStockSensorData: This message type will be used to send the material type of the material with low stock.
ThicknessSensorData: This message type will be used to get the thickness information from the device for scenario 3.
AdjustThicknessSensor: This message type will be used to send the corrected thickness data to the device.
Go to Device Management -> Device Types
Create one device type with all the above four message types. Three of the message types will be configured with direction ‘From Device’ and one with direction ‘To Device’.
Go to Device Management -> All Registered Devices
Register a device with the device type configured above. Note the device ID and the authentication token which the system generates during the device registration. These will be used in the Python script which runs in the device to send and receive sensor data.
Configure Processing Service Mappings
For all four message types, configure processing service mappings so that the messages received from the device are stored in our own set of tables instead of the auto generated T_IOT_<message type id> tables.
For more information about this topic, you can refer https://blogs.sap.com/2016/09/12/message-processing-in-hcp-iot-service/
Create three tables as below and map them to the corresponding message type/device type IDs .
T_TH_IOTMESSAGES – To store temperature and humidity data from device.
T_LS_IOTMESSAGES – To store data from low stock sensor.
T_THICK_IOTMESSAGES – To store data from thickness sensor.
Note that this step is optional. You can process the messages directly from the IOT message tables. I will mention where you have to make changes in the code to accommodate this.
Step 2: Business Rules
Lets configure the business rules for this scenario.
Go to Business Rule Editor – https://bpmruleseditor-s000xxxxxxxtrial.dispatcher.hanatrial.ondemand.com/index.html
Create a project to hold the business rule service for the scenario.
Create two Data Objects
measures: with two attributes – temperature and humidity
result: with one attribute to hold the result to indicate whether the temperature and humidity values are exceeded.
Create two Rules
Temperature Rule – This is a simple text rule which returns true if the input temperature is more than the set limit.
Humidity Rule – This too is a simple text rule which returns true if the input humidity is more than the set limit.
Create two Rulesets
Check Temperature – Add Temperature rule to this rule set
Check Humidity – Add Humidity rule to this rule set
Create two Rule Services
Temperature Exceeded Service – This rule service takes the data object ‘measures’ as input and returns ‘result’ as output.
Humidity Exceeded Service – This rule service takes the data object ‘measures’ as input and returns ‘result’ as output.
Assign these rule services to the corresponding rule sets. Activate all the objects created in the steps above. Do not forget to deploy both the rule services.
The rule services will be called from the IoT application to validate the temperature/humidity from the sensors.
You can test the Business Rule Services by using Postman. Steps are mentioned below.
Testing Business Rules using Postman
Get the XSRF Token
Send HTTP request to (URL – https://bpmrulesruntimebpm-sxxxxxxxxtrial.hanatrial.ondemand.com/rules-service/v1/rules/xsrf-token) get the XSRF token required to invoke the business rule service. Do not forget to set the authorization to basic and give your SCP trial account user/password.
Call the Business Rule Service
Invoke the Business Rule Service by passing the XSRF token from the response header of the previous request.
Pass the input values as JSON in the request body.The rule service returns the result as true since the input temperature ’25’ is more than the set limit ’24’.
I tried to keep it very simple. Please feel free to comment if you have any questions. See you in Part 2.2 with the next steps for this scenario.