Skip to Content

Introduction

As promised, I’m about to continue my series on my microbrewery. In the following article I will show you how I developed an intelligent beer scale. So sit tight open yourself a cool IPA and most overall I want to encourage you to write about your own stories.

 

Previous posts

In the previous article I stated that my next goal is to connect my refrigerator to an SAP S4/HANA system. First of all I want to have the ability to record the receipt of new beer as goods receipt. In order to determine the amount of beer I’m planing use a bathroom scale to record weight changes and automatically update the stock within the ERP.

 

Hardware

There are many scales that are suitable for this purpose. In order to have as little soldering work as possible, I decided on a Nintento Wii Balance Board. The main reason why I chose this particular device was due to its Bluetooth capability.

 

Software

In order to be able to receive constant measurements from the Wii board. I wanted to use the gr8w8upd8m8 script by skorokithakis. The program works as follows. During the receiving process, the device stores all measured values as long as the load exceeds 30 kilograms. As soon as the load is lower than this value, the measuring process and the Bluetooth connection will be terminated. The weight can then be determined by calculating the most frequent measured value. The basic functionality of the program met my requirements. However, there were two points that did not fully meet my requirements. On the one hand, I found it disturbing that the connection to the device was broken after the measurement was completed. On the other hand, I did not want to receive the measurement result only after the weight had been removed from the scale. For this reason I have extended the script with additional classes, which allow me to get measured values while the weight is still on the scale and to terminate the Bluetooth connection only when needed. The class can now be used as follows:

So far we have the possibility to measure the required data according to the needs. In order access the values via the brew controller, we also need a corresponding extension. Within CraftBeerPi values are made available via so called sensors. The corresponding sensor I have developed is available in my GitHub directory. Users have the ability to define certain offsets and choose whether the values are calculated in KG or LBS. The final result looks as follows:

 

SAP Cloud Platform IoT Service

We’ve almost made it. There’re only a few tweaks left to do. Now that we’re up and running the next task is to configure the new sensor within SAP Cloud Platform IoT Service for the Cloud Foundry environment. In my second article I described how to configure a sensor using the GUI approach. Obviously you’re aware of this approach therefore I will focus on how to achieve the same result using the API. There’s a broad variety of tools that do the job (cURLInvoke-WebRequest, …). For the ease of use I’ll be using the Chrome extension Postman. In order to complete the setup we have to create a new capability as well a new sensor type and link those two entities to the device. The access to the SAP IoT Service API is protected by means of Basic Auth. Therefore we have to configure the security settings within Postman via the “Authorization” tab. In order to test the the configuration we’re calling the capabilities service of the REST-API.

https://{{tennant}}.{{zone}}.cp.iot.sap/iot/core/api/v1/capabilities

As our goal is to create a new capability we’re using the HTTP POST method. The properties of the new capability are being trasmitted via the body secttion of the HTTP protocol. Based on your requirements you can configure the values as follows:

{
  "alternateId": "string",
  "id": "string",
  "name": "string",
  "properties": [
    {
      "dataType": "integer",
      "formatter": {
        "dataType": "integer",
        "scale": 0,
        "shift": 0,
        "swap": true
      },
      "name": "string",
      "unitOfMeasure": "string"
    }
  ]
}

After the request is being processed by the service it will display the capabilites in the server response. The next step is to create a new sensor type. In order to link the created capability to the sensor set the capability id / type your specific values.

https://{{tennant}}.{{zone}}.cp.iot.sap/iot/core/api/v1/sensorTypes

The final step on the SAP Cloud Platform is to update the device model. Therefore, we use the HTTP PUT method of the device service.

https://{{tennant}}.{{zone}}.cp.iot.sap/iot/core/api/v1/devices/{{deviceId}}

 

CraftBeerPi customizing

So far so good. The sensor is collecting real time data and the SAP Cloud Platform is configured to receive its measurements. Now we have to reconfigure the brew controller in order to send additional data to the cloud service. The corresponding background task has to be configured as follows:

import json
import ssl

from MQTTClient import MQTTClient
from modules import cbpi
from modules.base_plugins.one_wire import ONE_WIRE_SENSOR
from modules.plugins.cbpi_Wii import WiiSensor

sap_iot_cfg = {                                                           # define SAP IoT Service device properties
    'device_alternate_id': 'aabbccddeeffgghhii',
    'capability_alternate_id': {
        'MashTemperature': '0123456789101112',
        'Weight': '1314151617181920'
    },
    'sensorAlternateId': {
        'DS18B20': 'zzyyxxwwvvuutt',
        'WiiBoard': 'ttuuvvwwxxyyzz'
    }
}
mqttc = MQTTClient({                                                      # init MQTT client
    'id': sap_iot_cfg.get('device_alternate_id'),
    'host': 'mytennant.zone.cp.iot.sap',
    'port': 8883,
    'keepalive': 60,
    'tls_settings': {
        'ca_certs': '/some/dir/certs/ca-certificates.crt',
        'certfile': '/some/dir/certs/SAP_IoT/credentials.crt',
        'keyfile': '/some/dir/certs/SAP_IoT/credentials.key',
        'tls_version': ssl.PROTOCOL_TLSv1_2
    }
}).connect()

@cbpi.backgroundtask(key='mqtt_client', interval=2.5)                     # create bg job with an interval of 2.5 seconds
def mqtt_client_background_task(api):
    sensors = cbpi.cache.get('sensors')                                   # read available sensors
    
    for key, value in sensors.iteritems():                                # loop over the sensors
        topic = 'measures/' + sap_iot_cfg.get('device_alternate_id')      # define the MQTT topic
        
        if isinstance(value.instance, WiiSensor):
            caid = sap_iot_cfg.get('capability_alternate_id').get('Weight')
            said = sap_iot_cfg.get('sensorAlternateId').get('WiiBoard')
        if isinstance(value.instance, ONE_WIRE_SENSOR):
            caid = sap_iot_cfg.get('capability_alternate_id').get('MashTemperature')
            said = sap_iot_cfg.get('sensorAlternateId').get('DS18B20')
        
        data = {                                                          # define the playload
            'capabilityAlternateId': caid,
            'sensorAlternateId': said,
            'measures': value.instance.last_value
        }
        payload = json.dumps(data, ensure_ascii=False)                    # convert payload to JSON
        mqttc.publish(topic, payload)                                     # connect to the MQTT server and publish the payload
    

 

Roundup

In this episode we’ve setup a new CraftBeerPi sensor using some Python code and attached it to the SAP Cloud Platform IoT Service for Cloud Foundry.

 

Outlook

We now have the ability to measure the necessary values in order to update the stock information. In the next article I will describe how to connect the device to SAP Cloud Platform IoT Application Enablement using the API and how you can utilize SAP Cloud Platform Integration in addition with SAP Cloud Platform Cloud Connector to post the receipt of new beer as goods receipt within the SAP S4/HANA system.

 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply