Migrate Your Business from GCP IoT Core 04|VPC Network Peering and Transfer Data to GCP

As you have set up the connection to EMQX Cloud under the guide in our previous articles, it’s time to send the data back to the services in GCP(If you are using databases or data analytics in GCP). This tutorial will demonstrate the VPC Network Peering between EMQX Cloud and GCP to help you with data transmission.

VPC Peering Setup

Log in to EMQX Cloud console, go to the deployment overview, click the + VPC Peering Connection, fill in the information and keep the information for further use:

  • VPC Network Name of deployment
  • Project ID of EMQX Cloud
  • CIDR of deployment

The Project ID and VPC Network Name can be found in your GCP Control Panel — VPC network peering.

Log in to your GCP console, create the peering connection.

  1. In the Google Cloud Console, click VPC network peering.
  2. Click CREATE PEERING CONNECTION, and click Continue.
  3. In Name, enter a name for your peering connection.
  4. In Your VPC Network, enter the name of your GCP VPC network.
  5. In Peered VPC network, select In another project.
  6. In Project ID, enter EMQX Cloud Project ID. You can find this name in the VPC Peering view in EMQX Cloud.
  7. In VPC network name, enter your EMQX Cloud VPC Network Name. You can find this name in the VPC Peering view in EMQX Cloud.
  8. Click CREATE.

Fill the following information in the VPC Network Peering into the EMQX Cloud VPC configration panel.

  • Project ID: GCP Project ID of your peering VPC
  • VPC Network Name: Your VPC network

You will see the status of peering connection is Active if everything goes well.

and you will see the status of VPC Peering on EMQX Cloud is running.

Create Firewall to allow your EMQX Cloud deployment to access your GCP network.

  1. Click Firewall, and Click CREATE FIREWALL RULE.
  2. In Network, select your GCP network.
  3. In Targets, select All instances in the network, or you can select other option according to your situation.
  4. In Source IP ranges, fill in the CIDR of EMQX Cloud VPC in step 1.
  5. Seletc your Protocols and ports.

Data Integration

I will create a VM instance in GCP console and use Docker to install Redis for data storage.

1. Redis Configuration

Start a Redis container on the server with the following command.

docker run -itd --name redis -p 6379:6379 redis:latest
docker exec -it redis redis-cli

Once Redis is ready, it’s time to start the configuration of the EMQX Cloud data integration!

2. Save device data to Redis

Enter Data Integration, and choose the Redis Single Mode card.

Create Redis Single Mode Resource

In the Create Resource page, set Redis Server (IP address and port of the server) as follows:

Click Test button when the configuration is complete, then click New button to create a resource when it is available. If it’s shown that init resource failed, you need to check your VPC peering and Firewall configuration. You can view more on the log details page.

Create Rule

After the resource is available, click New to enter the create rule page.

Our goal is to trigger the engine when the client sends a temperature and humidity message to the temp_hum/emqx topic. Here you need a certain process of SQL:

Based on the above principles, our final SQL should look like this.

SELECT
timestamp div 1000 as up_timestamp, clientid as client_id, payload as temp_hum
FROM
"temp_hum/emqx"

Create Action

After completing the rule configuration, click Next to configure and create an action. We read the up_timestamp, client ID, temperature and humidity from the topic and save it to Redis.

HMSET ${client_id} ${up_timestamp} ${temp_hum}

Test

  1. Use MQTT X to connect the deployment

You need to replace broker.emqx.io with the created deployment connection address, and add client authentication information to the EMQX Cloud console.

2. View the data saving results

keys *
HGETALL <client_id>

3. View rules monitoring

Under View rule monitoring, the number of data dumping successes has increased.

Summary

At this point, I hope you have successfully forwarded the data to GCP and integrated EMQX Cloud with other cloud services you have.

With this series of tutorials, you can enjoy a smooth migration of your business from GCP IoT Core to its alternative, EMQX Cloud. EMQX Cloud also provides some other excellent features, such as Shadow Service which implements IoT applications quickly through Topics and APIs. Readers are welcome to explore more in your IoT development journey with EMQX Cloud.

Originally published at https://www.emqx.com.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
EMQ Technologies

EMQ is an open-source IoT data infrastructure software provider, delivering the world’s leading open-source MQTT message broker and stream processing database.