How To Empower Google Cloud IoT Core With Geolocation REST API

What is Google Cloud IoT Core?

Google Cloud IoT Core is a fully managed service. It allows you to easily and securely connect, manage, and ingest data from millions of globally dispersed devices.

Cloud IoT Core, in combination with other services on the Google Cloud IoT platform, provides a complete solution for collecting, processing, analyzing, and visualizing IoT data in real-time to support improved operational efficiency.

Service Components and Data Flow of Google Cloud IoT Core. Image Source: Google QwikLabs.

What is apilayer, and What Makes it Trusted by 1 Million++ Customer Worldwide?

apilayer Products. Browse Our Homepage apilayer.com for More Information.

apilayer is an unparalleled suite of productivity-boosting Web APIs & cloud-based microservice applications for developers and companies of any size.

The world’s most innovative companies use apilayer to gain efficiency by automating data verification, lookup, and verification using straightforward API interfaces. Thousands of applications also request currency data every day, and hundreds of thousands of users leverage apilayer’s microservice APIs to automate workflows.

How to Get Started with Google Cloud IoT Core?

Before you begin, here are some steps you need to prepare:

1. Select or create a Cloud Platform project.

First, navigate to this URL, to start your project: https://console.cloud.google.com/project

Once you have created your project, take note of the auto-generated ID. We will need it for the next sections:

2. Enable billing for your project.

In the Cloud Console, click the Navigation menu > Billing.

Set the billing account for your project:

If you enabled your billing successfully, you will be redirected to this dashboard:

Congratulations, you enabled billing for your account!

3. Enable the Google Cloud Internet of Things (IoT) Core API.

After that, visit the following URL to enable the Google Cloud Internet of Things (IoT) Core API:

https://console.cloud.google.com/flows/enableapi?apiid=cloudiot.googleapis.com, or just search for “iot” on the Google Cloud Console:

Next, register your application for Google Cloud Platform IoT Core:

You have enabled the IoT Core API!

How to Activate Google Cloud Shell?

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell also provides command-line access to your Google Cloud resources.

First, in the Google Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell provides command-line access to your Google Cloud resources.

For your first command, you can list the active account name with this command:

When you want to execute your first command, Click “Authorize” when you see this window:

Be patient! It takes a few moments to connect to the environment. When you connect and receive authorization, the project is set to your PROJECT_ID. For example:

List the project ID with this command:

The Project_ID is an auto-generated name that Google assigns when you first set up your project in the Google Cloud Console (see the previous section).

 

How to Create a Pub/Sub Topic for Your Google Cloud IoT Core?

1. In the Cloud Console, click the Navigation menu > Pub/Sub > Topics:

2. Click + CREATE TOPIC from the top menu:

3. Give the topic the ID cloud-builds and click CREATE TOPIC:

4. If you created your Pub/Sub Topic successfully, then this dashboard will show up:

 

How to Create a Device Registry for IoT Core?

1. First, in the Cloud Console, click Navigation menu > IoT Core.

2. Then, click Create Registry and set the following fields (you will need to click on the Show Advanced Options button):

Field Value
Registry ID apilayer-iot
Region us-central1
Cloud Pub/Sub Topics Select a Cloud Pub/Sub Topic
Select the default cloud-builds topic from the drop-down.
Click Show Advanced Options
Device State Topic Leave at the default value
Protocols MQTT only
Cloud Logging Ignore
CA Certificate Ignore

Here is the screenshot of the field:

Advanced Options:

3. Finally, click Create. You should see a similar page once you have successfully created your registry:

You’ve just created a device registry with a Cloud Pub/Sub topic for publishing device telemetry events! The next section is about adding a device to the registry.

How to Add a Device to the Registry?

1. On the IoT Core page, click on Devices in the left menu, then click + Create a Device.

2. Then, set the following fields (you will need to click on the COMMUNICATION, CLOUD LOGGING, AUTHENTICATION link):

Field Value
Device ID my-device
Click COMMUNICATION, CLOUD LOGGING, AUTHENTICATION
Device Communication Allow
Authentication Default value (for now)
Public Key Format Default value (for now)
Public Key Value Default value (for now)

