Virtual Sensors for Data Processing

Your Smart Mailbox project consists of software/hardware components that are not fully connected. The PIR motion sensor is programmed to provide its status to the openremote smart mailbox asset via the MQTT protocol and thus, application developers can exploit the MQTT protocol to implement IoT applications that leverage such data.

However, even in simple smart mailbox projects, raw IoT data such as “motion detected”, is not enough to support accuracy. In this case, using only motion data, is not enough to accurately estimate the number of letters received. IoT systems usually require processing of data flows in order to extract meaningful data (e.g., letter count). For example, to determine letter count, motion detection and weight of letters are required.

During this lab you will develop a Virtual Sensor. This is a software artifact enabling to accept one or more data flows, processes them, and finally provides meaningful data.

Additional attributes in the Smart Mailbox asset

Up to now, your smart mailbox asset has the letter_count attribute. This attribute is not updated using the measurements coming from the PIR motion sensor. However, using only this value, it is not possible to accurately measure the number of letters.

Using the Open Remote tutorial, create following attributes: motion_detected and weight.

These attributes will be used to subscribe and receive related messages in the virtual sensor.

Sensor for measuring weight

We have already provided you with a PIR motion sensor in order to update the motion_detected value. Regarding the weight, an additional sensor is required. To expedite the overall development process, you will develop a software component that provides synthetic data (simple values of weight).

You can create a python script and generate weight values as follows:

# Generate random weight
nb_mail = random.randint(0, 10)     # 0 < number of mails < 10
mail_weight = 15                    # average weight of a single mail
total_weight = nb_mail * mail_weight 

After generating a weight value, you have to update the weight attribute of the openremote smart mailbox asset. This requires creating a publisher as already described in the MQTT publisher tutorial

Note that, weight values can be generated and published every 5 seconds as follows:

sleep_period = 5                    # in seconds
while (True): 
    # Publish the message to the topic
    mid = client.publish(topic, total_weight)
    time.sleep(sleep_period)

Development of the letter count virtual sensor

To develop the letter count virtual sensor, you have to create another python script. Then, it is essential to monitor the sensed attributes of the smart mailbox asset. To do this, an MQTT client must be created and subscribe to receive values. As a 1st step, create the corresponding topics as follows:

# Define MQTT topics based on what you have in OpenRemote
motion_topic = "master/{clientId}/attribute/{attributeName}/{assetId}"
weight_topic = "master/{clientId}/attribute/{attributeName}/{assetId}"

Then, using the MQTT subscribe tutorial, you have to connect and subscribe to the corresponding topics. MQTT subscribers leverage callbacks to asynchronously receive messages. This tutorial follows an event-driven data processing pattern in order to estimate the letter_count values.

Therefore, you have to modify the on_message callback function and accordingly process the data received.

# Callback function that is executed when a message is received    
def on_message(client, userdata, message):
    msg = json.loads(message.payload.decode("utf-8"))
    att_name = msg["attributeState"]["ref"]["name"]
    att_value = msg["attributeState"]["value"]
    process_data(att_name, att_value)

Finally, in the process_data function you have to introduce your estimation logic for counting the letters received.

Note that, after estimating the number of letters, you have to update the letter_count attribute by publishing the information to Open Remote (MQTT publisher tutorial).

Previous
Next