# Forwarding Device Data to Webhook Using the Rule Engine

In this article, we will simulate temperature and humidity data and report these data to EMQ X Cloud via the MQTT protocol and then use the EMQ X Cloud rules engine to dump the data into Kafka.

Before you start, you need to complete the following operations:

  • Deployments have already been created on EMQ X Cloud (EMQ X Cluster).

  • For professional deployment users: Please complete Peering Connection Creation first, all IPs mentioned below refer to the intranet IP of the resource.

  • For basic deployment users: No peering connection is required, all IPs below refer to the public IP of the resource.

# Create a Web server

  1. You could use the following python code to create a simple Web server.

    from http.server import HTTPServer, BaseHTTPRequestHandler
    
    class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
       def do_GET(self):
          self.send_response(200)
          self.end_headers()
          self.wfile.write(b'Hello, world!')
       
        def do_POST(self):
            content_length = int(self.headers['Content-Length'])
            body = self.rfile.read(content_length)
            print("Received POST request with body: " + str(body))
            self.send_response(201)
            self.end_headers()
    
    httpd = HTTPServer(('0.0.0.0', 8080), SimpleHTTPRequestHandler)
    httpd.serve_forever()
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17

# EMQ X Cloud rules engine configuration

Go to Deployment Details and click on EMQ X Dashboard to go to Dashboard.

  1. New Resource

    Click on Rules on the left menu bar → Resources, click on New Resource and drop to select the WebHook resource type. Fill in the URL and click Test. If you get an error, instantly check that the database configuration is correct. create resource

  2. Rule Testing Click on Rules on the left menu bar → Rules, click on Create and enter the following rule to match the SQL statement. In the following rule we read the time up_timestamp when the message is reported, the client ID, the message body (Payload) from the temp_hum/emqx topic and the temperature and humidity from the message body respectively.

    SELECT 
    
    timestamp as up_timestamp, clientid as client_id, payload.temp as temp, payload.hum as hum
    
    FROM
    
    "temp_hum/emqx"
    
    1
    2
    3
    4
    5
    6
    7

    rule engine

  3. Add a response action Click on Add Action in the bottom left corner, drop down and select → Data Forwarding → Send Data to Web Service, select the resource created in the first step and fill in the following data:

    Message content template:

    {"up_timestamp": ${up_timestamp}, "client_id": ${client_id}, "temp": ${temp}, "hum": ${hum}}
    
    1

    rule_action

  4. Click on New Rule and return to the list of rules rule list

  5. View rules monitoring view monitor

# Test

  1. Use MQTT X (opens new window) to simulate temperature and humidity data reporting

    You need to replace broker.emqx.io with the created deployment connection address, and add client authentication information to the EMQ X Dashboard. MQTTX

  2. View data dump results

    kafka