3. Finally, click Create.

You’ve just added a device to your registry! The device won’t be able to connect to Google Cloud without a valid key.

 

How to Add a Public Key to the Device?

For your device to transmit telemetry data through the cloud, you must add a key to the device.

1. First, in Cloud Shell, run the following command to create an RS256 key:

2. Next, run ls command in Cloud Shell, and you will see the following files:

Open the rsa_cert.pem with the nano editor:

3. Now, copy the contents of rsa_cert.pem to the clipboard, including the —–BEGIN CERTIFICATE—– and —–END CERTIFICATE—–.

4. Exit the editor with CTRL + X.

5. In the Cloud Console, on the Authentication tab for the device you created, click Add public key.

6. Enter the following values:

Field Value
Input method Enter manually
Public key format RS256_X509
Public key value Paste contents of rsa_cert.pem
Public key expiration date Default value

7. Click Add.

8. An RS256_X509 key is now listed for your device.

 

How to Run a Node.js Sample to Connect a Virtual Device and View Telemetry

1. In Cloud Shell run the following, replacing <YOUR_PROJECT_ID> with your Project ID:

2. Next, get the Cloud IoT Core Node.js samples from GitHub by cloning the full Node.js repository. Enter the following command to clone the repo:

3. Navigate to the Cloud IoT Core samples in the samples directory:

You’ll complete the rest of these steps in this directory.

4. Copy the private key to the current working directory, using this command:

5. Install the Node.js dependencies:

6. Run the following command to create a subscription:

7. Download the root certificate:

8. Run the following command to connect a virtual device to Cloud IoT Core using the MQTT bridge:

The output shows that the sample device is publishing messages to the telemetry topic. Twenty-five messages are published.

9. Run the following command to read the messages published to the telemetry topic:

Repeat the subscriptions pull command to view additional messages:

Telemetry events should be visible in the IoT Core Monitoring section:

Congratulations! You created a Cloud IoT Core device registry, connected a device, and published device telemetry events.

How to Empower Your IoT Apps with Geolocation API?

According to Market Insight Reports, geolocation is one of the most compelling and fast-growing Internet of Things (IoT) applications. The anticipated market size for “Geo IoT” is expected to reach $74 billion by the end of 2025 from its current value of $40 billion.

In this section, we will demonstrate a basic tutorial to add IP to geolocation API to your IoT app. We will use ipstack API, one of the powerful products from apilayer.

ipstack is a powerful, real-time IP to geolocation API capable of looking up accurate location data and assessing security threats originating from risky IP addresses. It delivers results in milliseconds in either JSON or XML format.

What began as freegeoip.net, ipstack completely rebuilt into a full-fledged IP geolocation platform. ipstack.com is now one of the leading providers of its kind with over 100.000 satisfied customers.

The following are simple steps to get you started: 

1. Requirement 1: Get API access key, by following this simple documentation.

2. Requirement 2: Install axios. 

axios is a promise-based HTTP client for the browser and node.js. Here is how you can install it on GCP using npm:

3. Open up nano command line text editor to create a new file:

4. Write the following node.js code inside nano text editor:

5. Save and exit nano by pressing CTRL+X, when prompted to save the file, press Y.

6. Run ipstackDemo.js:

Congratulations, you successfully interact with ipstack API inside Google Cloud Platform!

Are You Ready to Empower Your IoT Devices and Systems with Powerful Geolocation APIs?

As you can see ipstack REST API provides many real-time IP to geolocation endpoints that can connect to any Cloud platform in any programming language you work with. 

In this article, we show you a comprehensive demo of how you to set up the Google Cloud IoT Core environment, and empower it with ipstack REST API. 

Take advantage of the free tier on ipstack. We strongly recommend you upgrade your subscription plan if you need more powerful features (SSL Encryption, Security Module, Time Zone Module, and, and many more!) or contact us for a custom solution. We can’t wait to see what you build with our REST API!

Head over to ipstack and start empowering your IoT devices and systems with precise and rich geolocation data.