Using EMQX Cloud Data Integrations to save data to Firebase Realtime Database

EMQX 企业版技术支持发表于:2023年03月15日 11:43:59

Intro 

    In this article, we will simulate temperature and humidity data and report it to the  EMQX cloud via the MQTT protocol, after which we will use the EMQX Cloud data  integration to enable NAT gateway and save the data to the Firebase Realtime Database  over the public network.

Introduction to EMQX Cloud

    EMQX Cloud is an MQTT middleware for the IoT from EMQ. As the world's first fully  managed MQTT 5.0 public cloud service, EMQX Cloud provides a one-stop O&M  colocation and a unique isolated environment for MQTT services. In the era of Internet of  Everything, EMQX Cloud can help you quickly build industry applications and easily realize  the collection, transmission, computation and persistence of IoT data.

image.png

    With the infrastructure provided by cloud providers, EMQX Cloud is available in  dozens of countries and regions around the world, providing low-cost, secure, and  reliable cloud services for 5G and Internet of Everything applications. 

    For more information, please go to the EMQX Cloud website or see the EMQX Cloud  documentation.

Introduction to Firebase Realtime Database

    The Firebase Realtime Database is a cloud-hosted database. Data is stored as JSON  and synchronized in realtime to every connected client. When you build cross-platform  apps with our Apple platforms, Android, and JavaScript SDKs, all of your clients share one  Realtime Database instance and automatically receive updates with the newest data.

Pre-requisites 

  1. create a deployment of EMQX Cloud

    Create a deployment of EMQX Cloud, other options default.

    image.png

    When the status is Running, the creation of the deployment is complete.

    image.png

    When the status is Running, the creation of the deployment is complete.

    image.png

  2. Enable NAT Gateway

    NAT gateways can provide network address translation services to provide  Professional deployments with the ability to access public network resources without the  need for VPC peering connections.

    image.png

  3. Create Firebase Realtime Database instances

    If you are creating a Firebase Realtime Database instance for the first time, you can  refer to the help document. First, go to the Firebase Realtime Database console and  create a database.

    image.png

Data Integrations

  1. Create a resource

    Go to the Data Integrations page. On the data integration page, click webhook  resources.

    image.png

    Fill in the Firebase Realtime Database connection address, and then click test. Please  check the Firebase Realtime Database service if the test fails. Click the New button after  the test is passed and you will see the Create Resource successfully message.

    image.png

  2. Create a new rule

    After the resource is successfully created, you can return to the data integration page  and find the newly created resource, and click create rule. Our goal is that as long as the  emqx/test topic has monitoring information, the engine will be triggered. Certain SQL  processing is required here:

     Only target the topic "emqx/test"

     Get the three data we need: "msg"

    According to the above principles, the SQL we finally get should be as follows:

  3. SELECT  
    payload.msg as msg 
    FROM "emqx/test"

    image.png

    You can click SQL Test under the SQL input box to fill in the data:

     topic: emqx/test

     payload:

  4. { "msg": "hello" }

    Click Test to view the obtained data results. If the settings are correct, the test output  box should get the complete JSON data as follows:

    image.png

    If the test fails, please check whether the SQL is compliant and whether the topic in  the test is consistent with the SQL filled in.

  5. Add Action to Rule

    After completing the rule configuration, click Next to configure and create an action. Then enter the fields and tags as follows:

  6. #path /emqxcloud/demo.json 
    
    #Body ${msg}

    image.png

Verification

  1. Use MQTTX to simulate data reporting

    We recommend you to use MQTT X, an elegant cross-platform MQTT 5.0 desktop  client to subscribe/publish messages. Click on the add button and fill in the deployment information to connect to the  deployment. You need to replace broker.emqx.io with the created  deployment connection address, add client authentication information to the EMQX  Cloud console. Enter the topic name and payload message to publish the message.

    image.png

  2. View results in Firebase Realtime Database

    Go to the operation page. Choose the 'data' bar. Here you can see the results of the  MQTT data forwarding.

    image.png

    So far, we have used EMQX Cloud data integration to save the entire process of data  to the Firebase Realtime Database over the public network.


    您需要登录后才可以回复