百度智能云

All Product Document

          IoT Core

          Data is Forwarded to Function Computing Service CFC

          I. Introduction

          In this tutorial, you learn how to forward the device messages of IoT Core to CFC through Rule Engine and carry out the user-defined data processing through CFC.

          II. Application Scenarios Description

          The rule engine can filter, modify and forward massive data. However, when you put forward more complex and personalized requirements for device data processing, and it may not meet all requirements using the rule engine only, you can meet relevant processing requirements by combining with CFC. The data processing becomes more flexible and diverse by combining with Rule Engine and CFC. The main application scenarios include:

          1. Carry out the data processing in complex scenarios.
          2. Store the device log information.
          3. Forward the device message to the user-defined service.

          III. Example Scenarios Description

          To give you an in-depth understanding of the combined use of Rule Engine and CFC, we take a simple example:

          • The rule engine forwards the system log generated by the device to CFC.
          • CFC decodes the log message received and stores the message content in BOS.

          The operation steps are as follows:

          1. Create a BOS instance.
          2. Create a CFC instance, and configure BOS as the log storage destination.
          3. Create an IOT Core instance and a forwarding rule.
          4. Verify the device log forwarding and storage function.

          IV. Operating Steps

          4.1 Create a BOS Instance

          For the specific method, please see BOS Creating Bucket.

          Log in to the BOS Management console. Click "Bucket List" in the left navigation bar, and find and click the + button in the Bucket list ("New Bucket" button) to create a Bucket in the pop-up box by following the prompts.

          image.png

          Click "Confirm" to complete the creation of this Bucket.

          4.2 Creating CFC Instance and Configuring Destination

          Select "Create Blank Function".

          image.png

          Select "BOS" as log storage in "unction Configuration. The log storage path is custom.

          image.png

          Select "None" for Trigger.

          image.png

          The rule engine sends the Base64 encoded message content to CFC. For more information, see [Forwarding to CFC Function](https://cloud.baidu.com/doc/IoTCore/s/zkd8kfdc8#Forward to Function Computing#). Therefore, to obtain the data transmitted by the rule engine in CFC, we need to carry out the following two steps:

          1. Carry out the Base64 decoding of message in event ['message'].

          2. Convert the decoded data into the object according to the description file of Protocol Buffers.

          In this tutorial, CFC uses Python3.6 during running, whose function is to decode the device data and print the log.

          The Python function program reference code is as follows:

          import base64
          import time
          from google.protobuf import json_format
          import json
          import MqttLogConstants_pb2 as MqttLog
          from TrafficLogEntry_pb2 import TrafficLogEntries
          import logging
          logging.basicConfig()
          logger = logging.getLogger()
          logger.setLevel(logging.INFO)
          
          # Decode byte data and convert it into log object
          def parse_log_entries(bytes):
              entries = TrafficLogEntries()
              entries.ParseFromString(bytes)
              return entries
          
          # CFC Function Entry
          def handler(event, context):
              # base64 decoding
              logBytes=base64.b64decode(event['message'])
              # Decode byte data and convert it into log object
              logEntries = parse_log_entries(logBytes)
              for entry in logEntries.entries:
                  if entry.code.startswith('MQT'):
                      entryJson = json.loads(json_format.MessageToJson(entry))
                      details = entryJson['details']
                      newDetails = {}
                      for keyNum in details:
                          detail = MqttLog.LogDetailKey.Name(int(keyNum))
                          newDetails[detail] = details[keyNum]
                      entryJson['details'] = newDetails
                      # Print the decoding result as a log
                      logger.info(entryJson)
              return None

          Upload the Python function program processing package.

          image.png

          4.3 Creating IoT Core Instance and Configuring Rule Engine

          4.3. 1 Create a IoT Core instance.

          Enter the console and click "create IOT core".

          image.png

          After you fill in the information, click "Submit".

          image.png

          After successful creation, the instance is displayed in the instance list.

          image.png

          4.3.2 Add a Template.

          Click the "Template" menu in Device Management, and then click "Add Template" to add a template.

          image.png

          Fill in the「template name and click「confirm」.

          image.png

          After confirmation, create a device template named fortest, which can be viewed in the template list.

          image.png

          4.3.3 Add a device.

          Click "New Device" to create a device.

          image.png

          Select "Key Authentication" as the authentication mode, and select the template "Forest" created in the previous steps, and then click "Submit".

          image.png

          After successful creation, it displays the key of the device. Click "Download" to save the key record text.

          image.png

          4.3.4 Generating Connection Information

          To connect the device, you need to use the MQTT Connection Information Generator to generate the connection authentication information of the device and record the Results (MQTT Connection Information) for standby.

          image.png

          Please save the Connection Authentication Information of Device, which is used in Step 4.4.

          4.3.5 Configuring Rule Engine

          Log in to the IoT Core console, and find the "Rule List" under the instance, and then click "Create Rule" to create a rule.

          image.png

          After you enter the rule information, click "Confirm" to create a rule.

          image.png

          Click "Edit and Debug" to enter the Edit interface.

          image.png

          Edit the "Input Source" of rule, and then click "Topic Template".

          When the device is connected, monitored and disconnected, the log data is recorded in the directory $sys/log/info/#.

          image.png

          Select "SYS_LOG_TOPIC" for the device template, select "$sys/log/info/#" for optional topic.

          image.png

          Click "Rule Destination".

          image.png

          Select "CFC" as the destination, and select the function corresponding to the Python program, and then click 「Save」.

          image.png

          After saving the rule successfully, enter the rule list and start the rule. When the rule is Running, it indicates that the rule takes effect.

          image.png

          4.4 Verifying Device Log Forwarding and Storage Function

          On completion of the above steps, the environment has been built, and the device message can be forwarded to CFC through the rule engine. CFC stores the log information generated in BOS.

          Fill in the connection authentication information generated in Step 4.3.4 into the MQTT client, and then click "Connect".

          image.png

          Observe the Log in CFC, and you find that the data has been successfully received and transcoded.

          image.png

          image.png

          The INFO log in the figure above is the log printed in the Python program.

          View BOS, and you can see the log file. The device log information is successfully stored, and the verification is completed.

          image.png

          Previous
          Data is Forwarded to Baidu Mmessaging Service BMS
          Next
          FAQs